【K8S】常用的 Kubernetes(K8S)指令

常用的 Kubernetes(K8s)指令:

  1. 创建一个资源:kubectl create <资源类型> <资源名称>
    例如:kubectl create deployment my-deployment

  2. 获取资源列表:kubectl get <资源类型>
    例如:kubectl get pods

  3. 查看资源详细信息:kubectl describe <资源类型> <资源名称>
    例如:kubectl describe pod my-pod

  4. 删除一个资源:kubectl delete <资源类型> <资源名称>
    例如:kubectl delete deployment my-deployment

  5. 执行一个命令在容器内部:kubectl exec <pod名称> <命令>
    例如:kubectl exec my-pod ls

  6. 在控制台中查看一个Pod的日志:kubectl logs <pod名称>
    例如:kubectl logs my-pod

  7. 扩展一个Deployment的副本数:kubectl scale deployment <deployment名称> --replicas=<副本数>
    例如:kubectl scale deployment my-deployment --replicas=3

  8. 滚动更新一个Deployment:kubectl set image deployment/<deployment名称> <容器名称>=<新镜像名称>
    例如:kubectl set image deployment/my-deployment my-container=my-new-image:latest

  9. 暂停一个Deployment的滚动更新:kubectl rollout pause deployment/<deployment名称>
    例如:kubectl rollout pause deployment/my-deployment

  10. 继续一个Deployment的滚动更新:kubectl rollout resume deployment/<deployment名称>
    例如:kubectl rollout resume deployment/my-deployment

  11. 回滚一个Deployment到上一个版本:kubectl rollout undo deployment/<deployment名称>
    例如:kubectl rollout undo deployment/my-deployment

  12. 创建一个Service:kubectl expose <资源类型> <资源名称> --port=<端口号> --target-port=<目标端口号>
    例如:kubectl expose deployment my-deployment --port=80 --target-port=8080

  13. 获取集群中的节点列表:kubectl get nodes

  14. 获取Pod的详细信息,包括IP地址、节点等:kubectl get pods -o wide

  15. 查看集群事件:kubectl get events

  16. 查看集群中的命名空间:kubectl get namespaces

  17. 在特定的命名空间中执行指令:kubectl -n <命名空间> <指令>
    例如:kubectl -n my-namespace get pods

  18. 更新一个资源的配置:kubectl apply -f <配置文件>
    例如:kubectl apply -f deployment.yaml

  19. 检查资源的健康状态:kubectl describe <资源类型> <资源名称> | grep Conditions
    例如:kubectl describe pod my-pod | grep Conditions

  20. 查看当前集群的上下文:kubectl config current-context

  21. 查看特定资源的日志:kubectl logs <资源类型>/<资源名称>
    例如:kubectl logs pod/my-pod

  22. 查看特定资源的配置:kubectl get <资源类型> <资源名称> -o yaml
    例如:kubectl get pod my-pod -o yaml

  23. 查看特定资源的标签:kubectl get <资源类型> --show-labels
    例如:kubectl get pods --show-labels

  24. 根据标签选择资源:kubectl get <资源类型> -l <标签选择器>
    例如:kubectl get pods -l app=my-app

  25. 根据标签删除资源:kubectl delete <资源类型> -l <标签选择器>
    例如:kubectl delete pods -l app=my-app

  26. 查看特定命名空间中的所有资源:kubectl get all -n <命名空间>
    例如:kubectl get all -n my-namespace

  27. 创建一个命名空间:kubectl create namespace <命名空间名称>
    例如:kubectl create namespace my-namespace

  28. 删除一个命名空间及其所有资源:kubectl delete namespace <命名空间名称>
    例如:kubectl delete namespace my-namespace

  29. 暴露一个Deployment为外部服务:kubectl expose deployment <deployment名称> --type=<服务类型> --port=<端口号>
    例如:kubectl expose deployment my-deployment --type=LoadBalancer --port=80

  30. 在Pod中执行交互式终端:kubectl exec -it <pod名称> – <命令>
    例如:kubectl exec -it my-pod – /bin/bash

  31. 查看节点的详细信息:kubectl describe node <节点名称>
    例如:kubectl describe node my-node

  32. 查看特定节点上运行的Pod列表:kubectl describe node <节点名称> | grep Pods
    例如:kubectl describe node my-node | grep Pods

  33. 强制删除一个资源:kubectl delete <资源类型> <资源名称> --grace-period=0 --force
    例如:kubectl delete pod my-pod --grace-period=0 --force

  34. 创建一个持久卷(Persistent Volume):kubectl apply -f <持久卷配置文件>
    例如:kubectl apply -f persistent-volume.yaml

  35. 创建一个持久卷声明(Persistent Volume Claim):kubectl apply -f <持久卷声明配置文件>
    例如:kubectl apply -f persistent-volume-claim.yaml

  36. 查看持久卷列表:kubectl get pv

  37. 查看持久卷声明列表:kubectl get pvc

  38. 查看特定持久卷的详细信息:kubectl describe pv <持久卷名称>
    例如:kubectl describe pv my-pv

  39. 查看特定持久卷声明的详细信息:kubectl describe pvc <持久卷声明名称>
    例如:kubectl describe pvc my-pvc

  40. 创建一个配置映射(ConfigMap):kubectl create configmap <配置映射名称> --from-file=<文件路径>
    例如:kubectl create configmap my-config --from-file=config.txt

  41. 查看配置映射列表:kubectl get configmaps

  42. 查看特定配置映射的详细信息:kubectl describe configmap <配置映射名称>
    例如:kubectl describe configmap my-config

  43. 创建一个密钥(Secret):kubectl create secret generic <密钥名称> --from-literal=<键>=<值>
    例如:kubectl create secret generic my-secret --from-literal=username=admin --from-literal=password=pass123

  44. 查看密钥列表:kubectl get secrets

  45. 查看特定密钥的详细信息:kubectl describe secret <密钥名称>
    例如:kubectl describe secret my-secret

  46. 创建一个水平自动伸缩器(Horizontal Pod Autoscaler):kubectl autoscale <资源类型> <资源名称> --min=<最小副本数> --max=<最大副本数> --cpu-percent=<CPU百分比>
    例如:kubectl autoscale deployment my-deployment --min=2 --max=5 --cpu-percent=80

  47. 查看水平自动伸缩器列表:kubectl get hpa

  48. 查看特定水平自动伸缩器的详细信息:kubectl describe hpa <水平自动伸缩器名称>
    例如:kubectl describe hpa my-hpa

  49. 创建一个Job:kubectl create job <Job名称> --image=<镜像名称>
    例如:kubectl create job my-job --image=my-image:latest

  50. 查看Job列表:kubectl get jobs

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值