kubectl常规命令操作,超级全

前言

Kubectl是管理k8s集群的命令行工具,通过生成的json格式传递给apiserver进行创建、查看、管理的操作。

kubectl --help帮助信息查看所有命令信息

[root@localhost bin]# kubectl --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         使用 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

查询命令

查看节点状态

[root@master ~]# kubectl get node                  
NAME            STATUS   ROLES    AGE   VERSION
192.168.100.5   Ready    <none>   23h   v1.12.3
192.168.100.6   Ready    <none>   23h   v1.12.3

查看etcd状态

[root@master ~]# kubectl get cs                     NAME                 STATUS    MESSAGE             ERROR
controller-manager   Healthy   ok
scheduler            Healthy   ok
etcd-0               Healthy   {"health":"true"}
etcd-2               Healthy   {"health":"true"}
etcd-1               Healthy   {"health":"true"}

查看pode

[root@master ~]# kubectl get pods                   查看pode
NAME                    READY   STATUS    RESTARTS   AGE
nginx-dbddb74b8-2zbmc   1/1     Running   0          22h
[root@master ~]#

查看service

kubectl get pods,svc

查看deployment

kubectl get deployment

查看所有

kubectl get all

项目的生命周期,创建–》发布–》更新–》回滚–》删除

创建 kubectl run命令

### 查看kubectl run命令用法
[root@master ~]# kubectl run --help
Create and run a particular image, possibly replicated.

Creates a deployment or job to manage the created container(s).

Examples:
  # Start a single instance of nginx.
  kubectl run nginx --image=nginx
.............................................
Usage:
  kubectl run NAME --image=image [--env="key=value"] [--port=port]
[--replicas=replicas] [--dry-run=bool] [--overrides=inline-json] [--command] --
[COMMAND] [args...] [options]

创建nginx2资源,无状态化资源

[root@master ~]# kubectl run nginx2 --image=nginx:latest --port=80 --replicas=3
kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.
deployment.apps/nginx2 created

查看创建的资源

[root@master ~]# kubectl get pods
NAME                      READY   STATUS    RESTARTS   AGE
nginx-dbddb74b8-2zbmc     1/1     Running   0          22h
nginx2-745b67fb4b-gs9sl   1/1     Running   0          47s
nginx2-745b67fb4b-vdp4x   1/1     Running   0          47s
nginx2-745b67fb4b-xf4s8   1/1     Running   0          47s

发布service 服务

kubectl expose 用法,指定类型

Usage:
  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]

-port 群集之间内部通讯的端口
--target-port 暴露出去的端口
 --type  指定类型,客户访问的IP类型
      --type='': Type for this service: ClusterIP, NodePort, LoadBalancer, or
ExternalName. Default is 'ClusterIP'.

默认是以   ClusterIP  容器群集IP  ,10.0.0.0 网段
我们设置是用   NodePort   node  节点IP 
 LoadBalancer   群集负载均衡的IP

发布

kubectl expose deployment nginx2 --port=80 --target-port=80 --name=nginx2-service --type=NodePort

[root@master ~]# kubectl expose deployment nginx2 --port=80 --target-port=80 --name=nginx2-service --type=NodePort
service/nginx2-service exposed
[root@master ~]# kubectl get pods,svc
NAME                          READY   STATUS    RESTARTS   AGE
pod/nginx-dbddb74b8-2zbmc     1/1     Running   0          22h
pod/nginx2-745b67fb4b-gs9sl   1/1     Running   0          15m
pod/nginx2-745b67fb4b-vdp4x   1/1     Running   0          15m
pod/nginx2-745b67fb4b-xf4s8   1/1     Running   0          15m

NAME                     TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
service/kubernetes       ClusterIP   10.0.0.1     <none>        443/TCP        23h
service/nginx2-service   NodePort    10.0.0.117   <none>        80:48075/TCP   31s
[root@master ~]#

缩写命令查看 kubectl api-resources

