K8S常见操作
  zLDVzbHALzQ2 2023年11月02日 47 0

一、基本操作

  1. kubectl version - 显示kubectl的版本信息
  2. kubectl cluster-info - 显示集群的基本信息
  3. kubectl get nodes - 显示所有节点信息
  4. kubectl get pods - 显示所有的pods信息
  5. kubectl create deployment {name} --image={image} - 创建pods
  6. Kubectl scale deployment {name} --replicas={num} - 修改pods的数量
  7. Kubectl describe pod {name} - 显示指定的pod的详细信息
  8. Kubectl logs {name} - 显示指定pod的日志信息
  9. Kubectl exec -it {name} -- /bin/bash - 进入指定pod的bash终端
  10. Kubectl delete deployment {name} - 删除pods
  11. Kubectl edit svc {name} - 编辑svc

二、调试和故障排查

  1. Kubectl describe node {name} -显示指定节点的详细信息
  2. Kubectl describe deployment {name} - 显示指定deployment的详细信息
  3. Kubectl describe service {name} - 显示指定service的详细信息
  4. Kubectl get events -显示所有事件信息
  5. Kubectl get events --sort-by=.metadata.creationTimestamp - 显示所有事件信息,并按照创建时间排序
  6. Kubectl get pods --all-namespaces - 显示所有namespace下的pods信息
  7. Kubectl get deployments --all-namespaces -显示所有namespace下的deployment信息
  8. Kubectl get services --all-namespaces - 显示所有namespace下的service信息
  9. Kubectl get nodes -o yaml | grep -A5 -B2 SchedulingDisabled - 显示所有已禁用调度的节点信息
  10. Kubectl run -i --tty busybox --image=busybox --restart=Never -- sh - 运行一个busybox容器并进行调试

三、部署和管理

  1. Kubectl create deployment {name} --image={image} --replicas={num} --port={port} - 创建deployment
  2. Kubectl expose deployment {name} --port={port} --target-port={targetport} --type={type} - 创建service
  3. Kubectl set image deployment/{name} {container}={image}:{tag} - 更新deployment 中某个容器的镜像
  4. Kubectl rollout history deployment/{name} - 显示指定deployment的版本历史
  5. Kubectl rollout undo deployment/{name} --to-revision=1 - 回滚指定deployment到指定版本
  6. Kubectl rollout pause deployment/{name} - 暂停指定deployment的升级
  7. Kubectl rollout resume deploymeng/{name} - 恢复指定deployment的升级
  8. Kubectl rollout status deployment/{name} - 热重启deployment
  9. Kubectl exec -it {name} -- /bin/bash - 在指定pod中进入bash终端
  10. Kubectl apply -f {filename} - 根据指定的yaml文件部署kubernetes资源

四、安全和访问控制

  1. Kubectl create secret generic {name} --from-file={key}={path} - 创建secret对象
  2. Kubectl create serviceaccount {name} - 创建sercieaccount对象
  3. Kubectl create role {name} --resource={resource} --verb={verb} - 创建role对象
  4. Kubectl create rolebinding {name} --role={role} --serviceaccount={namespace}:{sa_name} - 创建rolebinding对象
  5. Kubectl get secret {name} -o=jsonpath='{.data.{key}}' | base64 --decode - 显示secret中指定key的值
  6. Kubectl delete secret {name} - 删除指定的secret对象
  7. Kubectl get serviceaccount {name} - 显示指定的serviceaccount对象信息
  8. Kubectl create clusterrole {name} --resource={resource} -verb={verb} - 创建clusterrole对象
  9. Kubectl create clusterrolebinding {name} --clusterrole={role} --serviceacconunt={namespace}:{sa_name} - 创建clusterrolebinding对象
  10. Kubectl auth can-i <VERB> <RESOURCE> [--namespace=<NAMESPACE>] - 检查当前用户在某个ns是否有执行指定资源操作的权限

- <VERB>: 表示要执行的操作,例如 create ,get ,update 等

- <RESOURCE>:表示资源类型,例如 pods ,deployments, services 等。

--namespace=<NAMESPACE>:(可选) 表示在指定的命名空间中搜索资源

五、网络和负载均衡

  1. Kubectl get svc {name} -o jsonpath='{.spec.clusterIP}' - 显示指定service 的cluster IP 地址
  2. Kubectl get endpoints {name} - 显示指定service的后端地址信息
  3. Kubectl get ingress {name} - 显示指定ingress的详细信息
  4. Kubectl create ingress {name} --rule={host}/{path}=svc:{service_name}:{port} - 创建ingress条目
  5. Kubectl delete ingress {name} - 删除指定的ingress条目
  6. Kubectl get svc {name} -o jsonpath='{.spec.type}' - 显示指定service的类型信息
  7. Kubectl get pods -o jsonpath='{range.items[*]}{.metadata.name}{"\t"}{.status.podIP}{"\n"}{end}' - 显示所有pods的名称和IP地址信息
  8. Kubectl exec -it {name} -- nc -z -v {ip} {port} - 在指定pod中使用nc工具测试TCP连接
  9. Kubectl exec -it {name} -- curl {url} - 在指定pod中使用curl 工具测试HTTP连接
  10. Kubectl get pods -l {label} - 显示所有带有指定label的pods的信息

