kubectl 常用命令


查看

  • 查看所有kubelet節點
    kubectl get nodes
    kubectl get nodes -o wide --show-labels //可以看到所有節點的label
    kubectl describe node node-name //查看節點的詳細信息,包括資源配置等

  • 查看所有pod
    kubectl get pods --all-namespaces
    kubectl get pods -o wide
    kubectl get pods -o wide -w //可以看到實時狀態變化

  • 查看pod詳情
    kubectl describe pod podname -n namespace
    kubectl get -o json pod podname -n namespace
    kubectl get pods/podname -n namespace
    kubectl get pods/podname -n namespace -o json

  • 查看所有daemonset
    kubectl get daemonset --all-namespaces

  • kubectl get rc,services

  • List one or more resources by their type and names.
    kubectl get rc/web service/frontend pods/web-pod-13je7

  • kubectl get cs
    查看集群基本組件狀態

  • kubectl get csr
    查看未授權的 CSR 請求

  • kubectl certificate approve csr-name
    csr請求

  • kubectl get crd
    查看資源

namespace相關

  • 要使用新的namespace,必須先創建,然后才能寫在yaml的metadata的字段里
    kubectl create namespace newname

節點相關

  • kubectl describe node node-name 查看節點所有信息

設置節點不可調度/可調度

  • 計算節點狀態為 Ready, 執行 kubectl cordon node-name可致此節點為不可調度
  • 計算節點狀態為 Ready,SchedulingDisabled, 執行 kubectl uncordon node-name即可使之成為可調度的節點

節點的label

  • 查看節點的所有label
    kubectl get node --show-labels
  • 給節點添加一個新label
    kubectl label nodes kube-node node=kube-node
  • 修改節點的label
    kubectl label nodes --overwrite k8s-slave node-network-driver=ovs
  • 刪除節點的某個label
    kubectl label nodes kube-node node-
  • 根據node的label篩選節點
    kubectl label nodes kube-node node=kube-node
    kubectl label nodes --overwrite k8s-slave node-network-driver=sriov //修改節點k8s-slave的標簽node-network-driver的值為sriov
    kubectl get node -a -l "node=kube-node"
    pod或者rc的配置項中添加如下配置,位置跟cotainer平行:
    nodeSelector:
    node: kube-node4

節點taint 和 容器tolerations

  • 查看某節點的污點可以使用命令 kubectl describe node foo
  • kubectl taint nodes foo dedicated=special-user:NoSchedule 添加或更新節點'foo'的taint,key為'dedicated',value為'special-user',effect為NoSchedule'.
  • kubectl taint nodes foo dedicated:NoSchedule- 刪除節點foo的taint,key為dedicated,effect為NoSchedule

    kubectl taint nodes foo dedicated- 刪除節點foo的所有key為dedicated的污點
  • 一個污點包含一個key, value, and effect. 示例為 key=value:effect.
  • effect 只能是 NoSchedule, PreferNoSchedule or NoExecute
    有污點的節點,一般pod不會調度上去,但是如果pod能夠容忍這個污點就會調度上去,這就是pod的tolerations(taint是節點的屬性,tolerations是pod的屬性).
    如下在PodSpec中定義了toleration,該tolerations完全匹配taint那么該pod就可以被調度到此節點:
tolerations:
- key: "key"
  operator: "Equal"
  value: "value"
  effect: "NoSchedule"

tolerations:
- key: "key"
  operator: "Exists"
  effect: "NoSchedule"

官方指導https://kubernetes.io/docs/concepts/configuration/taint-and-toleration/

yaml文件、配置相關

  • 查看資源的yaml
    kubectl get deploy/my-nginx --export -o yaml
    kubectl get deploy/my-nginx --export -o yaml > example.yaml

  • 生成yaml文件
    由於yaml文件格式不好記住,可以先通過kubectl命令生成yaml,再更改yaml。 –dry-run表示嘗試,不真實執行。
    kubectl run --image=nginx my-nginx -o yaml --dry-run >> xxx.yaml

  • 修改API 資源的屬性
    kubectl edit pod/testnew2-prd-123 -n testnew2

    //Update a container's image; spec.containers[*].name is required because it's a merge key.
    kubectl patch pod valid-pod -p '{"spec":{"containers":[{"name":"kubernetes-serve-hostname","image":"new image"}]}}'

    //Update a single-container pod's image version (tag) to v4
    kubectl get pod mypod -o yaml | sed 's/(image: myimage):.*$/\1:v4/' | kubectl replace -f -

  • 解釋資源詳細信息,命令可能會顯示舊的group/version,我們可以通過--api-version參數顯示設置
    不知道該如何編寫YAML文件的時候,就可以使用改命令來幫助我們獲得更多提示信息。
    kubectl explain replicaset --api-version apps/v1

對容器執行命令

  • 對單容器的pod執行命令,注意--后面必須有空格(以在容器內執行命令ip a為例)
    kubectl exec -it pod_name -n namespace -- ip a

    有的時候一個pod中有多個容器,只有針對整個pod的命令才有效,此時需要指定container,這里指定的是container-name(可以用kubectl describe pod查看所有的容器名稱)
    kubectl exec -it podname -n namespace -c containername -- ip a

  • 強制刪除pod
    kubectl delete pod podname -n namespace --force --grace-period=0

  • 文件拷貝, 如下格式把容器中指定目錄的文件拷貝到本地磁盤指定目錄
    kubectl cp pod-name:/path/to/file /tmp/localpath/file

  • 修改容器的鏡像,如下是修改daemonset contiv-netplugin-ovs contiv-netplugin的鏡像為新鏡像,首先要先docker pull新鏡像

    • kubectl set image ds/contiv-netplugin-ovs contiv-netplugin=new-image -n kube-system
      然后需要刪除需要更新的daemonset中的pod,然后會重建pod,這個pod使用的就是新鏡像. 這里的標簽是container-name=new-image-name
  • 動態調整副本數

    • kubectl scale deployment.v1.apps/apitest-deployment --replicas=10 -n namespace
  • 查看pod的log

    • kubectl log -n namespace pod-name
    • 如果pod里有多個容器,需要制定容器名來查看log: kubectl log -n namespace pod-name container-1-name
    • 如果容器panic后重啟,想查看panic日志,需要查看上一個容器的log: kubectl logs --previous podname -n namespace


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM