K8S/Kubernetes 命令

举报
张谱继 发表于 2023/01/02 10:32:34 2023/01/02
【摘要】 命令作用kubectl rollout undo deployment/abcRollback to the previous deploymentkubectl rollout status daemonset/fooCheck the rollout status of a daemonsetkubectl rollout restart deployment/abcRestart a ...

命令

作用

kubectl rollout undo deployment/abc

Rollback to the previous deployment

kubectl rollout status daemonset/foo

Check the rollout status of a daemonset

kubectl rollout restart deployment/abc

Restart a deployment

kubectl rollout history deployment/abc

View the rollout history of a deployment

kubectl rollout history daemonset/abc --revision=3

View the details of daemonset revision 3  查看daemonset修订版3的详细信息

kubectl rollout undo daemonset/abc --to-revision=3

Roll back to daemonset revision 3

kubectl logs --since=1h nginx

Show all logs from pod nginx written in the last hour  h,m,s

kubectl logs --tail=20 nginx

Display only the most recent 20 lines of output in pod nginx

kubectl exec mypod -- date

Get output from running the 'date' command from pod mypod, using the first container by default

kubectl explain pods.spec.containers

Get the documentation of a specific field of a resource

kubectl top pod POD_NAME --containers

kubectl top pod POD_NAME --containers

kubectl get pods -o wide

List all pods in ps output format with more information (such as node name)

kubectl get -o json pod web-pod-13je7

List a single pod in JSON output format

List resource information in custom columns

kubectl get pod test-pod -o custom-columns=CONTAINER:.spec.containers[0].name,IMAGE:.spec.containers[0].image

kubectl label pods my-pod new-label=awesome  

Add a Label

kubectl scale --replicas=3 rs/foo

Scale a replicaset named 'foo' to 3

kubectl cp my-namespace/my-pod:/tmp/foo /tmp/bar

Copy /tmp/foo from a remote pod to /tmp/bar locally

Delete all pods matching the awk pattern1 or pattern2

kubectl get pods  -n mynamespace --no-headers=true | awk '/pattern1|pattern2/{print $1}' | xargs  kubectl delete -n mynamespace pod

kubectl get quota

查看资源情况

kubectl describe pod xxx

查看状态

kubectl get events

https://moelove.info/2021/12/28/%E5%BD%BB%E5%BA%95%E6%90%9E%E6%87%82-Kubernetes-%E4%B8%AD%E7%9A%84-Events/


管理多個 Kubernetes Cluster:建立、切換、合併 context   来自 <https://www.akiicat.com/2019/04/24/Kubernetes/setup-kubernetes-configuration/>

kubectl config --help

查看kubectl config 命令使用说明

kubectl config view

查看kubeconfig,上下文内容

kubectl config get-contexts

查看有哪些上下文

kubectl config use-context <context-name>

切换上下文,如果权限不足,采用每次kubectl命令指定上下文,kubectl --context=<context-name>  替代

kubectl api-resources

来查看所有api资源,比如pod,service,rs,rc,deploy,resource https://www.cnblogs.com/tylerzhou/p/11043285.html

kubectl get pod -A

加入-A参数,表示查看所有命名空间上的Pod

kubectl get pod podname  -o custom-columns='pod:.metadata.name,container:.spec.containers[*].name'

查看pod中的所有容器   container


获取容器规格限制

kubectl -n xxx get pods -l app=xxx -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{range .spec.containers[*]}{.resources.limits.cpu}{"\t"}{.resources.limits.memory}{end}{"\n"}{end}'

kubectl -n namespace get pods -l app=xxx -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{range .spec.containers[*]}{.resources.limits.cpu}{"\t"}{.resources.limits.memory}{end}{"\n"}{end}'

 

 

容器规格+启动时间

Examples using -o=custom-columns:

kubectl -n namespace get pods -o custom-columns='name:.metadata.name,cpu:.spec.containers[*].resources.limits.cpu,memory:.spec.containers[*].resources.limits.memory,startedAt:.status.containerStatuses[*].state.running.startedAt,image:.status.containerStatuses[*].image'


特殊场景处理样例

 kubectl get node -o custom-columns=node:.metadata.name,zone:.metadata.labels."topology\.kubernetes\.io/zone",region:.metadata.labels."topology\.kubernetes\.io/region",nodepool:.metadata.labels."cce\.cloud\.com/cce-nodepool"

对于特殊字符的处理,\ 转义,并用双引号处理


【版权声明】本文为华为云社区用户原创内容,未经允许不得转载,如需转载请自行联系原作者进行授权。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。