深入解读核心资源Pod(二)

一、Pod 资源清单

Pod 资源清单详解

apiVersion: v1          # 版本号,例如v1
kind: Pod               # 资源类型,例如Pod
metadata:               # 元数据,包含name、namespace、labels等
  name: string             # Pod名称,例如nginx
  namespace: string        # Pod所属的命名空间,例如default
  labels:                  # 自定义标签,例如app: nginx
    - name: string            # 标签名称,例如app
  annotations:             # 自定义注解,例如author: xxx
    - name: string            # 注解名称,例如author
spec:                   # Pod的规格(容器的详细定义)
  containers:             # 容器列表,一个Pod可以有多个容器
    - name: string            # 容器名称,例如nginx
      image: string           # 容器使用的镜像,例如nginx:1.7.9
      imagePullPolicy: PullIfNotPresent  # 镜像拉取策略,例如IfNotPresent、Always、Never
      command: [string]       # 容器启动命令,例如["/bin/sh"]
      args: [string]          # 容器启动参数,例如["-c", "echo hello"]
      workingDir: string      # 容器工作目录,例如/var/www
      volumeMounts:           # 挂载到容器内部的存储卷配置
        - name: string            # 存储卷名称,例如www
          mountPath: string       # 存储卷挂载路径,例如/var/www
          readOnly: boolean       # 是否只读,例如false
      ports:                  # 容器监听的端口配置
        - name: string            # 端口名称,例如http
          containerPort: int      # 容器监听的端口号,例如80
          hostPort: int           # Pod监听的端口号,例如80
          protocol: string        # 端口使用的协议,例如TCP
      env:                    # 容器运行前需设置的环境变量
        - name: string            # 环境变量名称,例如VERSION
          value: string           # 环境变量的值,例如v1
      resources:              # 容器的资源限制和请求配置
        limits:                   # 资源限制的配置
          cpu: string              # CPU的限制,单位为core数
          memory: string           # 内存的限制,单位为bytes或者MiB或者GiB
        requests:                 # 资源请求的配置
          cpu: string              # CPU的请求,容器启动的初始可用数量
          memory: string           # 内存的请求,容器启动的初始可用内存
      livenessProbe:          # 对Pod内个容器健康检查的配置,检查方法:exec、httpGet、tcpSocket
        exec:                     # 对 Pod 容器内检查方式设置为 exec 方式
          command: [string]         # exec方式需要制定的命令或者脚本
        httpGet:                  # 对 Pod 容器内检查方式设置为 httpGet 方式
          path: string              # HTTP请求路径,例如/healthz
          port: int                 # HTTP请求端口,例如8080
          host: string              # HTTP请求HOST,例如example.com
          scheme: string            # HTTP请求scheme,例如HTTP
          httpHeaders:              # HTTP请求头
            - name: string            # HTTP请求头名称,例如X-Custom-Header
              value: string           # HTTP请求头的值,例如Awesome
        tcpSocket:                # 对 Pod 容器内检查方式设置为 tcpSocket 方式
          port: int                 # TCP请求端口,例如8080
        initialDelaySeconds: int  # 容器启动完成后首次探测的时间,单位为秒,默认为0
        timeoutSeconds: int       # 探测等待响应的超时时间,单位为秒,默认为1
        periodSeconds: int        # 探测之间的间隔时间,单位为秒,默认为10
        successThreshold: int     # 探测成功的连续次数,默认为1
        failureThreshold: int     # 探测失败的连续次数,默认为3
        securityContext:          # Pod 安全上下文配置
          privileged: boolean         # 是否特权模式,默认为false
      restartPolicy: string   # Pod内个容器的重启策略,Always、OnFailure、Never,默认为Always
        nodeSelector: object      # 节点选择器,用于指定Pod调度到的节点
        imagePullSecrets:         # 镜像拉取凭证,用于从私有仓库拉取镜像
          - name: string              # 镜像拉取凭证名称,例如regsecret
        hostNetwork: boolean      # 是否使用主机网络模式,默认为false(不使用)
        volumes:                  # 在该pod上定义共享存储卷列表
        - name: string                # 共享存储卷名称,例如www
          emptyDir: {}                # 类型为emptyDir的存储卷,与Pod同生命周期的临时存储
          hostPath:                   # 类型为hostPath的存储卷,表示挂载pod所在宿主机的目录
            path: string                 # pod所在宿主机的目录,将被用于存储卷的挂载
          secret:                     # 类型为secret的存储卷,用于将Secret以文件或目录的形式挂载到Pod中
            secretName: string           # 密文存储卷名称
            items:                       # 配置文件存储卷项列表
              - key: string                 # 配置文件存储卷项的key
                path: string                # 配置文件存储卷项的路径
          configMap:                  # 类型为configMap的存储卷,用于将ConfigMap以文件或目录的形式挂载到Pod中
            name: string                 # 配置文件存储卷名称
            items:                       # 配置文件存储卷项列表
              - key: string                 # 配置文件存储卷项的key
                path: string                # 配置文件存储卷项的路径

