Kubectl是Kubernetes自带的客户端工具,用于直接操作Kubernetes集群。以下是100个常用的kubectl命令及其简要说明:
一、基础命令
- kubectl help:查看帮助信息。
- kubectl version:查看客户端和服务器端的版本信息。
- kubectl cluster-info:查看集群状态信息。
- kubectl api-resources:查看资源对象类型。
- kubectl api-versions:查看支持的API版本。
二、资源管理命令
获取资源信息
- kubectl get pods:列出所有的Pods。
- kubectl get pods -o wide:显示Pods的详细信息,包括IP地址和所在节点。
- kubectl get nodes:列出所有的节点。
- kubectl get nodes -o wide:显示节点的详细信息。
- kubectl get deployments:列出所有的Deployments。
- kubectl get services:列出所有的Services。
- kubectl get namespaces:列出所有的命名空间。
- kubectl get configmaps:列出所有的ConfigMaps。
- kubectl get secrets:列出所有的Secrets。
- kubectl get resourcequotas:列出所有的Resource Quotas。
- kubectl get limitranges:列出所有的Limit Ranges。
- kubectl get pods --all-namespaces:列出所有命名空间的Pods。
- kubectl get pods -l app=example:根据标签选择器获取Pods。
- kubectl get pods --show-labels:显示Pods的标签信息。
创建资源
- kubectl create namespace mynamespace:创建命名空间。
- kubectl create deployment mydeployment --image=nginx:创建Deployment。
- kubectl create service clusterip myservice --tcp=80:80:创建ClusterIP类型的Service。
- kubectl create configmap myconfigmap --from-file=config.conf:创建ConfigMap。
- kubectl create secret generic mysecret --from-literal=username=admin:创建Secret。
- kubectl run mypod --image=nginx --replicas=3:创建并运行Pod(已弃用,建议使用kubectl create deployment)。
删除资源
- kubectl delete pod mypod:删除Pod。
- kubectl delete deployment mydeployment:删除Deployment。
- kubectl delete service myservice:删除Service。
- kubectl delete namespace mynamespace:删除命名空间。
- kubectl delete configmap myconfigmap:删除ConfigMap。
- kubectl delete secret mysecret:删除Secret。
编辑资源
- kubectl edit pod mypod:编辑Pod。
- kubectl edit deployment mydeployment:编辑Deployment。
- kubectl edit service myservice:编辑Service。
三、资源操作命令
缩放资源
- kubectl scale deployment mydeployment --replicas=5:缩放Deployment的副本数。
滚动更新
- kubectl rollout status deployment mydeployment:查看Deployment的滚动更新状态。
- kubectl rollout undo deployment mydeployment:回滚Deployment的更新。
日志和调试
- kubectl logs mypod:查看Pod的日志。
- kubectl logs mypod -f:实时查看Pod的日志。
- kubectl exec -it mypod -- /bin/bash:进入Pod的容器内部。
- kubectl describe pod mypod:描述Pod的详细信息。
- kubectl debug pod mypod:调试Pod(需要安装debug插件)。
四、高级命令
自定义资源
- kubectl apply -f myresource.yaml:应用自定义资源定义。
- kubectl delete -f myresource.yaml:删除自定义资源。
标签和注解
- kubectl label pods mypod app=example:给Pod打标签。
- kubectl label nodes mynode role=master:给节点打标签。
- kubectl annotate pods mypod description='This is a test pod':给Pod打注解。
网络和存储
- kubectl expose pod mypod --port=80 --type=LoadBalancer:暴露Pod为Service,并创建LoadBalancer。
- kubectl port-forward mypod 8080:80:端口转发到Pod。
- kubectl attach mypod:附加到Pod的标准输入、输出和错误流。
集群管理
- kubectl cordon mynode:标记节点为不可调度。
- kubectl uncordon mynode:取消标记节点为不可调度。
- kubectl drain mynode:安全地驱逐节点上的Pods。
- kubectl taint nodes mynode key=value:NoSchedule:给节点打污点。
- kubectl untaint nodes mynode key=value:移除节点的污点。
插件和扩展
- kubectl plugin list:列出已安装的插件。
- kubectl plugin install <plugin-url>:安装插件。
- kubectl plugin uninstall <plugin-name>:卸载插件。
五、其他常用命令
- kubectl convert -f myresource.yaml:转换资源文件格式。
- kubectl top pods:查看Pods的资源使用情况(需要安装metrics-server)。
- kubectl top nodes:查看节点的资源使用情况(需要安装metrics-server)。
- kubectl auth can-i get pods:检查当前用户是否有权限获取Pods。
- kubectl auth reconcile:修复集群中的授权问题。
- kubectl cp /path/to/local/file mypod:/path/to/remote/file:复制文件到Pod中。
- kubectl cp mypod:/path/to/remote/file /path/to/local/file:从Pod中复制文件到本地。
六、特定资源类型的命令
DaemonSet
- kubectl create -f daemonset.yaml:根据YAML文件创建DaemonSet。
- kubectl get daemonsets:列出所有的DaemonSet。
- kubectl delete daemonset mydaemonset:删除DaemonSet。
Job和CronJob
- kubectl create job myjob --image=busybox --restart=Never -- command -- /bin/sh -c "echo Hello, Kubernetes!":创建Job。
- kubectl get jobs:列出所有的Job。
- kubectl delete job myjob:删除Job。
- kubectl create -f cronjob.yaml:根据YAML文件创建CronJob。
- kubectl get cronjobs:列出所有的CronJob。
- kubectl delete cronjob mycronjob:删除CronJob。
Ingress
- kubectl create -f ingress.yaml:根据YAML文件创建Ingress。
- kubectl get ingresses:列出所有的Ingress。
- kubectl delete ingress myingress:删除Ingress。
PersistentVolume和PersistentVolumeClaim
- kubectl create -f pv.yaml:根据YAML文件创建PersistentVolume。
- kubectl get persistentvolumes:列出所有的PersistentVolume。
- kubectl delete persistentvolume mypv:删除PersistentVolume。
- kubectl create -f pvc.yaml:根据YAML文件创建PersistentVolumeClaim。
- kubectl get persistentvolumeclaims:列出所有的PersistentVolumeClaim。
- kubectl delete persistentvolumeclaim mypvc:删除PersistentVolumeClaim。
StatefulSet
- kubectl create -f statefulset.yaml:根据YAML文件创建StatefulSet。
- kubectl get statefulsets:列出所有的StatefulSet。
- kubectl delete statefulset mystatefulset:删除StatefulSet。
七、实验和测试命令
- kubectl run testpod --image=nginx --rm --restart=Never:运行一个临时的Pod进行测试(运行后自动删除)。
- kubectl apply -f test-deployment.yaml --dry-run=client:在客户端进行干运行测试,不实际创建资源。
- kubectl diff -f test-deployment.yaml:比较当前资源状态和YAML文件中的定义(需要安装kubectl-diff插件)。
八、集群诊断和故障排除
- kubectl logs --all-containers=true mypod:查看Pod中所有容器的日志。
- kubectl describe node mynode:描述节点的详细信息。
- kubectl events:查看集群中的事件。
- kubectl debug node mynode:调试节点(需要安装debug插件)。
九、配置和管理kubectl
- kubectl config view:查看当前的kubectl配置。
- **kubectl