k8s学习笔记之五:Pod资源清单spec字段常用字段及含义

第一章、前言

  在上一篇博客中,我们大致简述了一般情况下资源清单的格式,以及如何获得清单配置的命令帮助,下面我们再讲解下清单中spec字段中比较常见的字段及其含义

第二章、常用字段讲解

spec.containers <[]object>

spec.containers <[]object>
    spec.containers.name <string>                      #pod的名称,必须字段,名称唯一且对象创建后不可以被修改
    spec.containers.image <string>                    #镜像仓库的路径/镜像的名称:镜像的标签
    spec.containers.image.imagePullPolicy  <string>        #镜像的下载策略。有三种:Always(总是去仓库下载) ,Never(从不去仓库下载) , IfNotPresent(如果本地没有就去仓库下载)           
        默认是"IfNotPresent"  但是,如果镜像的标签是latest,则总会是"Always,并且对象一旦被创建,这个字段不允许被改变
    spec.containers.ports:        #容器公开的端口列表。在这里公开端口可以为系统提供关于容器使用的网络连接的额外信息,但主要是提供信息。在这里不指定端口不会阻止该端口被公开。任何监听容器内默认的“0.0.0.0”地址的端口都可以从网络访问
        spec.containers.ports.containerPort <integer> -required-    #pod暴露的端口,此端口仅是额外的信息,对端口是否被暴露没有影响
        spec.containers.ports.hostPort <integer>   #主机上公开的端口
        spec.containers.ports.protocol <string>    #端口的协议
        spec.containers.ports.hostIP   <string>    #指定要绑定的主机
    spec.containers.command <[]string> #运行的程序,类似于docker中的entrypiont,并且这里的命令不会运行在shell中,如果没有这个字段docker镜像会运行自己entrypiont中的指令
    spec.containers.args <[]string>  #向docker镜像中传递参数 如果定义了这个字段,docker镜像中cmd命令不会被执行,如果引用变量使用$(VAR_NAME)格式引用,如果想使用命令引用的的方式,需要使用$$(VAR_NAME)方式来引用 
#关于args和command的官方文档链接:https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/ 
pod.spec.containers.volumeMounts
  pod.spec.containers.volumeMounts.name
  pod.spec.containers.volumeMounts.mountPath #可以被容器挂载的存储卷的路径,路径不能包含':' 符号
  pod.spec.containers.volumeMounts.subPath #可以被容器挂载的存储卷的路径,并且不会覆盖挂载点中的文件
  pod.spec.containers.volumeMounts.readOnly #是否只读,默认为false
pod.spec.containers.resources
  spec.containers.resources.limits #资源限制
    spec.containers.resources.limits.cpu : CPU 上限, 可以短暂超过, 容器也不会被停止
    spec.containers.resources.limits.memory : 内存上限, 不可以超过; 如果超过, 容器可能会被终止或调度到其他资源充足的机器上
  spec.containers.resources.requests #资源需求
    spec.containers.resources.requests.cpu : CPU 请求, 也是调度 CPU 资源的依据, 可以超过
    spec.containers.resources.requests.memory : 内存请求, 也是调度内存资源的依据, 可以超过; 但如果超过, 容器可能会在 Node 内存不足时清理

spec.nodeSelector <map[string]string>

pod.spec.nodeSelector: #指定对象的调度节点,节点必须存在

pod.spec.restartPolicy <string>

pod.spec.restartPolicy:#容器的重启策略。有三种Always(只有退出就重启),OnFailure(失败退出时不重启),Never(只要退出就不重启),kubelet重新启动的已退出容器将以指数退避延迟(10秒,20秒,40秒......)重新启动,上限为五分钟,并在成功执行十分钟后重置

 第三章、简单示例

1.常见pod清单(使用ConfigMap传递环境变量)

[root@k8s-master01 ~]#kubectl create configmap special-config --from literal=special.how=very --from-literal=sepcial.type=charm  #创建一个ConfigMap,ConfigMap是用来存储配置文件的kubernetes资源对象,获取命令帮助kubectl create configmap -h
[root@k8s-master01 ~]#kubectl create configmap env-config --from-literal=log_level=INFO
[root@k8s-master01 ~]#kubectl get configmap special-config -o go-template='{{.data}}'   #查看ConfigMap的内容
[root@k8s-master01 ~]#kubectl get configmap env-config -o go-template='{{.data}}'
[root@k8s-master01 manifests]# kubectl label node k8s-node01 disk=ssd    #对集群中的一个节点打上标签
[root@k8s-master01 manifests]# cat test-pod.yaml #创建一个测试pod清单
apiVersion: v1
kind: Pod
metadata:
  name: test-pod
