Completek8s Classroomnotes 26/Sep/2023

Helm templates contd

Conditionals

  • template
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
        {{- if .Values.livenessProbe.enabled }}
        livenessProbe:
          {{- if eq .Values.livenessProbe.type "http" }}
          httpGet:
            path: {{ .Values.livenessProbe.path }}
            port: {{ .Values.livenessProbe.port }}
          {{- else }}
          tcpSocket:
            port: {{ .Values.livenessProbe.port }}
          {{- end }}
          initialDelaySeconds: 3
          periodSeconds: 3
         {{- end }}
  • Values
---
livenessProbe:
  enabled: true
  type: tcp
  path: /orders
  port: 80

  • Output
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
        livenessProbe:
          tcpSocket:
            port: 80
          initialDelaySeconds: 3
          periodSeconds: 3
  • Follow the same and add resources and limits

Looping

  • Refer Here and look into sections with and range
  • Range helps in iterating lists
  • template
---
apiVersion: v1
kind: Service
metadata:
  name: {{ .Values.service.name }}
spec:
  selector:
    app.kubernetes.io/name: MyApp
  ports:
    {{- range .Values.service.ports }}
    - name: {{ .name }}
      port:  {{ .port | default 80 }}
      targetPort: {{ .targetPort | default 80 }}
      protocol: {{ .protocol | default "TCP" }}
    {{- end }}
  • values
service:
  name: myapp
  ports:
    - name: http
      port: 80
      targetPort: 8080
      protocol: TCP
    - name: https
      port: 443
      targetPort: 8443
  selector:
    app: myapp
    version: v1.0
  • output
---
apiVersion: v1
kind: Service
metadata:
  name: myapp
spec:
  selector:
    app.kubernetes.io/name: MyApp
  ports:
    - name: http
      port:  80
      targetPort: 8080
      protocol: TCP
    - name: https
      port:  443
      targetPort: 8443
      protocol: TCP
  • With is used to limit the scope to the specific values, refer labels and selector section
  • template
---
apiVersion: v1
kind: Service
metadata:
  name: {{ .Values.service.name }}
  labels:
    {{- with .Values.labels }}
        app: {{ .app }}
        version: {{ .version }}
        env: {{ .env }}
    {{- end }}
spec:
  ports:
    {{- range .Values.service.ports }}
    - name: {{ .name }}
      port:  {{ .port | default 80 }}
      targetPort: {{ .targetPort | default 80 }}
      protocol: {{ .protocol | default "TCP" }}
    {{- end }}
  selector:
    {{- with .Values.selector }}
   app: {{ .app }}
   version: {{ .version }}
   env: {{ .env }}
    {{- end }}
  • values
service:
  name: myapp
  ports:
    - name: http
      port: 80
      targetPort: 8080
      protocol: TCP
    - name: https
      port: 443
      targetPort: 8443
selector:
  app: myapp
  version: v1.0
  env: prod

labels:
  app: myservice
  version: v1.0
  env: prod


  • output
---
apiVersion: v1
kind: Service
metadata:
  name: myapp
  labels:
        app: myservice
        version: v1.0
        env: prod
spec:
  ports:
    - name: http
      port:  80
      targetPort: 8080
      protocol: TCP
    - name: https
      port:  443
      targetPort: 8443
      protocol: TCP
  selector:
   app: myapp
   version: v1.0
   env: prod
  • One more daemonset
  • template
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: fluentd-elasticsearch
  namespace: {{ .Values.namespace }}
  labels: {{ toYaml .Values.labels | nindent 4}}
spec:
  selector: {{toYaml .Values.selector | nindent 4}}
  template:
    metadata: {{ toYaml .Values.template.metadata | nindent 6}}
    spec:
      tolerations:
      {{- range .Values.tolerations }}
      - key: {{ .key }}
        operator: {{ .operator }}
        effect: {{ .effect }}
      {{- end }}
      containers:
      - name: fluentd-elasticsearch
        image: quay.io/fluentd_elasticsearch/fluentd:v2.5.2
        resources:
          limits:
            memory: 200Mi
          requests:
            cpu: 100m
            memory: 200Mi
        volumeMounts:
        {{- range .Values.volumes  }}
        - name: {{ .name }}
          mountPath: {{ .path }}
        {{- end }}
      terminationGracePeriodSeconds: 30
      volumes:
      {{- range .Values.volumes }}
      - name: {{ .name }}
        {{ .type }}:
           path: {{ .path }}
      {{- end }}

  • values
---
tolerations:
- key: node-role.kubernetes.io/control-plane
  operator: Exists
  effect: NoSchedule
- key: node-role.kubernetes.io/master
  operator: Exists
  effect: NoSchedule
selector:
  matchLabels:
    name: fluentd-elasticsearch
namespace: kube-system
labels:
  k8s-app: fluentd-loggin
  release: v1.0
template:
  metadata:
    labels:
        name: fluentd-elasticsearch
volumes:
  - name: varlog
    type: hostPath
    path: /var/log
  - name: varlog2
    type: hostPath
    path: /var/log2
  - name:  dbvol
    path: /var/lib/mysql
    type: emptyDir

  • output
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: fluentd-elasticsearch
  namespace: kube-system
  labels: 
    k8s-app: fluentd-loggin
    release: v1.0
spec:
  selector: 
    matchLabels:
      name: fluentd-elasticsearch
  template:
    metadata: 
      labels:
        name: fluentd-elasticsearch
    spec:
      tolerations:
      - key: node-role.kubernetes.io/control-plane
        operator: Exists
        effect: NoSchedule
      - key: node-role.kubernetes.io/master
        operator: Exists
        effect: NoSchedule
      containers:
      - name: fluentd-elasticsearch
        image: quay.io/fluentd_elasticsearch/fluentd:v2.5.2
        resources:
          limits:
            memory: 200Mi
          requests:
            cpu: 100m
            memory: 200Mi
        volumeMounts:
        - name: varlog
          mountPath: /var/log
        - name: varlog2
          mountPath: /var/log2
        - name: dbvol
          mountPath: /var/lib/mysql
      terminationGracePeriodSeconds: 30
      volumes:
      - name: varlog
        hostPath:
           path: /var/log
      - name: varlog2
        hostPath:
           path: /var/log2
      - name: dbvol
        emptyDir:
           path: /var/lib/mysql

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About continuous learner

devops & cloud enthusiastic learner