kubectl 命令技巧大全

    Kubectl 上下文和配置

    设置 命令交互的 kubernetes 集群并修改配置信息。参阅 获取关于配置文件的详细信息。

    1. $ kubectl config view # 显示合并后的 kubeconfig 配置
    2. # 同时使用多个 kubeconfig 文件并查看合并后的配置
    3. $ KUBECONFIG=~/.kube/config:~/.kube/kubconfig2 kubectl config view
    4. # 获取 e2e 用户的密码
    5. $ kubectl config view -o jsonpath='{.users[?(@.name == "e2e")].user.password}'
    6. $ kubectl config current-context # 显示当前的上下文
    7. $ kubectl config use-context my-cluster-name # 设置默认上下文为 my-cluster-name
    8. # 向 kubeconf 中增加支持基本认证的新集群
    9. $ kubectl config set-credentials kubeuser/foo.kubernetes.com --username=kubeuser --password=kubepassword
    10. # 使用指定的用户名和 namespace 设置上下文
    11. $ kubectl config set-context gce --user=cluster-admin --namespace=foo \
    12. && kubectl config use-context gce

    创建对象

    Kubernetes 的清单文件可以使用 json 或 yaml 格式定义。可以以 .yaml.yml、或者 .json 为扩展名。

    1. $ kubectl create -f ./my-manifest.yaml # 创建资源
    2. $ kubectl create -f ./my1.yaml -f ./my2.yaml # 使用多个文件创建资源
    3. $ kubectl create -f ./dir # 使用目录下的所有清单文件来创建资源
    4. $ kubectl create -f https://git.io/vPieo # 使用 url 来创建资源
    5. $ kubectl run nginx --image=nginx # 启动一个 nginx 实例
    6. $ kubectl explain pods,svc # 获取 pod 和 svc 的文档
    7. # 从 stdin 输入中创建多个 YAML 对象
    8. $ cat <<EOF | kubectl create -f -
    9. kind: Pod
    10. metadata:
    11. name: busybox-sleep
    12. spec:
    13. containers:
    14. - name: busybox
    15. image: busybox
    16. args:
    17. - sleep
    18. - "1000000"
    19. ---
    20. apiVersion: v1
    21. kind: Pod
    22. metadata:
    23. name: busybox-sleep-less
    24. spec:
    25. containers:
    26. - name: busybox
    27. image: busybox
    28. args:
    29. - sleep
    30. - "1000"
    31. EOF
    32. # 创建包含几个 key 的 Secret
    33. $ cat <<EOF | kubectl create -f -
    34. apiVersion: v1
    35. kind: Secret
    36. metadata:
    37. name: mysecret
    38. data:
    39. password: $(echo "s33msi4" | base64)
    40. username: $(echo "jane" | base64)
    41. EOF

    显示和查找资源

    1. $ kubectl rolling-update frontend-v1 -f frontend-v2.json # 滚动更新 pod frontend-v1
    2. $ kubectl rolling-update frontend-v1 frontend-v2 --image=image:v2 # 更新资源名称并更新镜像
    3. $ kubectl rolling-update frontend --image=image:v2 # 更新 frontend pod 中的镜像
    4. $ kubectl rolling-update frontend-v1 frontend-v2 --rollback # 退出已存在的进行中的滚动更新
    5. $ cat pod.json | kubectl replace -f - # 基于 stdin 输入的 JSON 替换 pod
    6. $ kubectl replace --force -f ./pod.json
    7. # 为 nginx RC 创建服务,启用本地 80 端口连接到容器上的 8000 端口
    8. $ kubectl expose rc nginx --port=80 --target-port=8000
    9. # 更新单容器 pod 的镜像版本(tag)到 v4
    10. $ kubectl get pod mypod -o yaml | sed 's/\(image: myimage\):.*$/\1:v4/' | kubectl replace -f -
    11. $ kubectl label pods my-pod new-label=awesome # 添加标签
    12. $ kubectl annotate pods my-pod icon-url=http://goo.gl/XXBTWq # 添加注解
    13. $ kubectl autoscale deployment foo --min=2 --max=10 # 自动扩展 deployment “foo”

    修补资源

    1. $ kubectl patch node k8s-node-1 -p '{"spec":{"unschedulable":true}}' # 部分更新节点
    2. # 更新容器镜像; spec.containers[*].name 是必须的,因为这是合并的关键字
    3. $ kubectl patch pod valid-pod -p '{"spec":{"containers":[{"name":"kubernetes-serve-hostname","image":"new image"}]}}'
    4. # 使用具有位置数组的 json 补丁更新容器镜像
    5. $ kubectl patch pod valid-pod --type='json' -p='[{"op": "replace", "path": "/spec/containers/0/image", "value":"new image"}]'
    6. # 使用具有位置数组的 json 补丁禁用 deployment 的 livenessProbe
    7. $ kubectl patch deployment valid-deployment --type json -p='[{"op": "remove", "path": "/spec/template/spec/containers/0/livenessProbe"}]'

    编辑资源

    在编辑器中编辑任何 API 资源。

    Scale 资源

    1. $ kubectl scale --replicas=3 rs/foo # Scale a replicaset named 'foo' to 3
    2. $ kubectl scale --replicas=3 -f foo.yaml # Scale a resource specified in "foo.yaml" to 3
    3. $ kubectl scale --current-replicas=2 --replicas=3 deployment/mysql # If the deployment named mysql's current size is 2, scale mysql to 3
    4. $ kubectl scale --replicas=5 rc/foo rc/bar rc/baz # Scale multiple replication controllers
    1. $ kubectl delete -f ./pod.json # 删除 pod.json 文件中定义的类型和名称的 pod
    2. $ kubectl delete pod,service baz foo # 删除名为“baz”的 pod 和名为“foo”的 service
    3. $ kubectl delete pods,services -l name=myLabel # 删除具有 name=myLabel 标签的 pod 和 serivce
    4. $ kubectl delete pods,services -l name=myLabel --include-uninitialized # 删除具有 name=myLabel 标签的 pod 和 service,包括尚未初始化的
    5. $ kubectl -n my-ns delete po,svc --all # 删除 my-ns namespace 下的所有 pod 和 serivce,包括尚未初始化的

    与运行中的 Pod 交互

    与节点和集群交互

    1. $ kubectl cordon my-node # 标记 my-node 不可调度
    2. $ kubectl drain my-node # 清空 my-node 以待维护
    3. $ kubectl uncordon my-node # 标记 my-node 可调度
    4. $ kubectl top node my-node # 显示 my-node 的指标度量
    5. $ kubectl cluster-info # 显示 master 和服务的地址
    6. $ kubectl cluster-info dump # 将当前集群状态输出到 stdout
    7. $ kubectl cluster-info dump --output-directory=/path/to/cluster-state # 将当前集群状态输出到 /path/to/cluster-state
    8. # 如果该键和影响的污点(taint)已存在,则使用指定的值替换
    9. $ kubectl taint nodes foo dedicated=special-user:NoSchedule

    资源类型

    下表列出的是 kubernetes 中所有支持的类型和缩写的别名。

    Kubectl 详细输出和调试

    使用 -v--v 标志跟着一个整数来指定日志级别。

    本文是对官方文档的中文翻译,原文地址:https://kubernetes.io/docs/user-guide/kubectl-cheatsheet/