k8s学习(三)Pod的使用


前言

Pod 就像是豌豆荚一样,它由一个或者多个容器组成,单个 Pod 可以看成是运行独立应用的“逻辑主机”。


提示:以下是本篇文章正文内容,下面案例可供参考

一、创建Pod

(1)创建 nginx-pod.yaml

[root@k8s-master k8s]# cat nginx-pod.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  labels:
    app: nginx-pod
spec:
  containers:
  - name: nginx-container
    image: nginx:latest

(2)创建Pod

kubectl create -f nginx-pod.yaml

(3)查看Pod

[root@k8s-master k8s]# kubectl get pod
NAME        READY   STATUS    RESTARTS   AGE
nginx-pod   1/1     Running   0          55m
[root@k8s-master k8s]# kubectl describe pod nginx-pod
Name:         nginx-pod
Namespace:    default
Priority:     0
Node:         k8s-node01/172.16.10.159
Start Time:   Sat, 18 Dec 2021 09:16:00 -0500
Labels:       app=nginx-pod
Annotations:  cni.projectcalico.org/podIP: 10.244.85.196/32
              cni.projectcalico.org/podIPs: 10.244.85.196/32
Status:       Running
IP:           10.244.85.196
IPs:
  IP:  10.244.85.196
...

(4)访问Pod

[root@k8s-master k8s]# curl http://10.244.85.196:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
...

二、资源限制

[root@k8s-master k8s]# cat nginx-limit-pod.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: nginx-limit-pod
  labels:
    app: nginx-limit-pod
spec:
  containers:
  - name: nginx-limit-container
    image: nginx:latest
    resources:
      requests:
        cpu: "200m"
        memory: "100Mi"
      limits:
        cpu: "1"
        memory: "128Mi"

给容器配置内存申请,添加 resources:requests ;配置限制, 添加 resources:limits。

只要节点有足够的内存资源,那容器就可以使用超过其申请的资源,但是不允许容器使用超过其限制的资源;

CPU 的单位是 CPU 个数,可以用 millicpu (m) 表示少于 1 个 CPU 的情况,如 500m = 500millicpu = 0.5cpu;

内存的单位则包括 E, P, T, G, M, K, Ei, Pi, Ti, Gi, Mi, Ki 等。

三、健康检测

1、Probe

k8s 提供了两种探针(Probe)来探测容器的状态:

    LivenessProbe:探测应用是否处于健康状态,如果不健康则删除并重新创建容器。

    ReadinessProbe:探测应用是否启动完成并且处于正常服务状态,如果不正常则不会接收来自 Kubernetes Service 的流量,即将该Pod从Service的endpoint中移除。

k8s 支持三种方式来执行探针:

    exec:在容器中执行一个命令,如果命令退出码返回 0 则表示探测成功,否则表示失败。

    tcpSocket:对指定的容器 IP 及端口执行一个 TCP 检查,如果端口是开放的则表示探测成功,否则表示失败。

    httpGet:对指定的容器 IP、端口及路径执行一个 HTTP Get 请求,如果返回的 状态码 在 [200,400) 之间则表示探测成功,否则表示失败。

2、httpGet

[root@k8s-master k8s]# cat nginx-probe-http.yaml 
apiVersion: v1
kind: Pod
metadata:
    labels:
      app: nginx-probe-http
    name: nginx-probe-http
spec:
  containers:
  - image: nginx:latest
    imagePullPolicy: Always
    name: nginx-probe-http-container
    livenessProbe:
      httpGet:
        path: /
        port: 80
      initialDelaySeconds: 15
      timeoutSeconds: 1
    readinessProbe:
      exec:
        command:
        - cat
        - /usr/share/nginx/html/index.html
      initialDelaySeconds: 5
      timeoutSeconds: 1

3、tcpSocket

[root@k8s-master k8s]# cat nginx-probe-tcp.yaml 
apiVersion: v1
kind: Pod
metadata:
    labels:
      app: nginx-probe-tcp
    name: nginx-probe-tcp
