1. 程式人生 > >最簡單的 K8S 部署檔案編寫姿勢,沒有之一!

最簡單的 K8S 部署檔案編寫姿勢,沒有之一!

## 1. 頭疼編寫K8S部署檔案? * `K8S yaml` 引數很多,需要邊寫邊查? * 保留回滾版本數怎麼設? * 如何探測啟動成功,如何探活? * 如何分配和限制資源? * 如何設定時區?否則列印日誌是GMT標準時間 * 如何暴露服務供其它服務呼叫? * 如何根據CPU和記憶體使用率來配置水平伸縮? 首先,你需要知道有這些知識點,其次要把這些知識點都搞明白也不容易,再次,每次編寫依然容易出錯! ## 2. 建立服務映象 [前一篇文章](https://juejin.cn/post/6904508173328547854) 講解了如何快速建立自己的服務映象,不過為了演示,這篇文章我們以 `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` 檔案如下: ```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` 建立 ````shell $ 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 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 ``` * 測試服務 ```shell $ 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**