[root@master ~]# kubectl api-resources
NAME                              SHORTNAMES   APIGROUP                       NAMESPACEDKIND
bindings                                                                      trueBinding
componentstatuses                 cs                                          falseComponentStatus
configmaps                        cm                                          trueConfigMap
endpoints                         ep                                          trueEndpoints
events                            ev                                          trueEvent
limitranges                       limits                                      trueLimitRange
namespaces                        ns                                          falseNamespace
nodes                             no                                          falseNode
persistentvolumeclaims            pvc                                         truePersistentVolumeClaim
persistentvolumes                 pv                                          falsePersistentVolume
pods                              po                                          truePod
podtemplates                                                                  truePodTemplate
replicationcontrollers            rc                                          trueReplicationController
resourcequotas                    quota                                       trueResourceQuota
secrets                                                                       trueSecret
serviceaccounts                   sa                                          trueServiceAccount
services                          svc                                         trueService
mutatingwebhookconfigurations                  admissionregistration.k8s.io   falseMutatingWebhookConfiguration
validatingwebhookconfigurations                admissionregistration.k8s.io   falseValidatingWebhookConfiguration
customresourcedefinitions         crd,crds     apiextensions.k8s.io           falseCustomResourceDefinition
apiservices                                    apiregistration.k8s.io         falseAPIService
controllerrevisions                            apps                           trueControllerRevision
daemonsets                        ds           apps                           trueDaemonSet
deployments                       deploy       apps                           trueDeployment
replicasets                       rs           apps                           trueReplicaSet
statefulsets                      sts          apps                           trueStatefulSet
tokenreviews                                   authentication.k8s.io          falseTokenReview
localsubjectaccessreviews                      authorization.k8s.io           trueLocalSubjectAccessReview
selfsubjectaccessreviews                       authorization.k8s.io           falseSelfSubjectAccessReview
selfsubjectrulesreviews                        authorization.k8s.io           falseSelfSubjectRulesReview
subjectaccessreviews                           authorization.k8s.io           falseSubjectAccessReview
horizontalpodautoscalers          hpa          autoscaling                    trueHorizontalPodAutoscaler
cronjobs                          cj           batch                          trueCronJob
jobs                                           batch                          trueJob
certificatesigningrequests        csr          certificates.k8s.io            falseCertificateSigningRequest
leases                                         coordination.k8s.io            trueLease
events                            ev           events.k8s.io                  trueEvent
daemonsets                        ds           extensions                     trueDaemonSet
deployments                       deploy       extensions                     trueDeployment
ingresses                         ing          extensions                     trueIngress
networkpolicies                   netpol       extensions                     trueNetworkPolicy
podsecuritypolicies               psp          extensions                     falsePodSecurityPolicy
replicasets                       rs           extensions                     trueReplicaSet
networkpolicies                   netpol       networking.k8s.io              trueNetworkPolicy
poddisruptionbudgets              pdb          policy                         truePodDisruptionBudget
podsecuritypolicies               psp          policy                         falsePodSecurityPolicy
clusterrolebindings                            rbac.authorization.k8s.io      falseClusterRoleBinding
clusterroles                                   rbac.authorization.k8s.io      falseClusterRole
rolebindings                                   rbac.authorization.k8s.io      trueRoleBinding
roles                                          rbac.authorization.k8s.io      trueRole
priorityclasses                   pc           scheduling.k8s.io              falsePriorityClass
storageclasses                    sc           storage.k8s.io                 falseStorageClass
volumeattachments                              storage.k8s.io                 falseVolumeAttachment

查看创建资源关联的后端节点,负载均衡分布

[root@master ~]# kubectl get endpoints
NAME             ENDPOINTS                                      AGE
kubernetes       192.168.100.3:6443,192.168.100.8:6443          23h
nginx2-service   172.17.22.3:80,172.17.71.3:80,172.17.71.4:80   3m18s

查看真实node 节点的分布,发现已经自动负载均衡分布在两个节点上

[root@master ~]# kubectl get pods -o wide
NAME                      READY   STATUS    RESTARTS   AGE   IP            NODENOMINATED NODE
nginx-dbddb74b8-2zbmc     1/1     Running   0          22h   172.17.22.2   192.168.100.5<none>
nginx2-745b67fb4b-gs9sl   1/1     Running   0          21m   172.17.71.4   192.168.100.6<none>
nginx2-745b67fb4b-vdp4x   1/1     Running   0          21m   172.17.71.3   192.168.100.6<none>
nginx2-745b67fb4b-xf4s8   1/1     Running   0          21m   172.17.22.3   192.168.100.5<none>

Kubectl-proxy 负载均衡的验证

//kubernetes里kube-proxy支持三种模式,在v1.8之前我们使用的是iptables 以及 userspace两种模式,在kubernetes 1.8之后引入了ipvs模式

