Deploy, Rollback, Scale, and Autoscaling an Application with Helm (Kubernetes)

untuk memudahkan deploy ke kubernetes maka di sini memakai Helm (package manager untuk kubernetes) dan di sini juga memakai Minikube untuk menjalankan cluster.

How to Install Helm

$ wget https://get.helm.sh/helm-v3.12.0-linux-amd64.tar.gz
$ tar -zxvf helm-v3.12.0-linux-amd64.tar.gz
$ sudo mv linux-amd64/helm /usr/local/bin/helm
$ helm
The Kubernetes package manager

Common actions for Helm:

- helm search:    search for charts
- helm pull:      download a chart to your local directory to view
- helm install:   upload the chart to Kubernetes
- helm list:      list releases of charts

........

Deploy

create folder helm, next create file values.yml & secrets.yml di dalam folder helm.

helm/values.yml

php:
  repository: "cehamot/rssejahterapp"
  tag: "8"
  fpmEnabled: false
  envVars:
    - name: APP_ENV
      value: production
    - name: APP_DEBUG
      value: false
    - name: DB_PORT
      value: 3306
    - name: DB_HOST
      value: localhost

helm/secrets.yml

mysql:
  rootPassword: "secret123"
  user: dani
  password: "secret123"
  database: db_rssejahtera

php:
  envVars:
    - name: APP_KEY
      value: "base64:V0xAWsrcyAPMY1+kgysAWm2ptVSA11+UAu78o/MqDjI="
    - name: DB_DATABASE
      value: db_rssejahtera
    - name: DB_USERNAME
      value: dani
    - name: DB_PASSWORD
      value: "secret123"

selanjutnya secrets.yml di tambahkan ke .dockerignore & .gitignore

echo '/helm/secrets.yml' >> ./.dockerignore && echo '/helm/secrets.yml' >> ./.gitignore

tambahkan Repo LAMP (Linux, Apache, Mysql, PHP).

$ minikube start
$ helm repo add lamp https://lead4good.github.io/lamp-helm-repository
$ helm install rssejahteraapp -f helm/values.yml -f helm/secrets.yml lamp/lamp
$ minikube service rssejahteraapp-lamp --url

migrate database

$ kubectl exec **nama pods** -- php artisan migrate:fresh --seed --force

untuk upgrade dapat menggunakan command, namun di file helm/values.yml dirubah dahulu tag nya.

$ nano helm/values.yaml
$ helm upgrade rssejahteraapp lamp/lamp -f helm/values.yml -f helm/secrets.yml

Rollback

$ helm history rssejahteraapp

and then

$ helm rollback rssejahteraapp 1

Scale

Scale Up

$ kubectl get deployments
$ kubectl scale --replicas 2 deployments/rssejahteraapp-lamp

delete salah satu pod, agar terlihat akan mereplica kembali pod yang sudah di delete.

Scale down

$ kubectl scale --replicas 1 deployments/rssejahteraapp-lamp

Autoscaling

untuk autoscaling create hpa.yml

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: rssejahteraapp-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: rssejahteraapp-lamp
  minReplicas: 1
  maxReplicas: 4
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 60
$ kubectl apply -f helm/hpa.yml

terlihat pods autoscalling.

$ helm delete rssejahteraapp

$ minikube stop