一些基本的kubernets操作命令
kubectl 命令技巧大全
Kubectl 自动补全
1 | yum install -y bash-completion |
创建对象
Kubernetes 的清单文件可以使用 json 或 yaml 格式定义。可以以 .yaml、.yml、或者 .json 为扩展名。
1 | $ kubectl create -f ./my-manifest.yaml # 创建资源 |
显示和查找资源
列出所有 namespace 中的所有service
1 | $ kubectl get services |
列出所有 namespace 中的所有 pod
1 | $ kubectl get pods --all-namespaces |
列出所有 pod 并显示详细信息
1 | $ kubectl get pods -o wide |
列出指定 deployment
1 | $ kubectl get deployment my-dep |
列出该 namespace 中的所有 pod 包括未初始化的
1 | $ kubectl get pods --include-uninitialized |
使用详细输出来描述命令
1 | $ kubectl describe nodes my-node |
根据重启次数排序列出 pod
1 | $ kubectl get pods --sort-by='.status.containerStatuses[0].restartCount' |
获取所有具有 app=cassandra 的 pod 中的 version 标签
1 | $ kubectl get pods --selector=app=cassandra rc -o \ jsonpath='{.items[*].metadata.labels.version}' |
获取所有节点的 ExternalIP
1 | $ kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="ExternalIP")].address}' |
列出属于某个 PC 的 Pod 的名字,“jq”命令用于转换复杂的 jsonpath,参考 https://stedolan.github.io/jq/
1 | $ sel=${$(kubectl get rc my-rc --output=json | jq -j '.spec.selector | to_entries | .[] | "\(.key)=\(.value),"')%?} |
查看哪些节点已就绪
1 | $ JSONPATH='{range .items[*]}{@.metadata.name}:{range @.status.conditions[*]}{@.type}={@.status};{end}{end}' \ && kubectl get nodes -o jsonpath="$JSONPATH" | grep "Ready=True" |
列出当前 Pod 中使用的 Secret
1 | $ kubectl get pods -o json | jq '.items[].spec.containers[].env[]?.valueFrom.secretKeyRef.name' | grep -v null | sort | uniq |
更新资源
滚动更新 pod frontend-v1
1 | $ kubectl rolling-update frontend-v1 -f frontend-v2.json |
更新资源名称并更新镜像
1 | $ kubectl rolling-update frontend-v1 frontend-v2 --image=image:v2 |
更新 frontend pod 中的镜像
1 | $ kubectl rolling-update frontend --image=image:v2 |
退出已存在的进行中的滚动更新
1 | $ kubectl rolling-update frontend-v1 frontend-v2 --rollback |
基于 stdin 输入的 JSON 替换 pod
1 | $ cat pod.json | kubectl replace -f - |
强制替换,删除后重新创建资源。会导致服务中断。
1 | $ kubectl replace --force -f ./pod.json |
为 nginx RC 创建服务,启用本地 80 端口连接到容器上的 8000 端口
1 | $ kubectl expose rc nginx --port=80 --target-port=8000 |
更新单容器 pod 的镜像版本(tag)到 v4
1 | $ kubectl get pod mypod -o yaml | sed 's/\(image: myimage\):.*$/\1:v4/' | kubectl replace -f - |
添加标签
1 | $ kubectl label pods my-pod new-label=awesome |
添加注解
1 | $ kubectl annotate pods my-pod icon-url=http://goo.gl/XXBTWq |
自动扩展 deployment “foo”
1 | $ kubectl autoscale deployment foo --min=2 --max=10 |
删除资源
# 删除 pod.json 文件中定义的类型和名称的 pod
1 | $ kubectl delete -f ./pod.json |
删除名为“baz”的 pod 和名为“foo”的 service
1 | $ kubectl delete pod,service baz foo |
删除具有 name=myLabel 标签的 pod 和 serivce
1 | $ kubectl delete pods,services -l name=myLabel |
删除具有 name=myLabel 标签的 pod 和 service,包括尚未初始化的
1 | $ kubectl delete pods,services -l name=myLabel --include-uninitialized |
删除 my-ns namespace 下的所有 pod 和 serivce,包括尚未初始化的
1 | $ kubectl -n my-ns delete po,svc --all |
与运行中的Pod交互
# dump 输出 pod 的日志(stdout)
1 | $ kubectl logs my-pod |
dump 输出 pod 中容器的日志(stdout,pod 中有多个容器的情况下使用)
1 | $ kubectl logs my-pod -c my-container |
流式输出 pod 的日志(stdout)
1 | $ kubectl logs -f my-pod |
流式输出 pod 中容器的日志(stdout,pod 中有多个容器的情况下使用)
1 | $ kubectl logs -f my-pod -c my-container |
交互式 shell 的方式运行 pod
1 | $ kubectl run -i --tty busybox --image=busybox -- sh |
连接到运行中的容器
1 | $ kubectl attach my-pod -i |
转发 pod 中的 6000 端口到本地的 5000 端口
1 | $ kubectl port-forward my-pod 5000:6000 |
在已存在的容器中执行命令(只有一个容器的情况下)
1 | $ kubectl exec my-pod -- ls / |
在已存在的容器中执行命令(pod 中有多个容器的情况下)
1 | $ kubectl exec my-pod -c my-container -- ls / |
显示指定 pod 和容器的指标度量
1 | $ kubectl top pod POD_NAME --containers |
与节点和集群交互
# 标记 my-node 不可调度
1 | $ kubectl cordon my-node |
清空 my-node 以待维护
1 | $ kubectl drain my-node |
标记 my-node 可调度
1 | $ kubectl uncordon my-node |
显示 my-node 的指标度量
1 | $ kubectl top node my-node |
显示 master 和服务的地址
1 | $ kubectl cluster-info |
将当前集群状态输出到 stdout
1 | $ kubectl cluster-info dump |
将当前集群状态输出到 /path/to/cluster-state
1 | $ kubectl cluster-info dump --output-directory=/path/to/cluster-state |
如果该键和影响的污点(taint)已存在,则使用指定的值替换
1 | $ kubectl taint nodes foo dedicated=special-user:NoSchedule |
| 资源类型 | 缩写别名 |
|---|---|
| clusters | |
| componentstatuses | cs |
| configmaps | cm |
| daemonsets | ds |
| deployments | deploy |
| endpoints | ep |
| event | ev |
| horizontalpodautoscalers | hpa |
| ingresses | ing |
| jobs | |
| limitranges | limits |
| namespaces | ns |
| networkpolicies | |
| nodes | no |
| statefulsets | |
| persistentvolumeclaims | pvc |
| persistentvolumes | pv |
| pods | po |
| podsecuritypolicies | psp |
| podtemplates | |
| replicasets | rs |
| replicationcontrollers | rc |
| resourcequotas | quota |
| cronjob | |
| secrets | |
| serviceaccount | sa |
| services | svc |
| storageclasses |
kubectl get - 显示资源列表
#获取类型为Deployment的资源列表
1 | kubectl get deployments |
#获取类型为Pod的资源列表
1 | kubectl get pods |
#获取类型为Node的资源列表
1 | kubectl get nodes |
名称空间
在命令后增加 -A 或 –all-namespaces 可查看所有名称空间中的对象,使用参数 -n 可查看指定名称空间的对象,例如
# 查看所有名称空间的 Deployment
1 | kubectl get deployments -A |
查看 kube-system 名称空间的 Deployment
1 | kubectl get deployments -n kube-system |
检查 kubectl 是否知道集群地址及凭证
1 | $ kubectl config view |
通过 kubectl cluster-info 命令获得这些服务列表:
1 | [root@ebs ~]# kubectl cluster-info |
查看集群中的容器镜像
查看所有名称空间总的容器
使用 -o jsonpath={..image} 参数,输出结果将格式化为只包含容器镜像名字的形式。该参数将递归地查找 JSON 数据中所有 image 字段,例如:
1 | kubectl get pods --all-namespaces -o jsonpath={..image} |
使用工具 tr、sort、uniq 格式化输出结果, 此命令将递归返回所有 image 字段。
- 使用
tr将空格替换为新的行 - 使用
sort对结果排序 - 使用
uniq对镜像使用计数
1 | kubectl get pods --all-namespaces -o jsonpath="{..image}" |\ |
Jsonpath 的参数解释如下:
.items[*]:每一个返回值.spec: 获取 spec.containers[*]: 每一个 container.image:获取 image
按Pod查找容器
输出结果可以通过 rannge 操作遍历
1 | kubectl get pods --all-namespaces -o=jsonpath='{range .items[*]}{"\n"}{.metadata.name}{":\t"}{range .spec.containers[*]}{.image}{", "}{end}{end}' |\ |
按Pod的label查找容器
使用 -l 参数,可以查找指定标签的 Pod,下面的例子中只查找带有 app=nginx 标签的 Pod:
1 | kubectl get pods --all-namespaces -o=jsonpath="{..image}" -l app=nginx |
按名称空间查找容器
使用 --namespace 参数,可以查找指定名称空间下的 Pod,下面的例子只查找 kube-system 名称空间中的 Pod:
1 | kubectl get pods --namespace kube-system -o jsonpath="{..image}" |