[置顶] kubernetes创建资源yaml文件例子--rc

apiVersion: v1 #指定api版本,此值必须在kubectl apiversion中
kind: ReplicationController #指定创建资源的角色/类型
metadata: #资源的元数据/属性
  name: test-rc #资源的名字,在同一个namespace中必须唯一
  labels: #设定资源的标签
    software: apache
    project: test
    app: test-rc
    version: v1
  annotations:            #自定义注解列表
    - name: String        #自定义注解名字
spec:
  replicas: 2 #副本数量2 
  selector: #RC通过spec.selector来筛选要控制的Pod 
    software: apache
    project: test
    app: test-rc
    version: v1
    name: test-rc
  template: #这里写Pod的定义
    metadata:
      labels: #Pod的label,可以看到这个label与spec.selector相同
        software: apache
        project: test
        app: test-rc
        version: v1
        name: test-rc
    spec:#specification of the resource content 指定该资源的内容
      restartPolicy: Always #表明该容器一直运行,默认k8s的策略,在此容器退出后,会立即创建一个相同的容器
      nodeSelector:     #节点选择,先给主机打标签kubectl label nodes kube-node1 zone=node1
        zone: node1
      containers:
      - name: test-rc #容器的名字
        image: web:apache #容器使用的镜像地址
        imagePullPolicy: Never #[Always|Never|IfNotPresent]获取镜像的策略
        command: ['sh'] #覆盖容器中的Entrypoint,对应Dockefile中的ENTRYPOINT
        args: ["$(str)"] #对应Dockerfile中CMD参数
        env: #指定容器中的环境变量
        - name: str #变量的名字
          value: "/etc/run.sh" #变量的值
        resources: #资源管理
          requests: #最小资源需求
            cpu: 0.1 #设置可以使用CPU个数,两种方式,浮点数或者是整数+m,0.1=100m
            memory: 8Mi #内存使用量
          limits: #资源限制
            cpu: 0.5
            memory: 16Mi
        ports:
        - containerPort: 80
          name: httpd
          protocol: TCP
        livenessProbe: #pod内容器健康检查的设置
          httpGet: #通过httpget检查健康,返回200-399之间,则认为容器正常
            path: / #URI地址
            port: 80
            #host: 127.0.0.1 #主机地址
            scheme: HTTP
          initialDelaySeconds: 180 #表明第一次检测在容器启动后多长时间后开始
          timeoutSeconds: 5 #检测的超时时间
          periodSeconds: 15  #检查间隔时间
          #也可以用这种方法
          #exec: 执行命令的方法进行监测,如果其退出码不为0,则认为容器正常
          #  command:
          #    - cat
          #    - /tmp/health
          #也可以用这种方法
          #tcpSocket: //通过tcpSocket检查健康 
          #  port: number 
        lifecycle: #生命周期管理
          postStart: #容器运行之前运行的任务
            exec:
              command:
                - 'sh'
                - 'yum upgrade -y'
          preStop:#容器关闭之前运行的任务
            exec:
              command: ['service httpd stop']
        volumeMounts:
        - name: volume #挂载设备的名字,与volumes[*].name 需要对应  
          mountPath: /data #挂载到容器的某个路径下
          readOnly: True
      volumes: #定义一组挂载设备
      - name: volume #定义一个挂载设备的名字
        #meptyDir: {}
        hostPath:
          path: /opt #挂载设备类型为hostPath,路径为宿主机下的/opt,这里设备类型支持很多种

原文地址:https://www.cnblogs.com/lykops/p/7348014.html