Kubectl 常用命令大全

Kubectl 常用参数分类

基础命令:create,delete,get,run,expose,set,explain,edit

# 创建Deployment和Service资源
​
$ kubectl create -f demo-deployment.yaml
$ kubectl create -f demo-service.yaml
# 根据yaml文件删除对应的资源,但是yaml文件并不会被删除,这样更加高效
​
$ kubectl delete -f demo-deployment.yaml
$ kubectl delete -f demo-service.yaml
​
# 也可以通过具体的资源名称来进行删除,使用这个删除资源,同时删除deployment和service资源
​
$ kubectl delete 具体的资源名称
# 查看所有的资源信息
$ kubectl get all
$ kubectl get --all-namespaces
​
# 查看pod列表
$ kubectl get pod
​
# 显示pod节点的标签信息
$ kubectl get pod --show-labels
​
# 根据指定标签匹配到具体的pod
$ kubectl get pods -l app=example
​
# 查看node节点列表
$ kubectl get node
​
# 显示node节点的标签信息
$ kubectl get node --show-labels
​
# 查看pod详细信息,也就是可以查看pod具体运行在哪个节点上(ip地址信息)
$ kubectl get pod -o wide
​
# 查看服务的详细信息,显示了服务名称,类型,集群ip,端口,时间等信息
$ kubectl get svc
$ kubectl get svc -n kube-system
​
# 查看命名空间
$ kubectl get ns
$ kubectl get namespaces
​
# 查看所有pod所属的命名空间
$ kubectl get pod --all-namespaces
​
# 查看所有pod所属的命名空间并且查看都在哪些节点上运行
$ kubectl get pod --all-namespaces  -o wide
​
# 查看目前所有的replica set,显示了所有的pod的副本数,以及他们的可用数量以及状态等信息
$ kubectl get rs
​
# 查看已经部署了的所有应用,可以看到容器,以及容器所用的镜像,标签等信息
$ kubectl get deploy -o wide
$ kubectl get deployments -o wide
 
# 示例,运行一个名称为nginx,副本数为3,标签为app=example,镜像为nginx:1.10,端口为80的容器实例
​
$ kubectl run nginx --replicas=3 --labels="app=example" --image=nginx:1.10 --port=80
# 示例,运行一个名称为nginx,副本数为3,标签为app=example,镜像为nginx:1.10,端口为80的容器实例,并绑定到k8s-node1上$ kubectl run nginx --image=nginx:1.10 --replicas=3 --labels="app=example" --port=80 --overrides='{"apiVersion":"apps/v1","spec":{"template":{"spec":{"nodeSelector":{"kubernetes.io/hostname":"k8s-node1"}}}}}'
# 创建一个nginx服务并且暴露端口让外界可以访问
​
$ kubectl expose deployment nginx --port=88 --type=NodePort --target-port=80 --name=nginx-service
 

kubectl set resources 命令

# 将deployment的nginx容器cpu限制为“200m”,将内存设置为“512Mi”
$ kubectl set resources deployment nginx -c=nginx --limits=cpu=200m,memory=512Mi
​
# 设置所有nginx容器中 Requests和Limits
$ kubectl set resources deployment nginx --limits=cpu=200m,memory=512Mi --requests=cpu=100m,memory=256Mi
​
# 删除nginx中容器的计算资源值
$ kubectl set resources deployment nginx --limits=cpu=0,memory=0 --requests=cpu=0,memory=0

kubectl set selector 命令

 

kubectl set image 命令

 
# 将deployment中的nginx容器镜像设置为“nginx:1.9.1”
$ kubectl set image deployment/nginx busybox=busybox nginx=nginx:1.9.1
​
# 所有deployment和rc的nginx容器镜像更新为“nginx:1.9.1”
$ kubectl set image deployments,rc nginx=nginx:1.9.1 --all
​
# 将daemonset abc的所有容器镜像更新为“nginx:1.9.1”
$ kubectl set image daemonset abc *=nginx:1.9.1
​
# 从本地文件中更新nginx容器镜像
$ kubectl set image -f path/to/file.yaml nginx=nginx:1.9.1 --local -o yaml
$ kubectl explain rs
# 编辑Deployment nginx的一些信息
$ kubectl edit deployment nginx
​
# 编辑service类型的nginx的一些信息
$ kubectl edit service/nginx

设置命令:label,annotate,completion

  •  
  •  
  •  
 
# 给名为foo的Pod添加label unhealthy=true
$ kubectl label pods foo unhealthy=true
​
# 给名为foo的Pod修改label 为 'status' / value 'unhealthy',且覆盖现有的value
$ kubectl label --overwrite pods foo status=unhealthy
​
# 给 namespace 中的所有 pod 添加 label
$ kubectl label  pods --all status=unhealthy
​
# 仅当resource-version=1时才更新 名为foo的Pod上的label
$ kubectl label pods foo status=unhealthy --resource-version=1
​
# 删除名为“bar”的label 。(使用“ - ”减号相连)
$ kubectl label pods foo bar-
  •  
  •  
  •  
  •  
 
# 更新Pod“foo”,设置annotation “description”的value “my frontend”,如果同一个annotation多次设置,则只使用最后设置的value值
$ kubectl annotate pods foo description='my frontend'
​
# 根据“pod.json”中的type和name更新pod的annotation
$ kubectl annotate -f pod.json description='my frontend'
​
# 更新Pod"foo",设置annotation“description”的value“my frontend running nginx”,覆盖现有的值
$ kubectl annotate --overwrite pods foo description='my frontend running nginx'
​
# 更新 namespace中的所有pod
$ kubectl annotate pods --all description='my frontend running nginx'
​
# 只有当resource-version为1时,才更新pod 'foo'
$ kubectl annotate pods foo description='my frontend running nginx' --resource-version=1
​
# 通过删除名为“description”的annotations来更新pod 'foo'。
# 不需要 -overwrite flag。
$ kubectl annotate pods foo description-
BASH
# 在 bash 中设置当前 shell 的自动补全,要先安装 bash-completion 包
$ source <(kubectl completion bash)
​
# 在您的 bash shell 中永久的添加自动补全
$ echo "source <(kubectl completion bash)" >> ~/.bashrc
ZSH
# 在 zsh 中设置当前 shell 的自动补全
$ source <(kubectl completion zsh)
​
# 在您的 zsh shell 中永久的添加自动补全
$ echo "if [ $commands[kubectl] ]; then source <(kubectl completion zsh); fi" >> ~/.zshrc

kubectl 部署命令:rollout,rolling-update,scale,autoscale

  •  
  •  
  •  
  •  
  •  
# 语法
$ kubectl rollout SUBCOMMAND
​
# 回滚到之前的deployment
$ kubectl rollout undo deployment/abc
​
# 查看daemonet的状态
$ kubectl rollout status daemonset/foo
 
# 使用frontend-v2.json中的新RC数据更新frontend-v1的pod
$ kubectl rolling-update frontend-v1 -f frontend-v2.json
​
# 使用JSON数据更新frontend-v1的pod
$ cat frontend-v2.json | kubectl rolling-update frontend-v1 -f -
​
# 其他的一些滚动更新
$ kubectl rolling-update frontend-v1 frontend-v2 --image=image:v2
​
$ kubectl rolling-update frontend --image=image:v2
​
$ kubectl rolling-update frontend-v1 frontend-v2 --rollback
 
# 将名为foo中的pod副本数设置为3。
$ kubectl scale --replicas=3 rs/foo
kubectl scale deploy/nginx --replicas=30
​
# 将由“foo.yaml”配置文件中指定的资源对象和名称标识的Pod资源副本设为3
$ kubectl scale --replicas=3 -f foo.yaml
​
# 如果当前副本数为2,则将其扩展至3。
$ kubectl scale --current-replicas=2 --replicas=3 deployment/mysql
​
# 设置多个RC中Pod副本数量
$ kubectl scale --replicas=5 rc/foo rc/bar rc/baz
 
# 使用 Deployment “foo”设定,使用默认的自动伸缩策略,指定目标CPU使用率,使其Pod数量在2到10之间
$ kubectl autoscale deployment foo --min=2 --max=10
​
# 使用RC“foo”设定,使其Pod的数量介于1和5之间,CPU使用率维持在80%
$ kubectl autoscale rc foo --max=5 --cpu-percent=80

集群管理命令:certificate,cluster-info,top,cordon,uncordon,drain,taint

# 例如,当有node节点要向master请求,那么是需要master节点授权的
$ kubectl certificate approve node-csr-81F5uBehyEyLWco5qavBsxc1GzFcZk3aFM3XW5rT3mw node-csr-Ed0kbFhc_q7qx14H3QpqLIUs0uKo036O2SnFpIheM18
$ kubectl cluster-info
# 以前需要heapster,后替换为metrics-server
$ kubectl top pod --all-namespaces

