K8S常用的 KubernetesK8S指令,我的世界常用指令
墨初 知识笔记 44阅读
常用的 KubernetesK8s指令
创建一个资源kubectl create <资源类型> <资源名称>
例如kubectl create deployment my-deployment

获取资源列表kubectl get <资源类型>
例如kubectl get pods
查看资源详细信息kubectl describe <资源类型> <资源名称>
例如kubectl describe pod my-pod

删除一个资源kubectl delete <资源类型> <资源名称>
例如kubectl delete deployment my-deployment
执行一个命令在容器内部kubectl exec <pod名称> <命令>
例如kubectl exec my-pod ls
在控制台中查看一个Pod的日志kubectl logs <pod名称>
例如kubectl logs my-pod
扩展一个Deployment的副本数kubectl scale deployment <deployment名称> --replicas<副本数>
例如kubectl scale deployment my-deployment --replicas3
滚动更新一个Deploymentkubectl set image deployment/<deployment名称> <容器名称><新镜像名称>
例如kubectl set image deployment/my-deployment my-containermy-new-image:latest
暂停一个Deployment的滚动更新kubectl rollout pause deployment/<deployment名称>
例如kubectl rollout pause deployment/my-deployment
继续一个Deployment的滚动更新kubectl rollout resume deployment/<deployment名称>
例如kubectl rollout resume deployment/my-deployment
回滚一个Deployment到上一个版本kubectl rollout undo deployment/<deployment名称>
例如kubectl rollout undo deployment/my-deployment
创建一个Servicekubectl expose <资源类型> <资源名称> --port<端口号> --target-port<目标端口号>
例如kubectl expose deployment my-deployment --port80 --target-port8080
获取集群中的节点列表kubectl get nodes
获取Pod的详细信息包括IP地址、节点等kubectl get pods -o wide
查看集群事件kubectl get events
查看集群中的命名空间kubectl get namespaces
在特定的命名空间中执行指令kubectl -n <命名空间> <指令>
例如kubectl -n my-namespace get pods
更新一个资源的配置kubectl apply -f <配置文件>
例如kubectl apply -f deployment.yaml
检查资源的健康状态kubectl describe <资源类型> <资源名称> | grep Conditions
例如kubectl describe pod my-pod | grep Conditions
查看当前集群的上下文kubectl config current-context
查看特定资源的日志kubectl logs <资源类型>/<资源名称>
例如kubectl logs pod/my-pod
查看特定资源的配置kubectl get <资源类型> <资源名称> -o yaml
例如kubectl get pod my-pod -o yaml
查看特定资源的标签kubectl get <资源类型> --show-labels
例如kubectl get pods --show-labels
根据标签选择资源kubectl get <资源类型> -l <标签选择器>
例如kubectl get pods -l appmy-app
根据标签删除资源kubectl delete <资源类型> -l <标签选择器>
例如kubectl delete pods -l appmy-app
查看特定命名空间中的所有资源kubectl get all -n <命名空间>
例如kubectl get all -n my-namespace
创建一个命名空间kubectl create namespace <命名空间名称>
例如kubectl create namespace my-namespace
删除一个命名空间及其所有资源kubectl delete namespace <命名空间名称>
例如kubectl delete namespace my-namespace
暴露一个Deployment为外部服务kubectl expose deployment <deployment名称> --type<服务类型> --port<端口号>
例如kubectl expose deployment my-deployment --typeLoadBalancer --port80
在Pod中执行交互式终端kubectl exec -it <pod名称> – <命令>
例如kubectl exec -it my-pod – /bin/bash
查看节点的详细信息kubectl describe node <节点名称>
例如kubectl describe node my-node
查看特定节点上运行的Pod列表kubectl describe node <节点名称> | grep Pods
例如kubectl describe node my-node | grep Pods
强制删除一个资源kubectl delete <资源类型> <资源名称> --">-period0 --force
例如kubectl delete pod my-pod --">-period0 --force
创建一个持久卷Persistent Volumekubectl apply -f <持久卷配置文件>
例如kubectl apply -f persistent-volume.yaml
创建一个持久卷声明Persistent Volume Claimkubectl apply -f <持久卷声明配置文件>
例如kubectl apply -f persistent-volume-claim.yaml
查看持久卷列表kubectl get pv
查看持久卷声明列表kubectl get pvc
查看特定持久卷的详细信息kubectl describe pv <持久卷名称>
例如kubectl describe pv my-pv
查看特定持久卷声明的详细信息kubectl describe pvc <持久卷声明名称>
例如kubectl describe pvc my-pvc
创建一个配置映射ConfigMapkubectl create configmap <配置映射名称> --from-file<文件路径>
例如kubectl create configmap my-config --from-fileconfig.txt
查看配置映射列表kubectl get configmaps
查看特定配置映射的详细信息kubectl describe configmap <配置映射名称>
例如kubectl describe configmap my-config
创建一个密钥Secretkubectl create secret generic <密钥名称> --from-literal<键><值>
例如kubectl create secret generic my-secret --from-literalusernameadmin --from-literalpasswordpass123
查看密钥列表kubectl get secrets
查看特定密钥的详细信息kubectl describe secret <密钥名称>
例如kubectl describe secret my-secret
创建一个水平自动伸缩器Horizontal Pod Autoscalerkubectl autoscale <资源类型> <资源名称> --min<最小副本数> --max<最大副本数> --cpu-percent<CPU百分比>
例如kubectl autoscale deployment my-deployment --min2 --max5 --cpu-percent80
查看水平自动伸缩器列表kubectl get hpa
查看特定水平自动伸缩器的详细信息kubectl describe hpa <水平自动伸缩器名称>
例如kubectl describe hpa my-hpa
创建一个Jobkubectl create job <Job名称> --image<镜像名称>
例如kubectl create job my-job --imagemy-image:latest
查看Job列表kubectl get jobs