Understanding Kubernetes visually

Learn Kubernetes: From Pods and Nodes to Scaling and Services

Kubernetes is used for container orchestration.

In the real world, a product or system is expected to run 24/7. Users should be able to access it without worrying about what’s happening behind the scenes.

At the same time, developers are continuously working on the application, fixing bugs, adding features, and deploying new versions.

There shouldn’t be unnecessary downtime or interruptions every time something changes.

For example:

This is where Kubernetes comes in.

Kubernetes helps us manage containers across multiple machines. It can restart failed workloads, maintain multiple replicas of an application, distribute traffic through Services, and manage application updates.

It also helps us scale applications when the demand increases.

Let’s understand how it works.

1. Understanding Kubernetes Architecture

Before getting into the actual commands, we need to understand a few things.

Cluster

A Kubernetes cluster consists of a Control Plane and one or more Worker Nodes.

Control Plane:
This is responsible for managing the cluster. It maintains the desired state, schedules Pods, and coordinates the cluster.

Worker Nodes:
These are the machines that run our application workloads. More specifically, Nodes run Pods, and Pods contain containers.

The Control Plane communicates with the Nodes primarily through the Kubernetes API and the agents running on those Nodes.

A few important components:

What runs inside a Node?

Every Worker Node runs a few important components.

Kubelet — Responsible for communicating with the Control Plane and ensuring that the Pods assigned to its Node are running correctly.

Container Runtime — Responsible for running containers. For example, containerd.

Kube-proxy — Maintains network rules that help implement Kubernetes Service networking. Some clusters use alternative networking implementations.

Cluster structure

So, putting everything together:

Minikube

For learning and local development, we don’t necessarily need a cloud environment with multiple machines.

We can use Minikube.

Minikube creates and manages a local Kubernetes cluster on our machine. It allows us to experiment with Kubernetes without setting up an actual production cluster.

2. What is a Pod?

A Pod is the smallest deployable unit in Kubernetes. It can contain one or multiple containers.

Why does Kubernetes need a Pod instead of directly managing individual containers?
Because Kubernetes needs a unit that can be scheduled, assigned networking, given storage, and managed as a whole.

A Pod provides its containers with a shared environment.

For example, containers inside the same Pod can share:

Usually, we run one main application container inside a Pod. However, we can have multiple containers when they need to work closely together.

For example, an application container and a sidecar container that handles logging.

One important thing: A Pod is tied to the Node where it gets scheduled. If it gets terminated, Kubernetes may create a replacement Pod, but that replacement is a new Pod with a different identity and potentially a different IP address.

Here, in the image below, understand what’s happening overall.

3. Creating a Kubernetes Cluster

Let’s start working with Kubernetes.

First, start Minikube:

minikube start

This creates and starts a local Kubernetes cluster.
To check the status:

minikube status

To open the Kubernetes Dashboard:

minikube dashboard

We can also get the dashboard URL without automatically opening the browser:

minikube dashboard --url

4. Creating a Deployment

Now, let’s deploy an application.

We use kubectl, which is the command-line tool used to communicate with the Kubernetes API.

kubectl create deployment kubernetes-bootcamp \
--image=gcr.io/google-samples/kubernetes-bootcamp:v1

What happens here?

We are creating a Deployment named kubernetes-bootcamp using the specified container image.

The Deployment creates and manages a ReplicaSet, which in turn creates the Pods.

Learn it again here,
* A Kubernetes Cluster is the entire environment.

* A Node is a machine.
Example:
- physical server in a data center.
- virtual machine running on AWS, Google Cloud, Azure, etc
- local machine when using Minikube.

* Pod is a unit running on a Node. Each Pod contains one or more containers.

Back,

screenshot from sum-mit45.tistory.com

To list our Deployments:

kubectl get deployments

To see the Pods:

kubectl get pods

To see the Nodes:

kubectl get nodes

Notice that we created a Deployment, not a Pod directly.

This is because Deployments give us a way to manage application replicas, recover from failures, and perform updates.

5. Accessing the Application

Now that we have deployed our application, how do we access it?

For this tutorial, we’ll use kubectl proxy.

kubectl proxy

This starts a proxy server on our local machine, usually at:

http://localhost:8001

The proxy connects our local machine to the Kubernetes API Server.
We can verify that it’s working:

curl http://localhost:8001/version

We should get information about the Kubernetes API Server version.

Accessing a Pod through the API Server

First, let’s get the Pod name.

