一、基本操作
- kubectl version - 显示kubectl的版本信息
- kubectl cluster-info - 显示集群的基本信息
- kubectl get nodes - 显示所有节点信息
- kubectl get pods - 显示所有的pods信息
- kubectl create deployment {name} --image={image} - 创建pods
- Kubectl scale deployment {name} --replicas={num} - 修改pods的数量
- Kubectl describe pod {name} - 显示指定的pod的详细信息
- Kubectl logs {name} - 显示指定pod的日志信息
- Kubectl exec -it {name} -- /bin/bash - 进入指定pod的bash终端
- Kubectl delete deployment {name} - 删除pods
- Kubectl edit svc {name} - 编辑svc
二、调试和故障排查
- Kubectl describe node {name} -显示指定节点的详细信息
- Kubectl describe deployment {name} - 显示指定deployment的详细信息
- Kubectl describe service {name} - 显示指定service的详细信息
- Kubectl get events -显示所有事件信息
- Kubectl get events --sort-by=.metadata.creationTimestamp - 显示所有事件信息,并按照创建时间排序
- Kubectl get pods --all-namespaces - 显示所有namespace下的pods信息
- Kubectl get deployments --all-namespaces -显示所有namespace下的deployment信息
- Kubectl get services --all-namespaces - 显示所有namespace下的service信息
- Kubectl get nodes -o yaml | grep -A5 -B2 SchedulingDisabled - 显示所有已禁用调度的节点信息
- Kubectl run -i --tty busybox --image=busybox --restart=Never -- sh - 运行一个busybox容器并进行调试
三、部署和管理
- Kubectl create deployment {name} --image={image} --replicas={num} --port={port} - 创建deployment
- Kubectl expose deployment {name} --port={port} --target-port={targetport} --type={type} - 创建service
- Kubectl set image deployment/{name} {container}={image}:{tag} - 更新deployment 中某个容器的镜像
- Kubectl rollout history deployment/{name} - 显示指定deployment的版本历史
- Kubectl rollout undo deployment/{name} --to-revision=1 - 回滚指定deployment到指定版本
- Kubectl rollout pause deployment/{name} - 暂停指定deployment的升级
- Kubectl rollout resume deploymeng/{name} - 恢复指定deployment的升级
- Kubectl rollout status deployment/{name} - 热重启deployment
- Kubectl exec -it {name} -- /bin/bash - 在指定pod中进入bash终端
- Kubectl apply -f {filename} - 根据指定的yaml文件部署kubernetes资源
四、安全和访问控制
- Kubectl create secret generic {name} --from-file={key}={path} - 创建secret对象
- Kubectl create serviceaccount {name} - 创建sercieaccount对象
- Kubectl create role {name} --resource={resource} --verb={verb} - 创建role对象
- Kubectl create rolebinding {name} --role={role} --serviceaccount={namespace}:{sa_name} - 创建rolebinding对象
- Kubectl get secret {name} -o=jsonpath='{.data.{key}}' | base64 --decode - 显示secret中指定key的值
- Kubectl delete secret {name} - 删除指定的secret对象
- Kubectl get serviceaccount {name} - 显示指定的serviceaccount对象信息
- Kubectl create clusterrole {name} --resource={resource} -verb={verb} - 创建clusterrole对象
- Kubectl create clusterrolebinding {name} --clusterrole={role} --serviceacconunt={namespace}:{sa_name} - 创建clusterrolebinding对象
- Kubectl auth can-i <VERB> <RESOURCE> [--namespace=<NAMESPACE>] - 检查当前用户在某个ns是否有执行指定资源操作的权限
- <VERB>: 表示要执行的操作,例如 create ,get ,update 等
- <RESOURCE>:表示资源类型,例如 pods ,deployments, services 等。
--namespace=<NAMESPACE>:(可选) 表示在指定的命名空间中搜索资源
五、网络和负载均衡
- Kubectl get svc {name} -o jsonpath='{.spec.clusterIP}' - 显示指定service 的cluster IP 地址
- Kubectl get endpoints {name} - 显示指定service的后端地址信息
- Kubectl get ingress {name} - 显示指定ingress的详细信息
- Kubectl create ingress {name} --rule={host}/{path}=svc:{service_name}:{port} - 创建ingress条目
- Kubectl delete ingress {name} - 删除指定的ingress条目
- Kubectl get svc {name} -o jsonpath='{.spec.type}' - 显示指定service的类型信息
- Kubectl get pods -o jsonpath='{range.items[*]}{.metadata.name}{"\t"}{.status.podIP}{"\n"}{end}' - 显示所有pods的名称和IP地址信息
- Kubectl exec -it {name} -- nc -z -v {ip} {port} - 在指定pod中使用nc工具测试TCP连接
- Kubectl exec -it {name} -- curl {url} - 在指定pod中使用curl 工具测试HTTP连接
- Kubectl get pods -l {label} - 显示所有带有指定label的pods的信息
六、容器高级操作
- Kubectl exec -it <pod_name> --<command> - 在运行中的 pod 内部启动一个新的容器,并在其中执行指定的命令
- Kubectl cp <pod_name>:<container_name> - 将pod内指定路径的文件或目录拷贝到主机上
- Kubectl logs <pod_name> <container_name> - 查看 pod 内指定容器的日志输出
- Kubectl port-forward <pod_name> <local_port>:<pod_port> - 将本地端口与 pod 内部的特定端口进行转发,从而可以通过本地网络访问 pod 中的服务
- Kubectl describe pod <pod_name> - 查看pod 的详细信息,例如 pod 的状态、资源限制和调度信息等。
- Kubectl top pod <pod_name> - 查看pod的 CPU 和内存使用情况
- Kubectl get pod <pod_name> -o yaml - 获取 pod 的 YAML 文件,可以用于备份或还原 pod
- Kubectl rollout restart deployment <deployment_name> - 强制重启 Deployments 中的所有 pod ,以便应用程序更新能够生效
- Kubectl attch <pod_name> -c <container_name> - 将当前终端附加到 pod 中指定的容器上,从而可以直接在该容器内操作
- Kubectl apply -f <resource_file.yaml> - 使用 YAML 或 JSON 文件部署 pod ,支持声明式配置管理
七、状态查询和修改
- Kubectl get deployment.apps {name} - 显示指定deployment的详细信息
- Kubectl get deployment.apps -A - 显示所有namespace下的deployment信息
- Kubectl get replicasets.apps {name} - 显示指定replicaset的详细信息
- Kubectl get replicasets.apps -A - 显示所有namespace 下的replicaset 信息
- Kubectl get services -A - 显示所有namespace下的service信息
- Kubectl get pods -o jsonpath='{range.items[*]}{.metadata.name}{"\n"}{end}' - 显示所有pods的名称信息
- Kubectl get pod {name} -o jsonpath='{range.status.conditions[?(@.type=="Ready")]}{.status}{"\n"}{end}' - 显示指定pod的Ready状态
- Kubectl get pod {name} -o jsonpath='{range.status.conditions[?(@.type==""Ready)]}{.message}{"\n"}{end}' - 显示指定pod的Ready状态信息
- Kubectl get pod {name} -o jsonpath='{range.status.conditions[?(@.type=="Ready")]}{.reason}{"\n"}{end}' -显示指定pod的Ready状态原因信息
- Kubectl delete pod {name} --force --grace-period=0 - 强制删除指定pod
八、状态修复和调整
- Kubectl get pods -o jsonpath='{range.items[?@.status.phase=="Pending"]}{.metadata.name}{"\t"}{.reason}{"\n"}{end}' - 显示所有处于Pending状态的pods
- Kubectl describe pod {name} - 显示指定pod的详细信息
- Kubectl describe node {name} - 显示指定节点的详细信息
- Kubectl describe deployment {name} - 显示指定deployment的详细信息
- Kubectl delete pod {name} --force --grace-period=0 - 强制删除指定pod
- Kubectl delete deployment {name} - 删除指定deployment
- Kubectl delete service {name} - 删除指定service
- Kubectl rollout restart deployment {name} - 重启指定deployment
- Kubectl rollout undo deployment {name} - 回滚指定deployment
- Kubectl scale deployment {name} --replicas={num} - 修改指定deployment的pod数量
九、自定义资源
- Kubectl api-versions - 显示kubernetes API上的所有版本
- Kubectl api-resources - 显示kubernetes API 上的所有资源
- kubectl get crds - 显示集群中所有自定义资源的定义
- kubectl create -f {filename} - 创建自定义资源对象
- kubectl get crd {name} - 显示指定自定义资源的定义
十、日志和监控
- Kubectl logs {name} - 显示指定pod的日志信息
- Kubectl logs --tail={num} {name} - 显示指定pod最后几条日志信息
- Kubectl logs -f {name} - 实时显示指定pod的日志信息
- Kubectl top nodes - 显示所有节点的CPU和内存使用情况
- Kubectl top pods - 显示所有pods的CPU和内存使用情况
- Kubectl get events -显示所有事件信息
十一、扩展和插件
- Kubectl plugin list - 查看已安装的插件
- Kubectl plugin install https://github.com/kubernetes-sigs/krew/releases/latest/download/krew.{tar.gz|zip} - 安装krew插件管理器
- Kubectl plugin install {name} - 安装指定插件
- Kubectl krew search {name} - 搜索krew插件
- Kubectl krew uninstall {name} - 卸载指定插件
十二、高可用和容错
- Kubectl get endpoints {name} -显示指定service 的后端地址信息
- Kubectl get svc {name} -o jsonpath='{.spec.selector}' - 显示指定service的标签选择器
- Kubectl get pods -o jsonpath='{.items[*].metadata.labels}' - 显示所有pods的标签信息
- Kubectl label pods {name} {label}={value} - 给指定pod打上标签
- Kubectl taint node {name} key=value:taint_effect - 在指定节点上添加taint特性
- Kubectl drain <NODE_NAME> - 将节点设置为不可调度状态,以便于维护和升级该节点,并避免丢失重要的运行数据
- Kubectl cordon <NODE_NAME> - 阻止新的pod调度特定的节点上,以避免在节点维护期间出现过多负载
- Kubectl uncordon <NODE_NAME> - 允许新的Pod 调度到指定的节点上,以恢复该节点的正常工作状态
- Kubectl get componentstatuses - 获取集群中各组件(如 kube-apiserver、kube-controller-manager、kube-scheduler 等)的当前健康状态,以确保集群的高可用性和稳定性
- Kubectl debug <pod-name> -c <container-name> -it -- /bin/bash - 在指定的pod中的容器中运行一个新的 bash 进程,以便进行调试。
十三、Web界面
- Kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.7.0/aio/deploy/recommended.yaml - 部署kubernetes dashboard
- Kubectl proxy - 打开 kubernetes API 服务的代理端口
- 访问 http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/ - 使用web界面访问kubernetes dashboard
十四、资源限制和优化
- Kubectl describe pod {name} - 显示指定pod的详细信息
- Kubectl describe node {name} - 显示指定节点的详细信息
- Kubectl get pod {name} -o jsonpath='{.spec.containers[*].resources.limits.cpu}' - 显示指定pod的CPU限制
- Kubectl get pod {name} -o jsonpath='{.spec.containers[*].resources.limits.memory}' - 显示指定pod的内存限制
- Kubectl get pod -o jsonpath='{.spec.containers[*].resources.requests.cpu}' - 显示指定pod的CPU请求
- Kubectl get pod {name} -o jsonpath='{.spec.containers[*].resources.requests.memory}' - 显示指定pod的内存请求
- Kubectl get node {name} -o jsonpath='{.status.allocatable.cpu}' - 显示指定节点可用的CPU数量
十五、集群管理和维护
- Kubectl cluster-info - 显示集群的基本信息
- Kubectl get nodes - 显示所有节点信息
- Kubectl get pods - 显示所有的pods信息
- Kubectl get deployment -显示所有的deployment信息
- Kubectl get services - 显示所有的service信息
- Kubectl get namespaces -显示所有的namespace信息
- Kubectl get events - 显示所有的事件信息
- Kubectl get configmaps - 显示所有的configmap信息
- Kubectl get secrets - 显示所有的secret 信息
- Kubectl apply -f {filename} - 根据指定的yaml文件部署kubernetes资源
十六、镜像和存储
- Kubectl get nodes -o jsonpath='{.items[*].status.allocatable.attachable-volumes-aws-ebs}' - 显示可用的AWS EBS存储卷数量
- Kubectl get pods -o jsonpath='{.items[*].spec.volumes[*].name}' - 显示所有pods中使用的卷名称
- Kubectl get storageclass - 显示所有的storageclass信息
- Kubectl get persistentvolumeclaim - 显示所有的pvc信息
- Kubectl get persistentvolume - 显示所有的pv信息
十七、自动化和脚本化
- Kubectl apply -f {filename} - 使用yaml文件自动创建kubernetes资源
- Kubectl delete -f {filename} - 使用yaml文件自动删除kubernetes资源
- Kubectl create deployment {name} --image={image} --dry-run -o yaml > {filename} - 生成deployment的yaml文件
- Kubectl create service {name} --tcp={port}:{targetport} --dry-run -o yaml > {filename} - 生成service的yaml文件
- Kubectl create secret generic {name} --from-literal=key=value --dry-run -o yaml > {filename} - 生成secret的yaml文件
- Kubectl create cronjob [job_name] --image=[image_name] --schedule='*/1 * * * *' --restart=OnFailure --dry-run -o yaml > [filename].yaml - 创建一个cronjob实例,并将其定义文件保存到指定的YAML文件中
- Kubectl annotate <RESOURCE_TYPE> <RESOURCE_NAME> <KEY>=<VALUE> - 给指定类型的资源对象添加注释
十八、部署和发布
- Kubectl create deployment {name} --image={image} --replicas={num} --port={port} - 创建deployment
- Kubectl scale deployment {name} --replicas={num} - 修改deployment的pod数量
- Kubectl autoscale deployment {name} --cpu-percent={percent} --min={min} --max={max} - 自动扩缩deployment的pod数量
- Kubectl create service {name} --tcp={port}:{targetport} - 创建service
- Kubectl expose deployment {name} --port={port} --target-port={targetport} -type={type} - 创建service