kubectl 命令速查手册

myPod

  1. ---
  2. kind: Pod
  3. apiVersion: v1
  4. metadata:
  5. name: mypod
  6. spec:
  7. terminationGracePeriodSeconds: 0
  8. containers:
  9. - name: linux
  10. image: busybox:latest
  11. imagePullPolicy: IfNotPresent
  12. command: ["sh", "-c"]
  13. args:
  14. - |
  15. echo "hello world !!!" >/var/www/index.html
  16. httpd -v -f -p 0.0.0.0:80 -h /var/www
  17. restartPolicy: Always

myDeploy

---
kind: Deployment
apiVersion: apps/v1
metadata:
  name: myweb
spec:
  replicas: 1
  selector:
    matchLabels:
      app: apache
  template:
    metadata:
      labels:
        app: apache
    spec:
      containers:
      - name: httpd
        image: myos:httpd
        imagePullPolicy: IfNotPresent
        ports:
        - protocol: TCP
          containerPort: 80
        resources:
          requests:
            memory: "32Mi"
            cpu: "10m"
      restartPolicy: Always

annotate

# 更新资源所关联的注释信息
#-----------------------------------------#
[root@master K8S]# kubectl apply -f mypod.yaml --record
[root@master K8S]# kubectl get pod mypod -o custom-columns=podName:.metadata.name,annotations:.metadata.annotations."kubernetes\.io/change-cause"
podName   annotations
mypod     kubectl apply --filename=mypod.yaml --record=true
[root@master K8S]# kubectl annotate pods mypod kubernetes.io/change-cause='my description'
pod/mypod annotated
[root@master K8S]# kubectl get pod mypod -o custom-columns=podName:.metadata.name,annotations:.metadata.annotations."kubernetes\.io/change-cause"
podName   annotations
mypod     my description

api-resources

# 显示服务器上所支持的 API 资源
# -o wide 可以用来查询资源权限
#-----------------------------------------#
[root@master K8S]# kubectl api-resources -o wide
NAME         SHORTNAMES   APIVERSION       NAMESPACED   KIND         VERBS
pods         po           v1               true         Pod          [get list patch ...]
namespaces   ns           v1               false        Namespace    [create get ...]

api-versions

# 显示服务端所支持的 API 版本
#-----------------------------------------#
[root@master K8S]# kubectl api-versions
admissionregistration.k8s.io/v1
apps/v1
... ...
v1

apply

# 读取资源文件,将新的配置应用到资源上
#-----------------------------------------#
[root@master K8S]# 
[root@master K8S]# kubectl apply -f mypod.yaml 
pod/mypod created
[root@master K8S]# sed 's,mypod,myweb,g' mypod.yaml |kubectl apply -f -
pod/myweb created
[root@master K8S]# kubectl get pods
NAME    READY   STATUS    RESTARTS   AGE
mypod   1/1     Running   0          36s
myweb   1/1     Running   0          4s

attach

# 连接一个正在运行的容器的启动进程
#-----------------------------------------#
[root@master K8S]# kubectl attach mypod -c linux
If you don't see a command prompt, try pressing enter.
10.244.219.64:44372: response:200

auth

# 检查授权信息
#-----------------------------------------#
[root@master K8S]# kubectl --kubeconfig=admin.conf auth can-i get pods 
yes
[root@master K8S]# kubectl --kubeconfig=auth.conf  auth can-i get pods 
no

autoscale

# 创建一个HPA控制器,对资源对象进行自动扩缩
#-----------------------------------------#
[root@master K8S]# kubectl apply -f myDeploy.yaml
deployment.apps/myweb created
[root@master K8S]# kubectl autoscale deployment myweb --min=1 --max=10 --cpu-percent=80
horizontalpodautoscaler.autoscaling/myweb autoscaled
[root@master K8S]# kubectl get horizontalpodautoscalers.autoscaling 
NAME    REFERENCE          TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
myweb   Deployment/myweb   10%/80%   1         10        1          27m
#-----------------------------------------#
---
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: myweb
spec:
  minReplicas: 1
  maxReplicas: 10
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: myweb
  targetCPUUtilizationPercentage: 80

certificate

