Categories
DevOps Orchestration

Kubernetes Pods Introduction for Docker Users

Kubernetes pods are the smallest units of execution in Kubernetes. We’ll explore concepts behind Kubernetes pods and how to manage them using kubectl. Keep reading to find out more.

Introduction

I assume that you’ve already heard about Kubernetes and want to explore more. Today, we’ll explore Kubernetes Pods and how to manage them using kubectl.

Kubernetes Pods Concepts

Kubernetes pod is the most basic Kubernetes object. It’s also the smallest unit of execution in Kubernetes.

Few facts about Kubernetes pods. They:

  • wrap containers and provide an environment for them.
  • run on a Kubernetes Node.
  • provide horizontal scaling for Kubernetes.
  • live only once.
  • get a unique virtual IP address.
  • multiple pods may run on a single node.
Kubernetes pod

You can view pods as VMs or physical machines. Hence, pod loopback interface (localhost) is shared between the containers. It’s also true regarding memory and volumes. In addition, containers in the same pod have to bind to different ports. Of course, containers in different pods can use the same ports.

While it’s possible to run multiple containers in a pod, you usually do it in exceptional cases when pod containers need to be tightly coupled.

Next, let’s see how to manage Kubernetes pods using kubectl .

Prerequisites

Install on your machine:

  • docker
  • docker-compose
  • git
  • minikube

The tutorial assumes familiarity with basic git, docker, docker-compose and kubectl commands.

Manage Kuberenetes Pods using kubectl

If you come from Docker world, you probably wonder how to run container in a pod. Below, we’ll see basic CRUD kubectl commands for pods management.

For that purpose, let’s start multi-node Kubernetes cluster using minikube:

minikube start --nodes 2 -p multinode-demo

Our Kubernetes cluster will have 2 nodes:

$ kubectl get nodes
NAME                 STATUS     ROLES                  AGE   VERSION
multinode-demo       Ready      control-plane,master   86s   v1.21.2
multinode-demo-m02   NotReady   <none>                 51s   v1.21.2
  • Next let’s run 2 pods. One will run wordpress container.

$ kubectl run apache --image=httpd

  • And another one will run apache container:

kubectl run wordpress --image=wordpress

Reminds docker run, right?

  • Let’s view the pods (similar to docker ps)
$ kubectl get pods
NAME        READY   STATUS    RESTARTS   AGE
apache      1/1     Running   0          17m
wordpress   1/1     Running   0          15m
  • You wonder how to access the pods? In docker world we published container ports. In Kubernetes world we’ll use port-forwarding.
  • Forward wordpress 80 port to 8090 port on your workstation in a new terminal.

$ minikube -p multinode-demo kubectl -- port-forward wordpress 8090:80

wordpress installation
WordPress installation screen
  • Forward apache 80 port to 8080 port on your workstation in a new terminal.

$ minikube -p multinode-demo kubectl -- port-forward apache 8080:80

  • Navigate to http://localhost:8080/ and you should see It works! in your browser.
  • Let’s launch Kubernetes dashboard in original terminal where we ran minikube

$ minikube -p multinode-demo dashboard

A browser will show our Kubernetes cluster workloads which are 2 pods.

  • docker rm equivalent is kubectl remove. Let’s remove the pods:
$ kubectl delete pod wordpress apache
pod "wordpress" deleted
pod "apache" deleted
  • Finally, let’s let’s stop our cluster:

$  minikube stop -p multinode-demo

Note, that you can find more useful information in kubectl for Docker Users. Also, you would usually run pods using Kubernetes deployments. We’ll cover them in later posts.

Summary

That’s it about Kuberenetes pods. Of course, this post is mostly introductory. And we’ll definitely come back to Kubernetes pods in future posts. As always, feel free to share and comment.

Bonus:

Recommended Kubernetes courses on Pluralsight:

Sign up using this link to get exclusive discounts like 50% off your first month or 15% off an annual subscription)

Recommended Kubernetes books on Amazon:

You may find interesting below articles I wrote: