基本命令
kubectl version
:查看客户端和服务端的版本信息。kubectl cluster-info
:显示集群信息。kubectl get nodes
:列出集群中的所有节点。kubectl get pods
:列出所有命名空间中的所有Pod。kubectl get services
:列出所有命名空间中的所有服务。kubectl get namespaces
:列出所有命名空间。
创建资源
kubectl create -f <filename>
:根据配置文件创建资源。kubectl run <name> --image=<image>
:创建一个新的Pod。kubectl expose <resource> --port=<port> --target-port=<target-port>
:为资源创建服务。
查看和获取资源
kubectl get <resource>
:获取资源列表,例如kubectl get pods
。kubectl describe <resource> <name>
:获取资源的详细信息,例如kubectl describe pod mypod
。kubectl logs <pod>
:查看Pod的日志。kubectl get events
:查看集群事件。
更新和修改资源
kubectl apply -f <filename>
:通过配置文件更新资源。kubectl edit <resource> <name>
:在编辑器中编辑资源。kubectl set image <resource>/<name> <container>=<new_image>
:更新Pod的镜像,例如kubectl set image deployment/myapp mycontainer=myimage:latest
。
删除资源
kubectl delete <resource> <name>
:删除资源,例如kubectl delete pod mypod
。kubectl delete -f <filename>
:根据配置文件删除资源。
调试和故障排除
kubectl exec <pod> -- <command>
:在Pod中执行命令,例如kubectl exec mypod -- ls /
。kubectl port-forward <pod> <local_port>:<pod_port>
:将本地端口转发到Pod端口。kubectl attach <pod> -c <container>
:连接到正在运行的容器。
配置和管理
kubectl config view
:查看kubeconfig配置。kubectl config set-context <context>
:设置当前上下文。kubectl config use-context <context>
:切换上下文。
其他
kubectl top nodes
:显示节点的资源使用情况。kubectl top pods
:显示Pod的资源使用情况。
副本控制器(ReplicationController)命令
kubectl get rc
:列出所有命名空间中的所有副本控制器。kubectl get rc -n <namespace>
:列出指定命名空间中的所有副本控制器。kubectl describe rc <name>
:获取指定副本控制器的详细信息。kubectl scale rc <name> --replicas=<number>
:调整副本控制器的副本数量。kubectl delete rc <name>
:删除指定的副本控制器。
创建和删除命名空间
kubectl create namespace <name>
:创建一个新的命名空间。kubectl delete namespace <name>
:删除指定的命名空间。
其他命名空间管理命令
kubectl get namespaces
:列出所有命名空间。kubectl describe namespace <name>
:获取指定命名空间的详细信息。kubectl config set-context --current --namespace=<namespace>
:设置当前上下文的命名空间。
Pod 和容器管理命令
kubectl get pods -n <namespace>
:列出指定命名空间中的所有Pod。kubectl describe pod <name> -n <namespace>
:获取指定Pod的详细信息。kubectl logs <pod> -n <namespace>
:查看指定Pod的日志。kubectl exec <pod> -n <namespace> -- <command>
:在指定Pod中执行命令。kubectl cp <file-path> <pod>:<pod-file-path>
:将文件复制到Pod中。
服务(Service)管理命令
kubectl get services -n <namespace>
:列出指定命名空间中的所有服务。kubectl describe service <name> -n <namespace>
:获取指定服务的详细信息。kubectl expose deployment <name> --type=<type> --port=<port> -n <namespace>
:暴露一个Deployment作为服务。
部署(Deployment)管理命令
kubectl get deployments -n <namespace>
:列出指定命名空间中的所有部署。kubectl describe deployment <name> -n <namespace>
:获取指定部署的详细信息。kubectl scale deployment <name> --replicas=<number> -n <namespace>
:调整部署的副本数量。kubectl rollout status deployment/<name> -n <namespace>
:查看部署的滚动更新状态。kubectl rollout undo deployment/<name> -n <namespace>
:回滚部署到上一个版本。
状态和调试
kubectl get events -n <namespace>
:查看指定命名空间中的事件。kubectl top nodes
:显示节点的资源使用情况。kubectl top pods -n <namespace>
:显示指定命名空间中Pod的资源使用情况。kubectl describe node <node-name>
:获取节点的详细信息。
配置和上下文管理
kubectl config view
:查看当前的kubeconfig配置。kubectl config use-context <context>
:切换到指定的上下文。kubectl config current-context
:显示当前的上下文。kubectl config set-context <context> --namespace=<namespace>
:为上下文设置默认命名空间。
资源定义管理
kubectl apply -f <filename>
:通过配置文件创建或更新资源。kubectl delete -f <filename>
:通过配置文件删除资源。kubectl replace -f <filename>
:替换配置文件中的资源。