二、node节点选择器

创建pod资源时,pod 会根据 scheduler 的调度策略,选择一个合适的 node 节点运行。

如果希望 pod 能够运行在指定的 node 节点上,可以通过 nodeSelector 字段来指定 pod 调度到的 node 节点。

  • nodeSelector 字段的值是一个 map,key 是 node 节点的标签名称,value 是 node 节点的标签值。
  • nodeSelector 字段的值必须和 node 节点的标签匹配,才能将 pod 调度到该 node 节点上。

1、nodeName

nodeName 字段用于指定 pod 调度到的 node 节点的名称,该字段的值必须是 node 节点的名称,而不是 node 节点的标签。

上传tomcat.tar.gz文件和busybox.tar.gz文件到node节点的/root目录下,使用ctr命令加载tomcat.tar.gz文件到容器镜像仓库,使用ctr命令加载busybox.tar.gz文件到容器镜像仓库,查看容器镜像仓库中的镜像。

# 上传 tomcat.tar.gz文件和busybox.tar.gz文件到node节点的/root目录下
[root@node ~]# ls
anaconda-ks.cfg  busybox.tar.gz  install.sh  original-ks.cfg  tomcat.tar.gz  xianchao-nginx.tar.gz  xianchao-tomcat.tar.gz

# 使用ctr命令加载tomcat.tar.gz文件到容器镜像仓库
[root@node ~]# ctr -n k8s.io images import tomcat.tar.gz
unpacking docker.io/library/tomcat:8.5-jre8-alpine (sha256:463a0b1de051bff2208f81a86bdf4e7004eb68c0edfcc658f2e2f367aab5e342)...done

# 使用ctr命令加载busybox.tar.gz文件到容器镜像仓库
[root@node ~]# ctr -n k8s.io images import busybox.tar.gz
unpacking docker.io/library/busybox:latest (sha256:2d86744fc4e303fbf4e71c67b89ee77cc6c60e9315cbd2c27f50e85b2d866450)...done

# 查看容器镜像仓库中的镜像
[root@node ~]# ctr -n k8s.io images ls | grep -E "tomcat|busybox"
docker.io/library/busybox:latest                                                                                           application/vnd.docker.distribution.manifest.v2+json      sha256:2d86744fc4e303fbf4e71c67b89ee77cc6c60e9315cbd2c27f50e85b2d866450 1.4 MiB   linux/amd64                                                                                             io.cri-containerd.image=managed 
docker.io/library/tomcat:8.5-jre8-alpine                                                                                   application/vnd.docker.distribution.manifest.v2+json      sha256:463a0b1de051bff2208f81a86bdf4e7004eb68c0edfcc658f2e2f367aab5e342 105.1 MiB linux/amd64                                                                                             io.cri-containerd.image=managed

创建 pod-node.yaml 文件,指定 pod 调度到 node 节点 node 上。

apiVersion: v1
kind: Pod
metadata:
  name: demo-pod
  namespace: default
  labels:
    app: demo
    env: dev
spec:
  nodeName: node
  containers:
  - name: tomcat-pod-java
    image: tomcat:8.5-jre8-alpine
    imagePullPolicy: IfNotPresent
  - name: busybox
    image: busybox:latest
    command: 
    - "/bin/sh"
    - "-c"
    - "sleep 3600"

启动并查看 pod 资源。

# 创建 pod 资源
[root@master ~]# kubectl apply -f pod-node.yaml
pod/demo-pod created
# 查看 pod 资源调度到的 node 节点
[root@master tomcat-test]# kubectl get pod -o wide -n default
NAME         READY   STATUS    RESTARTS   AGE     IP            NODE   NOMINATED NODE   READINESS GATES
demo-pod     2/2     Running   0          12m     10.244.1.29   node   <none>           <none>

2、nodeSelector

nodeSelector 字段用于指定 pod 调度到的 node 节点的标签,该字段的值是一个 map,key 是 node 节点的标签名称,value 是 node 节点的标签值。

# 给 node 节点打标签,打个具有 disk=ceph 的标签
[root@node ~]# kubectl label nodes node disk=ceph
node/node labeled

# 查看 node 节点的标签
[root@node ~]# kubectl get node --show-labels

# 定义 pod 资源,指定 pod 调度到具有 disk=ceph 标签的 node 节点上
[root@master ~]# cat pod-1.yaml
apiVersion: v1
kind: Pod
metadata:
  name: demo-pod-1
  namespace: default
  labels:
    app: myapp
    env: dev