# 修改证书资源
#-----------------------------------------#
[root@master K8S]# kubectl get certificatesigningrequests
NAME        AGE   REQUESTOR            CONDITION
csr-wsfz7   8s    system:node:master   Pending
[root@master K8S]# kubectl certificate approve csr-wsfz7
[root@master K8S]# kubectl get certificatesigningrequests
NAME        AGE   REQUESTOR            CONDITION
csr-wsfz7   86s   system:node:master   Approved,Issued

cluster-info

# 显示集群信息
#-----------------------------------------#
[root@master K8S]# kubectl cluster-info 
Kubernetes control plane is running at https://192.168.1.10:6443
CoreDNS is running at https://192.168.1.10:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

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

completion

# 根据已经给出的 Shell 输出 <Tab> 补全代码
#-----------------------------------------#
[root@master K8S]# kubectl completion bash >/etc/bash_completion.d/kubectl
[root@master K8S]# source <(kubectl completion bash)

config

# 配置管理 kubeconfig 文件
# 创建普通认证用户中的 CN 代表用户名,O 代表组名称
#-----------------------------------------#
[root@master K8S]# openssl genrsa -out luck.key 2048
[root@master K8S]# openssl req -new -key luck.key -out luck.csr -subj "/CN=luck/O=tedu"
[root@master K8S]# mycsr=$(base64 luck.csr |tr -d '\n')
[root@master K8S]# cat <<EOF |kubectl apply -f -
---
apiVersion: certificates.k8s.io/v1
kind: CertificateSigningRequest
metadata:
  name: luck-token
spec:
  groups:
  - system:authenticated
  request: ${mycsr}
  signerName: kubernetes.io/kube-apiserver-client
  usages:
  - client auth
EOF
[root@master K8S]# kubectl get certificatesigningrequests.certificates.k8s.io
NAME         AGE   SIGNERNAME                            ... CONDITION
luck-token   12s   kubernetes.io/kube-apiserver-client   ... Pending
[root@master K8S]# kubectl certificate approve luck-token
[root@master K8S]# kubectl get certificatesigningrequests.certificates.k8s.io
NAME         AGE   SIGNERNAME                            ... CONDITION
luck-token   33s   kubernetes.io/kube-apiserver-client   ... Approved,Issued
[root@master K8S]# kubectl get certificatesigningrequests.certificates.k8s.io luck-token -o jsonpath='{.status.certificate}'| base64 -d >luck.crt
[root@master K8S]# kubectl config --kubeconfig=auth.conf set-cluster k8s-cluster --server=https://192.168.1.10:6443 --certificate-authority=/etc/kubernetes/pki/ca.crt --embed-certs=true
[root@master K8S]# kubectl config --kubeconfig=auth.conf set-credentials adminuser-luck --client-certificate=luck.crt --client-key=luck.key --embed-certs=true
[root@master K8S]# kubectl config --kubeconfig=auth.conf set-context node-cluster --cluster=k8s-cluster --user=adminuser-luck --namespace=default
[root@master K8S]# kubectl config --kubeconfig=auth.conf use-context node-cluster
[root@master K8S]# kubectl create clusterrolebinding luckrole --clusterrole=cluster-admin --user=luck
[root@master K8S]# kubectl config --kubeconfig=auth.conf get-clusters 
NAME
k8s-cluster
[root@master K8S]# kubectl config --kubeconfig=auth.conf get-users 
NAME
adminuser-luck
[root@master K8S]# kubectl config --kubeconfig=auth.conf get-contexts 
CURRENT   NAME           CLUSTER       AUTHINFO         NAMESPACE
*         node-cluster   k8s-cluster   adminuser-luck   default

convert

# 在不同的 API 版本之间转换配置文件
# 在高版本中已经删除了
#-----------------------------------------#
[root@master K8S]# kubectl convert -f myPod.yaml

cordon

# 标记节点为不可调度的
#-----------------------------------------#
[root@master K8S]# kubectl get nodes
NAME        STATUS   ROLES    AGE   VERSION
master      Ready    master   15h   v1.22.3
node-0001   Ready    node     15h   v1.22.3
[root@master K8S]# kubectl cordon node-0001 
node/node-0001 cordoned
[root@master K8S]# kubectl get nodes
NAME        STATUS                     ROLES    AGE   VERSION
master      Ready                      master   15h   v1.22.3
node-0001   Ready,SchedulingDisabled   node     15h   v1.22.3

cp

