In this series, we previously had containerized an application. Here, we created a Dockerfile and ran our application—also, we published our image to docker hub. Now, we are going to pull our image we created and pushed before and create a pod and run it in our K8s cluster.
While deploying an application, Kubernetes does not directly deploy containers on the worker nodes. Those containers are first encapsulated into objects known as Pods. Hence, Pods are the smallest object that contains a single instance of an application in Kubernetes.
Note: Single Pods can have multiple containers. But, they usually don't have multiple containers of same kind.
Now, let's create our own first pod in this series. Before that, let's check if we have any pods running beforehand.
kubectl get pods
There is no pods running on default namespace.
Let's get going.
1) We need first to create a YAML file. We would require apiVersion, kind, metadata, and spec to be in the file.
- name: pyapi-pod
- containerPort: 8000
- apiVersion specifies the version of api we use (v1).
- kind specifies what we are creating here. In this case, we are creating a Pod
- metadata has our pod's information here. We named our pod pyapi-pod
- spec declares the state and characteristics of our pod. Here we used an image from our previous blog and specified the port.
2) Now, to create our pod, we use the following command.
kubectl create -f pyapi-pod.yaml
Your pod is running.
Another feature we have in k8s is we can describe our pods. For this, we use the following command,
kubectl describe pods pyapi-pod
Here, you can see all the information about your pods.
Now, let's try visiting our python app. we hosted it on 127.0.0.1:8000.
Oh no! The application is unavailable.
As we did in Docker, we need to forward our port to access it via our main machine. We have multiple ways/services in k8s, but in this case, we will use port-forward.
We are going to use the following command to forward our pod.
kubectl port-forward pyapi-pod 8000:8000
Here, pyapi-pod is our pod name (the same can be done for deployments, replicasets, and services). It is then followed by the pods we desire to forward.
Now, visit your URL, http://127.0.0.1:8000/ in our case,
Your application is now perfectly running via pod. Now, we can create Kubernetes Pods.