Kubectl常用命令(三)

版权声明:欢迎转载,转载请注明出处! https://blog.csdn.net/miss1181248983/article/details/88181434

Kubectl是一个用于操作kubernetes集群的命令行接口,通过利用kubectl的各种命令可以实现各种功能,是在使用kubernetes中非常常用的工具。

接下来我们会通过一些简单的实例来展现其中一些高频命令的使用方法,更为重要的是这些命令使用的场景以及能够解决什么样的问题。

环境构成

  • 集群:
类型 主机名 IP
Master master 192.168.30.128
Master master2 192.168.30.150
Node node1 192.168.30.129
Node node2 192.168.30.130
# kubectl get nodes
NAME              STATUS                     ROLES     AGE        VERSION
192.168.30.129    Ready                      node      10d        v1.11.6
192.168.30.130    Ready                      node      10d        v1.11.6
192.168.30.128    Ready,SchedulingDisabled   master    10d        v1.11.6
192.168.30.150    Ready,SchedulingDisabled   master    10d        v1.11.6
  • 版本:
# kubectl version
Client Version: version.Info{Major:"1", Minor:"11", GitVersion:"v1.11.6", GitCommit:"b1d75deca493a24a2f87eb1efde1a569e52fc8d9", GitTreeState:"clean", BuildDate:"2018-12-16T04:39:52Z", GoVersion:"go1.10.3", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"11", GitVersion:"v1.11.6", GitCommit:"b1d75deca493a24a2f87eb1efde1a569e52fc8d9", GitTreeState:"clean", BuildDate:"2018-12-16T04:30:10Z", GoVersion:"go1.10.3", Compiler:"gc", Platform:"linux/amd64"}

部署命令

kubectl help可以查看kubectl相关的命令。

Basic Commands (Beginner):
  create         Create a resource from a file or from stdin.
  expose         使用 replication controller, service, deployment 或者 pod 并暴露它作为一个 新的
Kubernetes Service
  run            在集群中运行一个指定的镜像
  set            为 objects 设置一个指定的特征
 
Basic Commands (Intermediate):
  explain        查看资源的文档
  get            显示一个或更多 resources
  edit           在服务器上编辑一个资源
  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          为 Deployment, ReplicaSet, Replication Controller 或者 Job 设置一个新的副本数量
  autoscale      自动调整一个 Deployment, ReplicaSet, 或者 ReplicationController 的副本数量
 
Cluster Management Commands:
  certificate    修改 certificate 资源.
  cluster-info   显示集群信息
  top            Display Resource (CPU/Memory/Storage) usage.
  cordon         标记 node 为 unschedulable
  uncordon       标记 node 为 schedulable
  drain          Drain node in preparation for maintenance
  taint          更新一个或者多个 node 上的 taints
 
Troubleshooting and Debugging Commands:
  describe       显示一个指定 resource 或者 group 的 resources 详情
  logs           输出容器在 pod 中的日志
  attach         Attach 到一个运行中的 container
  exec           在一个 container 中执行一个命令
  port-forward   Forward one or more local ports to a pod
  proxy          运行一个 proxy 到 Kubernetes API server
  cp             复制 files 和 directories 到 containers 和从容器中复制 files 和 directories.
  auth           Inspect authorization
 
Advanced Commands:
  apply          通过文件名或标准输入流(stdin)对资源进行配置
  patch          使用 strategic merge patch 更新一个资源的 field(s)
  replace        通过 filename 或者 stdin替换一个资源
  wait           Experimental: Wait for one condition on one or many resources
  convert        在不同的 API versions 转换配置文件
 
Settings Commands:
  label          更新在这个资源上的 labels
  annotate       更新一个资源的注解
  completion     Output shell completion code for the specified shell (bash or zsh)
 
Other Commands:
  alpha          Commands for features in alpha
  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         修改 kubeconfig 文件
  plugin         Runs a command-line plugin
  version        输出 client 和 server 的版本信息