六、容器高级操作

  1. Kubectl exec -it <pod_name> --<command> - 在运行中的 pod 内部启动一个新的容器,并在其中执行指定的命令
  2. Kubectl cp <pod_name>:<container_name> - 将pod内指定路径的文件或目录拷贝到主机上
  3. Kubectl logs <pod_name> <container_name> - 查看 pod 内指定容器的日志输出
  4. Kubectl port-forward <pod_name> <local_port>:<pod_port> - 将本地端口与 pod 内部的特定端口进行转发,从而可以通过本地网络访问 pod 中的服务
  5. Kubectl describe pod <pod_name> - 查看pod 的详细信息,例如 pod 的状态、资源限制和调度信息等。
  6. Kubectl top pod <pod_name> - 查看pod的 CPU 和内存使用情况
  7. Kubectl get pod <pod_name> -o yaml - 获取 pod 的 YAML 文件,可以用于备份或还原 pod
  8. Kubectl rollout restart deployment <deployment_name> - 强制重启 Deployments 中的所有 pod ,以便应用程序更新能够生效
  9. Kubectl attch <pod_name> -c <container_name> - 将当前终端附加到 pod 中指定的容器上,从而可以直接在该容器内操作
  10. Kubectl apply -f <resource_file.yaml> - 使用 YAML 或 JSON 文件部署 pod ,支持声明式配置管理

七、状态查询和修改

  1. Kubectl get deployment.apps {name} - 显示指定deployment的详细信息
  2. Kubectl get deployment.apps -A - 显示所有namespace下的deployment信息
  3. Kubectl get replicasets.apps {name} - 显示指定replicaset的详细信息
  4. Kubectl get replicasets.apps -A - 显示所有namespace 下的replicaset 信息
  5. Kubectl get services -A - 显示所有namespace下的service信息
  6. Kubectl get pods -o jsonpath='{range.items[*]}{.metadata.name}{"\n"}{end}' - 显示所有pods的名称信息
  7. Kubectl get pod {name} -o jsonpath='{range.status.conditions[?(@.type=="Ready")]}{.status}{"\n"}{end}' - 显示指定pod的Ready状态
  8. Kubectl get pod {name} -o jsonpath='{range.status.conditions[?(@.type==""Ready)]}{.message}{"\n"}{end}' - 显示指定pod的Ready状态信息
  9. Kubectl get pod {name} -o jsonpath='{range.status.conditions[?(@.type=="Ready")]}{.reason}{"\n"}{end}' -显示指定pod的Ready状态原因信息
  10. Kubectl delete pod {name} --force --grace-period=0 - 强制删除指定pod

八、状态修复和调整

  1. Kubectl get pods -o jsonpath='{range.items[?@.status.phase=="Pending"]}{.metadata.name}{"\t"}{.reason}{"\n"}{end}' - 显示所有处于Pending状态的pods
  2. Kubectl describe pod {name} - 显示指定pod的详细信息
  3. Kubectl describe node {name} - 显示指定节点的详细信息
  4. Kubectl describe deployment {name} - 显示指定deployment的详细信息
  5. Kubectl delete pod {name} --force --grace-period=0 - 强制删除指定pod
  6. Kubectl delete deployment {name} - 删除指定deployment
  7. Kubectl delete service {name} - 删除指定service
  8. Kubectl rollout restart deployment {name} - 重启指定deployment
  9. Kubectl rollout undo deployment {name} - 回滚指定deployment
  10. Kubectl scale deployment {name} --replicas={num} - 修改指定deployment的pod数量

九、自定义资源

  1. Kubectl api-versions - 显示kubernetes API上的所有版本
  2. Kubectl api-resources - 显示kubernetes API 上的所有资源
  3. kubectl get crds - 显示集群中所有自定义资源的定义
  4. kubectl create -f {filename} - 创建自定义资源对象
  5. kubectl get crd {name} - 显示指定自定义资源的定义

十、日志和监控

  1. Kubectl logs {name} - 显示指定pod的日志信息
  2. Kubectl logs --tail={num} {name} - 显示指定pod最后几条日志信息
  3. Kubectl logs -f {name} - 实时显示指定pod的日志信息
  4. Kubectl top nodes - 显示所有节点的CPU和内存使用情况
  5. Kubectl top pods - 显示所有pods的CPU和内存使用情况
  6. Kubectl get events -显示所有事件信息

十一、扩展和插件

  1. Kubectl plugin list - 查看已安装的插件
  2. Kubectl plugin install https://github.com/kubernetes-sigs/krew/releases/latest/download/krew.{tar.gz|zip} - 安装krew插件管理器
  3. Kubectl plugin install {name} - 安装指定插件
  4. Kubectl krew search {name} - 搜索krew插件
  5. Kubectl krew uninstall {name} - 卸载指定插件

