iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 14
0
DevOps

k8s 生態鏈 完美組合系列 第 14

Day14 怎麼撰寫 helm template

昨天大致介紹了 helm 的一些基礎知識,跟為什麼要用 helm ,我們今天就來實際使用一下,怎麼編寫自己的 helm 模板。

如果有照著昨天的方式,應該已經安裝 helm 指令,首先

$ helm create redis-test
$ ls
Chart.yaml  charts      templates   values.yaml

就會看到自動生成這些檔案,我們從 template 資料夾進去,會看到一個 deployment.yaml,我們打開後它,會發現它是不是跟我們前幾天寫的 deployment.yaml 很像 ? 只是它在很多可以抽換的參數導入模板語法,像這樣{{...}},我們找到 30 行開始,把他修改成如下

helm 是用 golang 撰寫的,所以他是使用 golang templete 的方式

containers:
  - name: {{ .Chart.Name }}
    securityContext:
    {{- toYaml .Values.securityContext | nindent 12 }}
    image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
    imagePullPolicy: {{ .Values.image.pullPolicy }}
    ports:
      #這邊替他取名為 redis
      - name: redis
        # port 改成 6379
        containerPort: 6379
        protocol: TCP
    # 把下面 livenessProbe & redinessProbe 先刪掉

再來打開 services.yaml

apiVersion: v1
kind: Service
metadata:
  name: {{ include "redis-test.fullname" . }}
  labels:
    {{- include "redis-test.labels" . | nindent 4 }}
spec:
  type: {{ .Values.service.type }}
  ports:
    - port: {{ .Values.service.port }}
      targetPort: 6379
      protocol: TCP

      # 這裡改成 redis
      name: redis
  select;r:
    {{- include "redis-test.selectorLabels" . | nindent 4 }}

最後我們回到上一層資料夾目錄找到 value.yaml

replicaCount: 1

image:

  # 這裡改成 redis
  repository: redis
  pullPolicy: IfNotPresent
  # Overrides the image tag whose default is the chart appVersion.
  # 抓取 5.0.5版
  tag: "5.0.5"

imagePullSecrets: []
nameOverride: ""
fullnameOverride: ""

serviceAccount:
  # Specifies whether a service account should be created
  create: true
  # Annotations to add to the service account
  annotations: {}
  # The name of the service account to use.
  # If not set and create is true, a name is generated using the fullname template
  name: ""

podAnnotations: {}

podSecurityContext: {}
  # fsGroup: 2000

securityContext: {}
  # capabilities:
  #   drop:
  #   - ALL
  # readOnlyRootFilesystem: true
  # runAsNonRoot: true
  # runAsUser: 1000

service:
  type: ClusterIP
  # service 改成 6379
  port: 6379

接下來只要下

$ helm install -n default redis-test ./redis-test  -f ./redis-test/values.yaml
NAME: redis-test
LAST DEPLOYED: Tue Sep 22 11:03:21 2020
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
1. Get the application URL by running these commands:
  export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=redis-test,app.kubernetes.io/instance=redis-test" -o jsonpath="{.items[0].metadata.name}")
  echo "Visit http://127.0.0.1:8080 to use your application"
  kubectl --namespace default port-forward $POD_NAME 8080:80

就完成了一個簡單的 redis 服務部署。 上面指令有一個 -f 後面可依照情況帶入不同的 value.yaml,可能會依據站別不同,而需要不同版本,這時只要輕鬆去切換不同的 value,就可以很輕鬆部署在不同的環境。

這時侯你下 helm ls 就能看到剛剛部署上去的列表,如果要刪除,只要下 helm uninstall redis-test,就可以完成刪除動作。

上面所有範例完整 code 我有放在 github,請自行取用。

明天我們來聊聊 helm template value 繼承關係。


上一篇
Day13 怎麼安裝 helm ?
下一篇
Day15 helm values 介紹
系列文
k8s 生態鏈 完美組合30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言