这个比较全,可以参考

=================

https://www.addops.cn/post/kubernetes-deployment-fileds.html

=================

最近在看 kubernetes deployment 部分,按照其文档中的例子进行了一些增删改查DP(deployment 简称DP)的操作,感觉还是很有意思的官方文档。不过,其参考例子都比较简单,要是在生产环境中使用时肯定是不够的,那么问题来了: DP到底支持哪些键值呢?

我花了点时间对DP支持的键值对进行了整理(当然能力有限,不一定非常全面),希望能对 kubernetes 初学者有所帮助。

Deloyment example yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx-deployment2
  namespace: hmm-test
  labels:
    app: nginx2
    zone: us-est-coast
    cluster: test-cluster1
    rack: rack-22
  # label的扩展, 通常label是给selector用的,具有识别的目的。有时候我们也需要添加一些非识别目的的数据(用来API检索等用)。
  # annotations的数据没有label严格(长度、是都结构化等等),可以添加入:环境信息(build/release/image等)、code 信息(commit/repo)、user等等
  annotations:
    # 自定义字段
    build: two
    builder: john-doe
    # init container 也是放到了annotations中
    pod.beta.kubernetes.io/init-containers: '[
        {
            "name": "install",
            "image": "busybox",
            "command": ["wget", "-O", "/work-dir/index.html", "http://kubernetes.io"],
            "volumeMounts": [
                {
                    "name": "workdir",
                    "mountPath": "/work-dir"
                }
            ]
        },
        {
            "name": "init-myservice",
            "image": "busybox",
            "command": ["sh", "-c", "until nslookup myservice; do echo waiting for myservice; sleep 2; done;"]
        },
        {
            "name": "init-mydb",
            "image": "busybox",
            "command": ["sh", "-c", "until nslookup mydb; do echo waiting for mydb; sleep 2; done;"]
        }
    ]'
