kubectl备忘录
说明:
这些指令适用于 Kubernetes v1.27。要检查版本,请使用 kubectl version
命令。
本页列举了常用的 kubectl
命令和标志。Kubectl 自动补全
kubectl 备忘单 | Kubernetes
Kubectl 命令参考文档
Kubectl 自动补全
BASH
1 2 3
| apt-get install bash-completion source <(kubectl completion bash) echo "source <(kubectl completion bash)" >> ~/.bashrc
|
关于 --all-namespaces
的一点说明
我们经常用到--all-namespaces
参数,应该要知道它的简写:
kubectl -A
Kubectl 上下文和配置
设置kubectl
与哪个 Kubernetes 集群进行通信并修改配置信息。 查看使用 kubeconfig 跨集群授权访问 文档获取配置文件详细信息。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| kubectl config view
KUBECONFIG=~/.kube/config:~/.kube/kubconfig2 kubectl config view
kubectl config view -o jsonpath='{.users[?(@.name == "e2e")].user.password}'
kubectl config view -o jsonpath='{.users[].name}' kubectl config view -o jsonpath='{.users[*].name}' kubectl config get-contexts kubectl config current-context kubectl config use-context my-cluster-name
kubectl config set-cluster my-cluster-name
kubectl config set-cluster my-cluster-name --proxy-url=my-proxy-url
kubectl config set-credentials kubeuser/foo.kubernetes.com --username=kubeuser --password=kubepassword
kubectl config set-context --current --namespace=ggckad-s2
kubectl config set-context gce --user=cluster-admin --namespace=foo \ && kubectl config use-context gce
kubectl config unset users.foo
alias kx='f() { [ "$1" ] && kubectl config use-context $1 || kubectl config current-context ; } ; f' alias kn='f() { [ "$1" ] && kubectl config set-context --current --namespace $1 || kubectl config view --minify | grep namespace | cut -d" " -f6 ; } ; f'
|
创建对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| kubectl apply -f ./my-manifest.yaml kubectl apply -f ./my1.yaml -f ./my2.yaml kubectl apply -f ./dir kubectl apply -f https://git.io/vPieo kubectl create deployment nginx --image=nginx
kubectl create job hello --image=busybox:1.28 -- echo "Hello World"
kubectl create cronjob hello --image=busybox:1.28 --schedule="*/1 * * * *" -- echo "Hello World"
kubectl explain pods
kubectl apply -f - <<EOF apiVersion: v1 kind: Pod metadata: name: busybox-sleep spec: containers: - name: busybox image: busybox:1.28 args: - sleep - "1000000" --- apiVersion: v1 kind: Pod metadata: name: busybox-sleep-less spec: containers: - name: busybox image: busybox:1.28 args: - sleep - "1000" EOF
kubectl apply -f - <<EOF apiVersion: v1 kind: Secret metadata: name: mysecret type: Opaque data: password: $(echo -n "s33msi4" | base64 -w0) username: $(echo -n "jane" | base64 -w0) EOF
|
查看和查找资源
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| kubectl get services kubectl get pods --all-namespaces kubectl get pods -o wide kubectl get deployment my-dep kubectl get pods kubectl get pod my-pod -o yaml
kubectl describe nodes my-node kubectl describe pods my-pod
kubectl get services --sort-by=.metadata.name
kubectl get pods --sort-by='.status.containerStatuses[0].restartCount'
kubectl get pv --sort-by=.spec.capacity.storage
kubectl get pods --selector=app=cassandra -o \ jsonpath='{.items[*].metadata.labels.version}'
kubectl get configmap myconfig \ -o jsonpath='{.data.ca\.crt}'
kubectl get secret my-secret --template='{{index .data "key-name-with-dashes"}}'
kubectl get node --selector='!node-role.kubernetes.io/control-plane'
kubectl get pods --field-selector=status.phase=Running
kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="ExternalIP")].address}'
sel=${$(kubectl get rc my-rc --output=json | jq -j '.spec.selector | to_entries | .[] | "\(.key)=\(.value),"')%?} echo $(kubectl get pods --selector=$sel --output=jsonpath={.items..metadata.name})
kubectl get pods --show-labels
JSONPATH='{range .items[*]}{@.metadata.name}:{range @.status.conditions[*]}{@.type}={@.status};{end}{end}' \ && kubectl get nodes -o jsonpath="$JSONPATH" | grep "Ready=True"
kubectl get secret my-secret -o go-template='{{range $k,$v := .data}}{{"### "}}{{$k}}{{"\n"}}{{$v|base64decode}}{{"\n\n"}}{{end}}'
kubectl get pods -o json | jq '.items[].spec.containers[].env[]?.valueFrom.secretKeyRef.name' | grep -v null | sort | uniq
kubectl get pods --all-namespaces -o jsonpath='{range .items[*].status.initContainerStatuses[*]}{.containerID}{"\n"}{end}' | cut -d/ -f3
kubectl get events --sort-by=.metadata.creationTimestamp
kubectl events --types=Warning
kubectl diff -f ./my-manifest.yaml
kubectl get nodes -o json | jq -c 'paths|join(".")'
kubectl get pods -o json | jq -c 'paths|join(".")'
for pod in $(kubectl get po --output=jsonpath={.items..metadata.name}); do echo $pod && kubectl exec -it $pod -- env; done
kubectl get deployment nginx-deployment --subresource=status
|
更新资源
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| kubectl set image deployment/frontend www=image:v2 kubectl rollout history deployment/frontend kubectl rollout undo deployment/frontend kubectl rollout undo deployment/frontend --to-revision=2 kubectl rollout status -w deployment/frontend kubectl rollout restart deployment/frontend
cat pod.json | kubectl replace -f -
kubectl replace --force -f ./pod.json
kubectl expose rc nginx --port=80 --target-port=8000
kubectl get pod mypod -o yaml | sed 's/\(image: myimage\):.*$/\1:v4/' | kubectl replace -f -
kubectl label pods my-pod new-label=awesome kubectl label pods my-pod new-label- kubectl annotate pods my-pod icon-url=http://goo.gl/XXBTWq kubectl autoscale deployment foo --min=2 --max=10
|
部分更新资源
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| kubectl patch node k8s-node-1 -p '{"spec":{"unschedulable":true}}'
kubectl patch pod valid-pod -p '{"spec":{"containers":[{"name":"kubernetes-serve-hostname","image":"new image"}]}}'
kubectl patch pod valid-pod --type='json' -p='[{"op": "replace", "path": "/spec/containers/0/image", "value":"new image"}]'
kubectl patch deployment valid-deployment --type json -p='[{"op": "remove", "path": "/spec/template/spec/containers/0/livenessProbe"}]'
kubectl patch sa default --type='json' -p='[{"op": "add", "path": "/secrets/1", "value": {"name": "whatever" } }]'
kubectl patch deployment nginx-deployment --subresource='scale' --type='merge' -p '{"spec":{"replicas":2}}'
|
对资源进行伸缩
1 2 3 4
| kubectl scale --replicas=3 rs/foo kubectl scale --replicas=3 -f foo.yaml kubectl scale --current-replicas=2 --replicas=3 deployment/mysql kubectl scale --replicas=5 rc/foo rc/bar rc/baz
|
删除资源
1 2 3 4 5 6 7
| kubectl delete -f ./pod.json kubectl delete pod unwanted --now kubectl delete pod,service baz foo kubectl delete pods,services -l name=myLabel kubectl -n my-ns delete pod,svc --all
kubectl get pods -n mynamespace --no-headers=true | awk '/pattern1|pattern2/{print $1}' | xargs kubectl delete -n mynamespace pod
|
与运行中的 Pod 进行交互
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| kubectl logs my-pod kubectl logs -l name=myLabel kubectl logs my-pod --previous kubectl logs my-pod -c my-container kubectl logs -l name=myLabel -c my-container kubectl logs my-pod -c my-container --previous kubectl logs -f my-pod kubectl logs -f my-pod -c my-container kubectl logs -f -l name=myLabel --all-containers kubectl run -i --tty busybox --image=busybox:1.28 -- sh kubectl run nginx --image=nginx -n mynamespace kubectl run nginx --image=nginx --dry-run=client -o yaml > pod.yaml
kubectl attach my-pod -i kubectl port-forward my-pod 5000:6000 kubectl exec my-pod -- ls / kubectl exec --stdin --tty my-pod -- /bin/sh kubectl exec my-pod -c my-container -- ls / kubectl top pod POD_NAME --containers kubectl top pod POD_NAME --sort-by=cpu
|
从容器中复制文件和目录
1 2 3 4
| kubectl cp /tmp/foo_dir my-pod:/tmp/bar_dir kubectl cp /tmp/foo my-pod:/tmp/bar -c my-container kubectl cp /tmp/foo my-namespace/my-pod:/tmp/bar kubectl cp my-namespace/my-pod:/tmp/foo /tmp/bar
|
与 Deployments 和 Services 进行交互
1 2 3 4 5 6 7 8
| kubectl logs deploy/my-deployment kubectl logs deploy/my-deployment -c my-container
kubectl port-forward svc/my-service 5000 kubectl port-forward svc/my-service 5000:my-service-port
kubectl port-forward deploy/my-deployment 5000:6000 kubectl exec deploy/my-deployment -- ls
|
与节点和集群进行交互
1 2 3 4 5 6 7 8 9 10 11 12 13
| kubectl cordon my-node kubectl drain my-node kubectl uncordon my-node kubectl top node my-node kubectl cluster-info kubectl cluster-info dump kubectl cluster-info dump --output-directory=/path/to/cluster-state
kubectl get nodes -o='custom-columns=NodeName:.metadata.name,TaintKey:.spec.taints[*].key,TaintValue:.spec.taints[*].value,TaintEffect:.spec.taints[*].effect'
kubectl taint nodes foo dedicated=special-user:NoSchedule
|
资源类型
列出所支持的全部资源类型和它们的简称、API 组, 名字空间作用域 和 Kind。
用于探索 API 资源的其他操作:
1 2 3 4 5 6
| kubectl api-resources --namespaced=true kubectl api-resources --namespaced=false kubectl api-resources -o name kubectl api-resources -o wide kubectl api-resources --verbs=list,get kubectl api-resources --api-group=extensions
|