乐趣区

关于golang:最简单的-K8S-部署文件编写姿势没有之一

1. 头疼编写 K8S 部署文件?

  • K8S yaml 参数很多,须要边写边查?
  • 保留回滚版本数怎么设?
  • 如何探测启动胜利,如何探活?
  • 如何调配和限度资源?
  • 如何设置时区?否则打印日志是 GMT 规范工夫
  • 如何裸露服务供其它服务调用?
  • 如何依据 CPU 和内存使用率来配置程度伸缩?

首先,你须要晓得有这些知识点,其次要把这些知识点都搞明确也不容易,再次,每次编写仍然容易出错!

2. 创立服务镜像

前一篇文章 解说了如何疾速创立本人的服务镜像,不过为了演示,这篇文章咱们以 redis:6-alpine 镜像为例。

3. 残缺 K8S 部署文件编写过程

  • 首先装置 goctl 工具

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

  • 一键生成 K8S 部署文件

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

    生成的 yaml 文件如下:

    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
  • 部署服务,如果 adhoc namespace 不存在的话,请先通过 kubectl create namespace adhoc 创立

    $ 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
  • 查看服务容许状态

    $ 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
  • 测试服务

    $ 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. 总结

goctl 工具极大简化了 K8S yaml 文件的编写,提供了开箱即用的最佳实际,并且反对了模板自定义。

如果感觉文章有帮忙,欢送 star ????

5. 我的项目地址

https://github.com/tal-tech/go-zero

我的项目地址:
https://github.com/tal-tech/go-zero

退出移动版