Skip to content

Helm_Lab_04 | Template

deepak gupta edited this page Dec 11, 2020 · 19 revisions

Working-with-Helm

Helm Lab 04

Purpose

The Purpose of this lab is to give the understanding of templating in helm

Till now the gowebapp helm chart was completely hardcoded or static in nature in this Lab we will be making it dynamic in nature i.e while creating the release of the helm chart end-user can change the behaviour of workload in terms of: * Containers * Containers Probes * Labels

01

Let's start with a very straight forward operation by updating our helm chart to give an option to modify container detail such as

  • Container Name
  • Container image
  • Container tag

Update gowebapp/values.yaml with below content

mysql:
  mysqlDatabase: employeedb
  mysqlRootPassword: password
  persistence:
    enabled: false

redis:
  usePassword: false
  cluster:
    enabled: false
  master:
    persistence:
      enabled: false

containers:
  name: goweb-app
  image: opstreedevops/ot-go-webapp
  tag: "v1"

Update gowebapp/templates/deployment.yaml with below content

apiVersion: apps/v1
kind: Deployment
metadata:
  name: goweb-app
  labels:
    app.kubernetes.io/name: app
    app.kubernetes.io/instance: goweb-app
    app.kubernetes.io/version: "1.16.0"
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: app
      app.kubernetes.io/instance: goweb-app
  template:
    metadata:
      labels:
        app.kubernetes.io/name: app
        app.kubernetes.io/instance: goweb-app
    spec:
      containers:
        - name: {{ .Values.containers.name }}
          image: "{{ .Values.containers.image }}:{{ .Values.containers.tag }}"
          imagePullPolicy: IfNotPresent
          ports:
          - name: http
            containerPort: 8080
            protocol: TCP
          env:
          - name: DB_URL
            value: goweb-app-mysql
          - name: DB_PORT
            value: "3306"
          - name: DB_USER
            value: "root"
          - name: DB_PASSWORD
            value: password
          - name: REDIS_HOST
            value: "goweb-app-redis-headless"
          - name: REDIS_PORT
            value: "6379"

Validate the manifest content by running helm template command

$ helm template gowebapp

Update your release with the updated helm chart

helm upgrade sandy gowebapp

Validate the pod details by running kubectl describe pod command.

Update your release with a new name to your container

helm upgrade sandy gowebapp --set containers.name=sandy

Validate the pod details by running kubectl describe pod command

02

In this section we will add Probes capability in our chart

Update gowebapp/values.yaml with below content

mysql:
  mysqlDatabase: employeedb
  mysqlRootPassword: password
  persistence:
    enabled: false

redis:
  usePassword: false
  cluster:
    enabled: false
  master:
    persistence:
      enabled: false

containers:
  name: gowebapp
  image: opstreedevops/ot-go-webapp-master
  tag: "1.0-15"
  livenessProbe:
    tcpSocket:
      port: 8080
  readinessProbe:
    tcpSocket:
      port: 8080

Update gowebapp/templates/deployment.yaml with below content

apiVersion: apps/v1
kind: Deployment
metadata:
  name: goweb-app
  labels:
    app.kubernetes.io/name: app
    app.kubernetes.io/instance: goweb-app
    app.kubernetes.io/version: "1.16.0"
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: app
      app.kubernetes.io/instance: goweb-app
  template:
    metadata:
      labels:
        app.kubernetes.io/name: app
        app.kubernetes.io/instance: goweb-app
    spec:
      containers:
        - name: {{ .Values.containers.name }}
          image: "{{ .Values.containers.image }}:{{ .Values.containers.tag }}"
          imagePullPolicy: IfNotPresent
          ports:
          - name: http
            containerPort: 8080
            protocol: TCP
          env:
          - name: DB_URL
            value: goweb-app-mysql
          - name: DB_PORT
            value: "3306"
          - name: DB_USER
            value: "root"
          - name: DB_PASSWORD
            value: password
          - name: REDIS_HOST
            value: "goweb-app-redis-headless"
          - name: REDIS_PORT
            value: "6379"
          livenessProbe:
            tcpSocket:
              port: {{ int .Values.containers.livenessProbe.tcpSocket.port }}
            initialDelaySeconds: 5
            periodSeconds: 5
          readinessProbe:
            tcpSocket:
              port: {{ int .Values.containers.readinessProbe.tcpSocket.port }}
            initialDelaySeconds: 5
            periodSeconds: 5

Validate the manifest content by running helm template command

$ helm template gowebapp

Update your release with the updated helm chart

helm upgrade sandy gowebapp

Validate the pod details by running kubectl describe pod command.

Update your release with a new name to your container

helm upgrade sandy gowebapp --set containers.livenessProbe.tcpSocket.port=8080

Validate the pod details by running kubectl describe pod command.

