100个常用的kubectl命令

Kubectl是Kubernetes自带的客户端工具,用于直接操作Kubernetes集群。以下是100个常用的kubectl命令及其简要说明:

一、基础命令

  1. kubectl help:查看帮助信息。
  2. kubectl version:查看客户端和服务器端的版本信息。
  3. kubectl cluster-info:查看集群状态信息。
  4. kubectl api-resources:查看资源对象类型。
  5. kubectl api-versions:查看支持的API版本。

二、资源管理命令

获取资源信息
  1. kubectl get pods:列出所有的Pods。
  2. kubectl get pods -o wide:显示Pods的详细信息,包括IP地址和所在节点。
  3. kubectl get nodes:列出所有的节点。
  4. kubectl get nodes -o wide:显示节点的详细信息。
  5. kubectl get deployments:列出所有的Deployments。
  6. kubectl get services:列出所有的Services。
  7. kubectl get namespaces:列出所有的命名空间。
  8. kubectl get configmaps:列出所有的ConfigMaps。
  9. kubectl get secrets:列出所有的Secrets。
  10. kubectl get resourcequotas:列出所有的Resource Quotas。
  11. kubectl get limitranges:列出所有的Limit Ranges。
  12. kubectl get pods --all-namespaces:列出所有命名空间的Pods。
  13. kubectl get pods -l app=example:根据标签选择器获取Pods。
  14. kubectl get pods --show-labels:显示Pods的标签信息。
创建资源
  1. kubectl create namespace mynamespace:创建命名空间。
  2. kubectl create deployment mydeployment --image=nginx:创建Deployment。
  3. kubectl create service clusterip myservice --tcp=80:80:创建ClusterIP类型的Service。
  4. kubectl create configmap myconfigmap --from-file=config.conf:创建ConfigMap。
  5. kubectl create secret generic mysecret --from-literal=username=admin:创建Secret。
  6. kubectl run mypod --image=nginx --replicas=3:创建并运行Pod(已弃用,建议使用kubectl create deployment)。
删除资源
  1. kubectl delete pod mypod:删除Pod。
  2. kubectl delete deployment mydeployment:删除Deployment。
  3. kubectl delete service myservice:删除Service。
  4. kubectl delete namespace mynamespace:删除命名空间。
  5. kubectl delete configmap myconfigmap:删除ConfigMap。
  6. kubectl delete secret mysecret:删除Secret。
编辑资源
  1. kubectl edit pod mypod:编辑Pod。
  2. kubectl edit deployment mydeployment:编辑Deployment。
  3. kubectl edit service myservice:编辑Service。

三、资源操作命令

缩放资源
  1. kubectl scale deployment mydeployment --replicas=5:缩放Deployment的副本数。
滚动更新
  1. kubectl rollout status deployment mydeployment:查看Deployment的滚动更新状态。
  2. kubectl rollout undo deployment mydeployment:回滚Deployment的更新。
日志和调试
  1. kubectl logs mypod:查看Pod的日志。
  2. kubectl logs mypod -f:实时查看Pod的日志。
  3. kubectl exec -it mypod -- /bin/bash:进入Pod的容器内部。
  4. kubectl describe pod mypod:描述Pod的详细信息。
  5. kubectl debug pod mypod:调试Pod(需要安装debug插件)。

四、高级命令

自定义资源
  1. kubectl apply -f myresource.yaml:应用自定义资源定义。
  2. kubectl delete -f myresource.yaml:删除自定义资源。
标签和注解
  1. kubectl label pods mypod app=example:给Pod打标签。
  2. kubectl label nodes mynode role=master:给节点打标签。
  3. kubectl annotate pods mypod description='This is a test pod':给Pod打注解。
网络和存储
  1. kubectl expose pod mypod --port=80 --type=LoadBalancer:暴露Pod为Service,并创建LoadBalancer。
  2. kubectl port-forward mypod 8080:80:端口转发到Pod。
  3. kubectl attach mypod:附加到Pod的标准输入、输出和错误流。