集群故障排查和调试命令:describe,logs,exec,attach,port-foward,proxy,cp,auth

# 查看my-nginx pod的详细状态
$ kubectl describe po my-nginx
 
# 返回仅包含一个容器的pod nginx的日志快照
$ kubectl logs nginx
​
# 返回pod ruby中已经停止的容器web-1的日志快照
$ kubectl logs -p -c ruby web-1
​
# 持续输出pod ruby中的容器web-1的日志
$ kubectl logs -f -c ruby web-1
​
# 仅输出pod nginx中最近的20条日志
$ kubectl logs --tail=20 nginx
​
# 输出pod nginx中最近一小时内产生的所有日志
$ kubectl logs --since=1h nginx
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
 
  •  
  •  
  •  
  •  
# 进入nginx容器,执行一些命令操作
$ kubectl exec -it nginx-deployment-58d6d6ccb8-lc5fp bash
 
  •  
  •  
  •  
# 获取正在运行中的pod 123456-7890的输出,默认连接到第一个容器
$ kubectl attach 123456-7890
​
# 获取pod 123456-7890中ruby-container的输出
$ kubectl attach 123456-7890 -c ruby-container
​
# 切换到终端模式,将控制台输入发送到pod 123456-7890的ruby-container的“bash”命令,并将其输出到控制台/
# 错误控制台的信息发送回客户端。
$ kubectl attach 123456-7890 -c ruby-container -i -t

其他命令:api-servions,config,help,plugin,version

# 打印当前集群支持的api版本
$ kubectl api-versions
# 显示全部的命令帮助提示
$ kubectl --help
​
# 具体的子命令帮助,例如
$ kubectl create --help
# 显示合并的 kubeconfig 配置
$ kubectl config view
​
# 同时使用多个 kubeconfig 文件并查看合并的配置
$ KUBECONFIG=~/.kube/config:~/.kube/kubconfig2 kubectl config view
​
# 获取 e2e 用户的密码
$ kubectl config view -o jsonpath='{.users[?(@.name == "e2e")].user.password}'
​
# 展示当前所处的上下文
$ kubectl config current-context
​
# 设置默认的上下文为 my-cluster-name
$ kubectl config use-context my-cluster-name
​
# 添加新的集群配置到 kubeconf 中,使用 basic auth 进行鉴权
$ kubectl config set-credentials kubeuser/foo.kubernetes.com --username=kubeuser --password=kubepassword
​
# 使用特定的用户名和命名空间设置上下文。
$ kubectl config set-context gce --user=cluster-admin --namespace=foo \
  && kubectl config use-context gce
# 打印客户端和服务端版本信息
$ kubectl version

高级命令:apply,patch,replace,convert

 
# 将pod.json中的配置应用到pod
$ kubectl apply -f ./pod.json
​
# 将控制台输入的JSON配置应用到Pod
$ cat pod.json | kubectl apply -f -
  •  
  •  
  •  
  •  
  •  
  •  
  •  
 
# Partially update a node using strategic merge patch
$ kubectl patch node k8s-node-1 -p '{"spec":{"unschedulable":true}}'
​
# Update a container's image; spec.containers[*].name is required because it's a merge key
$ kubectl patch pod valid-pod -p '{"spec":{"containers":[{"name":"kubernetes-serve-hostname","image":"new image"}]}}'
 
# Replace a pod using the data in pod.json.
$ kubectl replace -f ./pod.json
​
# Replace a pod based on the JSON passed into stdin.
$ cat pod.json | kubectl replace -f -
​
# Update a single-container pod's image version (tag) to v4
$ kubectl get pod mypod -o yaml | sed 's/\(image: myimage\):.*$/\1:v4/' | kubectl replace -f -
​
# Force replace, delete and then re-create the resource
$ kubectl replace --force -f ./pod.json
 
# Convert 'pod.yaml' to latest version and print to stdout.
$ kubectl convert -f pod.yaml
​
# Convert the live state of the resource specified by 'pod.yaml' to the latest version
# and print to stdout in json format.
$ kubectl convert -f pod.yaml --local -o json
​
# Convert all files under current directory to latest version and create them all.
$ kubectl convert -f . | kubectl create -f -

格式化输出

Kubectl 日志输出详细程度和调试

  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值