Kubernetes實戰指南(三十三):都0202了,你還在手寫k8s的yaml文件?

[toc]java

1. k8s的yaml文件到底有多複雜

Kubernetes建立、更新、刪除資源等操做時都可以使用json或yaml文件進行操做,更新和刪除能夠依賴以前的文件進行更改,可是建立具備多變形,每每編輯起來比較複雜,容器出錯,並且k8s的配置項實在太多,稍微不注意就會犯錯。要寫好一個yaml文件,你須要瞭解yaml的語法,須要掌握k8s的各類配置,對於一個k8s的初學者而言,這將是一件很難的事情。 node

 

好比咱們看一個同時建立一個Deployment、Service、Ingress的yaml文件內容:nginx

---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: test-yaml
  name: test-yaml
  namespace: freeswitch
spec:
  ports:
  - name: container-1-web-1
    port: 8080
    protocol: TCP
    targetPort: 8080
  selector:
    app: test-yaml
  sessionAffinity: None
  type: ClusterIP
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  creationTimestamp: null
  name: test-yaml
spec:
  rules:
  - host: test.com
    http:
      paths:
      - backend:
          serviceName: test-yaml
          servicePort: 8080
        path: /
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: test-yaml
  name: test-yaml
  namespace: freeswitch
spec:
  replicas: 3
  selector:
    matchLabels:
      app: test-yaml
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate
  template:
    metadata:
      annotations:
        info: test for yaml
      labels:
        app: test-yaml
    spec:
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: app
                  operator: In
                  values:
                  - test-yaml
              topologyKey: kubernetes.io/hostname
            weight: 100
      containers:
      - env:
        - name: TZ
          value: Asia/Shanghai
        - name: LANG
          value: C.UTF-8
        image: nginx
        imagePullPolicy: Always
        lifecycle: {}
        livenessProbe:
          failureThreshold: 2
          initialDelaySeconds: 30
          periodSeconds: 10
          successThreshold: 1
          tcpSocket:
            port: 8080
          timeoutSeconds: 2
        name: test-yaml
        ports:
        - containerPort: 8080
          name: web
          protocol: TCP
        readinessProbe:
          failureThreshold: 2
          initialDelaySeconds: 30
          periodSeconds: 10
          successThreshold: 1
          tcpSocket:
            port: 8080
          timeoutSeconds: 2
        resources:
          limits:
            cpu: 195m
            memory: 375Mi
          requests:
            cpu: 10m
            memory: 10Mi
        securityContext:
          allowPrivilegeEscalation: false
          capabilities: {}
          privileged: false
          procMount: Default
          readOnlyRootFilesystem: false
          runAsNonRoot: false
        volumeMounts:
        - mountPath: /usr/share/zoneinfo/Asia/Shanghai
          name: tz-config
        - mountPath: /etc/localtime
          name: tz-config
        - mountPath: /etc/timezone
          name: timezone
      dnsPolicy: ClusterFirst
      hostAliases:
      - hostnames:
        - www.baidu.com
        ip: 114.114.114.114
      imagePullSecrets:
      - name: myregistrykey
      - name: myregistrykey2
      restartPolicy: Always
      securityContext: {}
      volumes:
      - hostPath:
          path: /usr/share/zoneinfo/Asia/Shanghai
          type: ""
        name: tz-config
      - hostPath:
          path: /etc/timezone
          type: ""
        name: timezone

這是一個包含了Service、Ingress、Deployment比較經常使用而且沒有用到高級功能的yaml配置,就已經有上百行,若是是在添加了一些高級配置或者是Deployment中的容器不止一個,這個yaml會更大,就會形成一種視覺上疲勞,更改起來也比較麻煩並且很是容易出錯。git

 

2. 基於圖形化的方式自動生成yaml

 

2.1 k8s圖形化管理工具Ratel安裝

 

本次採用Ratel自動生成yaml文件,Ratel安裝文檔:https://github.com/dotbalo/ratel-doc/blob/master/cluster/Install.mdgithub

 

2.2 使用Ratel建立生成yaml文件

 

2.2.1 基本配置

 

安裝完成後,能夠生成、建立管理經常使用的k8s核心資源,好比建立一個Deployment:
點擊Deployment -- 建立如圖所示:
Kubernetes實戰指南(三十三):都0202了,你還在手寫k8s的yaml文件?web

 

以後能夠填寫一些基本的配置信息,好比Deployment名稱、副本數、標籤信息等,固然也能夠點擊必須/儘可能部署至不一樣宿主機進行Pod親和力的配置json

 

同時也可添加一些複雜的配置,好比內核配置、容忍配置、節點親和力快捷配置:
Kubernetes實戰指南(三十三):都0202了,你還在手寫k8s的yaml文件?api

 

2.2.2 親和力配置

 

基本配置編譯完成之後,點擊NEXT,下一個配置親和力配置,若是上一頁使用了親和力快捷鍵,這邊會自動生成親和力配置,你能夠再次編輯或者添加、刪除:
Kubernetes實戰指南(三十三):都0202了,你還在手寫k8s的yaml文件?session

 

2.2.3 存儲配置

 

親和力配置完成之後,能夠點擊NEXT進行存儲配置,目前支持volume和projectedVolume配置,volume支持configMap、Secret、HostPath、PVC、NFS、Empty等經常使用類型的配置:
Kubernetes實戰指南(三十三):都0202了,你還在手寫k8s的yaml文件?app

 

2.2.4 容器配置

 

接下來是容器配置,支持經常使用的容器配置,固然也能夠添加多個容器:
Kubernetes實戰指南(三十三):都0202了,你還在手寫k8s的yaml文件?

 
稍微複製一點的配置:
Kubernetes實戰指南(三十三):都0202了,你還在手寫k8s的yaml文件?

 