十二、高可用和容错

  1. Kubectl get endpoints {name} -显示指定service 的后端地址信息
  2. Kubectl get svc {name} -o jsonpath='{.spec.selector}' - 显示指定service的标签选择器
  3. Kubectl get pods -o jsonpath='{.items[*].metadata.labels}' - 显示所有pods的标签信息
  4. Kubectl label pods {name} {label}={value} - 给指定pod打上标签
  5. Kubectl taint node {name} key=value:taint_effect - 在指定节点上添加taint特性
  6. Kubectl drain <NODE_NAME> - 将节点设置为不可调度状态,以便于维护和升级该节点,并避免丢失重要的运行数据
  7. Kubectl cordon <NODE_NAME> - 阻止新的pod调度特定的节点上,以避免在节点维护期间出现过多负载
  8. Kubectl uncordon <NODE_NAME> - 允许新的Pod 调度到指定的节点上,以恢复该节点的正常工作状态
  9. Kubectl get componentstatuses - 获取集群中各组件(如 kube-apiserver、kube-controller-manager、kube-scheduler 等)的当前健康状态,以确保集群的高可用性和稳定性
  10. Kubectl debug <pod-name> -c <container-name> -it -- /bin/bash - 在指定的pod中的容器中运行一个新的 bash 进程,以便进行调试。

十三、Web界面

  1. Kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.7.0/aio/deploy/recommended.yaml - 部署kubernetes dashboard
  2. Kubectl proxy - 打开 kubernetes API 服务的代理端口
  3. 访问 http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/ - 使用web界面访问kubernetes dashboard

十四、资源限制和优化

  1. Kubectl describe pod {name} - 显示指定pod的详细信息
  2. Kubectl describe node {name} - 显示指定节点的详细信息
  3. Kubectl get pod {name} -o jsonpath='{.spec.containers[*].resources.limits.cpu}' - 显示指定pod的CPU限制
  4. Kubectl get pod {name} -o jsonpath='{.spec.containers[*].resources.limits.memory}' - 显示指定pod的内存限制
  5. Kubectl get pod -o jsonpath='{.spec.containers[*].resources.requests.cpu}' - 显示指定pod的CPU请求
  6. Kubectl get pod {name} -o jsonpath='{.spec.containers[*].resources.requests.memory}' - 显示指定pod的内存请求
  7. Kubectl get node {name} -o jsonpath='{.status.allocatable.cpu}' - 显示指定节点可用的CPU数量

十五、集群管理和维护

  1. Kubectl cluster-info - 显示集群的基本信息
  2. Kubectl get nodes - 显示所有节点信息
  3. Kubectl get pods - 显示所有的pods信息
  4. Kubectl get deployment -显示所有的deployment信息
  5. Kubectl get services - 显示所有的service信息
  6. Kubectl get namespaces -显示所有的namespace信息
  7. Kubectl get events - 显示所有的事件信息
  8. Kubectl get configmaps - 显示所有的configmap信息
  9. Kubectl get secrets - 显示所有的secret 信息
  10. Kubectl apply -f {filename} - 根据指定的yaml文件部署kubernetes资源

十六、镜像和存储

  1. Kubectl get nodes -o jsonpath='{.items[*].status.allocatable.attachable-volumes-aws-ebs}' - 显示可用的AWS EBS存储卷数量
  2. Kubectl get pods -o jsonpath='{.items[*].spec.volumes[*].name}' - 显示所有pods中使用的卷名称
  3. Kubectl get storageclass - 显示所有的storageclass信息
  4. Kubectl get persistentvolumeclaim - 显示所有的pvc信息
  5. Kubectl get persistentvolume - 显示所有的pv信息

十七、自动化和脚本化

  1. Kubectl apply -f {filename} - 使用yaml文件自动创建kubernetes资源
  2. Kubectl delete -f {filename} - 使用yaml文件自动删除kubernetes资源
  3. Kubectl create deployment {name} --image={image} --dry-run -o yaml > {filename} - 生成deployment的yaml文件
  4. Kubectl create service {name} --tcp={port}:{targetport} --dry-run -o yaml > {filename} - 生成service的yaml文件
  5. Kubectl create secret generic {name} --from-literal=key=value --dry-run -o yaml > {filename} - 生成secret的yaml文件
  6. Kubectl create cronjob [job_name] --image=[image_name] --schedule='*/1 * * * *' --restart=OnFailure --dry-run -o yaml > [filename].yaml - 创建一个cronjob实例,并将其定义文件保存到指定的YAML文件中
  7. Kubectl annotate <RESOURCE_TYPE> <RESOURCE_NAME> <KEY>=<VALUE> - 给指定类型的资源对象添加注释

十八、部署和发布

  1. Kubectl create deployment {name} --image={image} --replicas={num} --port={port} - 创建deployment
  2. Kubectl scale deployment {name} --replicas={num} - 修改deployment的pod数量
  3. Kubectl autoscale deployment {name} --cpu-percent={percent} --min={min} --max={max} - 自动扩缩deployment的pod数量
  4. Kubectl create service {name} --tcp={port}:{targetport} - 创建service
  5. Kubectl expose deployment {name} --port={port} --target-port={targetport} -type={type} - 创建service
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

zLDVzbHALzQ2
作者其他文章 更多