The simplest k8s deployment file writing posture, no one!

Time:2021-6-8

1. How to write k8s deployment file?

  • K8S yamlThere are many parameters, which need to be checked while writing?
  • How to set the number of reserved rollback versions?
  • How to detect and activate successfully?
  • How to allocate and limit resources?
  • How to set time zone? Otherwise, the printing log is GMT standard time
  • How to expose a service for other services to call?
  • How to configure horizontal scaling based on CPU and memory usage?

First of all, you need to know these knowledge points. Secondly, it’s not easy to understand these knowledge points. Thirdly, it’s still easy to make mistakes every time you write!

2. Create service image

Previous articleThis article explains how to quickly create your own service image, but for the sake of demonstration, let’sredis:6-alpineImage is an example.

3. Complete k8s deployment file writing process

  • Install firstgoctltool

    GO111MODULE=on GOPROXY=https://goproxy.cn/,direct go get -u github.com/tal-tech/go-zero/tools/goctl

  • One click generation of k8s deployment file

    goctl kube deploy -name redis -namespace adhoc -image redis:6-alpine -o redis.yaml -port 6379

    GeneratedyamlThe documents are as follows:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: redis
      namespace: adhoc
      labels:
        app: redis
    spec:
      replicas: 3
      revisionHistoryLimit: 5
      selector:
        matchLabels:
          app: redis
      template:
        metadata:
          labels:
            app: redis
        spec:
          containers:
          - name: redis
            image: redis:6-alpine
            lifecycle:
              preStop:
                exec:
                  command: ["sh","-c","sleep 5"]
            ports:
            - containerPort: 6379
            readinessProbe:
              tcpSocket:
                port: 6379
              initialDelaySeconds: 5
              periodSeconds: 10
            livenessProbe:
              tcpSocket:
                port: 6379
              initialDelaySeconds: 15
              periodSeconds: 20
            resources:
              requests:
                cpu: 500m
                memory: 512Mi
              limits:
                cpu: 1000m
                memory: 1024Mi
            volumeMounts:
            - name: timezone
              mountPath: /etc/localtime
          volumes:
            - name: timezone
              hostPath:
                path: /usr/share/zoneinfo/Asia/Shanghai
    
    ---
    
    apiVersion: v1
    kind: Service
    metadata:
      name: redis-svc
      namespace: adhoc
    spec:
      ports:
        - port: 6379
      selector:
        app: redis
    
    ---
    
    apiVersion: autoscaling/v2beta1
    kind: HorizontalPodAutoscaler
    metadata:
      name: redis-hpa-c
      namespace: adhoc
      labels:
        app: redis-hpa-c
    spec:
      scaleTargetRef:
        apiVersion: apps/v1
        kind: Deployment
        name: redis
      minReplicas: 3
      maxReplicas: 10
      metrics:
      - type: Resource
        resource:
          name: cpu
          targetAverageUtilization: 80
    
    ---
    
    apiVersion: autoscaling/v2beta1
    kind: HorizontalPodAutoscaler
    metadata:
      name: redis-hpa-m
      namespace: adhoc
      labels:
        app: redis-hpa-m
    spec:
      scaleTargetRef:
        apiVersion: apps/v1
        kind: Deployment
        name: redis
      minReplicas: 3
      maxReplicas: 10
      metrics:
      - type: Resource
        resource:
          name: memory
          targetAverageUtilization: 80
  • Deploy services ifadhocIf the namespace does not exist, please go through thekubectl create namespace adhocestablish

    $ kubectl apply -f redis.yaml
    deployment.apps/redis created
    service/redis-svc created
    horizontalpodautoscaler.autoscaling/redis-hpa-c created
    horizontalpodautoscaler.autoscaling/redis-hpa-m created
  • View service permission status

    $ kubectl get all -n adhoc
    NAME                         READY   STATUS    RESTARTS   AGE
    pod/redis-585bc66876-5ph26   1/1     Running   0          6m5s
    pod/redis-585bc66876-bfqxz   1/1     Running   0          6m5s
    pod/redis-585bc66876-vvfc9   1/1     Running   0          6m5s
    
    NAME                TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)    AGE
    service/redis-svc   ClusterIP   172.24.15.8   <none>        6379/TCP   6m5s
    
    NAME                    READY   UP-TO-DATE   AVAILABLE   AGE
    deployment.apps/redis   3/3     3            3           6m6s
    
    NAME                               DESIRED   CURRENT   READY   AGE
    replicaset.apps/redis-585bc66876   3         3         3       6m6s
    
    NAME                                              REFERENCE          TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
    horizontalpodautoscaler.autoscaling/redis-hpa-c   Deployment/redis   0%/80%    3         10        3          6m6s
    horizontalpodautoscaler.autoscaling/redis-hpa-m   Deployment/redis   0%/80%    3         10        3          6m6s
  • Testing services

    $ kubectl run -i --tty --rm cli --image=redis:6-alpine -n adhoc -- sh
    /data # redis-cli -h redis-svc
    redis-svc:6379> set go-zero great
    OK
    redis-svc:6379> get go-zero
    "great"

4. Summary

goctlThe tool greatly simplifies the writing of k8s yaml files, provides out of the box best practices, and supports template customization.

If you think the article is helpful, welcomestar ?

5. Project address

github.com/tal-tech/go-zero

This work adoptsCC agreementReprint must indicate the author and the link of this article