Kubernets Yaml 编写

1、定义pod的完整内容

# yaml格式的pod定义文件完整内容:
apiVersion: v1                  #必选,版本号,例如v1
kind: Pod                       #必选,Pod
metadata:                       #必选,元数据
  name: string                  #必选,Pod名称
  namespace: string             #必选,Pod所属的命名空间
  labels:                       #自定义标签
    - name: string              #自定义标签名字
  annotations:                  #自定义注释列表
    - name: string
spec:                           #必选,Pod中容器的详细定义
  containers:                   #必选,Pod中容器列表
  - name: string                #必选,容器名称
    image: string               #必选,容器的镜像名称
    imagePullPolicy: [Always | Never | IfNotPresent] #获取镜像的策略 Alawys表示下载镜像
                                #IfnotPresent表示优先使用本地镜像,否则下载镜像,Nerver表示仅使用本地镜像
    command: [string]           #容器的启动命令列表,如不指定,使用打包时使用的启动命令
    args: [string]              #容器的启动命令参数列表
    workingDir: string          #容器的工作目录
    volumeMounts:               #挂载到容器内部的存储卷配置
    - name: string              #引用pod定义的共享存储卷的名称,需用volumes[]部分定义的的卷名
      mountPath: string         #存储卷在容器内mount的绝对路径,应少于512字符
      readOnly: boolean         #是否为只读模式
    ports:                      #需要暴露的端口库号列表
    - name: string              #端口号名称
      containerPort: int        #容器需要监听的端口号
      hostPort: int             #容器所在主机需要监听的端口号,默认与Container相同
      protocol: string          #端口协议,支持TCP和UDP,默认TCP
    env:                        #容器运行前需设置的环境变量列表
    - name: string              #环境变量名称
      value: string             #环境变量的值
    resources:                  #资源限制和请求的设置
      limits:                   #资源限制的设置
        cpu: string             #Cpu的限制,单位为core数,将用于docker run --cpu-shares参数
        memory: string          #内存限制,单位可以为Mib/Gib,将用于docker run --memory参数
      requests:                 #资源请求的设置
        cpu: string             #Cpu请求,容器启动的初始可用数量
        memory: string          #内存清楚,容器启动的初始可用数量
    livenessProbe:              #对Pod内个容器健康检查的设置,当探测无响应几次后将自动重启该容器,
                                #检查方法有exec、httpGet和tcpSocket,对一个容器只需设置其中一种方法即可
      exec:                     #对Pod容器内检查方式设置为exec方式
        command: [string]       #exec方式需要制定的命令或脚本
      httpGet:                  #对Pod内个容器健康检查方法设置为HttpGet,需要制定Path、port
        path: string
        port: number
        host: string
        scheme: string
        HttpHeaders:
        - name: string
          value: string
      tcpSocket:                #对Pod内个容器健康检查方式设置为tcpSocket方式
         port: number
       initialDelaySeconds: 0   #容器启动完成后首次探测的时间,单位为秒
       timeoutSeconds: 0        #对容器健康检查探测等待响应的超时时间,单位秒,默认1秒
       periodSeconds: 0         #对容器监控检查的定期探测时间设置,单位秒,默认10秒一次
       successThreshold: 0
       failureThreshold: 0
       securityContext:
         privileged:false
    restartPolicy: [Always | Never | OnFailure]#Pod的重启策略,Always表示一旦不管以何种方式终止运行,
                               #kubelet都将重启,OnFailure表示只有Pod以非0退出码退出才重启,Nerver表示不再重启该Pod
    nodeSelector: obeject      #设置NodeSelector表示将该Pod调度到包含这个label的node上,以key:value的格式指定
    imagePullSecrets:          #Pull镜像时使用的secret名称,以key:secretkey格式指定
    - name: string
    hostNetwork:false          #是否使用主机网络模式,默认为false,如果设置为true,表示使用宿主机网络
    volumes:                   #在该pod上定义共享存储卷列表
    - name: string             #共享存储卷名称 (volumes类型有很多种)
      emptyDir: {}             #类型为emtyDir的存储卷,与Pod同生命周期的一个临时目录。为空值
      hostPath: string         #类型为hostPath的存储卷,表示挂载Pod所在宿主机的目录
        path: string           #Pod所在宿主机的目录,将被用于同期中mount的目录
      secret:                  #类型为secret的存储卷,挂载集群与定义的secre对象到容器内部
        scretname: string  
        items:     
        - key: string
          path: string
      configMap:               #类型为configMap的存储卷,挂载预定义的configMap对象到容器内部
        name: string
        items:
        - key: string
          path: string

通过 查看apiVersion

kubectl api-version
kubectl get namespace

2、创建deployment,一般通过deployment 来创建pod