# 将文件和目录拷入/拷出容器
#-----------------------------------------#
[root@master K8S]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
myweb-759ffdd494-9956m   1/1     Running   0          5h13m
[root@master K8S]# kubectl cp myweb-759ffdd494-9956m:/var/www/html/index.html ./index.html
tar: Removing leading `/' from member names
[root@master K8S]# ls index.html 
index.html
[root@master K8S]# echo "hello world" > index.html 
[root@master K8S]# kubectl cp index.html myweb-759ffdd494-9956m:/var/www/html/index.html
[root@master K8S]# curl http://10.244.21.168
hello world

create

# 通过文件或标准输入来创建资源或用来生成资源文件
#-----------------------------------------#
[root@master K8S]# kubectl create namespace testapp
[root@master K8S]# kubectl create namespace testapp --dry-run=client -o yaml
apiVersion: v1
kind: Namespace
metadata:
  creationTimestamp: null
  name: testapp
spec: {}
status: {}

debug

# 创建调试会话,使用附加容器引用目标资源
# 所有节点都需要打开临时容器特性
#-----------------------------------------#
[root@master K8S]# kubectl run myapp --image=registry:5000/k8s/pause:3.5 --restart=Never
[root@master K8S]# kubectl debug myapp -it --image=registry:5000/busybox:latest --share-processes --copy-to=debugger
/ # pstree -p
pause(1)
#-----------------------------------------#
[root@master K8S]# vim /etc/kubernetes/manifests/kube-apiserver.yaml
    - --feature-gates=EphemeralContainers=true
[root@master K8S]# vim /var/lib/kubelet/config.yaml
featureGates:
  EphemeralContainers: true
[root@master K8S]# systemctl restart kubelet
[root@master K8S]# kubectl run myapp --image=registry:5000/k8s/pause:3.5 --restart=Never
pod/myapp created
[root@master K8S]# kubectl debug -it myapp --image=registry:5000/busybox:latest --target=myapp
~ # pstree -p 0
?(0)-+-pause(1)
     `-sh(7)---pstree(27)
~ # ls /proc/1/root/
dev    etc    pause  proc   sys    var
~ #

delete

# 通过文件名或资源和名字删除资源
#-----------------------------------------#
[root@master K8S]# kubectl get pods
NAME       READY   STATUS    RESTARTS     AGE
debugger   2/2     Running   1 (3s ago)   6s
mypod      1/1     Running   0            40s
[root@master K8S]# kubectl delete pod debugger 
pod "debugger" deleted
[root@master K8S]# kubectl delete -f mypod.yaml 
pod "mypod" deleted
[root@master K8S]# kubectl get pods
No resources found in default namespace.

describe

# 显示某个资源或某组资源的详细信息
#-----------------------------------------#
[root@master K8S]# kubectl describe pod mypod 
Name:         mypod
Namespace:    default
Priority:     0
Node:         node-0001/192.168.1.11
...

diff

# 显示目前版本与将要应用的版本之间的差异
#-----------------------------------------#
[root@master K8S]# kubectl diff -f deploy.yaml 
diff -u -N /tmp/LIVE-242154721/apps.v1.Deployment.default.myweb /tmp/MERGED-718616268/apps.v1.Deployment.default.myweb
--- /tmp/LIVE-242154721/apps.v1.Deployment.default.myweb    2021-11-07 15:52:02.711915439 +0800
+++ /tmp/MERGED-718616268/apps.v1.Deployment.default.myweb  2021-11-07 15:52:02.711915439 +0800
@@ -5,9 +5,9 @@
     deployment.kubernetes.io/revision: "1"
-    kubernetes.io/change-cause: httpd.v1
+    kubernetes.io/change-cause: httpd.v2
   creationTimestamp: "2021-11-07T07:51:49Z"
-  generation: 1
+  generation: 2
   managedFields:
   - apiVersion: apps/v1
     fieldsType: FieldsV1
...

drain

# 清空节点,节点资源被删除也不能被调度
#-----------------------------------------#
[root@master K8S]# kubectl get nodes
NAME        STATUS   ROLES    AGE   VERSION
master      Ready    master   36h   v1.22.3
node-0001   Ready    node     36h   v1.22.3
[root@master K8S]# kubectl drain node-0001 --delete-emptydir-data --ignore-daemonsets --force
[root@master K8S]# kubectl get nodes
NAME        STATUS                     ROLES    AGE   VERSION
master      Ready                      master   36h   v1.22.3
node-0001   Ready,SchedulingDisabled   node     36h   v1.22.3