2.2.4 初始化容器配置

 

初始化容器和容器配置相似

 

2.2.5 Service和Ingress配置

 

建立Deployment時能夠一鍵添加Service和Ingress,添加Service時會自動讀取容器的端口配置,添加Ingress時會自動讀取Service配置

Kubernetes實戰指南(三十三):都0202了,你還在手寫k8s的yaml文件?
Kubernetes實戰指南(三十三):都0202了,你還在手寫k8s的yaml文件?

 

2.2.6 建立資源或生成yaml文件

 

上述配置完成之後,能夠選擇建立資源或生成yaml文件,假如點擊生成yaml文件,會自動生成Service、Ingress、Deployment的yaml文件,能夠直接拿着使用:
Kubernetes實戰指南(三十三):都0202了,你還在手寫k8s的yaml文件?

 

生成的內容以下:

---
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: test-yaml
  name: test-yaml
  namespace: default
spec:
  ports:
  - name: container-1-web-1
    port: 8080
    protocol: TCP
    targetPort: 8080
  selector:
    app: test-yaml
  sessionAffinity: None
  type: ClusterIP
status:
  loadBalancer: {}
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  creationTimestamp: null
  name: test-yaml
spec:
  rules:
  - host: test.com
    http:
      paths:
      - backend:
          serviceName: test-yaml
          servicePort: 8080
        path: /
status:
  loadBalancer: {}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: test-yaml
  name: test-yaml
  namespace: default
spec:
  replicas: 3
  selector:
    matchLabels:
      app: test-yaml
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: test-yaml
    spec:
      affinity:
        nodeAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - preference:
              matchExpressions:
              - key: loki
                operator: In
                values:
                - "true"
            weight: 100
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: master
                operator: NotIn
                values:
                - "true"
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - test-yaml
            topologyKey: kubernetes.io/hostname
      containers:
      - args:
        - '*.jar --server.port=80'
        command:
        - java -jar
        env:
        - name: TZ
          value: Asia/Shanghai
        - name: LANG
          value: C.UTF-8
        - name: POD_IP
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        envFrom:
        - configMapRef:
            name: testcm
        image: nginx
        imagePullPolicy: IfNotPresent
        lifecycle:
          postStart:
            exec:
              command:
              - echo "start"
          preStop:
            exec:
              command:
              - sleep 30
        livenessProbe:
          failureThreshold: 2
          initialDelaySeconds: 30
          periodSeconds: 10
          successThreshold: 1
          tcpSocket:
            port: 8080
          timeoutSeconds: 2
        name: test-yaml
        ports:
        - containerPort: 8080
          name: web
          protocol: TCP
        readinessProbe:
          failureThreshold: 2
          httpGet:
            httpHeaders:
            - name: a
              value: b
            path: /
            port: 8080
            scheme: HTTP
          initialDelaySeconds: 30
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 2
        resources:
          limits:
            cpu: 493m
            memory: 622Mi
          requests:
            cpu: 10m
            memory: 10Mi
        securityContext:
          allowPrivilegeEscalation: false
          capabilities: {}
          privileged: false
          procMount: Default
          readOnlyRootFilesystem: false
          runAsNonRoot: false
        volumeMounts:
        - mountPath: /usr/share/zoneinfo/Asia/Shanghai
          name: tz-config
        - mountPath: /etc/localtime
          name: tz-config
        - mountPath: /etc/timezone
          name: timezone
        - mountPath: /mnt
          name: nfs-test
      dnsPolicy: ClusterFirst
      initContainers:
      - args:
        - init
        command:
        - echo
        env:
        - name: TZ
          value: Asia/Shanghai
        - name: LANG
          value: C.UTF-8
        image: nignx-init
        imagePullPolicy: Always
        name: init
        resources:
          limits:
            cpu: 351m
            memory: 258Mi
          requests:
            cpu: 10m
            memory: 10Mi
        securityContext:
          allowPrivilegeEscalation: false
          capabilities: {}
          privileged: false
          procMount: Default
          readOnlyRootFilesystem: false
          runAsNonRoot: false
        volumeMounts:
        - mountPath: /usr/share/zoneinfo/Asia/Shanghai
          name: tz-config
        - mountPath: /etc/localtime
          name: tz-config
        - mountPath: /etc/timezone
          name: timezone
      nodeSelector:
        ratel: "true"
      restartPolicy: Always
      securityContext:
        sysctls:
        - name: net.core.somaxconn
          value: "16384"
        - name: net.ipv4.tcp_max_syn_backlog
          value: "16384"
      tolerations:
      - effect: NoSchedule
        key: node-role.kubernetes.io/master
        operator: Exists
      volumes:
      - name: projected-test
        projected:
          defaultMode: 420
          sources:
          - downwardAPI:
              items:
              - fieldRef:
                  fieldPath: metadata.name
                path: /opt/x
      - hostPath:
          path: /usr/share/zoneinfo/Asia/Shanghai
          type: ""
        name: tz-config
      - hostPath:
          path: /etc/timezone
          type: ""
        name: timezone
      - name: nfs-test
        nfs:
          path: /data/nfs
          server: 1.1.1.1
status: {}

這個yaml比以前的稍複雜,而且添加了一些高級配置,手動編寫的仍是比較麻煩的,因此用Ratel自動生成仍是比較方便的,而且不會出錯。

 

3. 其餘資源文件自動生成

 

目前支持了不少資源文件的自動生成,好比:Deployment、StatefulSet、DaemonSet、Service、Ingress、CronJob、Secret、ConfigMap、PV、PVC等,能夠大大減小咱們的工做量和k8s的複雜度。

 
 

若是想要系統的學習k8s,能夠專一下k8s的課程:

51CTO 

相關文章
相關標籤/搜索