k8s资源清单配置详解(持续更新)

文章目录

  • 一、Pod资源配置
    • 1、非Object配置
    • 2、spec.affinity
      • 2.1、spec.affinity.nodeAffinity:节点亲和性配置
      • 2.2、spec.affinity.podAffinity:pod亲和性配置
      • 2.3、spec.affinity.podAntiAffinity:pod反亲和性配置
    • 3、spec.containers:容器配置
      • 3.1、spec.containers.ports:容器端口号配置
      • 3.2、spec.containers.env:环境变量设置
      • 3.3、spec.containers.volumeMounts:容器内部存储卷挂载配置
      • 3.4、spec.containers.livenessProbe:存活状态检测
      • 3.5、spec.containers.readinessProbe:就绪状态检测
      • 3.6、spec.containers.resources:资源限制
      • 3.7、spec.containers.lifecycle
      • 3.8、spec.containers.volumeDevices
    • 4、spec.volumes:数据存储卷配置
      • 4.1、spec.volumes.emptyDir:使用emptyDir存储
      • 4.2、spec.volumes.hostPath:使用宿主机路径存储
      • 4.3、spec.volumes.nfs:使用nfs网络存储
      • 4.4、spec.volumes.configMap:使用configmap存储
      • 4.5、spec.volumes.secret:使用Secret存储
      • 4.6、spec.volumes.persistentVolumeClaim:使用pv存储
      • 4.7、spec.volumes.storageos
    • 5、spec.tolerations:污点容忍度配置
    • 6、spec.securityContext
  • 二、Deployment资源配置(deploy)
  • 三、StatefulSet资源配置(sts)
  • 三、DaemonSet资源配置(ds)
  • 四、pv资源配置
    • 1、标配
    • 2、spec.nfs
    • 3、spec.hostPath
    • 4、spec.storageos
  • 五、pvc资源配置
  • 六、Service资源配置(svc)
  • 六、Ingress资源配置(ing)
  • 七、ConfigMap资源配置(cm)
  • 八、Secret资源配置
  • 九、HorizontalPodAutoscaler资源配置(hpa)

一、Pod资源配置

1、非Object配置

apiVersion: v1
kind: Pod
metadata: 
spec:
  activeDeadlineSeconds: >
  automountServiceAccountToken: >
  dnsPolicy: > # 'ClusterFirst', 'Default' or 'None'
  enableServiceLinks: >
  hostIPC: >
  hostNetwork: > # 是否使用宿主机网路,类似于--network=host
  hostPID: >
  hostname: > # 设置主机名称,类似于--hostname
  nodeName: > #
  nodeSelector: [string]string> # 节点标签选择器
  priority: >
  priorityClassName: >
  restartPolicy: > # 重启策略:Always, OnFailure, Never
  runtimeClassName: >
  schedulerName: >
  serviceAccount: >
  serviceAccountName: >
  shareProcessNamespace: >
  subdomain: >
  terminationGracePeriodSeconds: >

2、spec.affinity

2.1、spec.affinity.nodeAffinity:节点亲和性配置

spec:
  affinity: >
    nodeAffinity: >
      requiredDuringSchedulingIgnoredDuringExecution: >
        nodeSelectorTerms: <[]Object> -required-
        - matchExpressions: <[]Object>
          - key: > -required-
            operator: > -required- # 设置键值关系(In, NotIn, Exists, DoesNotExist)
            values: <[]string>
            - value1
            - value2
          matchFields: <[]Object> # 通过节点字段
          - key: > -required-
            operator: > -required- # 设置键值关系(In, NotIn, Exists, DoesNotExist)
            values: <[]string>
            - value1
            - value2
      preferredDuringSchedulingIgnoredDuringExecution: <[]Object>
      - weight: > -required-
        preference: > -required-
          matchExpressions: <[]Object> # 通过节点标签
          - key: > -required-
            operator: > -required- # 设置键值关系(In, NotIn, Exists, DoesNotExist)
            values: <[]string>
            - value1
            - value2
          matchFields: <[]Object> # 通过节点字段
          - key: > -required-
            operator: > -required- # 设置键值关系(In, NotIn, Exists, DoesNotExist)
            values: <[]string>
            - value1
            - value2

2.2、spec.affinity.podAffinity:pod亲和性配置

