kubectl 速查手册

资源对象文件

---
kind: Pod
apiVersion: v1
metadata:
  name: myweb
  labels:
    app: nginx
spec:
  containers:
  - name: webserver
    image: nginx
status: {}

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]# 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]# source <(kubectl completion bash|tee /etc/bash_completion.d/kubectl)

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.5
node-0001   Ready    node     15h   v1.22.5
[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.5
node-0001   Ready,SchedulingDisabled   node     15h   v1.22.5

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.5
node-0001   Ready    node     36h   v1.22.5
[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.5
node-0001   Ready,SchedulingDisabled   node     36h   v1.22.5
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.5
node-0001   Ready    node     36h   v1.22.5
[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

# Kustomize 是一个独立的工具,用来通过 kustomization 文件定制 Kubernetes 对象

# 参考文档: https://cloud.tencent.com/developer/article/1745189

#-----------------------------------------#

[root@master k8s]# vim kustomization.yaml 

---

apiVersion: kustomize.config.k8s.io/v1beta1

kind: Kustomization

secretGenerator:

- name: mysecret

  files:

  - password.txt

[root@master k8s]# vim password.txt 

username=admin

password=secret

[root@master k8s]# kubectl kustomize ./

apiVersion: v1

data:

  password.txt: dXNlcm5hbWU9YWRtaW4KcGFzc3dvcmQ9c2VjcmV0Cg==

kind: Secret

metadata:

  name: mysecret-2kdd8ckcc7

type: Opaque

[root@master k8s]# kubectl apply -k ./

secret/mysecret-2kdd8ckcc7 created

[root@master k8s]# kubectl get secrets 

NAME                  TYPE                                  DATA   AGE

default-token-m7vbm   kubernetes.io/service-account-token   3      73d

mysecret-2kdd8ckcc7   Opaque                                1      4s

[root@master k8s]# kubectl delete -k ./

secret "mysecret-2kdd8ckcc7" deleted

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.5",

  "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

复制代码

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]]
# 管理资源的上线

#-----------------------------------------#

[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

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.5

node-0001   Ready,SchedulingDisabled   node     15h   v1.22.5

[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.5

node-0001   Ready    node     15h   v1.22.5

version

# 显示客户端和服务器的版本信息
#-----------------------------------------#
[root@master k8s]# kubectl version -o yaml
clientVersion:
  buildDate: "2021-10-27T18:41:28Z"
  compiler: gc
  gitCommit: c92036820499fedefec0f847e2054d824aea6cd1
  gitTreeState: clean
  gitVersion: v1.22.5
  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.5
  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
相关推荐
运维&陈同学3 小时前
【Beats01】企业级日志分析系统ELK之Metricbeat与Heartbeat 监控
运维·elk·elasticsearch·云原生·kibana·heartbeat·metricbeat
AKA小徐4 小时前
Debian12使用RKE2离线部署3master2node三主两从的k8s集群详细教程
kubernetes·rancher·rke2
老大白菜11 小时前
Windows 11 安装 Dify 完整指南 非docker环境
windows·docker·容器
tntlbb15 小时前
Ubuntu20.4 VPN+Docker代理配置
运维·ubuntu·docker·容器
Gabriel_liao15 小时前
Docker安装Neo4j
docker·容器·neo4j
有一个好名字16 小时前
zookeeper分布式锁模拟12306买票
分布式·zookeeper·云原生
Anna_Tong18 小时前
云原生大数据计算服务 MaxCompute 是什么?
大数据·阿里云·云原生·maxcompute·odps
豆豆豆豆变18 小时前
Docker轻量级可视化工具Portainer
docker·容器·自动化运维
花晓木19 小时前
k8s etcd 数据损坏处理方式
容器·kubernetes·etcd
运维&陈同学19 小时前
【模块一】kubernetes容器编排进阶实战之基于velero及minio实现etcd数据备份与恢复
数据库·后端·云原生·容器·kubernetes·etcd·minio·velero