export POD_NAME=$(kubectl get pods \
-o go-template='{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
echo "Name of the Pod: $POD_NAME"

Here, we’re using a Go template to extract the names of the Pods.
For this example, we assume there is only one matching Pod.

Now, we can access the application through the Kubernetes API proxy:

curl http://localhost:8001/api/v1/namespaces/default/pods/$POD_NAME:8080/proxy/

Here:

6. Understanding Nodes and Pods

Let’s look at them again, now that we’ve actually created a Deployment.

Pods

A Pod is one or more application containers (similar to Docker containers) and provides a shared environment.

It includes:

A Pod is the atomic deployable unit of Kubernetes.

When we create a Deployment, it creates Pods through a ReplicaSet rather than creating containers directly.

Nodes

A Pod always runs on a Node.

A Node is a worker machine in Kubernetes. It can be a physical or virtual machine, depending on the cluster.

A Node can run multiple Pods.

Some important points:

We can list Nodes using:

kubectl get nodes

To see which Node each Pod is running on:

kubectl get pods -o wide

The output includes the Node name and Pod IP address.

7. ReplicaSet and Replication

Now, let’s say we have deployed an application, and it is running inside one Pod.

What happens if that Pod crashes?
Our application becomes unavailable until something brings it back.

We could manually create another Pod, but that’s not how we want to manage production applications.

We need something that continuously ensures the required number of Pods is running.

This is where ReplicaSet comes in.

A ReplicaSet maintains the desired number of identical Pods.

For example, if we want three instances of our application, we can configure a Deployment with three replicas.

The ReplicaSet ensures that three Pods exist.

If one Pod crashes or gets deleted, the ReplicaSet notices that the actual number of Pods is lower than the desired number and creates another one.

This is called maintaining the desired state.

We can see our ReplicaSets using:

kubectl get rs

We can also inspect a particular ReplicaSet:

kubectl describe rs 

Scaling a Deployment

Let’s scale our application from one Pod to four Pods.

kubectl scale deployments/kubernetes-bootcamp --replicas=4

Now, Kubernetes will work towards making sure four replicas exist.

Check the Deployment:

kubectl get deployments

Check the ReplicaSet:

kubectl get rs

Check the Pods:

kubectl get pods -o wide

We should now see four Pods running.

Notice that we didn’t manually create four Pods. We just changed the desired replica count, and Kubernetes handled the rest.

We can also scale down:

kubectl scale deployments/kubernetes-bootcamp --replicas=2

Now, the ReplicaSet will terminate the extra Pods to reach the desired count of two.

One important distinction: ReplicaSets maintain the number of Pods. They don’t distribute network traffic between them. That’s the responsibility of Services.

8. Services in Kubernetes

Now, imagine we have four Pods running the same application.

Each Pod has its own IP address.

But there is a problem.

Pods are not permanent. They can be deleted, recreated, or moved to another Node. When this happens, their IP addresses can change.

We cannot expect users or other applications to keep track of individual Pod IP addresses.

Also, if we have multiple replicas, we need a way to distribute incoming traffic among them.

This is where Services come in.

A Service provides a stable network endpoint for accessing a group of Pods.

It uses labels and selectors to identify which Pods should receive traffic.

For example, imagine our Pods have the following label:

labels:
app: kubernetes-bootcamp

We can create a Service that selects all Pods with this label.

The Service provides a stable IP address and DNS name, even if the individual Pods change.

How does a Service work?

Consider this structure:

            User Requests
|
|
Service
|
---------------------
| | |
Pod 1 Pod 2 Pod 3

When a user sends a request to the Service, Kubernetes networking directs it to one of the matching, available Pods.

The user doesn’t need to know which Pod is receiving the request.

This also means that if one Pod becomes unavailable, the Service can stop sending traffic to it once its readiness state is reflected in the Service endpoints.

Creating a Service

Let’s expose our Deployment.

kubectl expose deployment kubernetes-bootcamp \
--type=NodePort \
--port=8080

This creates a Service for our Deployment.

To see the Services:

kubectl get services

To get more information:

kubectl describe service kubernetes-bootcamp

We can also inspect the Service’s endpoints:

kubectl get endpoints kubernetes-bootcamp

These endpoints represent the network destinations associated with the Service.

For newer Kubernetes configurations, we can also inspect EndpointSlices:

kubectl get endpointslices

Types of Services

A Service can be exposed in different ways by specifying its type in the Service specification.

There are four main types.

1. ClusterIP (Default)

This makes the Service accessible only from within the Kubernetes cluster.

For example:

apiVersion: v1
kind: Service
metadata:
name: backend-service
spec:
type: ClusterIP
selector:
app: backend
ports:
- port: 80
targetPort: 8080

Here:

Other applications inside the cluster can access this Service using its DNS name.

For example:

http://backend-service

2. NodePort

This makes the Service accessible from outside the cluster using:

NodeIP:NodePort

For example:

apiVersion: v1
kind: Service
metadata:
name: backend-service
spec:
type: NodePort
selector:
app: backend
ports:
- port: 80
targetPort: 8080
nodePort: 30080

Here, Kubernetes exposes the Service through a port on the Nodes.

The usual NodePort range is 30000-32767.

We can then access the application through the Node’s IP address and NodePort, assuming the network allows access.

3. LoadBalancer

This type exposes the Service externally through a cloud provider’s load balancer, if supported.

For example:

apiVersion: v1
kind: Service
metadata:
name: backend-service
spec:
type: LoadBalancer
selector:
app: backend
ports:
- port: 80
targetPort: 8080

In a supported cloud environment, Kubernetes requests an external load balancer and an external IP address.

Users can then access the application through that external endpoint.

4. ExternalName

This is a little different from the previous three.

It maps a Kubernetes Service to an external DNS name using a CNAME record.

For example:

apiVersion: v1
kind: Service
metadata:
name: external-database
spec:
type: ExternalName
externalName: database.example.com

Now, applications inside the cluster can access:

external-database

And Kubernetes DNS resolves it to:

database.example.com

Unlike the other Service types, ExternalName doesn’t select Pods or provide a proxy to them.

9. Scaling and Load Balancing Together

Now let’s connect what we’ve learned.

We have a Deployment with four replicas.

The ReplicaSet ensures that all four Pods exist.

The Service provides a stable endpoint and distributes traffic to the available Pods.

                 Users
|
|
Service
|
-----------------------------
| | | |
Pod 1 Pod 2 Pod 3 Pod 4
| | | |
-----------------------------
Nodes

If traffic increases, we can manually increase the replica count:

kubectl scale deployment kubernetes-bootcamp --replicas=8

We can also configurng is working fine, but we have developed a new version of the application.

We need to deploy that new version.

The problem is that we don’t want to stop all four Pods at once, deploy the new version, and then start them again.

That would create downtime.

This is where Deployments and rolling updates come in.

A rolling update gradually replaces the old Pods with new Pods without needing to take the entire application offline.

For example:

Initially:

Pod 1 -> v1
Pod 2 -> v1
Pod 3 -> v1
Pod 4 -> v1

During a rolling update:

Pod 1 -> v2
Pod 2 -> v1
Pod 3 -> v1
Pod 4 -> v1

Then:

Pod 1 -> v2
Pod 2 -> v2
Pod 3 -> v1
Pod 4 -> v1

Eventually:

Pod 1 -> v2
Pod 2 -> v2
Pod 3 -> v2
Pod 4 -> v2

The Deployment manages this process by creating a new ReplicaSet and gradually scaling down the old ReplicaSet while scaling up the new one.

Performing a Rolling Update

Let’s update our application image from v1 to v2.

kubectl set image deployment/kubernetes-bootcamp \
kubernetes-bootcamp=gcr.io/google-samples/kubernetes-bootcamp:v2

Here:

Kubernetes detects the change in the Deployment’s Pod template and starts a new rollout.

To check the rollout status:

kubectl rollout status deployment/kubernetes-bootcamp

To see the rollout history:

kubectl rollout history deployment/kubernetes-bootcamp

To inspect a particular revision:

kubectl rollout history deployment/kubernetes-bootcamp --revision=2

How does Kubernetes avoid downtime?

Deployments use two important settings for rolling updates:

For example:

spec:
replicas: 4
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0

Here, we have four desired replicas.

maxSurge: 1 means Kubernetes can temporarily create one additional Pod, making a total of five.

maxUnavailable: 0 means Kubernetes should not reduce the number of available replicas below four during the rollout.

Kubernetes can create a new Pod, wait for it to become ready, and then terminate an old Pod.

This is why readiness probes are important. They help Kubernetes determine when a new application instance is actually ready to receive traffic.

Rollback

What if the new version has a bug?

We can roll back to the previous revision.

kubectl rollout undo deployment/kubernetes-bootcamp

To roll back to a particular revision:

kubectl rollout undo deployment/kubernetes-bootcamp --to-revision=1

Kubernetes restores the previous Deployment configuration and performs another rollout.

This is useful when a newly deployed version causes unexpected problems.

Final Thoughts

So, putting everything together:

The important thing We should start to understand about Kubernetes is that we don’t manually manage every container or Pod.

Instead, we define what we want the system to look like, and Kubernetes continuously works towards maintaining that state.

That’s what makes it useful for running applications that need to remain available while we’re continuously developing and deploying new changes.

Enjoy.

Go Back

Read this on medium