Over a decade span of cloud, multiple tools and technologies are created to work with the cloud. K8s (Kubernetes) is one of the widely used tools as part of development on the cloud. Whether it is server management or resources management for your production environment or developing an application and deploying it on Cloud, Kubernetes plays a crucial role.
Staring from running your container (docker, rkt etc) which contains your code binary to making it highly available for running servers, Kubernetes is used as an orchestration tool. Even though there is a team in every organisation (Operations team) to handle all the deployment processes, but it is important to understand the basic gesture of Kubernetes for the developers. (DevOps — a terminology used where developers and operations team work together to make an application available for customers).
Kubernetes provides resources/objects to work with clusters. Pods, services, deployments, replica sets etc. are some of the examples of Kube objects. I will start with creating a deployment and exposing it for accessing outside the cluster to give an idea of how Kubernetes can be used to deploy your application on the cloud.
You can start with creating a cluster on the IBM cloud for free.
- Create a free lite account on IBM cloud.
- Search for Kubernetes service in IBM Catalog.
- Create a cluster and start playing with it.
IBM Cloud provides a service to create an IBM Kubernetes cluster which will come with a master and worker node by default. It is easy to set up and play with to understand about cloud and its components or you can start with installing minikube in your local environment first. The benefit of starting with the IBM Kubernetes service is that it provides you with a multi-zone load balance which is not available in minikube.
Let’s start with creating a deployment with a basic demo app, then exposing it using a service to the external world.
Deployment is a Kube resource that contains pods running an application pulled using container images.
Kubectl applies -f hello-world.YAML
will create a deployment using above mentioned manifest file. We can verify the status of running pods in deployment using kubectl get pods.
The created pod will have a demo app image pulled from gcr.io/kuar-demo/kuard-amd64:blue. The name of the deployment will be declared in line 4 as hello-world-app.
Three main sections in the deployment manifest file are:
- Metadata: contains name, labels, annotations in each Kubernetes object, used to link the resources like creating a service for a pod or adding replicas to a deployment.
- Spec Selector: specify the selector to match the label of the pod created to be used by deployment.
- Spec template: section contains the detail of pod template with label app=hello-world. This section also declares the container image and the port on which it will run.
Kubernetes Pods are created and destroyed to match the state of your cluster. Pods are nonpermanent resources. If you use a Deployment to run your app, it can create and destroy Pods dynamically.
Service is a Kubernetes resource used for service discovery inside a cluster. It is used to discover IP of running pods. Since pods are temporary and each time an operation happens on deployment or a pod is failed with an error, a new pod get started and a new IP address is assigned to it. To keep track of running pods IP inside a cluster, services are used.
- One way is to create a service.YAML file with type set to node port with a nodePort address and targetPort which will target to the container port on which demo app is running inside deployment.
Kubectl apply -f hello-world-svc.yaml
will create a service to expose running pod with label hello-world outside of cluster with nodePort 31345 and you can access it using public endpoint( minikube ip ).
- Another way is to expose a deployment using kubectl client. The two arguments that should be passed are name and type (will define the type of service as a load balancer or nodeport).
kubectl expose deployment hello-world-app –type=NodePort –name=hello-world-svc
The issue in exposing deployment using the Kube client is that it will select exposed port randomly from a range (default: 30000–32767). Although you can also pass the service node port using –service-node-port-range.
The next step will be to view the cluster IP on which your app is exposed from outside the cluster. We can use kubectl cluster-infoto view the external ip of cluster and use it with node port to access the app, in case of minikube, we can use command minikube ip. Then we can use curl or rest client to view a running demo app on http://(minikube ip):<NodePort> For example in above case: http://(minikube ip):31345.