spec:
  affinity: >
    podAffinity: >
      requiredDuringSchedulingIgnoredDuringExecution: <[]Object>
        namespaces: <[]string>
        topologyKey: > -required-
        labelSelector: >
          matchLabels: [string]string>
            key1: value1
            key2: value2
          matchExpressions:
            key: > -required-
            operator: > -required- # 设置键值关系(In, NotIn, Exists, DoesNotExist)
            values: <[]string>
            - value1
            - value2
      preferredDuringSchedulingIgnoredDuringExecution: <[]Object>
      - weight: > -required-
        podAffinityTerm: > -required-
          namespaces: <[]string>
          topologyKey: > -required-
          labelSelector: >
            matchLabels: [string]string>
              key1: value1
              key2: value2
            matchExpressions:
              key: > -required-
              operator: > -required- # 设置键值关系(In, NotIn, Exists, DoesNotExist)
              values: <[]string>
              - value1
              - value2

2.3、spec.affinity.podAntiAffinity:pod反亲和性配置

spec:
  affinity: >
    podAntiAffinity: >
      requiredDuringSchedulingIgnoredDuringExecution: <[]Object>
        namespaces: <[]string>
        topologyKey: > -required-
        labelSelector: >
          matchLabels: [string]string>
            key1: value1
            key2: value2
          matchExpressions:
            key: > -required-
            operator: > -required- # 设置键值关系(In, NotIn, Exists, DoesNotExist)
            values: <[]string>
            - value1
            - value2
      preferredDuringSchedulingIgnoredDuringExecution: <[]Object>
      - weight: > -required-
        podAffinityTerm: > -required-
          namespaces: <[]string>
          topologyKey: > -required-
          labelSelector: >
            matchLabels: [string]string>
              key1: value1
              key2: value2
            matchExpressions:
              key: > -required-
              operator: > -required- # 设置键值关系(In, NotIn, Exists, DoesNotExist)
              values: <[]string>
              - value1
              - value2

3、spec.containers:容器配置

spec:
  containers: <[]Object> # 容器配置
  - name: > -required- # 容器名、DNS_LABEL
    image: > # 镜像名称
    imagePullPolicy: > # 镜像拉取策略,Always、Never、IfNotPresent
    command: ["/bin/sh"] # 类似于dockerfile中的entrypiont
    args: ["-c", "while true; do echo hello; sleep 10;done"] # 类似于dockerfile中的cmd
    stdin: >
    stdinOnce: >
    terminationMessagePath: >
    terminationMessagePolicy: >
    tty: >
    workingDir: >

3.1、spec.containers.ports:容器端口号配置

spec:
  containers: <[]Object>
    ports: <[]Object> # 端口配置
    - name: > # 定义端口名
      containerPort: > -required- # 容器暴露的端口
      protocol: > # 端口协议,UDP, TCP, or SCTP
      hostIP: > # 指定要绑定的主机IP
      hostPort: > # 指定主机开放的端口

3.2、spec.containers.env:环境变量设置

spec:
  containers: <[]Object> 
    env: <[]Object> #环境变量配置
    - name: > -required- # 环境变量名称,如:MYSQL_ROOT_PASSWORD
      value: > # 设置环境变量的值
      valueFrom: > 
        configMapKeyRef: > # 指定从configmap的键名获取
          name: > # configmap名称
          key: > -required- # configmap资源清单下data字段下的键名
          optional: >
        fieldRef: >
          apiVersion: >
          fieldPath: > -required-
        resourceFieldRef: >
          containerName: >
          divisor: >
          resource: > -required-
        secretKeyRef: > # 指定从Secret的键名获取
          name: > # Secret名称
          key: > -required- # Secret资源清单下data字段下的键名
          optional: >
    envFrom: <[]Object>
    - configMapRef: > # 指定从configmap资源获取
        name: > # 指定configmap名称
        optional: >
      secretRef: > # 指定从Secret资源获取
        name: > # 指定Secret名称
        optional: >
      prefix: >

3.3、spec.containers.volumeMounts:容器内部存储卷挂载配置

spec:
  containers: <[]Object> 
    volumeMounts: <[]Object> # 容器挂载卷配置
    - name: > -required- # 设置挂载卷名称
      mountPath: > -required- # 设置挂载存储卷的路径
      readOnly: > # 是否只读,默认为false
      mountPropagation: >
      subPath: >
      subPathExpr: >