下面将会简单介绍一下如下命令,与部署相关:

命令 说明
rollout 管理资源的部署
scale 为deployment、rs、rc或job设置新的副本数量
autoscale 自动调整deployment、rs或者rc的副本数量

rollout

kubectl rollout对资源进行管理。可用资源包括:deployment、daemonset。

子命令:

命令 说明
history 查看历史版本
pause 暂停资源
resume 恢复暂停资源
status 查看资源状态
undo 回滚版本

DaemonSet保证在每个Node上都运行一个容器副本,常用来部署一些集群的日志、监控或者其他系统管理应用。典型的应用包括:

  • 日志收集,比如fluentd,logstash等
  • 系统监控,比如Prometheus Node Exporter,collectd,New Relic agent,Ganglia gmond等
  • 系统程序,比如kube-proxy, kube-dns, glusterd, ceph等

下面以Deployment “mysql”为例,对其进行升级/回滚。

# kubectl get deploy
NAME      DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
mysql     1         1         1            1           18h

# kubectl rollout history deploy/mysql
deployments "mysql"
REVISION  CHANGE-CAUSE
1         <none>

# kubectl rollout pause  deploy/mysql
deployment.extensions/mysql paused

# kubectl rollout resume deploy/mysql
deployment.extensions/mysql resumed

升级:

# kubectl set image deploy/mysql mysql=mysql:5.7
deployment.extensions/mysql image updated

# kubectl rollout history deploy/mysql
deployments "mysql"
REVISION  CHANGE-CAUSE
1         <none>
2         <none>


# kubectl rollout status deploy/mysql
deployment "mysql" successfully rolled out

# kubectl rollout history deploy mysql --revision=2             #查看对应历史版本详情
deployments "mysql" with revision #2
Pod Template:
  Labels:	app=mysql
	pod-template-hash=2226664987
  Containers:
   mysql:
    Image:	mysql:5.7
    Port:	3306/TCP
    Host Port:	0/TCP
    Environment:
      MYSQL_ROOT_PASSWORD:	123456
    Mounts:	<none>
  Volumes:	<none>

回滚:

# kubectl rollout undo deploy mysql                 #回滚版本
deployment.extensions/mysql

# kubectl rollout history deploy/mysql
deployments "mysql"
REVISION  CHANGE-CAUSE
2         <none>
3         <none>


# kubectl rollout history deploy mysql --revision=3             
deployments "mysql" with revision #3
Pod Template:
  Labels:	app=mysql
	pod-template-hash=1962730642
  Containers:
   mysql:
    Image:	mysql:5.6
    Port:	3306/TCP
    Host Port:	0/TCP
    Environment:
      MYSQL_ROOT_PASSWORD:	123456
    Mounts:	<none>
  Volumes:	<none>

scale

kubectl scale用于横向扩展,扩容或缩容 Deployment、ReplicaSet、Replication Controller或Job 中Pod数量。

扫描二维码关注公众号,回复: 5707319 查看本文章

scale可以指定多个前提条件,如:当前副本数量--current-replicas或资源版本--resource-version,进行伸缩比例设置前,系统会先验证前提条件是否成立。

例如,将mysql的副本数设置为3。

# kubectl get pod
NAME                     READY     STATUS    RESTARTS   AGE
mysql-5fb6c74b86-d9qzg   1/1       Running   0          9m

# kubectl scale --replicas=3 deploy/mysql
deployment.extensions/mysql scaled

# kubectl get pod
NAME                     READY     STATUS    RESTARTS   AGE
mysql-5fb6c74b86-4nr59   1/1       Running   0          3s
mysql-5fb6c74b86-d9qzg   1/1       Running   0          10m
mysql-5fb6c74b86-gwn9r   1/1       Running   0          3s

或者,将由“mysql-deploy.yaml”配置文件中指定的资源对象和名称标识的pod副本数设为3。

# kubectl scale --replicas=2 -f mysql-deploy.yaml
deployment.apps/mysql scaled