[root@master ~]# kubectl get pods,svc -o wide
NAME                          READY   STATUS    RESTARTS   AGE   IP            NODE    NOMINATED NODE
pod/nginx-dbddb74b8-2zbmc     1/1     Running   0          22h   172.17.22.2   192.168.100.5   <none>
pod/nginx2-745b67fb4b-gs9sl   1/1     Running   0          23m   172.17.71.4   192.168.100.6   <none>
pod/nginx2-745b67fb4b-vdp4x   1/1     Running   0          23m   172.17.71.3   192.168.100.6   <none>
pod/nginx2-745b67fb4b-xf4s8   1/1     Running   0          23m   172.17.22.3   192.168.100.5   <none>

NAME                     TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE     SELECTOR
service/kubernetes       ClusterIP   10.0.0.1     <none>        443/TCP        24h     <none>
service/nginx2-service   NodePort    10.0.0.117   <none>        80:48075/TCP   8m51s   run=nginx2
[root@master ~]#kubectl expose deployment nginx2 --port=80 --target-port=80 --name=nginx2-service --type=NodePort
Pode  群集内部端口   
Target  Pode 暴露的端口 
48075 负载均衡器对外提供的端口

在这里插入图片描述

安装负载均衡器 ipvsadm 软件,查看负载均衡详细信息,及测试

[root@node2 ~]# yum install ipvsadm -y    
查看负载均衡详细信息
[root@node2 ~]# ipvsadm -L -n

在这里插入图片描述
[root@node1 ~]# ipvsadm -L -n
在这里插入图片描述
访问节点测试
在这里插入图片描述