spec:
  containers:
  - image: nginx:latest
    imagePullPolicy: Always
    name: nginx-probe-tcp-container
    livenessProbe:
      tcpSocket:
        port: 80
      initialDelaySeconds: 15
      periodSeconds: 10
    readinessProbe:
      tcpSocket:
        port: 80
      initialDelaySeconds: 15
      periodSeconds: 10

四、Init 容器

1、Init 容器

    Pod 中可以有一个或多个先于应用容器启动的 Init 容器。

    Init 容器总是运行到成功完成为止。

    每个 Init 容器都必须在下一个 Init 容器启动之前成功完成。

    Init 容器能够提供了一种简单的阻塞或延迟应用容器的启动的方法,直到满足了一组先决条件。

    如果 Init 容器执行失败,Pod 设置的 restartPolicy 为 Never,则 pod 将处于 fail 状态。否则 Pod 将一直重新执行每一个 Init 容器直到所有的 Init 容器都成功。

    如果 Pod 异常退出,重新拉取 Pod 后,Init 容器也会被重新执行。所以在 Init 容器中执行的任务,需要保证是幂等的。

2、创建 nginx-pod-01

(1)编写yaml

nginx-pod-01 的 Init 容器是 nginx-pod-02

[root@k8s-master k8s]# cat nginx-pod-01.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod-01
  labels:
    app: nginx-pod-01
spec:
  containers:
  - name: nginx-container-01
    image: nginx:latest
  initContainers: 
  - name: nginx-pod-02
    image: nginx:latest
    command: ['sh', '-c', 'until nslookup init; do echo waiting for init; sleep 2; done;']

(2)创建pod

kubectl create -f nginx-pod-01.yaml 

(3)查看pod

正在等待Init容器完成

[root@k8s-master k8s]# kubectl get pod |grep  nginx-pod-01
nginx-pod-01       0/1     Init:0/1   0          5s

(4)kubectl describe pod nginx-pod-01

可以看出,先启动 nginx-pod-02,再启动 nginx-pod-01

Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  45s   default-scheduler  Successfully assigned default/nginx-pod-01 to k8s-node01
  Normal  Pulling    44s   kubelet            Pulling image "nginx:latest"
  Normal  Pulled     39s   kubelet            Successfully pulled image "nginx:latest" in 4.467651943s
  Normal  Created    39s   kubelet            Created container nginx-pod-02
  Normal  Started    39s   kubelet            Started container nginx-pod-02
  Normal  Pulling    38s   kubelet            Pulling image "nginx:latest"
  Normal  Pulled     33s   kubelet            Successfully pulled image "nginx:latest" in 4.493338108s
  Normal  Created    33s   kubelet            Created container nginx-container-01
  Normal  Started    33s   kubelet            Started container nginx-container-01

五、容器钩子

1、Hooks

    容器生命周期钩子(Container Lifecycle Hooks)监听容器生命周期的特定事件,并在事件发生时执行已注册的回调函数。

两种钩子:
    postStart: 容器创建后立即执行,注意由于是异步执行,它无法保证一定在ENTRYPOINT 之前运行。如果失败,容器会被杀死,并根据 RestartPolicy 决定是否重启。

    preStop:容器终止前执行,常用于资源清理。如果失败,容器同样也会被杀死。

回调函数方式:

    exec:在容器内执行命令,如果命令的退出状态码是 0 表示执行成功,否则表示失败。

    httpGet:向指定 URL 发起 GET 请求,如果返回的 HTTP 状态码在 [200, 400)之间表示请求成功,否则表示失败。

2、示例

[root@k8s-master k8s]# cat nginx-hooks-pod.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: nginx-hooks-pod
  labels:
    app: nginx-hooks-pod
spec:
  containers:
  - name: nginx-hooks-container
    image: nginx:latest
    lifecycle:
      postStart:
        httpGet:
          host: 10.244.85.197 
          path: /
          port: 80
      preStop:
        exec:
          command: ["/usr/sbin/nginx","-s","quit"]

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

_lrs

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

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

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

打赏作者

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

抵扣说明:

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

余额充值