• Kubernetes(k8s)的Pod资源清单spec.containers属性详细讲解


    1. 概述

    通过命令kubectl explain pod.spec.containers查询,将重要结果整理如下:

    containers:                       # 数组,代表可以有多个容器:
      - name: <string>                # 容器名称
        image: <string>               # 容器需要的镜像地址
        imagePullPolicy: <string>     # 镜像拉取策略 
        command: <[]string>           # 容器的启动命令列表,默认使用打包时使用的启动命令
        args: <[]string>              # 容器的启动命令需要的参数列表 
        env: <[]Object>               # 容器环境变量的配置
        ports: <[]Object>             # 容器需要暴露的端口号列表
        resources: <Object>           # 资源限制和资源请求的设置
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2. Pod的基本配置

    新建pod-containers.yaml,内容如下:

    [root@k8s-master ~]# cat pod-containers.yaml
    apiVersion: v1
    kind: Pod
    metadata:
      name: pod-containers
      namespace: dev
      labels:
        user: bulut
    spec:
      containers:
        - name: nginx-container
          image: nginx:latest
    [root@k8s-master ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    3. 镜像拉取策略

    imagePullPolicy:用于设置镜像拉取的策略,有三种拉取策略:

    • Always:总是从远程仓库拉取镜像到本地
    • IfNotPresent:本地有则使用本地镜像,本地没有则从远程仓库拉取镜像到本地
    • Never:只使用本地镜像,本地没有就报错

    默认值说明:

    • 如果镜像tag为latest,默认策略是Always
    • 如果镜像tag为具体的版本号,默认策略是IfNotPresent

    新建pod-containers.yaml,内容如下:

    [root@k8s-master ~]# cat pod-containers.yaml
    apiVersion: v1
    kind: Pod
    metadata:
      name: pod-containers
      namespace: dev
      labels:
        user: bulut
    spec:
      containers:
        - name: nginx-container 
          image: nginx:latest
          imagePullPolicy: Always
    [root@k8s-master ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    4. 启动命令

    busybox不是一个程序,是一个工具类的集合。其容器启动后,由于没有前台的程序一直运行,会自动关闭。所以需要我们通过command参数传递命令让它一直运行

    新建pod-containers.yaml,内容如下。然后进行pod创建

    [root@k8s-master ~]# cat pod-containers.yaml 
    apiVersion: v1
    kind: Pod
    metadata:
      name: pod-containers
      namespace: dev
      labels:
        user: bulut
    spec:
      containers:
        - name: busybox-container
          image: busybox
          command: ["/bin/sh", "-c", "while true;do echo $(date +%T) >> /tmp/time.txt;sleep 10;done;"]
    [root@k8s-master ~]# 
    [root@k8s-master ~]# kubectl apply -f pod-containers.yaml 
    pod/pod-containers created
    [root@k8s-master ~]#
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    进入pod中的容器,查看结果

    [root@k8s-master ~]# kubectl exec pod-containers -c busybox-container -it -n dev -- /bin/sh 
    / # cat /tmp/time.txt 
    15:19:16
    15:19:26
    15:19:36
    / # 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    Kubernetes中的command和args两个参数覆盖Dockerfile中的ENTRYPOINT说明:

    • 如果command和args均没写,那么用Dockerfile的配置
    • 如果command写了,但是args没写,那么Dockerfile默认的配置会被忽略,执行command
    • 如果command没写,但是args写了,那么Dockerfile中配置的ENTRYPOINT命令会被执行,使用当前args作为参数
    • 如果command和args都写了,那么Dockerfile中的配置会被忽略,执行command并追加上args参数

    5. 环境变量

    新建pod-containers.yaml,内容如下。然后进行pod创建

    [root@k8s-master ~]# cat pod-containers.yaml 
    apiVersion: v1
    kind: Pod
    metadata:
      name: pod-containers
      namespace: dev
      labels:
        user: bulut
    spec:
      containers:
        - name: nginx-container
          image: nginx:latest
          env:
            - name: myKey
              value: myValue
    [root@k8s-master ~]# 
    [root@k8s-master ~]# kubectl apply -f pod-containers.yaml 
    pod/pod-containers created
    [root@k8s-master ~]#
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    进入pod中的容器,查看结果

    [root@k8s-master ~]# kubectl exec pod-containers -c nginx-container -it -n dev -- /bin/sh 
    # echo $myKey
    myValue
    #
    
    • 1
    • 2
    • 3
    • 4

    6. 端口设置

    通过kubectl explain pod.spec.containers.ports命令查看ports的子属性,整理如下:

    ports: 
      - name: <string>             # 端口名称,如果指定,必须保证name在pod中是唯一的
        containerPort: <integer>   # 容器要监听的端口(0 < 端口范围 < 65536)
        hostPort: <integer>        # 容器要在主机上绑定的端口,如果设置,主机上只能运行容器的一个副本(一般省略)
        hostIP: <string>           # 要将外部端口绑定到的主机IP(一般省略)
        protocol: <string>         # 端口协议。必须是UDP、TCP或SCTP。默认为“TCP”
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    新建pod-containers.yaml,内容如下。然后进行pod创建

    [root@k8s-master ~]# cat pod-containers.yaml 
    apiVersion: v1
    kind: Pod
    metadata:
      name: pod-containers
      namespace: dev
      labels:
        user: bulut
    spec:
      containers:
        - name: nginx-container
          image: nginx:latest
          ports:
          - name: nginx-port
            containerPort: 80
            protocol: TCP
    [root@k8s-master ~]#
    [root@k8s-master ~]# kubectl apply -f pod-containers.yaml 
    pod/pod-containers created
    [root@k8s-master ~]#
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    访问nginx服务

    [root@k8s-master ~]# kubectl get pod -n dev -o wide
    NAME             READY   STATUS    RESTARTS   AGE   IP               NODE        NOMINATED NODE   READINESS GATES
    pod-containers   1/1     Running   0          70s   10.244.169.147   k8s-node2   <none>           <none>
    [root@k8s-master ~]# 
    [root@k8s-master ~]# curl 10.244.169.147: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>
    <p>If you see this page, the nginx web server is successfully installed and
    working. Further configuration is required.</p>
    
    <p>For online documentation and support please refer to
    <a href="http://nginx.org/">nginx.org</a>.<br/>
    Commercial support is available at
    <a href="http://nginx.com/">nginx.com</a>.</p>
    
    <p><em>Thank you for using nginx.</em></p>
    </body>
    </html>
    [root@k8s-master ~]#
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    7. 资源配额

    容器中的程序运行会占用一定的资源,比如CPU和内存等,如果不对某个容器的资源做限制,那么它就可能吃掉大量的资源,导致其他的容器无法运行

    kubernetes提供了对内存和CPU的资源进行配额的机制,这种机制主要通过resources属性实现,它有两个子属性:

    • limits:用于限制容器的最大占用资源,当容器占用资源超过limits时会被终止,并进行重启
    • requests:用于设置容器需要的最小资源,如果环境资源不够,容器将无法启动

    其中cpu指定的是core数,可以为整数或小数。memory指定的是内存大小,可以使用Gi、Mi、G、M等格式

    新建pod-containers.yaml,内容如下:

    [root@k8s-master ~]# cat pod-containers.yaml 
    apiVersion: v1
    kind: Pod
    metadata:
      name: pod-containers
      namespace: dev
      labels:
        user: bulut
    spec:
      containers:
        - name: nginx-container
          image: nginx:latest
          resources:
            limits:
              cpu: "2"
              memory: "10Gi"
            requests:
              cpu: "1"
              memory: "10Mi"
    [root@k8s-master ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
  • 相关阅读:
    《算法系列》之队列与堆
    NFT:Meta 将 NFT 整合到 Ins, 那么这之后又会是什么?
    检测域名是否支持http2.0协议脚本
    shell 实现对Hive表字段脱敏写入新表
    倍福触摸屏维修倍福显示屏维修CP3916-0000故障概述
    单片机电子元器件-按键
    Vue53-Todo-list案例
    Docker基础(CentOS 7)
    快速排序和归并排序的非递归形式
    如何进行iOS技术博客的备案?
  • 原文地址:https://blog.csdn.net/yy8623977/article/details/124789913