玩k8s? Pod的资源限制&重启策略

本文介绍了Kubernetes Pod的资源限制,包括内存和CPU的申请配额与限制配额,以及资源限制的重要性。同时,讲解了Pod的重启策略,如Always、OnFailure和Never,并给出了不同场景下的适用情况。最后,详细阐述了健康检查机制,包括livenessProbe和readinessProbe,以及httpGet、exec和tcpSocket三种检查方法的应用示例。
摘要由CSDN通过智能技术生成

Pod 资源限制&重启策略

1.1: 资源限制

  • 官方文档

https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/

Pod资源配额有两种:

  • 申请配额:调度时使用,参考是否有节点满足该配置

    spec.containers[].resources.limits.cpu

    spec.containers[].resources.limits.memory

  • 限制配额:容器能使用的最大配置

    spec.containers[].resources.requests.cpu

    spec.containers[].resources.requests.memory

  • 容器资源限制:
    1、内存
    2、CPU

    limits:实际最大使用的配额

    requests:申请的配额,主要用于k8s做资源调度分配时参考值

    一般limits比requests大一些

  • 资源限制好处:避免某容器资源利用率异常突发影响其他容器,可能会产生雪崩效应!

  •   - name: wp
        image: wordpress
        resources:
          requests:
            memory: "64Mi"
            cpu: "250m"
          limits:
            memory: "128Mi"
            cpu: "500m"
    

示例:

[root@k8s-master1 demo]# vim resources.yaml
apiVersion: v1
kind: Pod
metadata:
  name: frontend
spec:
  containers:
  - name: db
    image: mysql
    env: "设定变量"
    - name: MYSQL_ROOT_PASSWORD
      value: "password"
    resources: "资源限额"
      requests: "请求资源"
        memory: "64Mi" "请求内存64M"
        cpu: "250m" "一秒钟能够使用一个cpu核心资源的25%的资源"
      limits: "最大限制资源"
        memory: "128Mi" "最大限制内存128M"
        cpu: "500m" "一秒钟能够使用一个cpu核心资源的50%资源"
  - name: wp
    image: wordpress
    resources:
      requests:
        memory: "64Mi"
        cpu: "250m"
      limits:
        memory: "128Mi"
        cpu: "500m"
[root@k8s-master1 demo]# kubectl apply -f resources.yaml 
pod/frontend created
[root@k8s-master1 demo]# kubectl describe pod  frontend "查看创建过程"
[root@k8s-master1 demo]# kubectl get pods
NAME               READY   STATUS    RESTARTS   AGE
frontend           2/2     Running   0          2m39s
[root@k8s-master1 demo]# kubectl get pods -o wide
NAME       READY   STATUS    RESTARTS   AGE     IP            NODE        NOMINATED NODE   READINESS GATES
frontend   2/2     Running   0          3m55s   10.244.1.33   k8s-node1   <none>           <none>
[root@k8s-master1 demo]#  kubectl describe nodes k8s-node1
...
  Namespace                   Name                                          CPU Requests  CPU Limits  Memory Requests  Memory Limits  AGE
  ---------                   ----                                          ------------  ----------  ---------------  -------------  ---
  default                     frontend                                      500m (12%)    1 (25%)     128Mi (7%)       256Mi (14%)    4m42s
...
Allocated resources:
  (Total limits may be over 100 percent, i.e., overcommitted.)
  Resource           Requests      Limits
  --------           --------      ------
  cpu                920m (23%)    1320m (33%)
  memory             1308Mi (76%)  1878Mi (109%)
  ephemeral-storage  0 (0%)        0 (0%)
  hugepages-1Gi      0 (0%)        0 (0%)
  hugepages-2Mi      0 (0%)        0 (0%)
Events:              <none>
[root@k8s-master1 demo]# kubectl get pods
NAME       READY   STATUS    RESTARTS   AGE
frontend   2/2     Running   0          6m9s
[root@k8s-master1 demo]# kubectl describe nodes 192.168.100.202 "查看节点信息"

[root@k8s-master1 demo]# kubectl get ns "查看命名空间"
NAME                   STATUS   AGE
default                Active   3d1h
ingress-nginx          Active   3d1h
kube-node-lease        Active   3d1h
kube-public            Active   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值