Kubectl 自动补全
BASH
| source <(kubectl completion bash) |
| echo "source <(kubectl completion bash)" >> ~/.bashrc |
还可以为 kubectl
使用一个速记别名,该别名也可以与 completion 一起使用:
| alias k=kubectl |
| complete -F __start_kubectl k |
Kubectl 上下文和配置
设置 kubectl
与哪个 Kubernetes 集群进行通信并修改配置信息。 查看使用 kubeconfig 跨集群授权访问 文档获取配置文件详细信息。
| 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-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' |
Kubectl apply
apply
通过定义 Kubernetes 资源的文件来管理应用。 它通过运行 kubectl apply
在集群中创建和更新资源。 这是在生产中管理 Kubernetes 应用的推荐方法
| |
| 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 |
| |
| |
| cat <<EOF | kubectl apply -f - |
| 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 get
get
查看pod和查找service controller资源
| |
| 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 get services --sort-by=.metadata.name |
| |
| kubectl get pv --sort-by=.spec.capacity.storage |
| # 列举所有 Pods 中初始化容器的容器 ID(containerID) |
| # 可用于在清理已停止的容器时避免删除初始化容器 |
| kubectl get pods --all-namespaces -o jsonpath='{range .items[*].status.initContainerStatuses[*]}{.containerID}{"\n"}{end}' | cut -d/ -f3 |
| |
| # 列出事件(Events),按时间戳排序 |
| kubectl get events --sort-by=.metadata.creationTimestamp |
| |
| # 比较当前的集群状态和假定某清单被应用之后的集群状态 |
| kubectl diff -f ./my-manifest.yaml |
| |
| # 生成一个句点分隔的树,其中包含为节点返回的所有键 |
| # 在复杂的嵌套JSON结构中定位键时非常有用 |
| kubectl get nodes -o json | jq -c 'paths|join(".")' |
| |
| # 生成一个句点分隔的树,其中包含为pod等返回的所有键 |
| kubectl get pods -o json | jq -c 'paths|join(".")' |
kubectl rollout
rollout
更新资源,版本 回退
| 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 |
kubectl patch
更新部分资源
| # 部分更新某节点 |
| kubectl patch node k8s-node-1 -p '{"spec":{"unschedulable":true}}' |
| |
| # 更新容器的镜像;spec.containers[*].name 是必须的。因为它是一个合并性质的主键。 |
| kubectl patch pod valid-pod -p '{"spec":{"containers":[{"name":"kubernetes-serve-hostname","image":"new image"}]}}' |
| |
| # 使用带位置数组的 JSON patch 更新容器的镜像 |
| kubectl patch pod valid-pod --type='json' -p='[{"op": "replace", "path": "/spec/containers/0/image", "value":"new image"}]' |
| |
| # 使用带位置数组的 JSON patch 禁用某 Deployment 的 livenessProbe |
| 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 edit
编辑资源
| kubectl edit svc/docker-registry |
| KUBE_EDITOR="nano" kubectl edit svc/docker-registry |
kubectl scale
对资源进行扩缩容
| 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 |
kubectl delete
删除资源
| kubectl delete -f ./pod.json |
| 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 |
kubectl log
日志查看
| 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 cp
从容器中复制文件和目录
| 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 |
Note: kubectl cp
要求容器镜像中存在 “tar” 二进制文件。如果 “tar” 不存在,kubectl cp
将失败。 对于进阶用例,例如符号链接、通配符扩展或保留文件权限,请考虑使用 kubectl exec
。
| tar cf - /tmp/foo | kubectl exec -i -n my-namespace my-pod -- tar xf - -C /tmp/bar |
| kubectl exec -n my-namespace my-pod -- tar cf - /tmp/foo | tar xf - -C /tmp/bar |
| <!-- |
| |
| --> |
| |
| |
| <!-- |
与节点和集群进行交互
| 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 taint nodes foo dedicated=special-user:NoSchedule |
资源类型
列出所支持的全部资源类型和它们的简称、API 组, 是否是名字空间作用域 和 Kind。
用于探索 API 资源的其他操作:
| kubectl api-resources --namespaced=true # 所有命名空间作用域的资源 |
| kubectl api-resources --namespaced=false # 所有非命名空间作用域的资源 |
| kubectl api-resources -o name # 用简单格式列举所有资源(仅显示资源名称) |
| kubectl api-resources -o wide # 用扩展格式列举所有资源(又称 "wide" 格式) |
| kubectl api-resources --verbs=list,get # 支持 "list" 和 "get" 请求动词的所有资源 |
| kubectl api-resources --api-group=extensions # "extensions" API 组中的所有资源 |
格式化输出
要以特定格式将详细信息输出到终端窗口,将 -o
(或者 --output
)参数添加到支持的 kubectl
命令中。
输出格式 |
描述 |
-o=custom-columns=<spec> |
使用逗号分隔的自定义列来打印表格 |
-o=custom-columns-file=<filename> |
使用 <filename> 文件中的自定义列模板打印表格 |
-o=json |
输出 JSON 格式的 API 对象 |
-o=jsonpath=<template> |
打印 jsonpath 表达式中定义的字段 |
-o=jsonpath-file=<filename> |
打印在 <filename> 文件中定义的 jsonpath 表达式所指定的字段。 |
-o=name |
仅打印资源名称而不打印其他内容 |
-o=wide |
以纯文本格式输出额外信息,对于 Pod 来说,输出中包含了节点名称 |
-o=yaml |
输出 YAML 格式的 API 对象 |
使用 -o=custom-columns
的示例:
| # 集群中运行着的所有镜像 |
| kubectl get pods -A -o=custom-columns= |
| |
| # 列举 default 名字空间中运行的所有镜像,按 Pod 分组 |
| kubectl get pods --namespace default --output=custom-columns="NAME:.metadata.name,IMAGE:.spec.containers[*].image" |
| |
| # 除 "k8s.gcr.io/coredns:1.6.2" 之外的所有镜像 |
| kubectl get pods -A -o=custom-columns= |
| |
| # 输出 metadata 下面的所有字段,无论 Pod 名字为何 |
| kubectl get pods -A -o=custom-columns= |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律