kubectl命令
kubectl的命令語法
kubectl [command] [TYPE] [NAME] [flags]其中command,TYPE,NAME,和flags分別是:
-
command: 指定要在一個或多個資源進行操作,例如create,get,describe,delete。
-
TYPE:指定資源類型。資源類型區分大小寫,您可以指定單數,復數或縮寫形式。例如,以下命令產生相同的輸出:
kubectl get pod pod1 kubectl get pods pod1 kubectl get po pod1 -
NAME:指定資源的名稱。名稱區分大小寫。如果省略名稱,則會顯示所有資源的詳細信息,比如$ kubectl get pods。
按類型和名稱指定多種資源:
* 要分組資源,如果它們都是相同的類型:`TYPE1 name1 name2 name<#>`.<br/> 例: `$ kubectl get pod example-pod1 example-pod2`* 要分別指定多種資源類型: `TYPE1/name1 TYPE1/name2 TYPE2/name3 TYPE<#>/name<#>`.<br/> 例: `$ kubectl get pod/example-pod1 replicationcontroller/example-rc1` -
flags:指定可選標志。例如,您可以使用-s或--serverflags來指定Kubernetes API服務器的地址和端口。
更多命令介紹:
[root@node5 ~]# kubectl kubectl controls the Kubernetes cluster manager.Find more information at https://github.com/kubernetes/kubernetes.Basic Commands (Beginner):create Create a resource from a file or from stdin.expose Take a replication controller, service, deployment or pod and expose it as a new Kubernetes Servicerun Run a particular image on the clusterset Set specific features on objectsrun-container Run a particular image on the cluster. This command is deprecated, use "run" insteadBasic Commands (Intermediate):get Display one or many resourcesexplain Documentation of resourcesedit Edit a resource on the serverdelete Delete resources by filenames, stdin, resources and names, or by resources and label selectorDeploy Commands:rollout Manage the rollout of a resourcerolling-update Perform a rolling update of the given ReplicationControllerscale Set a new size for a Deployment, ReplicaSet, Replication Controller, or Jobautoscale Auto-scale a Deployment, ReplicaSet, or ReplicationControllerCluster Management Commands:certificate Modify certificate resources.cluster-info Display cluster infotop Display Resource (CPU/Memory/Storage) usage.cordon Mark node as unschedulableuncordon Mark node as schedulabledrain Drain node in preparation for maintenancetaint Update the taints on one or more nodesTroubleshooting and Debugging Commands:describe Show details of a specific resource or group of resourceslogs Print the logs for a container in a podattach Attach to a running containerexec Execute a command in a containerport-forward Forward one or more local ports to a podproxy Run a proxy to the Kubernetes API servercp Copy files and directories to and from containers.auth Inspect authorizationAdvanced Commands:apply Apply a configuration to a resource by filename or stdinpatch Update field(s) of a resource using strategic merge patchreplace Replace a resource by filename or stdinconvert Convert config files between different API versionsSettings Commands:label Update the labels on a resourceannotate Update the annotations on a resourcecompletion Output shell completion code for the specified shell (bash or zsh)Other Commands:api-versions Print the supported API versions on the server, in the form of "group/version"config Modify kubeconfig fileshelp Help about any commandplugin Runs a command-line pluginversion Print the client and server version informationUse "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).2. 操作的常用資源對象
具體對象類型及縮寫:
* all* certificatesigningrequests (aka 'csr')* clusterrolebindings* clusterroles* componentstatuses (aka 'cs')* configmaps (aka 'cm')* controllerrevisions* cronjobs* customresourcedefinition (aka 'crd')* daemonsets (aka 'ds')* deployments (aka 'deploy')* endpoints (aka 'ep')* events (aka 'ev')* horizontalpodautoscalers (aka 'hpa')* ingresses (aka 'ing')* jobs* limitranges (aka 'limits')* namespaces (aka 'ns')* networkpolicies (aka 'netpol')* nodes (aka 'no')* persistentvolumeclaims (aka 'pvc')* persistentvolumes (aka 'pv')* poddisruptionbudgets (aka 'pdb')* podpreset* pods (aka 'po')* podsecuritypolicies (aka 'psp')* podtemplates* replicasets (aka 'rs')* replicationcontrollers (aka 'rc')* resourcequotas (aka 'quota')* rolebindings* roles* secrets* serviceaccounts (aka 'sa')* services (aka 'svc')* statefulsets (aka 'sts')* storageclasses (aka 'sc')3. kubectl命令分類[command]
3.1 增
1)create:[Create a resource by filename or stdin]
2)run:[ Run a particular image on the cluster]
3)apply:[Apply a configuration to a resource by filename or stdin]
4)proxy:[Run a proxy to the Kubernetes API server ]
3.2 刪
1)delete:[Delete resources ]
3.3 改
1)scale:[Set a new size for a Replication Controller]
2)exec:[Execute a command in a container]
3)attach:[Attach to a running container]
4)patch:[Update field(s) of a resource by stdin]
5)edit:[Edit a resource on the server]
6) label:[Update the labels on a resource]
7)annotate:[Auto-scale a replication controller]
8)replace:[Replace a resource by filename or stdin]
9)config:[config modifies kubeconfig files]
3.4 查
1)get:[Display one or many resources]
2)describe:[Show details of a specific resource or group of resources]
3)log:[Print the logs for a container in a pod]
4)cluster-info:[Display cluster info]
5) version:[Print the client and server version information]
6)api-versions:[Print the supported API versions]
4. Pod相關命令
4.1 查詢Pod
kubectl get pod -o wide --namespace=<NAMESPACE>4.2 進入Pod
kubectl exec -it <PodName> /bin/bash --namespace=<NAMESPACE># 進入Pod中指定容器 kubectl exec -it <PodName> -c <ContainerName> /bin/bash --namespace=<NAMESPACE>4.3 刪除Pod
kubectl delete pod <PodName> --namespace=<NAMESPACE># 強制刪除Pod,當Pod一直處于Terminating狀態 kubectl delete pod <PodName> --namespace=<NAMESPACE> --force --grace-period=0# 刪除某個namespace下某個類型的所有對象 kubectl delete deploy --all --namespace=test4.4 日志查看
$ 查看運行容器日志 kubectl logs <PodName> --namespace=<NAMESPACE> $ 查看上一個掛掉的容器日志 kubectl logs <PodName> -p --namespace=<NAMESPACE>5. 常用命令
5.1. Node隔離與恢復
說明:Node設置隔離之后,原先運行在該Node上的Pod不受影響,后續的Pod不會調度到被隔離的Node上。
1. Node隔離
# cordon命令 kubectl cordon <NodeName> # 或者 kubectl patch node <NodeName> -p '{"spec":{"unschedulable":true}}'2. Node恢復
# uncordon kubectl uncordon <NodeName> # 或者 kubectl patch node <NodeName> -p '{"spec":{"unschedulable":false}}'5.2. kubectl label
1. 固定Pod到指定機器
kubectl label node <NodeName> namespace/<NAMESPACE>=true2. 取消Pod固定機器
kubectl label node <NodeName> namespace/<NAMESPACE>-5.3. 升級鏡像
# 升級鏡像 kubectl set image deployment/nginx nginx=nginx:1.15.12 -n nginx # 查看滾動升級情況 kubectl rollout status deployment/nginx -n nginx5.4. 調整資源值
# 調整指定容器的資源值 kubectl set resources sts nginx-0 -c=agent --limits=memory=512Mi -n nginx5.5. 調整readiness probe
# 批量查看readiness probe timeoutSeconds kubectl get statefulset -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.spec.template.spec.containers[0].readinessProbe.timeoutSeconds}{"\n"}{end}'# 調整readiness probe timeoutSeconds參數 kubectl patch statefulset nginx-sts --type='json' -p='[{"op": "replace", "path": "/spec/template/spec/containers/0/readinessProbe/timeoutSeconds", "value":5}]' -n nginx5.6. 調整tolerations屬性
kubectl patch statefulset nginx-sts --patch '{"spec": {"template": {"spec": {"tolerations": [{"effect": "NoSchedule","key": "dedicated","operator": "Equal","value": "nginx"}]}}}}' -n nginx5.7. 查看所有節點的IP
kubectl get nodes -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.addresses[0].address}{"\n"}{end}'5.8. 查看當前k8s組件leader節點
當k8s集群高可用部署的時候,kube-controller-manager和kube-scheduler只能一個服務處于實際邏輯運行狀態,通過參數--leader-elect=true來開啟選舉操作。以下提供查詢leader節點的命令。
$ kubectl get endpoints kube-controller-manager --namespace=kube-system -o yamlapiVersion: v1 kind: Endpoints metadata:annotations:control-plane.alpha.kubernetes.io/leader: '{"holderIdentity":"xxx.xxx.xxx.xxx_6537b938-7f5a-11e9-8487-00220d338975","leaseDurationSeconds":15,"acquireTime":"2019-05-26T02:03:18Z","renewTime":"2019-05-26T02:06:08Z","leaderTransitions":1}'creationTimestamp: "2019-05-26T01:52:39Z"name: kube-controller-managernamespace: kube-systemresourceVersion: "1965"selfLink: /api/v1/namespaces/kube-system/endpoints/kube-controller-manageruid: f1755fc5-7f58-11e9-b4c4-00220d338975以上表示"holderIdentity":"xxx.xxx.xxx.xxx為kube-controller-manager的leader節點。
同理,可以通過以下命令查看kube-scheduler的leader節點。
kubectl get endpoints kube-scheduler --namespace=kube-system -o yaml5.9. 修改副本數
kubectl scale deployment.v1.apps/nginx-deployment --replicas=105.10. 批量刪除pod
kubectl get po -n default |grep Evicted |awk '{print $1}' |xargs -I {} kubectl delete po {} -n default參考文章:
- https://kubernetes.io/docs/reference/kubectl/overview/
- https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/
總結
- 上一篇: 内双眼皮适合三点定位双眼皮吗
- 下一篇: kubernetes问题排查