edit

# 修改服务器上的某资源
#-----------------------------------------#
[root@master K8S]# kubectl edit pod mypod
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
kind: Pod
...

exec

# 在一个正在运行的容器中执行命令
#-----------------------------------------#
[root@master K8S]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
myweb-759ffdd494-9956m   1/1     Running   0          27m
[root@master K8S]# kubectl exec -it myweb-759ffdd494-9956m -c httpd -- /bin/bash
[root@myweb-759ffdd494-9956m html]# ls
index.html  info.html  info.php

explain

# 显示资源的帮助信息
#-----------------------------------------#
[root@master K8S]# kubectl explain pod.spec
KIND:     Pod
VERSION:  v1

RESOURCE: spec <Object>

DESCRIPTION:
     Specification of the desired behavior of the pod. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

     PodSpec is a description of a pod.
...

expose

# 为资源创建 service
#-----------------------------------------#
[root@master K8S]# [root@master K8S]# kubectl expose deployment myweb --port=80 --protocol=TCP --target-port=80 --name=webservice --type ClusterIP
#-----------------------------------------#
---
apiVersion: v1
kind: Service
metadata:
  name: webservice
spec:
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
  selector:
    app: apache
  type: ClusterIP
#-----------------------------------------#
[root@master K8S]# kubectl get service
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.254.0.1      <none>        443/TCP   36h
webservice   ClusterIP   10.254.17.185   <none>        80/TCP    2s
[root@master K8S]# curl http://10.254.17.185
hello world.

get

# 显示一个或者多个资源信息
#-----------------------------------------#
[root@master K8S]# kubectl get nodes
ku  NAME        STATUS   ROLES    AGE   VERSION
master      Ready    master   36h   v1.22.3
node-0001   Ready    node     36h   v1.22.3
[root@master K8S]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
mypod                    1/1     Running   0          5m9s
[root@master K8S]# kubectl get pod mypod -o wide
NAME    READY   STATUS    RESTARTS   IP              ...
mypod   1/1     Running   0          10.244.21.154   ...
[root@master K8S]# kubectl get pod mypod -o yaml
apiVersion: v1
kind: Pod
metadata:
...

help

# 显示帮助信息
#-----------------------------------------#
[root@master K8S]# kubectl help run
Create and run a particular image in a pod.

Examples:
  # Start a nginx pod
  kubectl run nginx --image=nginx

kustomize

# 从目录或远程 URL 中构建 kustomization
# https://cloud.tencent.com/developer/article/1745189
#-----------------------------------------#
[root@master K8S]#

label

# 更新资源的标签
#-----------------------------------------#
[root@master K8S]# kubectl get pods --show-labels 
NAME    READY   STATUS    RESTARTS   AGE     LABELS
mypod   1/1     Running   0          7m22s   <none>
[root@master K8S]# kubectl label pod mypod app=webapp
pod/mypod labeled
[root@master K8S]# kubectl get pods --show-labels 
NAME    READY   STATUS    RESTARTS   AGE     LABELS
mypod   1/1     Running   0          7m41s   app=webapp
[root@master K8S]# kubectl label pod mypod app-
pod/mypod labeled
[root@master K8S]# kubectl get pods --show-labels 
NAME    READY   STATUS    RESTARTS   AGE   LABELS
mypod   1/1     Running   0          68m   <none>

logs

# 显示 pod 中某容器的日志
#-----------------------------------------#
[root@master K8S]# kubectl get pod
NAME                     READY   STATUS    RESTARTS   AGE
mypod                    1/1     Running   0          5h4m
[root@master K8S]# kubectl logs mypod -c linux
10.244.219.64:34666: response:200

options

# 显示所有命令都支持的共有参数列表
#-----------------------------------------#
[root@master K8S]# kubectl options 
The following options can be passed to any command:

      --insecure-skip-tls-verify=false: If true, the server's certificate will not be checked for validity. This will
...

patch

# 基于策略性合并修补规则更新某资源中的字段
#-----------------------------------------#
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: mypv
spec:
  capacity:
    storage: 5Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Recycle
  hostPath:
    path: /var/webroot
    type: DirectoryOrCreate