# kubectl get pod
NAME                     READY     STATUS    RESTARTS   AGE
mysql-5fb6c74b86-d9qzg   1/1       Running   0          14m
mysql-5fb6c74b86-gwn9r   1/1       Running   0          4m

autoscale

kubectl autoscale自动设置在kubernetes集群中运行的pod数量(水平自动伸缩)。

指定Deployment、ReplicaSet或ReplicationController,并创建已经定义好资源的自动伸缩器。使用自动伸缩器可以根据需要自动增加或减少系统中部署的pod数量。

例如,使用Deployment “mysql”设定,使用默认的自动伸缩策略,指定目标CPU使用率,使其Pod数量在3到10之间。

# kubectl autoscale deploy mysql --min=3 --max=10
horizontalpodautoscaler.autoscaling/mysql autoscaled

# kubectl get horizontalpodautoscalers.autoscaling 
NAME      REFERENCE          TARGETS         MINPODS   MAXPODS   REPLICAS   AGE
mysql     Deployment/mysql   <unknown>/80%   3         10        3          2m

# kubectl get pod
NAME                     READY     STATUS    RESTARTS   AGE
mysql-5fb6c74b86-4jl98   1/1       Running   0          2m
mysql-5fb6c74b86-d9qzg   1/1       Running   0          22m
mysql-5fb6c74b86-gwn9r   1/1       Running   0          12m

或者,使用Deployment “mysql”设定,使其Pod的数量介于1和10之间,CPU使用率维持在80%。

# kubectl delete horizontalpodautoscalers.autoscaling mysql             #先删除之前的autoscaling,否则提示已存在
horizontalpodautoscaler.autoscaling "mysql" deleted

# kubectl autoscale deploy mysql --max=10 --cpu-percent=80
horizontalpodautoscaler.autoscaling/mysql autoscaled

# kubectl get horizontalpodautoscalers.autoscaling mysql
NAME      REFERENCE          TARGETS         MINPODS   MAXPODS   REPLICAS   AGE
mysql     Deployment/mysql   <unknown>/80%   1         10        3          35s

集群管理命令

下面将会简单介绍一下如下命令,与集群的管理相关:

命令 说明
certificate 修改证书资源
cluster-info 显示集群信息
top 显示资源(CPU/内存/存储)使用情况
cordon 将node标记为不可调度
uncordon 将node标记为可调度的
drain 维护期间排除节点
taint 更新一个或多个节点上的taint

certificate

kubectl certificate用来修改证书资源。该命令不常用。

子命令:

命令 说明
approve 批准证书签名请求
deny 拒绝证书签名请求

cluster-info

kubectl cluster-info可以显示集群信息。

# kubectl cluster-info 
Kubernetes master is running at https://192.168.30.150:8443
Heapster is running at https://192.168.30.150:8443/api/v1/namespaces/kube-system/services/heapster/proxy
CoreDNS is running at https://192.168.30.150:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
kubernetes-dashboard is running at https://192.168.30.150:8443/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy
monitoring-grafana is running at https://192.168.30.150:8443/api/v1/namespaces/kube-system/services/monitoring-grafana/proxy
monitoring-influxdb is running at https://192.168.30.150:8443/api/v1/namespaces/kube-system/services/monitoring-influxdb/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

要进一步调试和诊断集群问题,可以使用 kubectl cluster-info dump 命令查看信息。

top

kubectl top用于显示资源(CPU/内存/存储)使用情况。可用资源包括:node、pod。

例如,显示集群中node的资源使用情况。

# kubectl top node
NAME             CPU(cores)   CPU%      MEMORY(bytes)   MEMORY%   
192.168.30.129   117m         2%        3164Mi          15%       
192.168.30.130   125m         3%        4206Mi          26%       
192.168.30.128   180m         4%        4089Mi          20%       
192.168.30.150   576m         14%       4431Mi          22% 

或者,显示集群中pod的资源使用情况。

