Part 4 : Deploy and Run applications in Kubernetes

Goal

Using Helm (The package manager for kubernetes) to deploy applications.  Run applications in Kubernetes cluster built using the Azure Container Service.

Assumptions

Running Kubernetes cluster and basic knowledge on Helm https://docs.helm.sh/helm/helm

Azure container registry (Private) is used as image repository

Pre-requisites

Installing Helm (Client) and Tiller (Server)


brew install kubernetes-helm

# Install Tiller on the kubernetes cluster and associated configuration on the local machine.

helm init

Now Check the version


helm version

# Run below command only if Tiller version is different

helm init --upgrade

Make sure the output versions are same for both client and server. If versions are different please update the version as required.

Follow the steps…

Since we are using azure container registry to store the images and pull the images into kubernetes cluster, we need to create the secrets for docker registry to enable the access.

Note: Make sure you are running them in appropriate context.


kubectl create secret docker-registry welcomeappacr --docker-server=https://welcomeappacr.azurecr.io --docker-username=<Replace with cluster SPN Object id> --docker-password=<Replace with SPN password>--docker-email=info@dinventive.com

# Run the below command to confirm the secret is available

kubectl get secret welcomeappacr

Secondly, we going to create the helm scripts. This scripts are basic template please modify as per your needs. If its a simple application then kubectl apply -f deployment.yaml is more sufficient. Helm is more useful for complex applications deployment scenario.


helm create helm-welcomeapp

Following directory structure will be created. Note: If you are in need of tree command please use the below or clear a alias in .zhrc


find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'
|____Chart.yaml
|____charts
|____.helmignore
|____templates
| |____deployment.yaml
| |____NOTES.txt
| |____ingress.yaml
| |____service.yaml
| |_____helpers.tpl
|____values.yaml

Now, Lets get our hands dirty in changing the following files to get our welcomeapp application deployed


apiVersion: v1
appVersion: "1.0"
description: A Helm chart for deploying welcomeapp
name: helm-welcomeapp
version: 0.1.0
icon : http://dinventive.com/wp-content/uploads/2015/01/icon_banner1.png

Under the templates folder open deployment.yaml file, update the container port to whatever you have exposed in the docker file. (Refer to Part 1 : Running nodejs app (welcomeapp) on Docker Containers)

 ports:
            - name: http
              containerPort: 8080
              protocol: TCP

Update the service.yaml file with correct target port. (Note : Values.service.internalPort this can be passed from the values.yaml)

ports:
    - port: {{ .Values.service.port }}
      targetPort: {{ .Values.service.internalPort }} #8080
      protocol: TCP
      name: http

update the values.yaml with repository, service name, type, port and internalport as shown below.


replicaCount: 1
image:
  repository: welcomeappacr.azurecr.io/welcomeapp
  tag: v1
  pullPolicy: IfNotPresent
nameOverride: ""
fullnameOverride: ""

service:
  name: welcomeapp
  type: LoadBalancer
  port : 80
  internalPort : 8080

ingress:
  enabled: false
  annotations: {}
  path: /
  hosts:
  tls: []
resources: {}
nodeSelector: {}

tolerations: []

affinity: {}

Now, its time to package and install.

helm lint

helm package helm-welcomeapp --debug --version 0.1.0

helm install helm-welcomeapp --version 0.1.0

#Run the below command and wait for the public IP : Replace the service name with yours
kubectl get svc -w boisterous-tarsier-helm-welcomeapp

#NAME                                 TYPE           CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
#boisterous-tarsier-helm-welcomeapp   LoadBalancer   10.0.151.101   <pending>     80:30315/TCP   34s
#boisterous-tarsier-helm-welcomeapp   LoadBalancer   10.0.151.101   40.114.192.214   80:30315/TCP   53s

Now you can browse our welcomeapp on the below above ip

curl 40.114.192.214
# I am the Welcome app, used for the demo

All the helm chart is available under https://github.com/mani0070/acsk8s/tree/master/helm-welcomeapp

Thats all for now, next blog is more about making the whole process as part of CI/CD pipeline and do upgrades to the application.
Hope you enjoy and let know if you stuck anywhere.

Leave a Reply

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