kubectl

    kubectl 提供了大量的子命令,方便管理 Kubernetes 集群中的各种功能。这里不再罗列各种子命令的格式,而是介绍下如何查询命令的帮助

    • 查看子命令列表
    • kubectl options 查看全局选项
    • kubectl <command> --help 查看子命令的帮助
    • kubectl [command] [PARAMS] -o=<format> 设置输出格式(如 json、yaml、jsonpath 等)

    使用 kubectl 的第一步是配置 Kubernetes 集群以及认证方式,包括

    • cluster 信息:Kubernetes server 地址
    • 用户信息:用户名、密码或密钥
    • Context:cluster、用户信息以及 Namespace 的组合

    示例

    常用命令格式

    • 创建:kubectl run <name> --image=<image> 或者 kubectl create -f manifest.yaml
    • 查询:kubectl get <resource>
    • 更新
    • 删除:kubectl delete <resource> <name> 或者 kubectl delete -f manifest.yaml
    • 查询 Pod IP:kubectl get pod <pod-name> -o jsonpath='{.status.podIP}'
    • 容器内执行命令:kubectl exec -ti <pod-name> sh
    • 容器日志:kubectl logs [-f] <pod-name>
    • 导出服务:kubectl expose deploy <name> --port=80

    注意,kubectl run 仅支持 Pod、Replication Controller、Deployment、Job 和 CronJob 等几种资源。具体的资源类型是由参数决定的,默认为 Deployment:

    命令行自动补全

    Linux 系统 Bash:

    1. source /usr/share/bash-completion/bash_completion
    2. source <(kubectl completion bash)

    MacOS zsh

    1. source <(kubectl completion zsh)

    日志查看

    1. kubectl logs nginx
    2. # Return snapshot of previous terminated ruby container logs from pod web-1
    3. kubectl logs -p -c ruby web-1
    4. # Begin streaming the logs of the ruby container in pod web-1
    5. kubectl logs -f -c ruby web-1

    kubectl attach 用于连接到一个正在运行的容器。跟 docker 的 attach 命令类似。

    1. # Get output from running pod 123456-7890, using the first container by default
    2. # Get output from ruby-container from pod 123456-7890
    3. kubectl attach 123456-7890 -c ruby-container
    4. # Switch to raw terminal mode, sends stdin to 'bash' in ruby-container from pod 123456-7890
    5. # and sends stdout/stderr from 'bash' back to the client
    6. kubectl attach 123456-7890 -c ruby-container -i -t
    7. Options:
    8. -c, --container='': Container name. If omitted, the first container in the pod will be chosen
    9. -i, --stdin=false: Pass stdin to the container
    10. -t, --tty=false: Stdin is a TTY

    在容器内部执行命令

    kubectl exec 用于在一个正在运行的容器执行命令。跟 docker 的 exec 命令类似。

    端口转发

    kubectl port-forward 用于将本地端口转发到指定的 Pod。

    1. # Listen on port 8888 locally, forwarding to 5000 in the pod
    2. kubectl port-forward mypod 8888:5000

    API Server 代理

    kubectl proxy 命令提供了一个 Kubernetes API 服务的 HTTP 代理。

    1. $ kubectl proxy --port=8080
    2. Starting to serve on 127.0.0.1:8080

    可以通过代理地址 http://localhost:8080/api/ 来直接访问 Kubernetes API,比如查询 Pod 列表

    1. curl http://localhost:8080/api/v1/namespaces/default/pods

    注意,如果通过 --address 指定了非 localhost 的地址,则访问 8080 端口时会报未授权的错误,可以设置 --accept-hosts 来避免这个问题( 不推荐生产环境这么设置 ):

    1. kubectl proxy --address='0.0.0.0' --port=8080 --accept-hosts='^*$'

    注意:文件拷贝依赖于 tar 命令,所以容器中需要能够执行 tar 命令

    kubectl drain

    1. kubectl drain NODE [Options]
    • 它会删除该 NODE 上由 ReplicationController, ReplicaSet, DaemonSet, StatefulSet or Job 创建的 Pod
    • 不删除 mirror pods(因为不可通过 API 删除 mirror pods)
    • 如果还有其它类型的 Pod(比如不通过 RC 而直接通过 kubectl create 的 Pod)并且没有 —force 选项,该命令会直接失败
    • 如果命令中增加了 —force 选项,则会强制删除这些不是通过 ReplicationController, Job 或者 DaemonSet 创建的 Pod

    有的时候不需要 evict pod,只需要标记 Node 不可调用,可以用 kubectl cordon 命令。

    恢复的话只需要运行 kubectl uncordon NODE 将 NODE 重新改成可调度状态。

    权限检查

    kubectl auth 提供了两个子命令用于检查用户的鉴权情况:

    1. # Check to see if I can create pods in any namespace
    2. kubectl auth can-i create pods --all-namespaces
    3. # Check to see if I can list deployments in my current namespace
    4. kubectl auth can-i list deployments.extensions
    5. # Check to see if I can do everything in my current namespace ("*" means all)
    6. kubectl auth can-i '*' '*'
    7. # Check to see if I can get the job named "bar" in namespace "foo"
    8. kubectl auth can-i list jobs.batch/bar -n foo
    • kubectl auth reconcile 自动修复有问题的 RBAC 策略,如
    1. # Reconcile rbac resources from a file
    2. kubectl auth reconcile -f my-rbac-rules.yaml

    kubectl 插件

    kubectl 插件提供了一种扩展 kubectl 的机制,比如添加新的子命令。插件可以以任何语言编写,只需要满足以下条件即可

    • 插件放在 ~/.kube/plugins 或环境变量 KUBECTL_PLUGINS_PATH 指定的目录中
    • 插件的格式为 子目录 / 可执行文件或脚本 且子目录中要包括 plugin.yaml 配置文件

    比如

    1. $ tree
    2. .
    3. └── hello
    4. └── plugin.yaml
    5. 1 directory, 1 file
    6. $ cat hello/plugin.yaml
    7. name: "hello"
    8. shortDesc: "Hello kubectl plugin!"
    9. command: "echo Hello plugins!"
    10. $ kubectl plugin hello
    11. Hello plugins!
    • kubectl get --raw /apis/metrics.k8s.io/v1beta1/nodes
    • kubectl get --raw /apis/metrics.k8s.io/v1beta1/pods

    附录

    kubectl 的安装方法