# kubectl top pod
NAME                     CPU(cores)   MEMORY(bytes)   
mysql-5fb6c74b86-4jl98   1m           465Mi           
mysql-5fb6c74b86-d9qzg   1m           465Mi           
mysql-5fb6c74b86-gwn9r   1m           460Mi

cordon

kubectl cordon将node标记为不可调度的状态,这样就不会让新创建的pod在此node上运行。

例如,将192.168.30.130这个node标记为不可调度状态。

# kubectl get node
NAME             STATUS                     ROLES     AGE       VERSION
192.168.30.129   Ready                      node      10d       v1.11.6
192.168.30.130   Ready                      node      10d       v1.11.6
192.168.30.128   Ready,SchedulingDisabled   master    10d       v1.11.6
192.168.30.150   Ready,SchedulingDisabled   master    10d       v1.11.6

# kubectl get pod -o wide
NAME                     READY     STATUS    RESTARTS   AGE       IP            NODE             NOMINATED NODE
mysql-5fb6c74b86-4jl98   1/1       Running   0          1h        172.20.2.22   192.168.30.129   <none>
mysql-5fb6c74b86-d9qzg   1/1       Running   0          1h        172.20.2.21   192.168.30.129   <none>
mysql-5fb6c74b86-gwn9r   1/1       Running   0          1h        172.20.3.15   192.168.30.130   <none>

# kubectl cordon 192.168.30.130              #将node标记为不可调度状态
node/192.168.30.130 cordoned

# kubectl get node
NAME             STATUS                     ROLES     AGE       VERSION
192.168.30.129   Ready                      node      10d       v1.11.6
192.168.30.130   Ready,SchedulingDisabled   node      10d       v1.11.6                 #该node已经显示为SchedulingDisabled状态
192.168.30.128   Ready,SchedulingDisabled   master    10d       v1.11.6
192.168.30.150   Ready,SchedulingDisabled   master    10d       v1.11.6

# kubectl get pod -o wide
NAME                     READY     STATUS    RESTARTS   AGE       IP            NODE             NOMINATED NODE
mysql-5fb6c74b86-4jl98   1/1       Running   0          1h        172.20.2.22   192.168.30.129   <none>
mysql-5fb6c74b86-d9qzg   1/1       Running   0          1h        172.20.2.21   192.168.30.129   <none>
mysql-5fb6c74b86-gwn9r   1/1       Running   0          1h        172.20.3.15   192.168.30.130   <none>

可以看到,即使192.168.30.130这个node是不可调度状态,其下还是有pod在运行。

uncordon

kubectl uncordon将node标记为可调度的状态。

下面,我们将192.168.30.130这个node恢复为可调度的状态。

# kubectl uncordon 192.168.30.130           #将node标记为可调度的状态
node/192.168.1.253 uncordoned

# kubectl get node
NAME             STATUS                     ROLES     AGE       VERSION
192.168.30.129   Ready                      node      10d       v1.11.6
192.168.30.130   Ready                      node      10d       v1.11.6
192.168.30.128   Ready,SchedulingDisabled   master    10d       v1.11.6
192.168.30.150   Ready,SchedulingDisabled   master    10d       v1.11.6

drain

kubectl drain可以让node在维护期间排除节点。drain本意排水,意思是将出问题的node下的pod转移到其它node下运行。

下面新建deployment “nginx”,对192.168.30.130这个node进行drain。

# kubectl run nginx --image=nginx --expose --port=80 --replicas=5
service/nginx created
deployment.apps/nginx created

