K8S/Kubernetes 命令
命令 |
作用 |
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 |
|
|
管理多個 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"
对于特殊字符的处理,\ 转义,并用双引号处理
- 点赞
- 收藏
- 关注作者
评论(0)