集群管理
  1. kubectl cordon mynode:标记节点为不可调度。
  2. kubectl uncordon mynode:取消标记节点为不可调度。
  3. kubectl drain mynode:安全地驱逐节点上的Pods。
  4. kubectl taint nodes mynode key=value:NoSchedule:给节点打污点。
  5. kubectl untaint nodes mynode key=value:移除节点的污点。
插件和扩展
  1. kubectl plugin list:列出已安装的插件。
  2. kubectl plugin install <plugin-url>:安装插件。
  3. kubectl plugin uninstall <plugin-name>:卸载插件。

五、其他常用命令

  1. kubectl convert -f myresource.yaml:转换资源文件格式。
  2. kubectl top pods:查看Pods的资源使用情况(需要安装metrics-server)。
  3. kubectl top nodes:查看节点的资源使用情况(需要安装metrics-server)。
  4. kubectl auth can-i get pods:检查当前用户是否有权限获取Pods。
  5. kubectl auth reconcile:修复集群中的授权问题。
  6. kubectl cp /path/to/local/file mypod:/path/to/remote/file:复制文件到Pod中。
  7. kubectl cp mypod:/path/to/remote/file /path/to/local/file:从Pod中复制文件到本地。

六、特定资源类型的命令

DaemonSet
  1. kubectl create -f daemonset.yaml:根据YAML文件创建DaemonSet。
  2. kubectl get daemonsets:列出所有的DaemonSet。
  3. kubectl delete daemonset mydaemonset:删除DaemonSet。
Job和CronJob
  1. kubectl create job myjob --image=busybox --restart=Never -- command -- /bin/sh -c "echo Hello, Kubernetes!":创建Job。
  2. kubectl get jobs:列出所有的Job。
  3. kubectl delete job myjob:删除Job。
  4. kubectl create -f cronjob.yaml:根据YAML文件创建CronJob。
  5. kubectl get cronjobs:列出所有的CronJob。
  6. kubectl delete cronjob mycronjob:删除CronJob。
Ingress
  1. kubectl create -f ingress.yaml:根据YAML文件创建Ingress。
  2. kubectl get ingresses:列出所有的Ingress。
  3. kubectl delete ingress myingress:删除Ingress。
PersistentVolume和PersistentVolumeClaim
  1. kubectl create -f pv.yaml:根据YAML文件创建PersistentVolume。
  2. kubectl get persistentvolumes:列出所有的PersistentVolume。
  3. kubectl delete persistentvolume mypv:删除PersistentVolume。
  4. kubectl create -f pvc.yaml:根据YAML文件创建PersistentVolumeClaim。
  5. kubectl get persistentvolumeclaims:列出所有的PersistentVolumeClaim。
  6. kubectl delete persistentvolumeclaim mypvc:删除PersistentVolumeClaim。
StatefulSet
  1. kubectl create -f statefulset.yaml:根据YAML文件创建StatefulSet。
  2. kubectl get statefulsets:列出所有的StatefulSet。
  3. kubectl delete statefulset mystatefulset:删除StatefulSet。

七、实验和测试命令

  1. kubectl run testpod --image=nginx --rm --restart=Never:运行一个临时的Pod进行测试(运行后自动删除)。
  2. kubectl apply -f test-deployment.yaml --dry-run=client:在客户端进行干运行测试,不实际创建资源。
  3. kubectl diff -f test-deployment.yaml:比较当前资源状态和YAML文件中的定义(需要安装kubectl-diff插件)。

八、集群诊断和故障排除

  1. kubectl logs --all-containers=true mypod:查看Pod中所有容器的日志。
  2. kubectl describe node mynode:描述节点的详细信息。
  3. kubectl events:查看集群中的事件。
  4. kubectl debug node mynode:调试节点(需要安装debug插件)。

九、配置和管理kubectl

  1. kubectl config view:查看当前的kubectl配置。
  2. **kubectl

猜你喜欢

转载自blog.csdn.net/mopmgerg54mo/article/details/143301615