spec:
  nodeSelector:
    disk: ceph
  containers:
  - name: tomcat-pod-java
    ports:
    - containerPort: 8081
    image: tomcat:8.5-jre8-alpine
    imagePullPolicy: IfNotPresent

# 创建 pod 资源
[root@master ~]# kubectl apply -f pod-1.yaml
pod/demo-pod-1 created

# 查看 pod 资源调度到的 node 节点
[root@master ~]# kubectl get pods -o wide -n default
NAME         READY   STATUS    RESTARTS   AGE     IP            NODE   NOMINATED NODE   READINESS GATES
demo-pod     2/2     Running   0          12m     10.244.1.29   node   <none>           <none>
demo-pod-1   1/1     Running   0          2m32s   10.244.1.30   node   <none>           <none>

# 修改命名空间为default
[root@master ~]# kubectl config set-context --current --namespace=default
Context "kubernetes-admin@kubernetes" modified.

三、污点和容忍度

污点是一种标记,用于标记 node 节点的特殊属性,例如:node 节点的 CPU、内存、磁盘等资源不足,或者 node 节点上运行了一些系统级的服务,这些服务占用了大量的 CPU、内存、磁盘等资源,这些都可以称之为污点。

污点是 node 节点的属性,可以通过 kubectl taint nodes 命令查看 node 节点的污点信息,也可以通过 kubectl taint nodes 命令给 node 节点打上污点。

容忍度是 pod 资源的属性,可以通过 kubectl describe pod 命令查看 pod 资源的容忍度信息,也可以通过 kubectl edit pod 命令给 pod 资源打上容忍度。

1、node节点亲和性

node节点亲和性是一种特殊的亲和性,用于指定 pod 资源调度到具有某些污点的 node 节点上。

nodeAffinity: 用于指定 pod 资源调度到具有某些污点的 node 节点上。

# 查看 pod 资源的亲和性
[root@master tomcat-test]# kubectl explain pods.spec.affinity
KIND:     Pod
VERSION:  v1

RESOURCE: affinity <Object>

DESCRIPTION:
     If specified, the pods scheduling constraints

     Affinity is a group of affinity scheduling rules.

FIELDS:
   nodeAffinity	<Object>
     Describes node affinity scheduling rules for the pod.

   podAffinity	<Object>
     Describes pod affinity scheduling rules (e.g. co-locate this pod in the
     same node, zone, etc. as some other pod(s)).

   podAntiAffinity	<Object>
     Describes pod anti-affinity scheduling rules (e.g. avoid putting this pod
     in the same node, zone, etc. as some other pod(s)).

# 查看 pod 资源的 节点亲和性
[root@master tomcat-test]# kubectl explain pods.spec.affinity.nodeAffinity
KIND:     Pod
VERSION:  v1
RESOURCE: nodeAffinity <Object>

DESCRIPTION:
     Describes node affinity scheduling rules for the pod.

     Node affinity is a group of node affinity scheduling rules.

FIELDS:
   preferredDuringSchedulingIgnoredDuringExecution	<[]Object>
     The scheduler will prefer to schedule pods to nodes that satisfy the
     affinity expressions specified by this field, but it may choose a node that
     violates one or more of the expressions. The node that is most preferred is
     the one with the greatest sum of weights, i.e. for each node that meets all
     of the scheduling requirements (resource request, requiredDuringScheduling
     affinity expressions, etc.), compute a sum by iterating through the
     elements of this field and adding "weight" to the sum if the node matches
     the corresponding matchExpressions; the node(s) with the highest sum are
     the most preferred.

   requiredDuringSchedulingIgnoredDuringExecution	<Object>
     If the affinity requirements specified by this field are not met at
     scheduling time, the pod will not be scheduled onto the node. If the
     affinity requirements specified by this field cease to be met at some point
     during pod execution (e.g. due to an update), the system may or may not try
     to eventually evict the pod from its node.

2、node节点亲和性示例

案例1:使用 requiredDuringSchedulingIgnoredDuringExecution 硬亲和性

# 上传myapp-v1.tar.gz到node节点
[root@node ~]# ls
anaconda-ks.cfg   myapp-v1.tar.gz

# ctr上传镜像到node节点
[root@node ~]# ctr image import myapp-v1.tar.gz
unpacking docker.io/ikubernetes/myapp:v1 (sha256:943e72fda89669b10a29723a10f261836dca71aa9b181d362020bb3b715bc9a1)...done

# 查看镜像
[root@node ~]# ctr images ls | grep myapp
docker.io/ikubernetes/myapp:v1 application/vnd.docker.distribution.manifest.v2+json sha256:943e72fda89669b10a29723a10f261836dca71aa9b181d362020bb3b715bc9a1 15.2 MiB linux/amd64 - 
posted @ 2023-07-06 15:37  休耕  阅读(33)  评论(0编辑  收藏  举报