Kubernetes 运维核心命令速查手册

内容分享2个月前发布
0 0 0

Kubernetes 运维核心命令速查手册


1. 集群管理

功能

命令

查看集群版本

kubectl version –short

查看集群信息

kubectl cluster-info

查看 API 资源列表

kubectl api-resources

查看当前上下文

kubectl config current-context

查看所有上下文

kubectl config get-contexts

切换上下文

kubectl config use-context <context-name>


2. 节点管理

功能

命令

查看节点列表

kubectl get nodes -o wide

查看节点详细信息

kubectl describe node <node-name>

节点隔离(不可调度)

kubectl cordon <node-name>

节点恢复调度

kubectl uncordon <node-name>

驱逐节点上的 Pod

kubectl drain <node-name> –ignore-daemonsets –delete-emptydir-data

查看节点资源使用情况

kubectl top node

查看节点分配资源

`kubectl describe node


3. 命名空间管理

功能

命令

查看命名空间

kubectl get ns

创建命名空间

kubectl create ns <namespace>

删除命名空间

kubectl delete ns <namespace>

设置默认命名空间

kubectl config set-context –current –namespace=<ns>


4. Pod 管理

功能

命令

查看所有 Pod

kubectl get pods -A -o wide

查看指定命名空间 Pod

kubectl get pods -n <namespace>

查看 Pod 详细信息

kubectl describe pod <pod-name> -n <namespace>

查看 Pod 日志

kubectl logs <pod-name> -n <namespace>

实时查看日志

kubectl logs -f <pod-name> -n <namespace>

查看 Pod 之前的日志

kubectl logs <pod-name> –previous -n <namespace>

在 Pod 内执行命令

kubectl exec -it <pod-name> -n <namespace> — /bin/sh

删除 Pod

kubectl delete pod <pod-name> -n <namespace>

强制删除 Pod

kubectl delete pod <pod-name> -n <namespace> –grace-period=0 –force

查看 Pod 状态字段

kubectl get pod <pod> -o jsonpath='{.status.containerStatuses[*].state}'

实时监控 Pod

kubectl get pods -n <ns> -w


5. Deployment 管理

功能

命令

查看 Deployment

kubectl get deploy -n <namespace>

查看详细信息

kubectl describe deploy <deploy-name> -n <namespace>

创建 Deployment

kubectl create deploy <name> –image=<image> -n <namespace>

更新镜像

kubectl set image deploy/<deploy-name> <container-name>=<image> -n <namespace>

扩缩容 Deployment

kubectl scale deploy <deploy-name> –replicas=3 -n <namespace>

滚动更新

kubectl rollout restart deploy <deploy-name> -n <namespace>

查看更新历史

kubectl rollout history deploy <deploy-name> -n <namespace>

回滚 Deployment

kubectl rollout undo deploy <deploy-name> -n <namespace>

导出 Deployment YAML

kubectl get deploy <name> -o yaml > deploy.yaml


6. StatefulSet 管理

功能

命令

查看 StatefulSet

kubectl get sts -n <namespace>

扩缩容 StatefulSet

kubectl scale sts <sts-name> –replicas=3 -n <namespace>

删除并保留 PVC

kubectl delete sts <sts-name> –cascade=orphan -n <namespace>


7. Service 与 Ingress 管理

功能

命令

查看 Service

kubectl get svc -n <namespace>

查看 Service 详细信息

kubectl describe svc <svc-name> -n <namespace>

创建 ClusterIP Service

kubectl expose deploy <deploy-name> –port=80 –target-port=8080 -n <namespace>

创建 NodePort Service

kubectl expose deploy <deploy-name> –port=80 –type=NodePort -n <namespace>

查看 Endpoints

kubectl get endpoints -n <namespace>

查看 Ingress

kubectl get ingress -n <namespace>

查看 Ingress 详细信息

kubectl describe ingress <ingress-name> -n <namespace>


8. 配置与存储管理

功能

命令

查看 ConfigMap

kubectl get configmap -n <namespace>

查看 Secret

kubectl get secret -n <namespace>

解码 Secret

kubectl get secret <secret-name> -o jsonpath='{.data.<key>}' | base64 -d

查看 PVC

kubectl get pvc -n <namespace>

查看 PV

kubectl get pv

查看 StorageClass

kubectl get sc


9. 调试与排查

功能

命令

查看资源使用情况

kubectl top pod -n <namespace>

查看 Pod 内网络信息

kubectl exec -it <pod-name> — netstat -tnlp

查看事件

kubectl get events -n <namespace> –sort-by=.lastTimestamp

查看所有命名空间事件

kubectl get events -A -w –sort-by='.lastTimestamp'

查看 API 资源详情

kubectl explain pod.spec.containers


10. 实用组合命令

功能

命令

查看所有命名空间 Pod 状态

kubectl get pods -A -o custom-columns=”NAMESPACE:.metadata.namespace,NAME:.metadata.name,STATUS:.status.phase”

删除指定标签的 Pod

kubectl delete pods -l app=myapp -n <namespace>

YAML 干跑生成

kubectl run nginx –image=nginx –dry-run=client -o yaml > pod.yaml

快速别名

alias kgp='kubectl get pods',alias kdp='kubectl describe pod',alias klf='kubectl logs -f'


© 版权声明

相关文章

暂无评论

none
暂无评论...