一、资源限制

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方式

在这里插入图片描述

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