spec:
  hostPID: true
  hostIPC: true
  hostNetwork: true
  replicas: 1
  selector:
    matchLabels:
      app: nginx2
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx2
    spec:
      imagePullSecrets:
        - name: regsecret
      # 调度到满足disktype=ssd的label的一组node上
      nodeSelector:
        disktype: ssd
      # 通过nodeName创建到1台node上
      nodeName: "node01.test.k8s.qihoo.net"
      # 还支持亲和性/反亲和性 与nodeSelector结合的还有一个亲和性和反亲和性的新特性(https://kubernetes.io/docs/user-guide/node-selection/),目前处于alpha阶段(支持类似正则表达式的联合判断)
      containers:
      - image: r.addops.cn/public/nginx:1.6.3
        imagePullPolicy: IfNotPresent
        name: nginx21

        #The command and arguments that you define in the configuration file override the default command and arguments provided by the container image. If you define args, but do not define a command, the default command is used with your new arguments.
        command: ["printenv"]
        args: ["HOSTNAME", "KUBERNETES_PORT"]

        # 资源分配
        resources:
          requests:
            memory: "64Mi"
            cpu: "250m"
          limits:
            memory: "128Mi"
            cpu: "1"
        workingDir: /data/nginx
        # 容器内部权限控制
        # https://kubernetes.io/docs/concepts/policy/container-capabilities/
        securityContext:
          privileged: true
          capabilities:
            add:
            - SYS_NICE
            drop:
            - KILL
        # 这个主要是用来调试程序用的(在开发测试环境的DP中可使用,线上就别用了)
        terminationMessagePath: /dev/termination-log
        ports:
          containerPort: 80
          containerPort: 443
        volumeMounts:
            # POD中的每个container可以设置自己的内部挂载点
            # 每个挂载点通过下面volumes中的name来指定把哪个volume挂载到哪里
          - name: redis-storage
            mountPath: /data/redis
            readOnly: true
          - name: hostpath-storage
            mountPath: /data/test
        env:
        - name: DEMO_GREETING
          value: "Hello from the environment"

        dnsPolicy: ClusterFirst
        restartPolicy: Always
        terminationGracePeriodSeconds: 30

        官方给了探活容器内服务的方案, 目前支持命令方式和HTTP方式
        命令行方式
        venessProbe:
        exec:
          command:
          - cat
          - /tmp/healthy
        initialDelaySeconds: 5 # container 启动5s后开始执行检测
        periodSeconds: 5 # 每隔 5s种执行一次
        timeoutSeconds: 1 # 超时时间,默认1s
        successThreshold: 1 # 几次成功才认为是成功,默认是1
        failureThreshold: 3 # 几次失败认为失败(有点报警合并的感觉)默认是3
        HTTP 方式
        venessProbe:
        httpGet:
          path: /healthz
          host: hostname/ip # 默认是POD/container的IP,通常你无须设置该值。有一种场景会用到,Suppose the Container listens on 127.0.0.1   and the Pod’s hostNetwork field is true. Then host, under httpGet, should be set to 127.0.0.1. If your pod relies on virtual   hosts, which is probably the more common case, you should not use host, but rather set the Host header in httpHeaders
          port: 8080
          httpHeaders:
            - name: X-Custom-Header
              value: Awesome
          scheme: HTTP
        initialDelaySeconds: 3
        periodSeconds: 3

        # container 的 HOOK 支持,可以在启停容器前做一些处理
        lifecycle:
        postStart:
          exec:
            command: ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]
        preStop:
          exec:
            command: ["/usr/sbin/nginx","-s","quit"]

      volumes:
      - name: redis-storage
        # emptDir生命周期与POD相同;创建在docker damon存储backend上,也支持使用RAM(emptyDir.medium设置为Memory即可)
        # emptyDir一般使用在下面这3中场景中:
        # 1. 适用与数据的临时存储,例如归并排序程序(用到磁盘来暂时存放和处理数据)
        # 2. 从crashs恢复时需要的临时存储
        # 3. 一个容器获取数据,POD内其它容器消费数据(共享数据)
        emptyDir: {}
      - name: hostpath-storage
        hostPath:
          # directory location on host
          path: /data
        # 对于 rbd 而言,如果设置成只读的话可以给多个使用者共享,对于RW模式而言的话,不暴增数据一致性
      - name: rbd-storage
        monitors:
           192.168.78:6789
           192.168.82:6789
           192.168.83:6789
        pool: kube,
        image: foo,
        user: admin,
        keyring: /etc/ceph/keyring,
        fsType: ext4,
        readOnly: true
        # 持久卷(要先创建persistentVolumeClaim资源)
      - name: task-pv-storage
        persistentVolumeClaim:
        claimName: task-pv-claim

        # 另外volume还提供了 secret volume的支持,用于对一些比较敏感的数据加密处理
        # 例如:username+password啥的,需要先创建kind为secret的资源对象
        # 在上面挂载点地方添加对应的挂载path就可以在容器内读写这些信息了
      - name: secret-volume
        secret:
          secretName: test-secret

补充

卷子目录

subPth 子目录: 提供一个POD下多个container共享同一个volume下不同目录的功能 例如:一个POD中运行1个mysql 容器和1个php容器,他们共享同一个volume下的不同目录,可以如下方式使用

      - name: mysql
        image: mysql
        volumeMounts:
        # 将 site-data 卷下的 mysql 目录挂载到mysql容器内的/var/lib/mysql 目录
        - mountPath: /var/lib/mysql
          name: site-data
          subPath: mysql
      - name: php
        image: php
        volumeMounts:
        # 将 site-data 卷下的 html 目录挂载到php容器内的/var/www/html 目录
        - mountPath: /var/www/html
          name: site-data
          subPath: html
      volumes:
      - name: site-data
        persistentVolumeClaim:
          claimName: my-lamp-site-data

POD 内 container 与 POD 互相获取信息