#-----------------------------------------#
[root@master K8S]# kubectl get pv
NAME   CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      AGE
mypv   5Gi        RWO            Recycle          Available   5s
[root@master K8S]# kubectl patch pv mypv -p '{"spec":{"capacity":{"storage":"8Gi"}}}'
persistentvolume/mypv patched
[root@master K8S]# kubectl get pv
NAME   CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      AGE
mypv   8Gi        RWO            Recycle          Available   67s

plugin

# 运行命令行插件
# 插件在 ${PATH} 下,是一个独立的可执行文件,名称以 kubectl- 开头
#-----------------------------------------#
[root@master K8S]# vim /usr/local/bin/kubectl-gettaint
#!/bin/bash
/usr/bin/kubectl get nodes -o custom-columns=NodeName:.metadata.name,Taints:.spec.taints
[root@master K8S]# chmod 755 /usr/local/bin/kubectl-gettaint
[root@master K8S]# kubectl plugin list
The following compatible plugins are available:
/usr/local/bin/kubectl-gettaint
[root@master K8S]# kubectl gettaint
NodeName    Taints
master      [map[effect:NoSchedule key:node-role.kubernetes.io/master]]
node-0001   <none>

port-forward

# 将一个或者多个本地端口转发到 pod
#-----------------------------------------#
[root@master K8S]# kubectl port-forward --address 0.0.0.0 pod/mypod 8080 80
Forwarding from 0.0.0.0:8080 -> 8080
Forwarding from 0.0.0.0:80 -> 80
#-----------------------------------------#
[root@master local]# curl http://master:8080
hello world.

proxy

# 运行一个 kubernetes API 服务器代理
#-----------------------------------------#
[root@master K8S]# kubectl proxy --port=80
Starting to serve on 127.0.0.1:80
#-----------------------------------------#
[root@master K8S]# curl http://127.0.0.1/version
{
  "major": "1",
  "minor": "22",
  "gitVersion": "v1.22.3",
  "gitCommit": "c92036820499fedefec0f847e2054d824aea6cd1",
  "gitTreeState": "clean",
  "buildDate": "2021-10-27T18:35:25Z",
  "goVersion": "go1.16.9",
  "compiler": "gc",
  "platform": "linux/amd64"
}

replace

# 基于文件名或标准输入替换资源
#-----------------------------------------#
[root@master K8S]# kubectl replace --force -f mypod.yaml 
pod "mypod" deleted
pod/mypod replaced

rollout

# 管理资源的上线
#-----------------------------------------#
[root@master K8S]# kubectl rollout history deployment 
deployment.apps/myweb 
REVISION  CHANGE-CAUSE
1         httpd.v1
2         httpd.v2
[root@master K8S]# kubectl rollout undo deployment myweb --to-revision=1
deployment.apps/myweb rolled back
[root@master K8S]# kubectl rollout history deployment 
deployment.apps/myweb 
REVISION  CHANGE-CAUSE
2         httpd.v2
3         httpd.v1

run

# 在集群中使用指定镜像启动容器
#-----------------------------------------#
[root@master K8S]# kubectl run mypod --image=registry:5000/myos:httpd
#-----------------------------------------#
---
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: mypod
  name: mypod
spec:
  containers:
  - image: registry:5000/myos:httpd
    name: mypod
  restartPolicy: Always

scale

# 为可扩充资源设置一个新副本数量
#-----------------------------------------#
[root@master K8S]# kubectl apply -f myDeploy.yaml
deployment.apps/myweb created
[root@master ~]# kubectl get deployments.apps 
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
myweb   1/1     1            1           21m
[root@master ~]# kubectl scale deployment myweb --replicas=3
deployment.apps/myweb scaled
[root@master ~]# kubectl get deployments.apps 
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
myweb   3/3     3            3           21m

set

# 为资源对象设置功能特性
#-----------------------------------------#
[root@master K8S]# kubectl set env pods --all --list
# Pod mypod, container linux
[root@master K8S]# kubectl set env deployment/myweb myEnv=prod
deployment.apps/myweb env updated
[root@master K8S]# kubectl exec -i -t myweb-6c645646c9-pqjc7 -- sh -c 'echo ${myEnv}'
prod
[root@master K8S]# kubectl get deployments.apps myweb -o wide
NAME    READY   AGE   CONTAINERS   IMAGES                     SELECTOR
myweb   1/1     25s   httpd        registry:5000/myos:httpd   app=apache
[root@master K8S]# kubectl set image deployment/myweb httpd=registry:5000/myos:nginx
deployment.apps/myweb image updated
[root@master K8S]# kubectl get deployments.apps myweb -o wide
NAME    READY   AGE   CONTAINERS   IMAGES                     SELECTOR
myweb   1/1     45s   httpd        registry:5000/myos:nginx   app=apache

