K8S中的pod资源之资源限制与重启策略

18 篇文章 0 订阅
13 篇文章 0 订阅

一、资源限制

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

在这里插入图片描述
在这里插入图片描述

(一)、Pod和Container的资源请求和限制:

spec.containers[].resources.limits.cpu     //cpu上限 
spec.containers[].resources.limits.memory   //内存上限
spec.containers[].resources.requests.cpu   //创建时分配的基本CPU资源
spec.containers[].resources.requests.memory  //创建时分配的基本内存资源

(二)、示例1

1、创建资源限制的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"
        cpu: "250m"
      limits:
        memory: "128Mi"
        cpu: "500m"
  - name: wp
    image: wordpress
    resources:
      requests:
        memory: "64Mi"
        cpu: "250m"
      limits:
        memory: "128Mi"
        cpu: "500m"

[root@localhost demo]# kubectl apply -f pod2.yaml 
pod/frontend created

2、查看具体事件

[root@localhost demo]# kubectl describe pod frontend
[root@localhost demo]# kubectl describe nodes 192.168.195.150

Namespace                  Name                                     CPU Requests  CPU Limits  Memory Requests  Memory Limits
  ---------                  ----                                     ------------  ----------  ---------------  -------------
  default                    frontend                                 500m (50%)    1 (100%)    128Mi (3%)       256Mi (6%)
Allocated resources:
  (Total limits may be over 100 percent, i.e., overcommitted.)
  Resource  Requests    Limits
  --------  --------    ------
  cpu       550m (55%)  1100m (110%)
  memory    228Mi (5%)  556Mi (14%)

3、成功部署好后查看状态

[root@localhost demo]# kubectl get pods
NAME                              READY   STATUS    RESTARTS   AGE
frontend                          2/2     Running   1          3m21s

4、查看node节点资源状态

[root@localhost demo]# kubectl describe nodes 192.168.195.151

5、查看命名空间

[root@localhost demo]# kubectl get ns
NAME          STATUS   AGE
default       Active   17d
kube-public   Active   17d
kube-system   Active   17d

6、重启策略:Pod在遇到故障之后重启的动作

1:Always:当容器终止退出后,总是重启容器,默认策略
2:OnFailure:当容器异常退出(退出状态码非0)时,重启容器
3:Never:当容器终止退出,从不重启容器。
(注意:k8s中不支持重启Pod资源,只有删除重建)

[root@localhost demo]# kubectl edit deploy
......
 restartPolicy: Always
......

//示例2

[root@localhost demo]# vim pod3.yaml
apiVersion: v1
kind: Pod
metadata:
  name: foo
spec:
  containers:
  - name: busybox
    image: busybox
    args:
    - /bin/sh
    - -c
    - sleep 30; exit 3
[root@localhost demo]# kubectl apply -f pod3.yaml 
pod/foo created

//查看重启次数加1
[root@localhost demo]# kubectl get pods
NAME                              READY   STATUS             RESTARTS   AGE
foo                               1/1     Running            1          50s


[root@localhost demo]# vim pod3.yaml

apiVersion: v1
kind: Pod
metadata:
  name: foo
spec:
  containers:
  - name: busybox
    image: busybox
    args:
    - /bin/sh
    - -c
    - sleep 10;exit 3
  restartPolicy: Never
//跟container同一个级别
//完成状态不会进行重启
[root@localhost demo]# kubectl get pods
NAME                              READY   STATUS      RESTARTS   AGE
foo                               0/1     Completed   0          29s

二、健康检查:又称为探针(Probe)

(注意:)规则可以同时定义
livenessProbe 如果检查失败,将杀死容器,根据Pod的restartPolicy来操作。
ReadinessProbe 如果检查失败,kubernetes会把Pod从service endpoints中剔除。

Probe支持三种检查方法:
httpGet 发送http请求,返回200-400范围状态码为成功。
exec 执行Shell命令返回状态码是0为成功。
tcpSocket 发起TCP Socket建立成功

https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/

示例1:exec方式
在这里插入图片描述

apiVersion: v1
kind: Pod
metadata:
  labels:
    test: liveness
  name: liveness-exec
spec:
  containers:
  - name: liveness
    image: busybox
    args:
    - /bin/sh
    - -c
    - touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy;sleep 30
    livenessProbe:
      exec:
        command:
        - cat
        - /tmp/healthy
      initialDelaySeconds: 5
      periodSeconds: 5

[root@localhost demo]# kubectl get pods
liveness-exec                     1/1     Running     4          4m11s

示例2:httpGet方式
在这里插入图片描述

示例3:tcpSocket方式

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

清风~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值