Now in our previous blog we have seen how we have packaged our SAPUI5 Hello World app inside an image and pushed to docker hub. In this blog we will be going a step further. Consider a scenario you have N number of containers running, how do we manage their end to end lifecycle easily. Of course we can do it via cli but it will be a big time consuming task. So what we need is something which can take care of this cycle which is nothing but an orchestrator like Kubernetes or in short k8s. So kubernetes actually helps in maintaining the end to end lifecycle of containers, refer this blog to understand the basics. Lets’s try to use Kubernetes in managing our containers. Please note that docker also has its own orchestrator docker swarm or 3rd party like Meso’s, but we will be focussing on k8s.
What we will do now?
K8s also follows a declarative approach like others. All we need is yaml file and describe what all we need to deploy. So we have our image available all we need is to use k8s to create and manage the containers. K8s has concept of pod’s which is nothing but collection of homogenous containers. For example you have a frontend application with a mysql database then you can create two containers one for DB and another for our frontend they are binded together as one pod.
What we will do is we will create a deployment where we will describe how many pods, which images etc. we need to get our system up and running.
Let’s create a deployment
Before actually starting, we need to install minikube on to our system, this is for testing and learning purposes. Of course a productive version will need a better hardware spec. Please check this step by step guide to install the same.Once minikube is installed lets start it.
Creating a deployment. As can be seen below we are mentioning the kind as deployment, 4 replicas meaning 4 pods containing container of the mentioned image. Don’t forget to mention the tag with the image else the pod creation will fail.
apiVersion: extensions/v1beta1 kind: Deployment metadata: name: ui5 spec: replicas: 4 template: metadata: labels: app: ui5 spec: containers: - name: ui5 image: nabheetmadan/helloworldui5:1.0 ports: - containerPort: 80
Use the below command to create the deployment
kubectl create -f webserverui5.yaml
Check all what has been created or running in the node using below command. As can be seen in screen shot we have 4 pods running along with one deployment and one service. What is a service read on!
kubectl get all
What the heck is a service?
Now we have multiple pod’s running on a node how do they communicate among themselves as well to the external world, it is via Services. Of course we will not want to expose everything to the external world for example database, so what k8s does is, it provides two type of services one is Nodeport which to expose to the external world another is clustered ip type which is used to communicate internally with pods. So lets create a service for our hello world application.
We are defining kind as service and type as Nodeport we are exposing the port 80. Important thing is how will it know which deployment it has to map to it is via selector(labels and tags) which is ui5 in our case.
apiVersion: v1 kind: Service metadata: name: ui5svc labels: run: ui5svc spec: type: NodePort ports: - port: 80 protocol: TCP selector: app: ui5
Lets create the service
kubectl create -f ui5svc.yaml
Checking what all is created, we can see a service is also created with port 30627.
Access this service
minikube service ui5svc
We can see our app running on Kubernetes
What is next?
So far we have covered the cycle of creating the docker image, publishing it to docker hub and then deploying it via Kubernetes. We have taken the first steps plan is to extend this app and explore other k8s features, learn and share. Feel free to provide your feedback!.