taint

# 在一个或者多个节点上更新污点配置
#-----------------------------------------#
[root@master K8S]# kubectl get nodes -o custom-columns=NodeName:.metadata.name,Taints:.spec.taints
NodeName    Taints
master      [map[effect:NoSchedule key:node-role.kubernetes.io/master]]
node-0001   <none>
[root@master K8S]# kubectl taint node node-0001 k=v:PreferNoSchedule
node/node-0001 tainted
[root@master K8S]# kubectl get nodes -o custom-columns=NodeName:.metadata.name,Taints:.spec.taints
NodeName    Taints
master      [map[effect:NoSchedule key:node-role.kubernetes.io/master]]
node-0001   [map[effect:PreferNoSchedule key:k value:v]]
[root@master K8S]# kubectl taint node node-0001 k-
node/node-0001 untainted
[root@master K8S]# kubectl get nodes -o custom-columns=NodeName:.metadata.name,Taints:.spec.taints
NodeName    Taints
master      [map[effect:NoSchedule key:node-role.kubernetes.io/master]]
node-0001   <none>

top

# 显示资源(CPU /内存/存储)使用率
#-----------------------------------------#
[root@master K8S]# kubectl top nodes
NAME        CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%   
master      90m          4%     1210Mi          14%       
node-0001   45m          2%     931Mi           11%       
[root@master K8S]# kubectl top pods
NAME                     CPU(cores)   MEMORY(bytes)   
mypod                    5m           8Mi
[root@master K8S]#

uncordon

# 解除(cordon、drain)资源不可调度标记
#-----------------------------------------#
[root@master K8S]# kubectl get nodes
NAME        STATUS                     ROLES    AGE   VERSION
master      Ready                      master   15h   v1.22.3
node-0001   Ready,SchedulingDisabled   node     15h   v1.22.3
[root@master K8S]# kubectl uncordon node-0001 
node/node-0001 uncordoned
[root@master K8S]# kubectl get nodes
NAME        STATUS   ROLES    AGE   VERSION
master      Ready    master   15h   v1.22.3
node-0001   Ready    node     15h   v1.22.3

version

# 显示客户端和服务器的版本信息
#-----------------------------------------#
[root@master K8S]# kubectl version -o yaml
clientVersion:
  buildDate: "2021-10-27T18:41:28Z"
  compiler: gc
  gitCommit: c92036820499fedefec0f847e2054d824aea6cd1
  gitTreeState: clean
  gitVersion: v1.22.3
  goVersion: go1.16.9
  major: "1"
  minor: "22"
  platform: linux/amd64
serverVersion:
  buildDate: "2021-10-27T18:35:25Z"
  compiler: gc
  gitCommit: c92036820499fedefec0f847e2054d824aea6cd1
  gitTreeState: clean
  gitVersion: v1.22.3
  goVersion: go1.16.9
  major: "1"
  minor: "22"
  platform: linux/amd64

wait

# 等待一个或多个资源达到某种状态
#-----------------------------------------#
---
kind: Pod
apiVersion: v1
metadata:
  name: mypod
spec:
  terminationGracePeriodSeconds: 0
  initContainers:
  - name: myinit
    image: registry:5000/busybox:latest
    imagePullPolicy: IfNotPresent
    command: ["sleep", "10"]
  containers:
  - name: linux
    image: registry:5000/busybox:latest
    imagePullPolicy: IfNotPresent
    command: ["sh", "-c"]
    args:
    - |
      echo "hello world !!!" >/var/www/index.html
      httpd -v -f -p 0.0.0.0:80 -h /var/www
  restartPolicy: Always
#-----------------------------------------#
[root@master K8S]# kubectl replace --force -f mypod.yaml 
pod "mypod" deleted
pod/mypod replaced
[root@master K8S]# time kubectl wait --for=condition=Ready pod/mypod
pod/mypod condition met

real    0m10.335s
user    0m0.034s
sys 0m0.008s