3.4、spec.containers.livenessProbe:存活状态检测

spec:
  containers: <[]Object> 
    livenessProbe: > # 存活探针
      exec: >
        command: <[]string>
      httpGet: >
        port: > -required- 
        path: >
        host: >
        httpHeaders: <[]Object>
          name: > -required-
          value: > -required-
        scheme: > 
      tcpSocket: > # TCPSocket指定涉及TCP端口的操作
        port: > -required- # 容器暴露的端口
        host: > # 默认pod的IP
      initialDelaySeconds: > # 设置多少秒后开始探测
      failureThreshold: > # 设置连续探测多少次失败后,标记为失败,默认三次
      successThreshold: > # 设置失败后探测的最小连续成功次数,默认为1
      timeoutSeconds: > # 设置探测超时的秒数,默认1s
      periodSeconds: > # 设置执行探测的频率(以秒为单位),默认1s

3.5、spec.containers.readinessProbe:就绪状态检测

spec:
  containers: <[]Object> 
    readinessProbe: > # 就绪探针
      exec: >
        command: <[]string>
      httpGet: >
        port: > -required- 
        path: >
        host: >
        httpHeaders: <[]Object>
          name: > -required-
          value: > -required-
        scheme: > 
      tcpSocket: > # TCPSocket指定涉及TCP端口的操作
        port: > -required- # 容器暴露的端口
        host: > # 默认pod的IP
      initialDelaySeconds: > # 设置多少秒后开始探测
      failureThreshold: > # 设置连续探测多少次失败后,标记为失败,默认三次
      successThreshold: > # 设置失败后探测的最小连续成功次数,默认为1
      timeoutSeconds: > # 设置探测超时的秒数,默认1s
      periodSeconds: > # 设置执行探测的频率(以秒为单位),默认1s

3.6、spec.containers.resources:资源限制

spec: 
  containers: <[]Object> 
    resources: > # 资源配置
      requests: [string]string> # 要求分配的最小资源
        memory: "1024Mi" # Mi,Gi
        cpu: "500m" # 500m代表0.5CPU
      limits: [string]string> # 能够分配的最大资源
        memory: 
        cpu: 

3.7、spec.containers.lifecycle

spec:
  containers: <[]Object> 
    lifecycle: >
      postStart: >
        exec: >
          command: <[]string>
        httpGet: >
          port: > -required- 
          path: >
          host: >
          httpHeaders: <[]Object>
            name: > -required-
            value: > -required-
          scheme: >
        tcpSocket: >
          port: > -required- # 容器暴露的端口
          host: > # 默认pod的IP
      preStop: >
        exec: >
          command: <[]string>
        httpGet: >
          port: > -required- 
          path: >
          host: >
          httpHeaders: <[]Object>
            name: > -required-
            value: > -required-
          scheme: >
        tcpSocket: >
          port: > -required- # 容器暴露的端口
          host: > # 默认pod的IP

3.8、spec.containers.volumeDevices

spec:
  containers: <[]Object>
    volumeDevices: <[]Object>
    - name: > -required-
      devicePath: > -required-

4、spec.volumes:数据存储卷配置

4.1、spec.volumes.emptyDir:使用emptyDir存储

spec:
  volumes: <[]Object> #数据卷配置
  - name: > -required- #设置卷名称,与volumeMounts名称对应
    emptyDir: >
      medium: >
      sizeLimit: >

4.2、spec.volumes.hostPath:使用宿主机路径存储

spec:
  volumes: <[]Object> #数据卷配置
  - name: > -required- #设置卷名称,与volumeMounts名称对应
    hostPath: > #设置挂载宿主机路径
      path: > -required- 
      type: > #类型:DirectoryOrCreate、Directory、FileOrCreate、File、Socket、CharDevice、BlockDevice

4.3、spec.volumes.nfs:使用nfs网络存储

spec:
  volumes: <[]Object> #数据卷配置
  - name: > -required- #设置卷名称,与volumeMounts名称对应
    nfs: > #设置NFS服务器
      server: > -required- #设置NFS服务器地址
      path: > -required- #设置NFS服务器路径(该路径必须存在)
      readOnly: > #设置是否只读

