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 

相关文章
相关标签/搜索