# kubectl get pod -o wide
NAME                     READY     STATUS    RESTARTS   AGE       IP            NODE             NOMINATED NODE
mysql-5fb6c74b86-4jl98   1/1       Running   0          3h        172.20.2.22   192.168.30.129   <none>
mysql-5fb6c74b86-d9qzg   1/1       Running   0          3h        172.20.2.21   192.168.30.129   <none>
mysql-5fb6c74b86-gwn9r   1/1       Running   0          3h        172.20.3.15   192.168.30.130   <none>
nginx-6f858d4d45-4r9b5   1/1       Running   0          24s       172.20.2.25   192.168.30.129   <none>
nginx-6f858d4d45-b98dj   1/1       Running   0          24s       172.20.2.24   192.168.30.129   <none>
nginx-6f858d4d45-m7q9m   1/1       Running   0          24s       172.20.3.18   192.168.30.130   <none>
nginx-6f858d4d45-q42l7   1/1       Running   0          24s       172.20.2.23   192.168.30.129   <none>
nginx-6f858d4d45-vmxq9   1/1       Running   0          24s       172.20.3.19   192.168.30.130   <none>

# kubectl drain 192.168.30.130 --ignore-daemonsets --delete-local-data                #排除192.168.30.130这个node
node/192.168.30.130 cordoned
WARNING: Ignoring DaemonSet-managed pods: kube-flannel-ds-amd64-mkxld; Deleting pods with local storage: metrics-server-75df6ff86f-dwphq, monitoring-grafana-c797777db-qtpsx
pod/monitoring-grafana-c797777db-qtpsx evicted
pod/nginx-6f858d4d45-vmxq9 evicted
pod/nginx-6f858d4d45-m7q9m evicted
pod/mysql-5fb6c74b86-gwn9r evicted
pod/metrics-server-75df6ff86f-dwphq evicted
pod/coredns-695f96dcd5-hc8hx evicted
pod/heapster-56c9dc749-t96kf evicted

# kubectl get pod -o wide
NAME                     READY     STATUS    RESTARTS   AGE       IP            NODE             NOMINATED NODE
mysql-5fb6c74b86-4jl98   1/1       Running   0          3h        172.20.2.22   192.168.30.129   <none>
mysql-5fb6c74b86-d9qzg   1/1       Running   0          3h        172.20.2.21   192.168.30.129   <none>
mysql-5fb6c74b86-h7f4j   1/1       Running   0          13s       172.20.2.26   192.168.30.129   <none>
nginx-6f858d4d45-4r9b5   1/1       Running   0          9m        172.20.2.25   192.168.30.129   <none>
nginx-6f858d4d45-5b2lv   1/1       Running   0          13s       172.20.2.28   192.168.30.129   <none>
nginx-6f858d4d45-b98dj   1/1       Running   0          9m        172.20.2.24   192.168.30.129   <none>
nginx-6f858d4d45-q42l7   1/1       Running   0          9m        172.20.2.23   192.168.30.129   <none>
nginx-6f858d4d45-zj2cv   1/1       Running   0          13s       172.20.2.27   192.168.30.129   <none>                #这里的pod全部运行在192.168.30.129上

# kubectl get node
NAME             STATUS                     ROLES     AGE       VERSION
192.168.30.129   Ready                      node      10d       v1.11.6
192.168.30.130   Ready,SchedulingDisabled   node      10d       v1.11.6
192.168.30.128   Ready,SchedulingDisabled   master    10d       v1.11.6
192.168.30.150   Ready,SchedulingDisabled   master    10d       v1.11.6

执行drain命令,发现这条命令做了两件事情:

  • 设定此node为不可调度状态(cordon)
  • evict(回收)了其上的三个pod

taint

kubectl taint用来更新一个或多个节点上的taint。该命令不常用。

节点亲和性是pod的一种属性(偏好或硬性要求),它使pod被吸引到一类特定的节点。taint 则相反,它使节点能够排斥一类特定的pod。

taint和toleration相互配合,可以用来避免pod被分配到不合适的节点上。每个节点上都可以应用一个或多个taint ,这表示对于那些不能容忍这些 taint 的 pod,是不会被该节点接受的。
如果将 toleration 应用于 pod 上,则表示这些 pod 可以(但不要求)被调度到具有匹配 taint 的节点上。


猜你喜欢

转载自blog.csdn.net/miss1181248983/article/details/88181434