4.4、spec.volumes.configMap:使用configmap存储

spec:
  volumes: <[]Object> #数据卷配置
  - name: > -required- #设置卷名称,与volumeMounts名称对应
    configMap: >
      name: > #configmap名称
      defaultMode: > #权限设置0~0777,默认0664
      optional: > #指定是否必须定义configmap或其keys
      items: <[]Object>
      - key: > -required-
        path: > -required-
        mode: >

4.5、spec.volumes.secret:使用Secret存储

spec:
  volumes: <[]Object> #数据卷配置
  - name: > -required- #设置卷名称,与volumeMounts名称对应
    secret: >
      secretName: >
      defaultMode: > #权限设置0~0777,默认0664
      optional: > #指定是否必须定义configmap或其keys

4.6、spec.volumes.persistentVolumeClaim:使用pv存储

spec:
  volumes: <[]Object> #数据卷配置
  - name: > -required- #设置卷名称,与volumeMounts名称对应
    persistentVolumeClaim: >
      claimName: > -required- #pvc名称
      readOnly: >

4.7、spec.volumes.storageos

spec:
  volumes: <[]Object> #数据卷配置
  - name: > -required- #设置卷名称,与volumeMounts名称对应
    storageos: >
      volumeName: >liangzhuyicong
      volumeNamespace: >
      fsType: >
      secretRef: >
        name: >   

5、spec.tolerations:污点容忍度配置

spec:
  tolerations: <[]Object>
    effect: > # NoSchedule, PreferNoSchedule, NoExecute
    key: >
    value: >
    operator: > # Exists, Equal
    tolerationSeconds: >

6、spec.securityContext

spec:
  securityContext: >
    fsGroup: >
    runAsGroup: >
    runAsNonRoot: >
    runAsUser: >
    supplementalGroups: <[]integer>
    seLinuxOptions: >
      level: >
      role: >
      type: >
      user: >
    sysctls: <[]Object>
      name: > -required-
      value: > -required-

二、Deployment资源配置(deploy)

apiVersion: extensions/v1beta1
kind: Deployment
metadata: 
spec:
  minReadySeconds: > # 设置pod准备就绪的最小秒数
  paused: > # 表示部署已暂停并且deploy控制器不会处理该部署
  progressDeadlineSeconds: >
  replicas: > # pod的副本数量
  revisionHistoryLimit: > # 设置保留的历史版本个数,默认是10
  rollbackTo: >
    revision: > # 设置回滚的版本,设置为0则回滚到上一个版本
  selector: > # pod标签选择器,匹配pod标签,默认使用pods的标签
    matchLabels: [string]string> 
      key1: value1
      key2: value2
    matchExpressions: <[]Object>
      operator: > -required- #设定标签键与一组值的关系,In, NotIn, Exists and DoesNotExist
      key: > -required-
      values: <[]string> 
  strategy: > # 将现有pod替换为新pod的部署策略
    rollingUpdate: > # 滚动更新配置参数,仅当类型为RollingUpdate
      maxSurge: > # 滚动更新过程产生的最大pod数量,可以是个数,也可以是百分比
      maxUnavailable: > #
    type: > #部署类型,Recreate,RollingUpdate
  template: > -required- # 同pod配置

三、StatefulSet资源配置(sts)

apiVersion: apps/v1
kind: StatefulSet
metadata:
spec:
  podManagementPolicy: > # pod更新替换策略,OrderedReady(默认)、Parallel
  replicas: > # 副本数
  revisionHistoryLimit: > # 历史版本
  serviceName: > -required- # 设置headless服务名
  selector: > -required- # 标签选择器
    matchLabels: [string]string> 
      key1: value1
      key2: value2
    matchExpressions: <[]Object>
      operator: > -required- #设定标签键与一组值的关系,In, NotIn, Exists and DoesNotExist
      key: > -required-
      values: <[]string> 
  template: > -required- # 同pod配置
  updateStrategy: >
    rollingUpdate: >
      partition: > # 默认为0
    type: > # 默认RollingUpdate
  volumeClaimTemplates: <[]Object>
  - apiVersion: >
    kind: >
    metadata: >
    spec: >
      accessModes: <[]string>
      resources: > # 参考链接:https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/
        limits: [string]string>
        requests: [string]string>
          storage: 5Gi
      dataSource: >
        apiGroup: >
        kind: > -required-
        name: > -required-
      selector: >
        matchLabels: [string]string> 
          key1: value1
          key2: value2
        matchExpressions: <[]Object>
          operator: > -required- #设定标签键与一组值的关系,In, NotIn, Exists and DoesNotExist
          key: > -required-
          values: <[]string>
      storageClassName: >
      volumeMode: >
      volumeName: > 