查看日志
[root@master ~]# kubectl logs nginx2-745b67fb4b-xf4s8
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
172.17.22.1 - - [11/Oct/2020:02:22:37 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36" "-"
2020/10/11 02:22:37 [error] 28#28: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 172.17.22.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.100.5:48075", referrer: "http://192.168.100.5:48075/"
172.17.22.1 - - [11/Oct/2020:02:22:37 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "http://192.168.100.5:48075/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36" "-"

业务更新

在这里插入图片描述
在这里插入图片描述

kubectl set 获取修改模板

[root@master ~]# kubectl set --help
Configure application resources

These commands help you make changes to existing application resources.

Available Commands:
  env            Update environment variables on a pod template
  image          更新一个 pod template 的镜像
  resources      在对象的 pod templates 上更新资源的 requests/limits
  selector       设置 resource 的 selector
  serviceaccount Update ServiceAccount of a resource
  subject        Update User, Group or ServiceAccount in a
RoleBinding/ClusterRoleBinding

Usage:
  kubectl set SUBCOMMAND [options]

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

将nginx 版本更新到1.14

[root@master ~] kubectl set image deployment/nginx2 nginx2=nginx:1.14
deployment.extensions/nginx2 image updated

动态监控查看pode -w

[root@master ~]# kubectl get pods -w   
NAME                      READY   STATUS              RESTARTS   AGE
nginx-dbddb74b8-2zbmc     1/1     Running             0          23h
nginx2-745b67fb4b-gs9sl   1/1     Running             0          58m
nginx2-745b67fb4b-vdp4x   1/1     Running             0          58m
nginx2-745b67fb4b-xf4s8   1/1     Running             0          58m
nginx2-766d4c6689-ggsrf   0/1     ContainerCreating   0          3s
nginx2-766d4c6689-ggsrf   1/1   Running   0     16s
nginx2-745b67fb4b-gs9sl   1/1   Terminating   0     58m
nginx2-766d4c6689-rpr7d   0/1   Pending   0     0s
nginx2-766d4c6689-rpr7d   0/1   Pending   0     0s
nginx2-766d4c6689-rpr7d   0/1   ContainerCreating   0     0s
nginx2-745b67fb4b-gs9sl   0/1   Terminating   0     58m
nginx2-745b67fb4b-gs9sl   0/1   Terminating   0     58m
nginx2-745b67fb4b-gs9sl   0/1   Terminating   0     58m
nginx2-766d4c6689-rpr7d   1/1   Running   0     10s
nginx2-745b67fb4b-vdp4x   1/1   Terminating   0     58m
nginx2-766d4c6689-5ttmd   0/1   Pending   0     0s
nginx2-766d4c6689-5ttmd   0/1   Pending   0     0s
nginx2-766d4c6689-5ttmd   0/1   ContainerCreating   0     0s
nginx2-745b67fb4b-vdp4x   0/1   Terminating   0     58m
nginx2-745b67fb4b-vdp4x   0/1   Terminating   0     58m
nginx2-745b67fb4b-vdp4x   0/1   Terminating   0     58m
nginx2-766d4c6689-5ttmd   1/1   Running   0     4s
nginx2-745b67fb4b-xf4s8   1/1   Terminating   0     58m
nginx2-745b67fb4b-xf4s8   0/1   Terminating   0     58m
nginx2-745b67fb4b-xf4s8   0/1   Terminating   0     58m
nginx2-745b67fb4b-xf4s8   0/1   Terminating   0     58m
^C[root@master ~]# kubectl get pods
NAME                      READY   STATUS    RESTARTS   AGE
nginx-dbddb74b8-2zbmc     1/1     Running   0          23h
nginx2-766d4c6689-5ttmd   1/1     Running   0          41s
nginx2-766d4c6689-ggsrf   1/1     Running   0          67s
nginx2-766d4c6689-rpr7d   1/1     Running   0          51s
[root@master ~]#

在这里插入图片描述

查看历史版本详解

[root@master ~]# kubectl rollout --help
Manage the rollout of a resource.

Valid resource types include:

  * deployments
  * daemonsets
  * statefulsets

Examples:
  # Rollback to the previous deployment
  kubectl rollout undo deployment/abc

  # Check the rollout status of a daemonset
  kubectl rollout status daemonset/foo

Available Commands:
  history     显示 rollout 历史
  pause       标记提供的 resource 为中止状态
  resume      继续一个停止的 resource
  status      显示 rollout 的状态
  undo        撤销上一次的 rollout

Usage:
  kubectl rollout SUBCOMMAND [options]

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

查看历史版本记录

[root@master ~]# kubectl rollout history deployment/nginx2
deployment.extensions/nginx2
REVISION  CHANGE-CAUSE
1         <none>
2         <none>

开始回滚

[root@master ~]#  kubectl rollout undo deployment/nginx2
deployment.extensions/nginx2

查看回滚状态

[root@master ~]#  kubectl rollout status deployment/nginx
deployment "nginx" successfully rolled out
[root@master ~]#

在这里插入图片描述

资源的删除

查看资源

[root@master ~]# kubectl get pods
NAME                      READY   STATUS    RESTARTS   AGE
nginx-dbddb74b8-2zbmc     1/1     Running   0          23h
nginx2-745b67fb4b-29czz   1/1     Running   0          3m23s
nginx2-745b67fb4b-hhdx8   1/1     Running   0          3m27s
nginx2-745b67fb4b-kvv67   1/1     Running   0          3m30s

查看 deployment资源

[root@master ~]# kubectl get deploy
NAME     DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
nginx    1         1         1            1           23h
nginx2   3         3         3            3           67m

删除deployment资源

[root@master ~]# kubectl delete deployment/nginx
deployment.extensions "nginx" deleted
[root@master ~]# kubectl delete deployment/nginx2
deployment.extensions "nginx2" deleted
[root@master ~]# kubectl get pods
No resources found.
查看service 资源
[root@master ~]# kubectl get svc
NAME             TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
kubernetes       ClusterIP   10.0.0.1     <none>        443/TCP        24h
nginx2-service   NodePort    10.0.0.117   <none>        80:48075/TCP   54m

删除nginx2-service 资源

[root@master ~]# kubectl delete svc/nginx2-service
service "nginx2-service" deleted
[root@master ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.0.0.1     <none>        443/TCP   24h
查看pods ,发现都删除干净了
[root@master ~]# kubectl get pods
No resources found.

创建pods资源

[root@master ~]# kubectl run nginx --image nginx:latest --port=80 --replicas=3
kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.
deployment.apps/nginx created

查看资源

[root@master ~]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-7697996758-gzqms   1/1     Running   0          25s
nginx-7697996758-j6tfj   1/1     Running   0          25s
nginx-7697996758-ldfvx   1/1     Running   0          25s
[root@master ~]#

查看资源的详细信息

[root@master ~]# kubectl describe pode/nginx-7697996758-gzqms
error: the server doesn't have a resource type "pode"
[root@master ~]# kubectl describe pods/nginx-7697996758-gzqms
Name:               nginx-7697996758-gzqms
Namespace:          default
Priority:           0
PriorityClassName:  <none>
Node:               192.168.100.6/192.168.100.6
Start Time:         Sun, 11 Oct 2020 10:55:00 +0800
Labels:             pod-template-hash=7697996758
                    run=nginx
Annotations:        <none>
Status:             Running
IP:                 172.17.71.3
Controlled By:      ReplicaSet/nginx-7697996758
Containers:
  nginx:
    Container ID:   docker://a6ea25c26eba520fb356019c93695cb779ab94c8344a5c84821238a9853270e7
    Image:          nginx:latest
    Image ID:       docker-pullable://nginx@sha256:fc66cdef5ca33809823182c9c5d72ea86fd2cef7713cf3363e1a0b12a5d77500
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Sun, 11 Oct 2020 10:55:05 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-rd8b7 (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             True
  ContainersReady   True
  PodScheduled      True
Volumes:
  default-token-rd8b7:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-rd8b7
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type    Reason     Age    From                    Message
  ----    ------     ----   ----                    -------
  Normal  Scheduled  3m59s  default-scheduler       Successfully assigned default/nginx-7697996758-gzqms to 192.168.100.6
  Normal  Pulling    3m59s  kubelet, 192.168.100.6  pulling image "nginx:latest"
  Normal  Pulled     3m54s  kubelet, 192.168.100.6  Successfully pulled image "nginx:latest"
  Normal  Created    3m54s  kubelet, 192.168.100.6  Created container
  Normal  Started    3m54s  kubelet, 192.168.100.6  Started container

查看 deployment 资源信息

[root@master ~]# kubectl describe deployment/nginx
Name:                   nginx
Namespace:              default
CreationTimestamp:      Sun, 11 Oct 2020 10:55:00 +0800
Labels:                 run=nginx
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               run=nginx
Replicas:               3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  run=nginx
  Containers:
   nginx:
    Image:        nginx:latest
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   nginx-7697996758 (3/3 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  8m4s  deployment-controller  Scaled up replica set nginx-7697996758 to 3

进入pode 容器

[root@master ~]# kubectl exec -it nginx-7697996758-gzqms bash
root@nginx-7697996758-gzqms:/# ls
bin   docker-entrypoint.d   home   media  proc	sbin  tmp
boot  docker-entrypoint.sh  lib    mnt	  root	srv   usr
dev   etc		    lib64  opt	  run	sys   var
root@nginx-7697996758-gzqms:/#

exit  退出
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: kubectl drain命令用于将节点调度器上的Pod删除或迁移到其他节点,以便可以安地维护节点。该命令通常在需要升级节点或替换节点时使用。 使用该命令时,Kubernetes会阻止新的Pod在节点上运行,同时将节点上现有的Pod删除或迁移到其他节点上。在执行该命令后,节点将被标记为不可调度状态,直到维护完成并且命令被撤销。 该命令的基本语法如下所示: ``` kubectl drain <node name> ``` 其中,`<node name>`是要维护的节点的名称。 需要注意的是,该命令默认情况下只会删除普通的Pod,而不会删除DaemonSet和StatefulSet类型的Pod。如果需要删除这些类型的Pod,可以使用`--ignore-daemonsets`和`--delete-local-data`选项。例如: ``` kubectl drain <node name> --ignore-daemonsets --delete-local-data ``` ### 回答2: kubectl drain命令是用于将节点上的工作负载迁移或删除的命令。当需要对节点进行维护、升级或者下线时,可以使用kubectl drain命令。 使用kubectl drain命令会触发下列操作: 1. 移除节点上的Pod:使用该命令会将节点上的所有非静态Pod删除或迁移到其他可用节点上。 2. 阻止调度新的Pod:命令执行之后,节点将不再接受新的Pod调度请求,已有Pod也不会在节点上重新调度。 3. 与节点上的kubelet协同工作:命令会与节点上的kubelet通信,用于删除Pod并将节点状态更新为不可调度。 使用kubectl drain命令时,可以加上一些选项来定制操作: - delete-local-data:删除本地数据目录,即删除节点上的Pod信息和数据。 - ignore-daemonsets:忽略DaemonSet控制器,即不迁移该类Pod,保持它们在原节点上运行。 - force:强制执行命令,即不等待Pod中未处理的请求或者正在运行的任务。 使用kubectl drain命令需要注意以下几点: 1. 应该在执行前进行详细计划和通知,确保工作负载迁移到其他节点后能够正常运行。 2. 应该确保有足够的可用节点来接收迁移的Pod,避免出现工作负载无法调度的情况。 3. 在执行命令过程中,可以使用kubectl uncordon命令来恢复节点的可调度状态。 总之,kubectl drain命令是一种有效管理节点上工作负载迁移的方式,可以帮助我们对节点进行维护和管理。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值