【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

相关推荐
苓诣11 小时前
Submariner 部署全过程
云计算·k8s
橙子家15 小时前
k8s 中的 Ingress 简介【k8s 系列之三】
k8s
KubeSphere 云原生1 天前
云原生周刊:Prometheus 3.0 Beta 发布|2024.09.16
云计算·k8s·容器平台·kubesphere
耐心坚持努力�2 天前
k8s重要知识点
linux·运维·k8s·k8s重要知识点
行走的山峰4 天前
k8s证书过期处理
k8s
G皮T5 天前
【Kubernetes】K8s 的鉴权管理(二):基于属性 / 节点 / Webhook 的访问控制
kubernetes·k8s·鉴权·rbac·webhook·访问控制·abac
橙子家7 天前
k8s 中的 Service 简介【k8s 系列之二】
k8s
KubeSphere 云原生7 天前
云原生周刊:OpenTofu Registry 获得用户界面和 API|2024.9.9
云计算·k8s·容器平台·kubesphere
BruceGerGer9 天前
flutter开发实战-flutter web加载html及HtmlElementView的使用
flutter·1024程序员节
阿珂来了10 天前
怎么安装Grafana-Loki
linux·k8s·grafana