官方提供了 container 获取其所属 POD 信息 和 POD 获取其内 container 信息的功能 两种方式:

  1. 通过环境变量(如下所示,这里fieldPath中的spec、metadata都是POD的)
    # 以下这是container获取POD的信息
   - name: MY_NODE_NAME
     valueFrom:
       fieldRef:
         fieldPath: spec.nodeName
   - name: MY_POD_NAME
     valueFrom:
       fieldRef:
         fieldPath: metadata.name
   - name: MY_POD_NAMESPACE
     valueFrom:
       fieldRef:
         fieldPath: metadata.namespace
   - name: MY_POD_IP
     valueFrom:
       fieldRef:
         fieldPath: status.podIP
   - name: MY_POD_SERVICE_ACCOUNT
     valueFrom:
       fieldRef:
         fieldPath: spec.serviceAccountName

   # 以下这是 POD 获取指定container(通过name)的信息
   - name: MY_CPU_REQUEST
     valueFrom:
       resourceFieldRef:
         containerName: test-container
         resource: requests.cpu
   - name: MY_CPU_LIMIT
     valueFrom:
       resourceFieldRef:
         containerName: test-container
         resource: limits.cpu
   - name: MY_MEM_REQUEST
     valueFrom:
       resourceFieldRef:
         containerName: test-container
         resource: requests.memory
   - name: MY_MEM_LIMIT
     valueFrom:
       resourceFieldRef:
         containerName: test-container
         resource: limits.memory
  1. 通过 downwardAPIVolumesFileds, 将信息以文件和卷的方式挂载到容器内部

    volumes:
     - name: podinfo
       downwardAPI:
         items:
           # 这种是将POD的信息挂载到容器内
           # 在容器/etc/目录下会生成lables和annotations文件
           # /etc/labels 和 /etc/annotations 里面的内容就是POD的labels值和annotations值
           - path: "labels"
             fieldRef:
               fieldPath: metadata.labels
           - path: "annotations"
             fieldRef:
               fieldPath: metadata.annotations
           # 这种是将指定container的信息挂载到容器内
           # 同理会在容器内部的/etc/目录下生成相应的文件,如/etc/limits_cpu
           - path: "cpu_limit"
             resourceFieldRef:
               containerName: client-container
               resource: limits.cpu
           - path: "cpu_request"
             resourceFieldRef:
               containerName: client-container
               resource: requests.cpu
           - path: "mem_limit"
             resourceFieldRef:
               containerName: client-container
               resource: limits.memory
           - path: "mem_request"
             resourceFieldRef:
               containerName: client-container
               resource: requests.memory
    
         # downwardAPI 目前支持如下几种信息
           #The following information is available to Containers through environment variables and DownwardAPIVolumeFiles:
           #The node’s name
           #The Pod’s name
           #The Pod’s namespace
           #The Pod’s IP address
           #The Pod’s service account name
           #A Container’s CPU limit
           #A container’s CPU request
           #A Container’s memory limit
           #A Container’s memory request
         #In addition, the following information is available through DownwardAPIVolumeFiles.
           #The Pod’s labels
           #The Pod’s annotations
         # 如果容器没有指定CPU和memory,则获取的NODE的值
    

持久卷

kind: PersistentVolume
apiVersion: v1
metadata:
  name: task-pv-volume
  labels:
    type: local
  annotations:
      # 支持分组(其在StorageClass定义时通过annotations指定
      # 详见 https://kubernetes.io/docs/user-guide/persistent-volumes/ 中StorageClass定义
      volume.beta.kubernetes.io/storage-class: "slow"
spec:
  capacity:
    storage: 10Gi
  accessModes:
    # ReadOnlyMany/ReadWriteMany
    - ReadWriteOnce
    # https://kubernetes.io/docs/user-guide/persistent-volumes/ 给出了每种存储支持的类型
  persistentVolumeReclaimPolicy: Recycle
  rbd:
    path: "/tmp/data"
---

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: task-pv-claim
  annotations:
   # 会与设置相同值的PV绑定
    volume.beta.kubernetes.io/storage-class: "slow"
spec:
  accessModes:
    - ReadWriteOnce # 选择与volume相同访问权限的persistentvolume
  resources:
    requests:
      storage: 3Gi
  selector:
    matchLabels:
      release: "stable"
    matchExpressions:
      - {key: environment, operator: In, values: [dev]}

对于 Service、PersistentVolumeClaim 等其它资源都可以放到 DP 中编排,以后补充。

未完,待续 ~~

本文链接:https://addops.cn/post/kubernetes-deployment-fileds.html

-- EOF --