#test-pod 
apiVersion: v1                       #指定api版本,此值必须在kubectl apiversion中   
kind: Deployment                     #指定创建资源的角色/类型   
metadata:                            #资源的元数据/属性   
  name: test-pod                     #资源的名字,在同一个namespace中必须唯一   
  labels:                             #设定资源的标签 
    k8s-app: apache   
    version: v1   
    kubernetes.io/cluster-service: "true"   
  annotations:            #自定义注解列表   
    - name: String        #自定义注解名字   
spec: #specification of the resource content 指定该资源的内容   
  restartPolicy: Always #表明该容器一直运行,默认k8s的策略,在此容器退出后,会立即创建一个相同的容器   
  nodeSelector:     #节点选择,先给主机打标签kubectl label nodes kube-node1 zone=node1   
    zone: node1   
  containers:   
  - name: test-pod #容器的名字   
    image: 10.192.21.18:5000/rjh/client:latest #容器使用的镜像地址   
    imagePullPolicy: Never #三个选择Always、Never、IfNotPresent,每次启动时检查和更新(从registery)images的策略, 
                           # 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,最少值为0.001核(1m) 
        memory: 32Mi #内存使用量   
      limits: #资源限制   
        cpu: 0.5   
        memory: 1000Mi   
    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,这里设备类型支持很多种 
    #nfs

Deployment 管理的是replicaset-controller,RC会创建Pod。Pod自身会下载镜像并启动镜像

3、service 完整文件

apiVersion: v1
kind: Service
matadata:
  name: string
  namespace: string
  labels:
    - name: string
  annotations:
    - name: string
spec:
  selector: [] #必须,将具有指定Label标签的Pod作为管理范围,与deployment的selector.matchLabels对应
  type: string # 指定Service的访问方式,默认为ClusterIP, ClusterIP:虚拟的服务IP地址,该地址用于Kubernets集群内部的
               # Pod访问,在Node上Kuber-proxy通过设置的iptables规则进行转发
               # NodePort: 使用宿主机的端口,使能够访问各Node的外部客户端通过Node的IP地址和端口号就能访问服务
               # LoadBalancer: 使用外部负载均衡器完成到服务的负载分发,需要在spec.status.loadBalancer 字段指定外部负
               # 载均衡器的IP地址,并同时定义nodePort和clusterIP
  clusterIP: string #当type=clusterIP时,如果不指定,则系统自动分配,也可以指定。当type=LoadBalancer时,需要指定
  sessionAffinity: string #是否支持Session, 可选值为clientIP,默认为空,ClientIP:表示将一个客户端
                          #(根据客户端的IP地址决定)的访问请求都转发到同一个后端端口
  ports:                  #service 需要暴露的端口列表
  - name: string         
    protocol: string      #端口协议 支持TCP和UDP
    port: int             #端口号
    targetPort: int       #需要转发到后端Pod的端口号
    nodePort: int         #当spec.type=NodePort时,指定映射到物理机的端口号
  status:                 #当scpe.type=LoadBalancer时,设置外部负载均衡器的地址,用于公有云环境
    loadBalancer:
      ingress:
        ip: string
        hostname: string  #均衡器的主机名

Service 中,NodePort,targetPort,Port三个关系很紧密,而且很难区分,先看下图

Kubernets Yaml 编写

外部客户端通过指定的负载均衡器,130.211.53.173:80 访问kubernets 集群,通过Nodeport:32143 访问到服务port,服务再去访问容器暴露出来的targetPort :8080

 

4、实践

apiVersion: v1
kind: ConfigMap
metadata:
  name: rjh-configmap
  namespace: default
data:
  test: rjh-server
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: test
  namespace: default
  labels:
    app: rjh-client
spec:
  replicas: 1
  selector:
    matchLabels:
      app:rjh-client
    spec:
      containers:
      - name: coredns
        image: coredns/coredns:latest
        imagePullPolicy: Always
        ports:
        - containerPort: 53
          name: client
          protocol: TCP
        resources: #资源管理 
          requests: #容器运行时,最低资源需求,也就是说最少需要多少资源容器才能正常运行   
            cpu: 0.1 #CPU资源(核数),两种方式,浮点数或者是整数+m,0.1=100m,最少值为0.001核(1m) 
            memory: 32Mi #内存使用量   
          limits: #资源限制   
            cpu: 0.5   
            memory: 1000Mi   
        livenessProbe:
          httpGet:
            path: /health
            port: 8080
            scheme: HTTP
          initialDelaySeconds: 60
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 5
      dnsPolicy: Default
      volumes:
        - name: config-volume
          configMap:
            name: test
            items:
            - key: rjh-configmap
              path:    #待考证
---
#内部使用服务
apiVersion: v1
kind: Service
metadata:
  name: rjh-service
  namespace: default
  labels:
    app:rjh-client
spec:
  selector:
   app:client
  type: ClusterIP
  ports:
  #coreDNS 支持TCP、UDP、HTTP、TTL四种协议
  - name: ownservice
    port: 53
    protocol: TCP