Introduction
In previous post about Kubernetes basics, we learned that a kubernetes cluster is made of masters and nodes. Masters contains the cluster control plane and nodes are where we run our applications (containers).
We also learned about workload objects in kubernetes such as Pods, services and deployments. Today, we will learn more about pods, we also see typical application deployment workflow and the steps involved to take our application source code, package it into a container image, push it to a registry, then reference it in our kubernetes pod manifest file and deploy it to kubernetes cluster.
Pods are the smallest deployable unit in kubernetes API. Pod hosts the container(s). Pods can host single or multiple containers. While pods can have multiple containers, a pod and all of its containers are deployed to a single node.
We can define Pods in a YAML manifest file and we use kubectl command to post these manifests to API server of kubernetes cluster.
Application Deployment Workflow
Following are steps for a typical application deployment workflow in kubernetes. Now first 3 steps are about app code and building and saving an image, this is typical docker stuff. The last two steps are where we are dealing with kubernetes.
- Get Application Code.
- Build code into a container image.
- Store image into a repo (e.g. DockerHub).
- Define Kubernetes manifest (YAML file).
- Post manifest to API Server (e.g. using kubectl tool).
Application Code and Docker Image
The first three item in workflow related to creating an application and then packaging it up into a docker image. We will not go into much details of docker part, but just to give you this background, I have setup a git-hub repository with a very simple .NET Core Web Api application and a docker file to package it up.
However, if you like, you can use other frameworks e.g. may be a NodeJS application or a Java application.
Dockerfile
Now we can use following commands to create an image and run a container:
docker image build -t dotnettokengen .
docker run --rm --name dotnettokengen -p 5000:5000 dotnettokengen
once container is running, you can make a GET request to token endpoint as shown below:
So, very basic .NET Core Web Application and we have a docker image of this application:
Kubernetes Pod Manifest
Once we have a docker image in registry, we can now wrap it in a Kubernetes Pod.
As you can see that this is very simple to follow whats going on in the manifest.
We can split this information in two parts. First part is dealing with kubernetes related items, there is apiVersion, kind which is set to Pod, some meta data attached to Pod.
Under spec, we have information about the container image and ports.
So this manifest is nesting a container into a Pod. Once we have this pod mainfest file, we can use kubectl to post it to API Server. Following command shows that:
kubectl apply -f pod-tokengen.yaml
here is the output of the command:
Pod Deployment
Lets get the list of pods by using following command:
kubectl get pods
Notice that our pod is not yet started and status is showing ImagePullBackOff. By default Kubernetes looks in the public Docker registry to find images. If your image doesn’t exist there it won’t be able to pull it.
There are different options to solve this issue. However, the easiest one, which I am going to use is to push the image to dockerhub.
Lets delete the pod using the following command:
kubectl delete pod tokengen
Then I pushed the local docker image to new created docker repository on DockerHub as shown below:
Once the image is pushed, I referenced it in the pod manifest file as shown below:
Let’s use the kubectl commands again and check if it is deployed:
and as you can see the pod is running as expected.
Exposing a Pod Port
So, our pod is deployed and we know that it is running a container which is listening on port 5000. However, now if we try to make an HTTP Get Request to token endpoint, we will not get any response.
Pods and containers are only accessible within kubernetes cluster by default. One way to expose a container port externally is by using command kubectl port-forward [podname] ports as shown below:
kubectl port-forward tokengen 5000:5000
Now, if we try to make an HTTP Get request, we will receive the response from the application:
Summary
In this post, we learn about kubernetes pods, what role they play and how to define them in a YAML manifest file and then deploy those via kubectl command line.
You can download the application code and pod yaml file from this git repository.
Let me know if you have some comments or questions. Till next time, Happy Coding.
My Recent Books
Discover more from Hex Quote
Subscribe to get the latest posts sent to your email.
1 thought on “Kubernetes Basics – Pods”
Comments are closed.