Kubectl工具
Kubernetes 提供 kubectl 是使用 Kubernetes API 与 Kubernetes 集群的控制面进行通信的命令行工具。
示例环境参考kubeadm安装环境
优化使用
自动补全
默认kubectl是没有命令的自动补全的,需要手动安装才可以。
- 在需要自动补全的机器上执行下面的命令
- 退出终端再次进入就会自动加载,
kubectl
后加上Tab
键就可以提示补全了。
yum install bash-completion -y
kubectl completion bash >/etc/bash_completion.d/kubectl
echo 'source <(kubectl completion bash)' >>~/.bashrc
复制
任意节点使用
- 将 master 节点中
/etc/kubernetes/admin.conf
拷贝到需要运行的服务器的/etc/kubernetes
目录中 - 在对应的服务器上配置环境变量
#在node1执行
scp /etc/kubernetes/admin.conf root@node2:/etc/kubernetes
scp /etc/kubernetes/admin.conf root@node3:/etc/kubernetes
#在node2和node3执行
echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> ~/.bash_profile
source ~/.bash_profile
复制
格式化输出
- 输出 json 格式
-o json
- 仅打印资源名称
-o name
- 以纯文本格式输出所有信息
-o wide
- 输出 yaml 格式
-o yaml
常用命令
创建资源
kubectl create -f ./my-manifest.yaml # 创建资源
kubectl create -f ./my1.yaml -f ./my2.yaml # 使用多个文件创建资源
kubectl create -f ./dir # 使用目录下的所有清单文件来创建资源
kubectl create -f https://git.io/vPieo # 使用 url 来创建资源
kubectl run nginx --image=nginx # 启动一个 nginx 实例
kubectl explain pods,svc # 获取 pod 和 svc 的文档
复制
显示和查询资源
#列出默认namespace的service
kubectl get services
#列出所有namespace的service
kubectl get services --all-namespaces
#列出kube-system空间下的service
kubectl get services -n kube-system
#列出所有 namespace 中的所有 pod
kubectl get pods --all-namespaces
#列出所有 pod 并显示详细信息
kubectl get pods -o wide
# 列出指定deployment
kubectl get deployment nginx
# 使用详细输出来描述命令
kubectl describe nodes node1
kubectl describe pods nginx-85b98978db-wb5jp
# 根据名称排序列出服务列表
kubectl get services --sort-by=.metadata.name
# 根据重启次数排序列出 pod
kubectl get pods --sort-by='.status.containerStatuses[0].restartCount'
# 获取所有节点的 ExternalIP
kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="ExternalIP")].address}'
# 列出属于某个 PC 的 Pod 的名字
# “jq”命令用于转换复杂的 jsonpath,参考 https://stedolan.github.io/jq/
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})
# 查看哪些节点已就绪
JSONPATH='{range .items[*]}{@.metadata.name}:{range @.status.conditions[*]}{@.type}={@.status};{end}{end}' \
&& kubectl get nodes -o jsonpath="$JSONPATH" | grep "Ready=True"
# 列出当前 Pod 中使用的 Secret
kubectl get pods -o json | jq '.items[].spec.containers[].env[]?.valueFrom.secretKeyRef.name' | grep -v null | sort | uniq
复制
更新资源
# 滚动更新 pod frontend-v1
kubectl rolling-update frontend-v1 -f frontend-v2.json
# 更新资源名称并更新镜像
kubectl rolling-update frontend-v1 frontend-v2 --image=image:v2
# 更新 frontend pod 中的镜像
kubectl rolling-update frontend --image=image:v2
# 退出已存在的进行中的滚动更新
kubectl rolling-update frontend-v1 frontend-v2 --rollback
# 基于 stdin 输入的 JSON 替换 pod
cat pod.json | kubectl replace -f -
# 强制替换,删除后重新创建资源。会导致服务中断。
kubectl replace --force -f ./pod.json
# 为 nginx RC 创建服务,启用本地 80 端口连接到容器上的 8000 端口
kubectl expose rc nginx --port=80 --target-port=8000
# 更新单容器 pod 的镜像版本(tag)到 v4
kubectl get pod mypod -o yaml | sed 's/\(image: myimage\):.*$/\1:v4/' | kubectl replace -f -
# 添加标签
kubectl label pods my-pod new-label=awesome
# 添加注解
kubectl annotate pods my-pod icon-url=http://goo.gl/XXBTWq
# 自动扩展 deployment “foo”
kubectl autoscale deployment foo --min=2 --max=10
复制
修补资源
# 部分更新节点
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 补丁更新容器镜像
kubectl patch pod valid-pod --type='json' -p='[{"op": "replace", "path": "/spec/containers/0/image", "value":"new image"}]'
# 使用具有位置数组的 json 补丁禁用 deployment 的 livenessProbe
kubectl patch deployment valid-deployment --type json -p='[{"op": "remove", "path": "/spec/template/spec/containers/0/livenessProbe"}]'
复制
编辑资源
# 编辑名为 docker-registry 的 service
kubectl edit svc/docker-registry
# 使用其它编辑器
KUBE_EDITOR="nano" kubectl edit svc/docker-registry
复制
Scale资源
# Scale a replicaset named 'foo' to 3
kubectl scale --replicas=3 rs/foo
# Scale a resource specified in "foo.yaml" to 3
kubectl scale --replicas=3 -f foo.yaml
# If the deployment named mysql's current size is 2, scale mysql to 3
kubectl scale --current-replicas=2 --replicas=3 deployment/mysql
# Scale multiple replication controllers
kubectl scale --replicas=5 rc/foo rc/bar rc/baz
复制
删除资源
# 删除 pod.json 文件中定义的类型和名称的 pod
kubectl delete -f ./pod.json
# 删除名为“baz”的 pod 和名为“foo”的 service
kubectl delete pod,service baz foo
# 删除具有 name=myLabel 标签的 pod 和 serivce
kubectl delete pods,services -l name=myLabel
# 删除 my-ns namespace 下的所有 pod 和 serivce,包括尚未初始化的
kubectl -n my-ns delete po,svc --all
复制
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。