【转】kubernetes 中 deployment 支持哪些键值的更多相关文章

  1. 用字典给Model赋值并支持map键值替换

    用字典给Model赋值并支持map键值替换 这个是昨天教程的升级版本,支持键值的map替换. 源码如下: NSObject+Properties.h 与 NSObject+Properties.m / ...

  2. Kubernetes中Deployment部署故障排除

    Kubernetes中Deployment部署故障排除 字符型思维导图 排查pod状态(带标签):kubectl get pods,是否有等待处理的pod? 是?kubectl describe po ...

  3. Map泛型集合-国家中文和英文的键值映射

    package collection; import java.util.HashMap; import java.util.Map; public class Test5 { public stat ...

  4. JDK6中System.getProperties返回键值说明

    JDK6中java.lang.System.getProperties()方法返回键值说明. 键 相关值的描述 java.version Java 运行时环境版本 java.vendor Java 运 ...

  5. (转载)php 合并数组中的数据,如果键值相等其值相加

    (转载)http://hi.baidu.com/syxrrrr/item/7dfb2387343ce0874414cfdd /* * 合并数据,如果键值相等其值相加 * @param array $d ...

  6. Pycharm中怎么给字典中的多个键-值对同时加上单引号

    今天看了个爬虫视频,崔庆才讲师的免费视频, 里面一个批量给header加引号2s完成,这波操作让我眼前一亮. 最终还是发现了骚操作的背后手速是真的快. pycharm中按ctrl+r 勾选右上角的Re ...

  7. 009-java中常用的单个键值对

    1.Java 6提供AbstractMap.SimpleEntry<K,V>和AbstractMap.SimpleImmutableEntry<K,V> Map.Entry&l ...

  8. CockroachDB学习笔记——[译]CockroachDB中的SQL:映射表中数据到键值存储

    CockroachDB学习笔记--[译]CockroachDB中的SQL:映射表中数据到键值存储 原文标题:SQL in CockroachDB: Mapping Table Data to Key- ...

  9. Redis中的键值过期操作

    1.过期设置 Redis 中设置过期时间主要通过以下四种方式: expire key seconds:设置 key 在 n 秒后过期: pexpire key milliseconds:设置 key ...

随机推荐

  1. JavaScript去除空格trim()的原生实现

    W3C那帮人的脑袋被驴踢了,直到javascript1.8.1才支持trim函数(与trimLeft,trimRight),可惜现在只有firefox3.5支持.由于去除字符串两边的空白实在太常用,各 ...

  2. bzoj 4831 [Lydsy1704月赛]序列操作 dp

    [Lydsy1704月赛]序列操作 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 203  Solved: 69[Submit][Status][Dis ...

  3. Java nio和io

    当学习了Java NIO和IO的API后,一个问题马上涌入脑海: 我应该何时使用IO,何时使用NIO呢?在本文中,我会尽量清晰地解析Java NIO和IO的差异.它们的使用场景,以及它们如何影响您的代 ...

  4. SQL Server作业没有执行的解决方法

    SQL Server作业没有执行的解决方法  确保SQL Agent服务启动,并设置为自动启动,否则你的作业不会被执行    设置方法:  我的电脑--控制面板--管理工具--服务--右键 SQLSE ...

  5. Android SearchView结合Filter实现列表检索功能使用总结

                   下面贴出主要代码: <span style="font-size:14px;"> public class MainActivity ex ...

  6. python字符串内置函数

    1.字符串 定义:它是一个有序的字符的集合,用于存储和表示基本的文本信息,‘’或“”或‘’‘ ’‘’中间包含的内容称之为字符串特性:1.只能存放一个值2.不可变3.按照从左到右的顺序定义字符集合,下标 ...

  7. C# 从串口读取数据

    最近要做系统集成,需要从串口读取数据,随学习一下相关知识: 以下是从串口读取数据 public static void Main() { SerialPort mySerialPort = new S ...

  8. 「6月雅礼集训 2017 Day8」route

    [题目大意] 给出平面上$n$个点,求一条连接$n$个点的不相交的路径,使得转换的方向符合所给长度为$n-2$的字符串. $n \leq 5000$ [题解] 考虑取凸包上一点,然后如果下一个是‘R' ...

  9. 【BZOJ】1607: [Usaco2008 Dec]Patting Heads 轻拍牛头

    [算法]模拟 #include<cstdio> #include<algorithm> using namespace std; ,maxm=; int a[maxn],A[m ...

  10. 【POJ】1222 EXTENDED LIGHTS OUT

    [算法]高斯消元 [题解] 高斯消元经典题型:异或方程组 poj 1222 高斯消元详解 异或相当于相加后mod2 异或方程组就是把加减消元全部改为异或. 异或性质:00 11为假,01 10为真.与 ...