🔥Your First Kubernetes Pod¶
In this project, we will deploy a Docker container on Kubernetes. One of the benefits of Kubernetes is that it will make sure your application is running all the time. For example, if your Docker container stops, you have to manually restart it. But with Kubernetes it's done automatically.
🎯 What you'll learn¶
- Writing a Kubernetes Pod YAML.
- Creating your first Pod.
- Port-forwarding the Pod to view your application.
🛑 Prerequisites¶
- Kubernetes cluster, local or in the cloud. If you want to install a local k3d cluster, follow this guide.
- Docker & kubectl installed on your machine.
Introduction¶
To run a container on Kubernetes we use a Pod. A Pod is an abstraction over a container. Each Pod has one main container and one or more helper(side-car) containers.
Create a Pod YAML¶
To create an Kubernetes object we write specific instructions. In this case its a Pod. Once you are done, give this file to kubernetes and it takes care of the rest.
- A Pod is created using the
apiVersion
v1
.
k8s-memes
.
3. Using spec
we specify the containers we want to define, what Docker image they use, ports we need to open etc.
-
You can add your own Docker image using
DockerHubUserName/ImageName:Tag
-
Port the application is to be exposed on and using what protocol.
-
Define the amount of CPU and memory a pod can use. Learn more about them here. Also read "Stop Using CPU Limits" to understand the misconceptions around CPU limits.
This is the result of the previous steps. Save it to a file called first-pod.yaml
first-pod.yaml | |
---|---|
Deploy the Pod¶
Next, lets use the Kubernetes Pod YAML to create our Pod.
Run kubectl apply -f first-pod.yaml
to create a Pod with all our specifications.
View the Pod¶
The Pod could take a few seconds to start running. You can see this using kubectl get pod meme-app
Access the application¶
port-forward
is a feature to expose any port of a Pod locally. It only works as long as the Pod is running and the terminal is open.
Voilà!!🎉 There, we have your first Kubernetes Pod. Well, done.👏
Delete the Pod¶
If you no longer need a Pod, you can use kubectl delete -f meme-pod.yaml
. Alternatively, kubectl delete pod meme-app
can also be used.
❓3 Questions to check your Kubernetes Pod understanding¶
- How do you see which Pods are running?
- What is the difference between a container and a Pod?
- Can you run a container on Kubernetes without a Pod?