第4章:Kubernetes应用快速入门

Kubernetes的资源对象有:pod,service,replicaset,deployment,statefulset,daemonset,job,cronjob,node等

Kubectl常见命令如下:
(可以使用Kubectl COMMAND --help获取具体命令的使用方法)
kubectl controls the Kubernetes cluster manager.

Find more information at: https://kubernetes.io/docs/reference/kubectl/overview/

Basic Commands (Beginner):
create Create a resource from a file or from stdin.
expose Take a replication controller, service, deployment or pod and expose it as a new Kubernetes Service
run Run a particular image on the cluster
set Set specific features on objects

Basic Commands (Intermediate):
explain Documentation of resources
get Display one or many resources
edit Edit a resource on the server
delete Delete resources by filenames, stdin, resources and names, or by resources and label selector

Deploy Commands:
rollout Manage the rollout of a resource
scale Set a new size for a Deployment, ReplicaSet or Replication Controller
autoscale Auto-scale a Deployment, ReplicaSet, or ReplicationController

Cluster Management Commands:
certificate Modify certificate resources.
cluster-info Display cluster info
top Display Resource (CPU/Memory/Storage) usage.
cordon Mark node as unschedulable
uncordon Mark node as schedulable
drain Drain node in preparation for maintenance
taint Update the taints on one or more nodes

Troubleshooting and Debugging Commands:
describe Show details of a specific resource or group of resources
logs Print the logs for a container in a pod
attach Attach to a running container
exec Execute a command in a container
port-forward Forward one or more local ports to a pod
proxy Run a proxy to the Kubernetes API server
cp Copy files and directories to and from containers.
auth Inspect authorization

Advanced Commands:
diff Diff live version against would-be applied version
apply Apply a configuration to a resource by filename or stdin
patch Update field(s) of a resource using strategic merge patch
replace Replace a resource by filename or stdin
wait Experimental: Wait for a specific condition on one or many resources.
convert Convert config files between different API versions
kustomize Build a kustomization target from a directory or a remote url.

Settings Commands:
label Update the labels on a resource
annotate Update the annotations on a resource
completion Output shell completion code for the specified shell (bash or zsh)

Other Commands:
api-resources Print the supported API resources on the server
api-versions Print the supported API versions on the server, in the form of “group/version”
config Modify kubeconfig files
plugin Provides utilities for interacting with plugins.
version Print the client and server version information

Usage:
kubectl [flags] [options]

Use “kubectl --help” for more information about a given command.
Use “kubectl options” for a list of global command-line options (applies to all commands).

kubectl get node或者kubectl get nodes,加s不加s都行,可以获取集群中节点的详细信息
,如状态,角色,距今创建时间,Kubernetes的版本信息等;

#kubectl get node -owide或者kubectl get node -o wide,加空格不加空格都行,可以获取集群中节点的详细信息,除了状态,角色,距今创建时间,Kubernetes的版本信息等之外,还有集群内IP,对外IP,系统镜像,内核版本,容器运行时版本等信息;

kubectl get node --show-labels
#用于查看该集群下所有节点的标签信息;
kubectl describe node $Node_NAME
#用于查看具体节点的标签污点资源使用情况等详细信息;

在整个集群中,需要安装的Addons附件除了kube-proxy,CoreDNS和flannel(网络组件)之外,还需要Ingress-Controller和Prometheus(监控组件)等。

kubectl run nginx-test --image=nginx --replicas=2
#用于创建一个nginx-test的deployment,镜像用的是nginx的最新镜像,副本数为2;
#用法:
kubectl run NAME --image=image [–env=“key=value”] [–port=port] [–replicas=replicas] [–dry-run=bool] [–overrides=inline-json] [–command] – [COMMAND] [args…] [options]

kubectl get deploy 或者kubectl get deployment,这里deploy或者deployment都行,,或者用kubectl get deploy -w来监控状态变化;
#用于查看该集群下所有deployment的信息;这里主要是查看刚刚创建的nginx-test是否已经ready;

kubectl get pod或者kubectl get pods,加不加s都行
#用于查看默认分区default下的pod信息,如ready与否(current/desired),状态,重启次数和距今创建时间等信息;

kubectl get pods -owide或者kubectl get pods -o wide,加不加空格都行
#用于查看默认分区default下的pod的详细信息,除ready与否(current/desired),状态,重启次数和距今创建时间等信息之外,还有pod ip和所处node ip等信息;(注意:这里pod ip是cni0桥的网段地址,不是docker0桥网段,默认只能在集群内使用。)

kubectl expose deployment nginx-test --name=nginx-service --port=80 --target-port=8080
#用于将nginx-test的deployment所创建的pod指定给服务nginx-servic,这里–port指的是service的port,–target-port指的是pod的port,可以相同可以不同;
#用法:
kubectl expose (-f FILENAME | TYPE NAME) [–port=port] [–protocol=TCP|UDP|SCTP] [–target-port=number-or-name] [–name=name] [–external-ip=external-ip-of-service] [–type=type] [options]
(注意:
1,–type=’’: Type for this service: ClusterIP, NodePort, LoadBalancer, or ExternalName. Default is ‘ClusterIP’.
2,service的IP地址只存在于iptables规则当中,可以被访问,但是不能被ping通)

kubectl get svc或者kubectl get service,svc是service的缩写
#用于查看默认分区下服务的信息,如服务类型,clusterip地址,暴露端口以及距今创建时间等;

kubectl get pod --show-labels
#主要用于查看pod的标签信息;

kubectl describe svc nginx-service
#用于查看服务nginx-service的详细信息,如命名空间,标签,标签选择器服务类型,IP,端口,pod端口(targetport),以及Endpoints等信息;

kubectl scale deploy nginx-test --replicas=COUNT
#用于手动扩缩容pod的副本数量,如果COUNT比当前数量多,则为扩容,否则为缩容。
#用法:
kubectl scale [–resource-version=version] [–current-replicas=count] --replicas=COUNT (-f FILENAME | TYPE NAME)
[options]

kubectl describe pod $Pod_NAME
#用于查看pod的详细信息,如标签信息,容器信息(镜像版本等),数据卷信息等;

kubectl set image deployment $deployment_Name container1=新的镜像版本
#用于更换新的镜像版本来实现pod的滚动升级;
#用法:
kubectl set image (-f FILENAME | TYPE NAME) CONTAINER_NAME_1=CONTAINER_IMAGE_1 … CONTAINER_NAME_N=CONTAINER_IMAGE_N
[options]

kubectl rollout status deployment $deployment_Name
#用于查看部署deployment的更新过程;

kubectl rollout undo deployment $deployment_Name
#用于回滚部署deployment到上一个版本;

kubectl edit svc $Service-NAME
#用于修改svc中的信息,比如可以将metadata–>spec–>type由之前的ClusterIP修改为NodePort,这样这个service就可以被集群外所访问;
(注意:这里的NodePort是动态生成的,一般介于30000~65535之间,集群当中的每一个节点都会开有这个端口,都可以以IP:Port的方式对外提供服务,但是便于用户访问,建议在集群外部加一个物理负载均衡和Keepalive的高可用。)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Davidwatt

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值