三、DaemonSet资源配置(ds)

apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
spec:
  minReadySeconds: >
  revisionHistoryLimit: >
  selector: >
  template: > -required-
  templateGeneration: > #已过时
  updateStrategy: >

四、pv资源配置

1、标配

apiVersion: v1
kind: PersistentVolume
metadata: 
spec:
  accessModes: <[]string>
  persistentVolumeReclaimPolicy: >
  storageClassName: >
  capacity: [string]string>
    storage: 5Gi #容量设置

2、spec.nfs

spec:
  nfs: >
    server: > -required- # nfs服务器地址
    path: > -required- # nfs服务器存储路径设置
    readOnly: > # 是否只读

3、spec.hostPath

spec:
  hostPath: >
    path: > -required-
    type: > # 参考链接:https://kubernetes.io/docs/concepts/storage/volumes/#hostpath

4、spec.storageos

spec:
  storageos: >
    fsType: > # "ext4", "xfs", "ntfs",未指定默认为ext4
    readOnly: >
    volumeName: >
    volumeNamespace: >
    secretRef: >
      apiVersion: >
      kind: >
      name: >
      namespace: >
      fieldPath: >
      resourceVersion: >
      uid: >

五、pvc资源配置

apiVersion: v1
kind: PersistentVolumeClaim
metadata: 
spec:
  accessModes: <[]string>
  storageClassName: >
  volumeMode: >
  volumeName: >
  resources: > # 参考链接:https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/
    limits: [string]string>
    requests: [string]string>
      storage: 5Gi
  dataSource: >
    apiGroup: >
    kind: > -required-
    name: > -required-
  selector: >
    matchLabels: [string]string> 
      key1: value1
      key2: value2
    matchExpressions: <[]Object>
      operator: > -required- #设定标签键与一组值的关系,In, NotIn, Exists and DoesNotExist
      key: > -required-
      values: <[]string> 

六、Service资源配置(svc)

apiVersion: v1
kind: Service
metadata:
spec:
  clusterIP: >
  externalIPs: <[]string>
  externalName: >
  externalTrafficPolicy: >
  healthCheckNodePort: >
  loadBalancerIP: >
  loadBalancerSourceRanges: <[]string>
  ports: <[]Object>
  publishNotReadyAddresses: >
  selector: [string]string>
    key: value
  sessionAffinity: >
  sessionAffinityConfig: >
    clientIP: >
      timeoutSeconds: >
  type: > # ExternalName, ClusterIP, NodePort, LoadBalancer

六、Ingress资源配置(ing)

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
spec:
  backend: >
    serviceName: > -required-
    servicePort: > -required-
  rules: <[]Object>
    host: >
    http: >
      paths: <[]Object> -required-
        backend: > -required-
          serviceName: > -required-
          servicePort: > -required-
        path: >
  tls: <[]Object>
    hosts: <[]string>
    secretName: >

七、ConfigMap资源配置(cm)

apiVersion: v1
kind: ConfigMap
metadata:
binaryData: [string]string>
data: [string]string>

八、Secret资源配置

apiVersion: v1
kind: Secret
metadata:
data: [string]string>
stringData: [string]string>
type: >

九、HorizontalPodAutoscaler资源配置(hpa)

kubectl autoscale (-f FILENAME | TYPE NAME | TYPE/NAME) [--min=MINPODS] --max=MAXPODS [--cpu-percent=CPU] [options]

spec:
  maxReplicas: > -required- # 最大副本数
  minReplicas: > # 最小副本数,默认为1
  targetCPUUtilizationPercentage: > # 
  scaleTargetRef: > -required- #设置指定资源类型
    apiVersion: >
    kind: > -required-
    name: > -required-

你可能感兴趣的:(Kubernetes)