• kubectl 速查手册


    资源对象文件

    1. ---
    2. kind: Pod
    3. apiVersion: v1
    4. metadata:
    5. name: myweb
    6. labels:
    7. app: nginx
    8. spec:
    9. containers:
    10. - name: webserver
    11. image: nginx
    12. status: {}

    annotate

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

    api-resources

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

    api-versions

    1. # 显示服务端所支持的 API 版本
    2. #-----------------------------------------#
    3. [root@master k8s]# kubectl api-versions
    4. admissionregistration.k8s.io/v1
    5. apps/v1
    6. ... ...
    7. v1

    apply

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

    attach

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

    auth

    1. # 检查授权信息
    2. #-----------------------------------------#
    3. [root@master k8s]# kubectl --kubeconfig=admin.conf auth can-i get pods
    4. yes
    5. [root@master k8s]# kubectl --kubeconfig=auth.conf auth can-i get pods
    6. no

    autoscale

    1. # 创建一个HPA控制器,对资源对象进行自动扩缩
    2. #-----------------------------------------#
    3. [root@master k8s]# kubectl apply -f myDeploy.yaml
    4. deployment.apps/myweb created
    5. [root@master k8s]# kubectl autoscale deployment myweb --min=1 --max=10 --cpu-percent=80
    6. horizontalpodautoscaler.autoscaling/myweb autoscaled
    7. [root@master k8s]# kubectl get horizontalpodautoscalers.autoscaling
    8. NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
    9. myweb Deployment/myweb 10%/80% 1 10 1 27m
    10. #-----------------------------------------#
    11. ---
    12. apiVersion: autoscaling/v1
    13. kind: HorizontalPodAutoscaler
    14. metadata:
    15. name: myweb
    16. spec:
    17. minReplicas: 1
    18. maxReplicas: 10
    19. scaleTargetRef:
    20. apiVersion: apps/v1
    21. kind: Deployment
    22. name: myweb
    23. targetCPUUtilizationPercentage: 80

    certificate

    1. # 修改证书资源
    2. #-----------------------------------------#
    3. [root@master k8s]# kubectl get certificatesigningrequests
    4. NAME AGE REQUESTOR CONDITION
    5. csr-wsfz7 8s system:node:master Pending
    6. [root@master k8s]# kubectl certificate approve csr-wsfz7
    7. [root@master k8s]# kubectl get certificatesigningrequests
    8. NAME AGE REQUESTOR CONDITION
    9. csr-wsfz7 86s system:node:master Approved,Issued

    cluster-info

    1. # 显示集群信息
    2. #-----------------------------------------#
    3. [root@master k8s]# kubectl cluster-info
    4. Kubernetes control plane is running at https://192.168.1.10:6443
    5. CoreDNS is running at https://192.168.1.10:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
    6. To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

    completion

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

    config

    1. # 配置管理 kubeconfig 文件
    2. # 创建普通认证用户中的 CN 代表用户名,O 代表组名称
    3. #-----------------------------------------#
    4. [root@master k8s]# openssl genrsa -out luck.key 2048
    5. [root@master k8s]# openssl req -new -key luck.key -out luck.csr -subj "/CN=luck/O=tedu"
    6. [root@master k8s]# mycsr=$(base64 luck.csr |tr -d '\n')
    7. [root@master k8s]# cat <<EOF |kubectl apply -f -
    8. ---
    9. apiVersion: certificates.k8s.io/v1
    10. kind: CertificateSigningRequest
    11. metadata:
    12. name: luck-token
    13. spec:
    14. groups:
    15. - system:authenticated
    16. request: ${mycsr}
    17. signerName: kubernetes.io/kube-apiserver-client
    18. usages:
    19. - client auth
    20. EOF
    21. [root@master k8s]# kubectl get certificatesigningrequests.certificates.k8s.io
    22. NAME AGE SIGNERNAME ... CONDITION
    23. luck-token 12s kubernetes.io/kube-apiserver-client ... Pending
    24. [root@master k8s]# kubectl certificate approve luck-token
    25. [root@master k8s]# kubectl get certificatesigningrequests.certificates.k8s.io
    26. NAME AGE SIGNERNAME ... CONDITION
    27. luck-token 33s kubernetes.io/kube-apiserver-client ... Approved,Issued
    28. [root@master k8s]# kubectl get certificatesigningrequests.certificates.k8s.io luck-token -o jsonpath='{.status.certificate}'| base64 -d >luck.crt
    29. [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
    30. [root@master k8s]# kubectl config --kubeconfig=auth.conf set-credentials adminuser-luck --client-certificate=luck.crt --client-key=luck.key --embed-certs=true
    31. [root@master k8s]# kubectl config --kubeconfig=auth.conf set-context node-cluster --cluster=k8s-cluster --user=adminuser-luck --namespace=default
    32. [root@master k8s]# kubectl config --kubeconfig=auth.conf use-context node-cluster
    33. [root@master k8s]# kubectl create clusterrolebinding luckrole --clusterrole=cluster-admin --user=luck
    34. [root@master k8s]# kubectl config --kubeconfig=auth.conf get-clusters
    35. NAME
    36. k8s-cluster
    37. [root@master k8s]# kubectl config --kubeconfig=auth.conf get-users
    38. NAME
    39. adminuser-luck
    40. [root@master k8s]# kubectl config --kubeconfig=auth.conf get-contexts
    41. CURRENT NAME CLUSTER AUTHINFO NAMESPACE
    42. * node-cluster k8s-cluster adminuser-luck default

    convert

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

    cordon

    1. # 标记节点为不可调度的
    2. #-----------------------------------------#
    3. [root@master k8s]# kubectl get nodes
    4. NAME STATUS ROLES AGE VERSION
    5. master Ready master 15h v1.22.5
    6. node-0001 Ready node 15h v1.22.5
    7. [root@master k8s]# kubectl cordon node-0001
    8. node/node-0001 cordoned
    9. [root@master k8s]# kubectl get nodes
    10. NAME STATUS ROLES AGE VERSION
    11. master Ready master 15h v1.22.5
    12. node-0001 Ready,SchedulingDisabled node 15h v1.22.5

    cp

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

    create

    1. # 通过文件或标准输入来创建资源或用来生成资源文件
    2. #-----------------------------------------#
    3. [root@master k8s]# kubectl create namespace testapp
    4. [root@master k8s]# kubectl create namespace testapp --dry-run=client -o yaml
    5. apiVersion: v1
    6. kind: Namespace
    7. metadata:
    8. creationTimestamp: null
    9. name: testapp
    10. spec: {}
    11. status: {}

    debug

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

    delete

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

    describe

    1. # 显示某个资源或某组资源的详细信息
    2. #-----------------------------------------#
    3. [root@master k8s]# kubectl describe pod mypod
    4. Name: mypod
    5. Namespace: default
    6. Priority: 0
    7. Node: node-0001/192.168.1.11
    8. ...

    diff(显示目前版本与将要应用的版本之间的差异)

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

    drain(清空节点,节点资源被删除也不能被调度)

    1. # 清空节点,节点资源被删除也不能被调度
    2. #-----------------------------------------#
    3. [root@master k8s]# kubectl get nodes
    4. NAME STATUS ROLES AGE VERSION
    5. master Ready master 36h v1.22.5
    6. node-0001 Ready node 36h v1.22.5
    7. [root@master k8s]# kubectl drain node-0001 --delete-emptydir-data --ignore-daemonsets --force
    8. [root@master k8s]# kubectl get nodes
    9. NAME STATUS ROLES AGE VERSION
    10. master Ready master 36h v1.22.5
    11. node-0001 Ready,SchedulingDisabled node 36h v1.22.5
    12. edit
    13. # 修改服务器上的某资源
    14. #-----------------------------------------#
    15. [root@master k8s]# kubectl edit pod mypod
    16. # Please edit the object below. Lines beginning with a '#' will be ignored,
    17. # and an empty file will abort the edit. If an error occurs while saving this file will be
    18. # reopened with the relevant failures.
    19. #
    20. apiVersion: v1
    21. kind: Pod
    22. ...

    exec

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

    explain

    1. # 显示资源的帮助信息
    2. #-----------------------------------------#
    3. [root@master k8s]# kubectl explain pod.spec
    4. KIND: Pod
    5. VERSION: v1
    6. RESOURCE: spec <Object>
    7. DESCRIPTION:
    8. Specification of the desired behavior of the pod. More info:
    9. https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
    10. PodSpec is a description of a pod.
    11. ...

    expose

    1. # 为资源创建 service
    2. #-----------------------------------------#
    3. [root@master k8s]# [root@master k8s]# kubectl expose deployment myweb --port=80 --protocol=TCP --target-port=80 --name=webservice --type ClusterIP
    4. #-----------------------------------------#
    5. ---
    6. apiVersion: v1
    7. kind: Service
    8. metadata:
    9. name: webservice
    10. spec:
    11. ports:
    12. - protocol: TCP
    13. port: 80
    14. targetPort: 80
    15. selector:
    16. app: apache
    17. type: ClusterIP
    18. #-----------------------------------------#
    19. [root@master k8s]# kubectl get service
    20. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
    21. kubernetes ClusterIP 10.254.0.1 <none> 443/TCP 36h
    22. webservice ClusterIP 10.254.17.185 <none> 80/TCP 2s
    23. [root@master k8s]# curl http://10.254.17.185
    24. hello world.

    get

    1. # 显示一个或者多个资源信息
    2. #-----------------------------------------#
    3. [root@master k8s]# kubectl get nodes
    4. ku NAME STATUS ROLES AGE VERSION
    5. master Ready master 36h v1.22.5
    6. node-0001 Ready node 36h v1.22.5
    7. [root@master k8s]# kubectl get pods
    8. NAME READY STATUS RESTARTS AGE
    9. mypod 1/1 Running 0 5m9s
    10. [root@master k8s]# kubectl get pod mypod -o wide
    11. NAME READY STATUS RESTARTS IP ...
    12. mypod 1/1 Running 0 10.244.21.154 ...
    13. [root@master k8s]# kubectl get pod mypod -o yaml
    14. apiVersion: v1
    15. kind: Pod
    16. metadata:
    17. ...

    help

    1. # 显示帮助信息
    2. #-----------------------------------------#
    3. [root@master k8s]# kubectl help run
    4. Create and run a particular image in a pod.
    5. Examples:
    6. # Start a nginx pod
    7. kubectl run nginx --image=nginx

    kustomize

    1. # Kustomize 是一个独立的工具,用来通过 kustomization 文件定制 Kubernetes 对象
    2. # 参考文档: https://cloud.tencent.com/developer/article/1745189
    3. #-----------------------------------------#
    4. [root@master k8s]# vim kustomization.yaml
    5. ---
    6. apiVersion: kustomize.config.k8s.io/v1beta1
    7. kind: Kustomization
    8. secretGenerator:
    9. - name: mysecret
    10. files:
    11. - password.txt
    12. [root@master k8s]# vim password.txt
    13. username=admin
    14. password=secret
    15. [root@master k8s]# kubectl kustomize ./
    16. apiVersion: v1
    17. data:
    18. password.txt: dXNlcm5hbWU9YWRtaW4KcGFzc3dvcmQ9c2VjcmV0Cg==
    19. kind: Secret
    20. metadata:
    21. name: mysecret-2kdd8ckcc7
    22. type: Opaque
    23. [root@master k8s]# kubectl apply -k ./
    24. secret/mysecret-2kdd8ckcc7 created
    25. [root@master k8s]# kubectl get secrets
    26. NAME TYPE DATA AGE
    27. default-token-m7vbm kubernetes.io/service-account-token 3 73d
    28. mysecret-2kdd8ckcc7 Opaque 1 4s
    29. [root@master k8s]# kubectl delete -k ./
    30. secret "mysecret-2kdd8ckcc7" deleted

    label

    1. # 更新资源的标签
    2. #-----------------------------------------#
    3. [root@master k8s]# kubectl get pods --show-labels
    4. NAME READY STATUS RESTARTS AGE LABELS
    5. mypod 1/1 Running 0 7m22s <none>
    6. [root@master k8s]# kubectl label pod mypod app=webapp
    7. pod/mypod labeled
    8. [root@master k8s]# kubectl get pods --show-labels
    9. NAME READY STATUS RESTARTS AGE LABELS
    10. mypod 1/1 Running 0 7m41s app=webapp
    11. [root@master k8s]# kubectl label pod mypod app-
    12. pod/mypod labeled
    13. [root@master k8s]# kubectl get pods --show-labels
    14. NAME READY STATUS RESTARTS AGE LABELS
    15. mypod 1/1 Running 0 68m <none>

    logs

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

    options

    1. # 显示所有命令都支持的共有参数列表
    2. #-----------------------------------------#
    3. [root@master k8s]# kubectl options
    4. The following options can be passed to any command:
    5. --insecure-skip-tls-verify=false: If true, the server's certificate will not be checked for validity. This will
    6. ...

    patch

    1. # 基于策略性合并修补规则更新某资源中的字段
    2. #-----------------------------------------#
    3. ---
    4. apiVersion: v1
    5. kind: PersistentVolume
    6. metadata:
    7. name: mypv
    8. spec:
    9. capacity:
    10. storage: 5Gi
    11. volumeMode: Filesystem
    12. accessModes:
    13. - ReadWriteOnce
    14. persistentVolumeReclaimPolicy: Recycle
    15. hostPath:
    16. path: /var/webroot
    17. type: DirectoryOrCreate
    18. #-----------------------------------------#
    19. [root@master k8s]# kubectl get pv
    20. NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS AGE
    21. mypv 5Gi RWO Recycle Available 5s
    22. [root@master k8s]# kubectl patch pv mypv -p '{"spec":{"capacity":{"storage":"8Gi"}}}'
    23. persistentvolume/mypv patched
    24. [root@master k8s]# kubectl get pv
    25. NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS AGE
    26. mypv 8Gi RWO Recycle Available 67s
    27. plugin
    28. # 运行命令行插件
    29. # 插件在 ${PATH} 下,是一个独立的可执行文件,名称以 kubectl- 开头
    30. #-----------------------------------------#
    31. [root@master k8s]# vim /usr/local/bin/kubectl-gettaint
    32. #!/bin/bash
    33. /usr/bin/kubectl get nodes -o custom-columns=NodeName:.metadata.name,Taints:.spec.taints
    34. [root@master k8s]# chmod 755 /usr/local/bin/kubectl-gettaint
    35. [root@master k8s]# kubectl plugin list
    36. The following compatible plugins are available:
    37. /usr/local/bin/kubectl-gettaint
    38. [root@master k8s]# kubectl gettaint
    39. NodeName Taints
    40. master [map[effect:NoSchedule key:node-role.kubernetes.io/master]]
    41. node-0001 <none>

    port-forward

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

    proxy

    1. # 运行一个 kubernetes API 服务器代理
    2. #-----------------------------------------#
    3. [root@master k8s]# kubectl proxy --port=80
    4. Starting to serve on 127.0.0.1:80
    5. #-----------------------------------------#
    6. [root@master k8s]# curl http://127.0.0.1/version
    7. {
    8. "major": "1",
    9. "minor": "22",
    10. "gitVersion": "v1.22.5",
    11. "gitCommit": "c92036820499fedefec0f847e2054d824aea6cd1",
    12. "gitTreeState": "clean",
    13. "buildDate": "2021-10-27T18:35:25Z",
    14. "goVersion": "go1.16.9",
    15. "compiler": "gc",
    16. "platform": "linux/amd64"
    17. }

    replace

    1. # 基于文件名或标准输入替换资源
    2. #-----------------------------------------#
    3. [root@master k8s]# kubectl replace --force -f mypod.yaml
    4. pod "mypod" deleted
    5. 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   
    [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]]
    1. # 管理资源的上线
    2. #-----------------------------------------#
    3. [root@master k8s]# kubectl rollout history deployment
    4. deployment.apps/myweb
    5. REVISION CHANGE-CAUSE
    6. 1 httpd.v1
    7. 2 httpd.v2
    8. [root@master k8s]# kubectl rollout undo deployment myweb --to-revision=1
    9. deployment.apps/myweb rolled back
    10. [root@master k8s]# kubectl rollout history deployment
    11. deployment.apps/myweb
    12. REVISION CHANGE-CAUSE
    13. 2 httpd.v2
    14. 3 httpd.v1
    15. run
    16. # 在集群中使用指定镜像启动容器
    17. #-----------------------------------------#
    18. [root@master k8s]# kubectl run mypod --image=registry:5000/myos:httpd
    19. #-----------------------------------------#
    20. ---
    21. apiVersion: v1
    22. kind: Pod
    23. metadata:
    24. labels:
    25. run: mypod
    26. name: mypod
    27. spec:
    28. containers:
    29. - image: registry:5000/myos:httpd
    30. name: mypod
    31. restartPolicy: Always
    32. scale
    33. # 为可扩充资源设置一个新副本数量
    34. #-----------------------------------------#
    35. [root@master k8s]# kubectl apply -f myDeploy.yaml
    36. deployment.apps/myweb created
    37. [root@master ~]# kubectl get deployments.apps
    38. NAME READY UP-TO-DATE AVAILABLE AGE
    39. myweb 1/1 1 1 21m
    40. [root@master ~]# kubectl scale deployment myweb --replicas=3
    41. deployment.apps/myweb scaled
    42. [root@master ~]# kubectl get deployments.apps
    43. NAME READY UP-TO-DATE AVAILABLE AGE
    44. myweb 3/3 3 3 21m

    top

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

    uncordon

    1. # 解除(cordon、drain)资源不可调度标记
    2. #-----------------------------------------#
    3. [root@master k8s]# kubectl get nodes
    4. NAME STATUS ROLES AGE VERSION
    5. master Ready master 15h v1.22.5
    6. node-0001 Ready,SchedulingDisabled node 15h v1.22.5
    7. [root@master k8s]# kubectl uncordon node-0001
    8. node/node-0001 uncordoned
    9. [root@master k8s]# kubectl get nodes
    10. NAME STATUS ROLES AGE VERSION
    11. master Ready master 15h v1.22.5
    12. node-0001 Ready node 15h v1.22.5

    version

    1. # 显示客户端和服务器的版本信息
    2. #-----------------------------------------#
    3. [root@master k8s]# kubectl version -o yaml
    4. clientVersion:
    5. buildDate: "2021-10-27T18:41:28Z"
    6. compiler: gc
    7. gitCommit: c92036820499fedefec0f847e2054d824aea6cd1
    8. gitTreeState: clean
    9. gitVersion: v1.22.5
    10. goVersion: go1.16.9
    11. major: "1"
    12. minor: "22"
    13. platform: linux/amd64
    14. serverVersion:
    15. buildDate: "2021-10-27T18:35:25Z"
    16. compiler: gc
    17. gitCommit: c92036820499fedefec0f847e2054d824aea6cd1
    18. gitTreeState: clean
    19. gitVersion: v1.22.5
    20. goVersion: go1.16.9
    21. major: "1"
    22. minor: "22"
    23. platform: linux/amd64
    24. wait
    25. # 等待一个或多个资源达到某种状态
    26. #-----------------------------------------#
    27. ---
    28. kind: Pod
    29. apiVersion: v1
    30. metadata:
    31. name: mypod
    32. spec:
    33. terminationGracePeriodSeconds: 0
    34. initContainers:
    35. - name: myinit
    36. image: registry:5000/busybox:latest
    37. imagePullPolicy: IfNotPresent
    38. command: ["sleep", "10"]
    39. containers:
    40. - name: linux
    41. image: registry:5000/busybox:latest
    42. imagePullPolicy: IfNotPresent
    43. command: ["sh", "-c"]
    44. args:
    45. - |
    46. echo "hello world !!!" >/var/www/index.html
    47. httpd -v -f -p 0.0.0.0:80 -h /var/www
    48. restartPolicy: Always
    49. #-----------------------------------------#
    50. [root@master k8s]# kubectl replace --force -f mypod.yaml
    51. pod "mypod" deleted
    52. pod/mypod replaced
    53. [root@master k8s]# time kubectl wait --for=condition=Ready pod/mypod
    54. pod/mypod condition met
    55. real 0m10.335s
    56. user 0m0.034s
    57. sys 0m0.008s

  • 相关阅读:
    nginx 反向代理 ElasticSearch es
    实测Tengine开源的Dubbo功能
    Python 内置函数详解 (2) 逻辑运算
    架构思考(七)
    Pytorch框架的学习(1)
    智慧公厕未来数据中台,实现公厕跨域跨级跨平台聚合应用
    我的创作纪念日,记录 Day,散
    无法对wsl-docker-data本身的unbutu镜像扩容操作
    设计模式——面向对象设计原则
    基于JAVA-心理健康管理-计算机毕业设计源码+系统+mysql数据库+lw文档+部署
  • 原文地址:https://blog.csdn.net/2301_81865838/article/details/136459117