spec:
  containers:
  - name: test-container                #容器名称
    image: busybox                      #镜像路径
    command: ["/bin/sh", "-c", "env"]   #执行命令,需要注意command、args和container中的entrypiont中的命令和参数相互的关系
    env:                                #配置env
    - name: SPECIAL_LEVEL_KEY          #env名称
      valueFrom:                       #指定env的来源
        configMapKeyRef:               #表示从ConfMap中选择
          name: special-config         #ConfigMap的文件名称
          key: special.how             #ConfigMap中的key名称
    - name: SPECIAL_TYPE_KEY
      valueFrom:
        configMapKeyRef:
          name: special-config
          key: sepcial.type
    envFrom:                         #容器中填充环境变量的列表
    - configMapRef:              #指定ConfigMap文件
        name: env-config          #ConfigMap文件
  restartPolicy: Never            #定义重启策略
  nodeSelector:                  #node选择器
    disk: ssd

2.常见pod清单(使用secret传递环境变量)

[root@k8s-master01 manifests]# cat secrets.yaml 
apiVersion: v1
kind: Secret
metadata:
  name: mysecret
type: Opaque
data:
  password: MTIzNDU2Cg==        #这里的环境变量的值需要使用base64加密,例如:echo "admin"|base64[加密] , echo "YWRtaW4K"|base64 -d [解密]
  username: YWRtaW4K
[root@k8s-master01 manifests]# cat secrets-demo.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: secret-nginx
  labels:
    name: secret-nginx
spec:
  volumes:
  - name: secret-nginx
    secret:
      secretName: mysecret
  containers:
  - name: secret-nginx
    image: nginx:1.14.0-alpine
    volumeMounts:
    - name: secret-nginx
      mountPath: "/etc/secrets"
      readOnly: true
    ports:
    -  name: www
       containerPort: 8888
       hostPort: 8888

pod清单配置3(定义pod postStart或preStop,即容器创建后和容器中止前需要做的操作)

[root@k8s-master01 manifests]# cat poststart-pod.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: poststart-pod
spec:
  containers:
  - name: buxybox-httpd
    image: busybox
    imagePullPolicy: IfNotPresent
    lifecycle:
      postStart:
        exec:
          command: ["mkdir", "-p"," /data/web/html"]   #注意,此处command和下面的command不可以有强依赖关系,下面的command会在此处command先执行
    command: ["/bin/sh","-c","sleep 3600"]

 pod清单配置4(以exec方式对pod进行存活性探测)

[root@k8s-master01 manifests]# cat liveness-exec.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: liveness-exec-pod
spec:
  containers:
  - name: liveness-exec-pod
    image: busybox:latest
    imagePullPolicy: IfNotPresent
    command: ["/bin/sh","-c","touch /tmp/healthy;sleep 30;rm -rf /tmp/healthy;sleep 3600"]
    livenessProbe:
      exec:      #探测方式由三种,exec(执行命令探测) HttpGet(Http请求)  tcpSocket(TCP端口探测)
        command: ["test","-e","/tmp/healthy"]
      initialDelaySeconds: 2  #容器启动后多长时间开始探测
      periodSeconds: 5  #每次探测的间隔时间,单位为秒

pod清单配置5(以HttpGet方式对pod进行存货性探测)

[root@k8s-master01 manifests]# cat liveness-httpget.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: liveness-httpget-pod
spec:
  containers:
  - name: liveness-httpget-pod
    image: ikubernetes/myapp:v1
    imagePullPolicy: IfNotPresent
    livenessProbe:
      httpGet:
        port: 80
        path: /index.html
      initialDelaySeconds: 2
      periodSeconds: 5

pod清单配置6(使用httpget方式对pod进行就绪性探测)

[root@k8s-master01 manifests]# cat readiness-httpget.yaml  #与存活性探测相类似
apiVersion: v1
kind: Pod
metadata:
  name: readiness-httpget-pod
spec:
  containers:
  - name: readiness-httpget-pod
    image: ikubernetes/myapp:v1
    imagePullPolicy: IfNotPresent
    readinessProbe:  
      httpGet:
        port: 80
        path: /index.html
      initialDelaySeconds: 2
      periodSeconds: 5
原文地址:https://www.cnblogs.com/panwenbin-logs/p/9895953.html