Learn Kubernetes Basics - Kubernetes
Learn Kubernetes Basics - Kubernetes
io/docs/tutorials/kubernetes-basics/_print/
• 1: Create a Cluster
◦ 1.1: Using Minikube to Create a Cluster
• 2: Deploy an App
◦ 2.1: Using kubectl to Create a Deployment
• 3: Explore Your App
◦ 3.1: Viewing Pods and Nodes
• 4: Expose Your App Publicly
◦ 4.1: Using a Service to Expose Your App
• 5: Scale Your App
◦ 5.1: Running Multiple Instances of Your App
• 6: Update Your App
◦ 6.1: Performing a Rolling Update
Kubernetes Basics
This tutorial provides a walkthrough of the basics of the
Kubernetes cluster orchestration system. Each module contains
some background information on major Kubernetes features
and concepts, and a tutorial for you to follow along.
1 of 23 11-10-2024, 20:52
Learn Kubernetes Basics | Kubernetes https://kubernetes.io/docs/tutorials/kubernetes-basics/_print/
2 of 23 11-10-2024, 20:52
Learn Kubernetes Basics | Kubernetes https://kubernetes.io/docs/tutorials/kubernetes-basics/_print/
Objectives
• Learn what a Kubernetes cluster is.
• Learn what Minikube is.
• Start a Kubernetes cluster on your computer.
Kubernetes Clusters
Summary:
• Kubernetes
The abstractions in Kubernetes allow you to deploy cluster
containerized applications to a cluster without tying • Minikube
them speci�cally to individual machines. To make use of
this new model of deployment, applications need to be
packaged in a way that decouples them from individual
hosts: they need to be containerized. Containerized Kubernetes is a
applications are more �exible and available than in past production-grade,
deployment models, where applications were installed open-source platform
directly onto speci�c machines as packages deeply that orchestrates the
integrated into the host. placement
(scheduling) and
Kubernetes is execution of
an open-source platform and is production-ready. application containers
within and across
A Kubernetes cluster consists of two types of resources:
computer clusters.
• The coordinates the cluster
• are the workers that run applications
Cluster Diagram
Node
Control Plane
Node Processes
Kubernetes Cluster
3 of 23 11-10-2024, 20:52
Learn Kubernetes Basics | Kubernetes https://kubernetes.io/docs/tutorials/kubernetes-basics/_print/
Each node
has a Kubelet, which is an agent for managing the node
and communicating with the Kubernetes control plane.
The node should also have tools for handling container
operations, such as containerd or CRI-O. A Kubernetes
cluster that handles production tra�c should have a
minimum of three nodes because if one node goes
down, both an etcd member and a control plane
instance are lost, and redundancy is compromised. You
can mitigate this risk by adding more control plane
nodes.
Now that you know more about what Kubernetes is, visit
Hello Minikube to try this out on your computer.
4 of 23 11-10-2024, 20:52
Learn Kubernetes Basics | Kubernetes https://kubernetes.io/docs/tutorials/kubernetes-basics/_print/
Objectives
• Learn about application Deployments.
• Deploy your �rst app on Kubernetes with kubectl.
Kubernetes Deployments
Summary:
• Deployments
• Kubectl
This tutorial uses a container that requires the
AMD64 architecture. If you are using minikube on a
computer with a di�erent CPU architecture, you
could try using minikube with a driver that can
A Deployment is
emulate AMD64. For example, the Docker Desktop
responsible for
driver can do this.
creating and updating
instances of your
application
Once you have a running Kubernetes cluster, you can
deploy your containerized applications on top of it. To do
so, you create a Kubernetes . The
Deployment instructs Kubernetes how to create and
update instances of your application. Once you've
created a Deployment, the Kubernetes control plane
schedules the application instances included in that
Deployment to run on individual Nodes in the cluster.
5 of 23 11-10-2024, 20:52
Learn Kubernetes Basics | Kubernetes https://kubernetes.io/docs/tutorials/kubernetes-basics/_print/
Node
containerized app
Deployment
Control Plane
node processes
Kubernetes Cluster
kubectl basics
The common format of a kubectl command is: kubectl
action resource
Check that kubectl is installed and you can see both the
client and the server versions.
6 of 23 11-10-2024, 20:52
Learn Kubernetes Basics | Kubernetes https://kubernetes.io/docs/tutorials/kubernetes-basics/_print/
Deploy an app
Let’s deploy our �rst app on Kubernetes with the kubectl create deployment command.
We need to provide the deployment name and app image location (include the full
repository url for images hosted outside Docker Hub).
Great! You just deployed your �rst application by creating a deployment. This
performed a few things for you:
• searched for a suitable node where an instance of the application could be run
(we have only 1 available node)
• scheduled the application to run on that Node
• con�gured the cluster to reschedule the instance on a new Node when needed
We see that there is 1 deployment running a single instance of your app. The instance is
running inside a container on your node.
We will cover other options on how to expose your application outside the Kubernetes
cluster later, in Module 4. Also as a basic tutorial, we're not explaining what Pods are in
any detail here, it will be covered in later topics.
The kubectl proxy command can create a proxy that will forward communications into
the cluster-wide, private network. The proxy can be terminated by pressing control-C
and won't show any output while it's running.
kubectl proxy
We now have a connection between our host (the terminal) and the Kubernetes cluster.
The proxy enables direct access to the API from these terminals.
You can see all those APIs hosted through the proxy endpoint. For example, we can
query the version directly through the API using the curl command:
curl http://localhost:8001/version
If port 8001 is not accessible, ensure that the kubectl proxy that you started
above is running in the second terminal.
The API server will automatically create an endpoint for each pod, based on the pod
name, that is also accessible through the proxy.
First we need to get the Pod name, and we'll store it in the environment variable
POD_NAME:
You can access the Pod through the proxied API, by running:
curl http://localhost:8001/api/v1/namespaces/default/pods/$POD_NAME:8080/proxy/
7 of 23 11-10-2024, 20:52
Learn Kubernetes Basics | Kubernetes https://kubernetes.io/docs/tutorials/kubernetes-basics/_print/
In order for the new Deployment to be accessible without using the proxy, a Service is
required which will be explained in Module 4.
8 of 23 11-10-2024, 20:52
Learn Kubernetes Basics | Kubernetes https://kubernetes.io/docs/tutorials/kubernetes-basics/_print/
Objectives
• Learn about Kubernetes Pods.
• Learn about Kubernetes Nodes.
• Troubleshoot deployed applications.
Pods overview
9 of 23 11-10-2024, 20:52
Learn Kubernetes Basics | Kubernetes https://kubernetes.io/docs/tutorials/kubernetes-basics/_print/
Node overview
10 of 23 11-10-2024, 20:52
Learn Kubernetes Basics | Kubernetes https://kubernetes.io/docs/tutorials/kubernetes-basics/_print/
If no pods are running, please wait a couple of seconds and list the Pods again. You can
continue once you see one Pod running.
Next, to view what containers are inside that Pod and what images are used to build
those containers we run the kubectl describe pods command:
We see here details about the Pod’s container: IP address, the ports used and a list of
events related to the lifecycle of the Pod.
The output of the describe subcommand is extensive and covers some concepts that
we didn’t explain yet, but don’t worry, they will become familiar by the end of this
bootcamp.
the describe subcommand can be used to get detailed information about most of
the Kubernetes primitives, including Nodes, Pods, and Deployments. The describe output is
designed to be human readable, not to be scripted against.
kubectl proxy
Now again, we'll get the Pod name and query that pod directly through the proxy. To
get the Pod name and store it in the POD_NAME environment variable:
curl http://localhost:8001/api/v1/namespaces/default/pods/$POD_NAME:8080/proxy/
We don't need to specify the container name, because we only have one container
inside the pod.
11 of 23 11-10-2024, 20:52
Learn Kubernetes Basics | Kubernetes https://kubernetes.io/docs/tutorials/kubernetes-basics/_print/
Again, it's worth mentioning that the name of the container itself can be omitted since
we only have a single container in the Pod.
We have now an open console on the container where we run our NodeJS application.
The source code of the app is in the server.js �le:
cat server.js
curl http://localhost:8080
here we used localhost because we executed the command inside the NodeJS Pod. If
you cannot connect to localhost:8080, check to make sure you have run the kubectl exec
command and are launching the command from within the Pod
12 of 23 11-10-2024, 20:52
Learn Kubernetes Basics | Kubernetes https://kubernetes.io/docs/tutorials/kubernetes-basics/_print/
Objectives
• Learn about a Service in Kubernetes
• Understand how labels and selectors relate to a
Service
• Expose an application outside a Kubernetes cluster
using a Service
13 of 23 11-10-2024, 20:52
Learn Kubernetes Basics | Kubernetes https://kubernetes.io/docs/tutorials/kubernetes-basics/_print/
14 of 23 11-10-2024, 20:52
Learn Kubernetes Basics | Kubernetes https://kubernetes.io/docs/tutorials/kubernetes-basics/_print/
If no Pods are running then it means the objects from the previous tutorials were
cleaned up. In this case, go back and recreate the deployment from the Using kubectl to
create a Deployment tutorial. Please wait a couple of seconds and list the Pods again.
You can continue once you see the one Pod running.
We have a Service called kubernetes that is created by default when minikube starts
the cluster. To create a new service and expose it to external tra�c we'll use the expose
command with NodePort as parameter.
We have now a running Service called kubernetes-bootcamp. Here we see that the
Service received a unique cluster-IP, an internal port and an external-IP (the IP of the
Node).
To �nd out what port was opened externally (for the type: NodePort Service) we’ll run
the describe service subcommand:
Create an environment variable called NODE_PORT that has the value of the Node port
assigned:
Now we can test that the app is exposed outside of the cluster using curl , the IP
address of the Node and the externally exposed port:
If you're running minikube with Docker Desktop as the container driver, a minikube
tunnel is needed. This is because containers inside Docker Desktop are isolated
from your host computer.
http://127.0.0.1:51082
! Because you are using a Docker driver on darwin, the terminal needs to be
open to run it.
15 of 23 11-10-2024, 20:52
Learn Kubernetes Basics | Kubernetes https://kubernetes.io/docs/tutorials/kubernetes-basics/_print/
Let’s use this label to query our list of Pods. We’ll use the kubectl get pods command
with -l as a parameter, followed by the label values:
Get the name of the Pod and store it in the POD_NAME environment variable:
To apply a new label we use the label subcommand followed by the object type,
object name and the new label:
This will apply a new label to our Pod (we pinned the application version to the Pod),
and we can check it with the describe pod command:
We see here that the label is attached now to our Pod. And we can query now the list of
pods using the new label:
This con�rms that our Service was removed. To con�rm that route is not exposed
anymore you can curl the previously exposed IP and port:
This proves that the application is not reachable anymore from outside of the cluster.
You can con�rm that the app is still running with a curl from inside the pod:
We see here that the application is up. This is because the Deployment is managing the
application. To shut down the application, you would need to delete the Deployment as
well.
16 of 23 11-10-2024, 20:52
Learn Kubernetes Basics | Kubernetes https://kubernetes.io/docs/tutorials/kubernetes-basics/_print/
Objectives
• Scale an app using kubectl.
Summary:
• Scaling a
Scaling an application Deployment
If you are trying this after the previous section , then you may have deleted the
service you created, or have created a Service of type: NodePort. In this section, it
is assumed that a service with type: LoadBalancer is created for the kubernetes-
bootcamp Deployment.
If you have not deleted the Service created in the previous section, �rst delete that
Service and then run the following command to create a new Service with its type
set to LoadBalancer:
Scaling overview
17 of 23 11-10-2024, 20:52
Learn Kubernetes Basics | Kubernetes https://kubernetes.io/docs/tutorials/kubernetes-basics/_print/
Scaling a Deployment
To list your Deployments, use the get deployments subcommand:
We should have 1 Pod. If not, run the command again. This shows:
kubectl get rs
• DESIRED displays the desired number of replicas of the application, which you
de�ne when you create the Deployment. This is the desired state.
• CURRENT displays how many replicas are currently running.
Next, let’s scale the Deployment to 4 replicas. We’ll use the kubectl scale command,
followed by the Deployment type, name and desired number of instances:
18 of 23 11-10-2024, 20:52
Learn Kubernetes Basics | Kubernetes https://kubernetes.io/docs/tutorials/kubernetes-basics/_print/
The change was applied, and we have 4 instances of the application available. Next, let’s
check if the number of Pods changed:
There are 4 Pods now, with di�erent IP addresses. The change was registered in the
Deployment events log. To check that, use the describe subcommand:
You can also view in the output of this command that there are 4 replicas now.
Load Balancing
Let's check that the Service is load-balancing the tra�c. To �nd out the exposed IP and
Port we can use the describe service as we learned in the previous part of the tutorial:
Create an environment variable called NODE_PORT that has a value as the Node port:
echo NODE_PORT=$NODE_PORT
Next, we’ll do a curl to the exposed IP address and port. Execute the command
multiple times:
We hit a di�erent Pod with every request. This demonstrates that the load-balancing is
working.
If you're running minikube with Docker Desktop as the container driver, a minikube
tunnel is needed. This is because containers inside Docker Desktop are isolated
from your host computer.
http://127.0.0.1:51082
! Because you are using a Docker driver on darwin, the terminal needs to be
open to run it.
Scale Down
To scale down the Deployment to 2 replicas, run again the scale subcommand:
List the Deployments to check if the change was applied with the get deployments
subcommand:
The number of replicas decreased to 2. List the number of Pods, with get pods :
19 of 23 11-10-2024, 20:52
Learn Kubernetes Basics | Kubernetes https://kubernetes.io/docs/tutorials/kubernetes-basics/_print/
20 of 23 11-10-2024, 20:52
Learn Kubernetes Basics | Kubernetes https://kubernetes.io/docs/tutorials/kubernetes-basics/_print/
Objectives
• Perform a rolling update using kubectl.
Updating an application
Users expect applications to be available all the time,
Summary:
and developers are expected to deploy new versions of • Updating an
them several times a day. In Kubernetes this is done with app
rolling updates. A allows a Deployment
update to take place with zero downtime. It does this by
incrementally replacing the current Pods with new ones.
The new Pods are scheduled on Nodes with available Rolling updates allow
resources, and Kubernetes waits for those new Pods to Deployments' update
start before removing the old Pods. to take place with
zero downtime by
In the previous module we scaled our application to run
incrementally
multiple instances. This is a requirement for performing
updating Pods
updates without a�ecting application availability. By
instances with new
default, the maximum number of Pods that can be
ones.
unavailable during the update and the maximum
number of new Pods that can be created, is one. Both
options can be con�gured to either numbers or
percentages (of Pods). In Kubernetes, updates are
versioned and any Deployment update can be reverted
to a previous (stable) version.
21 of 23 11-10-2024, 20:52
Learn Kubernetes Basics | Kubernetes https://kubernetes.io/docs/tutorials/kubernetes-basics/_print/
To view the current image version of the app, run the describe pods subcommand and
look for the Image �eld:
To update the image of the application to version 2, use the set image subcommand,
followed by the deployment name and the new image version:
The command noti�ed the Deployment to use a di�erent image for your app and
initiated a rolling update. Check the status of the new Pods, and view the old one
terminating with the get pods subcommand:
Verify an update
First, check that the service is running, as you might have deleted it in previous tutorial
step, run describe services/kubernetes-bootcamp . If it's missing, you can create it again
with:
Create an environment variable called NODE_PORT that has the value of the Node port
assigned:
Every time you run the curl command, you will hit a di�erent Pod. Notice that all Pods
are now running the latest version (v2).
You can also con�rm the update by running the rollout status subcommand:
To view the current image version of the app, run the describe pods subcommand:
In the Image �eld of the output, verify that you are running the latest image version
22 of 23 11-10-2024, 20:52
Learn Kubernetes Basics | Kubernetes https://kubernetes.io/docs/tutorials/kubernetes-basics/_print/
(v2).
Notice that the output doesn't list the desired number of available Pods. Run the get
pods subcommand to list all Pods:
To get more insight into the problem, run the describe pods subcommand:
In the Events section of the output for the a�ected Pods, notice that the v10 image
version did not exist in the repository.
To roll back the deployment to your last working version, use the rollout undo
subcommand:
The rollout undo command reverts the deployment to the previous known state (v2 of
the image). Updates are versioned and you can revert to any previously known state of
a Deployment.
To check the image deployed on the running Pods, use the describe pods
subcommand:
The Deployment is once again using a stable version of the app (v2). The rollback was
successful.
23 of 23 11-10-2024, 20:52