03

  • In the example we will giving labels in the values.yaml
mysql:
  mysqlDatabase: employeedb
  mysqlRootPassword: password
  persistence:
    enabled: false

redis:
  usePassword: false
  cluster:
    enabled: false
  master:
    persistence:
      enabled: false

labels:
  app.kubernetes.io/name: app
  app.kubernetes.io/instance: goweb-app
  app.kubernetes.io/version: "1.16.0"
matchLabels:
  app.kubernetes.io/name: app
  app.kubernetes.io/instance: goweb-app
containers:
  name: goweb-app
  image: opstreedevops/ot-go-webapp
  tag: "v1"
  livenessProbe:
    tcpSocket:
      port: 8080
  readinessProbe:
    tcpSocket:
      port: 8080
  • This is our template file which will fetch the data from values.yaml which includes labels and the templates uses if condition if we the labels are present or not
apiVersion: apps/v1
kind: Deployment
metadata:
  name: goweb-app
  {{- if .Values.labels }}
  labels:
    {{- toYaml .Values.labels | nindent 4 }}
  {{- end }}
spec:
  replicas: 1
  selector:
    matchLabels:
      {{- toYaml .Values.matchLabels | nindent 6 }}
  template:
    metadata:
      {{- if .Values.matchLabels }}
      labels:
        {{- toYaml .Values.matchLabels| nindent 8 }}
      {{- end }}
    spec:
      containers:
        - name: {{ .Values.containers.name }}
          image: "{{ .Values.containers.image }}:{{ .Values.containers.tag }}"
          imagePullPolicy: IfNotPresent
          ports:
          - name: http
            containerPort: 8080
            protocol: TCP
          env:
          - name: DB_URL
            value: goweb-app-mysql
          - name: DB_PORT
            value: "3306"
          - name: DB_USER
            value: "root"
          - name: DB_PASSWORD
            value: password
          - name: REDIS_HOST
            value: "goweb-app-redis-headless"
          - name: REDIS_PORT
            value: "6379"
          livenessProbe:
            tcpSocket:
              port: {{ int .Values.containers.livenessProbe.tcpSocket.port }}
            initialDelaySeconds: 5
            periodSeconds: 5
          readinessProbe:
            tcpSocket:
              port: {{ int .Values.containers.readinessProbe.tcpSocket.port }}
            initialDelaySeconds: 5
            periodSeconds: 5
  • You can use helm template . to check the your template is working fine

04

values.yaml

mysql:
  mysqlDatabase: employeedb
  mysqlRootPassword: password
  persistence:
    enabled: false

redis:
  usePassword: false
  cluster:
    enabled: false
  master:
    persistence:
      enabled: false
labels:
  app.kubernetes.io/name: app
  app.kubernetes.io/instance: goweb-app
  app.kubernetes.io/version: "1.16.0"
matchLabels:
  app.kubernetes.io/name: app
  app.kubernetes.io/instance: goweb-app
containers:
  name: goweb-app
  image: opstreedevops/ot-go-webapp
  tag: "v1"
  env:
  - name: DB_URL
    value: goweb-app-mysql
  - name: DB_PORT
    value: "3306"
  - name: DB_USER
    value: "root"
  - name: DB_PASSWORD
    value: password
  - name: REDIS_HOST
    value: "goweb-app-redis-headless"
  - name: REDIS_PORT
    value: "6379"
  livenessProbe:
    tcpSocket:
      port: 8080
  readinessProbe:
    tcpSocket:
      port: 8080

deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: goweb-app
  {{- if .Values.labels }}
  labels:
    {{- toYaml .Values.labels | nindent 4 }}
  {{- end }}
spec:
  replicas: 1
  selector:
    matchLabels:
      {{- toYaml .Values.matchLabels | nindent 6 }}
  template:
    metadata:
      {{- if .Values.matchLabels }}
      labels:
        {{- toYaml .Values.matchLabels| nindent 8 }}
      {{- end }}
    spec:
      containers:
        - name: {{ .Values.containers.name }}
          image: "{{ .Values.containers.image }}:{{ .Values.containers.tag }}"
          imagePullPolicy: IfNotPresent
          ports:
          - name: http
            containerPort: 8080
            protocol: TCP
          env:
          {{- range $env := .Values.containers.env }}
          - name: {{ $env.name | quote }}
            value: {{ $env.value | quote}}
          {{- end }}
          livenessProbe:
            tcpSocket:
              port: {{ int .Values.containers.livenessProbe.tcpSocket.port }}
            initialDelaySeconds: 5
            periodSeconds: 5
          readinessProbe:
            tcpSocket:
              port: {{ int .Values.containers.readinessProbe.tcpSocket.port }}
            initialDelaySeconds: 5
            periodSeconds: 5
Clone this wiki locally