10 min read

Getting Started with Kubernetes: A Beginner's Tutorial to Deploying Your First Application

Table of Contents

Welcome to the exciting world of Kubernetes! If you’ve been hearing about container orchestration, microservices, and scalable applications, chances are Kubernetes (often abbreviated as K8s) has popped up on your radar. While its reputation for complexity might seem daunting at first, mastering Kubernetes is an invaluable skill for any modern DevOps professional.

This tutorial is designed specifically for beginners. We’ll demystify the core concepts and walk you through the practical steps of deploying your first containerized application onto a Kubernetes cluster. By the end of this guide, you’ll have a foundational understanding of Kubernetes and the confidence to take your next steps in container orchestration.

Table of contents

What is Kubernetes and Why Does It Matter?

At its heart, Kubernetes is an open-source platform for automating the deployment, scaling, and management of containerized applications. Originally developed by Google, it’s become the de facto standard for orchestrating application containers, particularly those built with Docker.

Why is Kubernetes so crucial?

  • Automated Rollouts & Rollbacks: Kubernetes intelligently manages application updates and can roll back to a previous version if something goes wrong.
  • Self-Healing: If a container crashes, Kubernetes automatically restarts it. If a node dies, it moves containers to healthy nodes.
  • Service Discovery & Load Balancing: It automatically assigns unique DNS names to services and can load balance traffic across multiple instances of your application.
  • Horizontal Scaling: Easily scale your application up or down with a simple command or based on CPU usage.
  • Declarative Configuration: You describe the desired state of your applications and infrastructure, and Kubernetes works to achieve and maintain that state.

For anyone aiming for a CKA certification or simply looking to level up their DevOps game, understanding these fundamentals is key.

Prerequisites

Before we dive into the hands-on deployment, ensure you have the following ready:

  1. Basic Understanding of Containers: Familiarity with Docker concepts like images and containers is beneficial.
  2. kubectl: The Kubernetes command-line tool. This allows you to run commands against Kubernetes clusters.
  3. A Local Kubernetes Environment: For this tutorial, we recommend Minikube, a tool that runs a single-node Kubernetes cluster on your local machine. Alternatives include Docker Desktop’s built-in Kubernetes or Kind.
  4. A Text Editor: Any code editor like VS Code will work for creating our YAML manifest files.

Setting Up Your Local Kubernetes Environment with Minikube

Let’s get your local cluster up and running.

1. Install kubectl

The kubectl command-line tool lets you run commands against Kubernetes clusters. You can use it to deploy applications, inspect and manage cluster resources, and view logs.

On macOS (using Homebrew):

brew install kubectl

On Windows (using Chocolatey):

choco install kubernetes-cli

On Linux (Debian/Ubuntu):

sudo apt-get update && sudo apt-get install -y apt-transport-https ca-certificates curl
sudo curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg
echo "deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
sudo apt-get update
sudo apt-get install -y kubectl

Verify the installation:

kubectl version --client

2. Install Minikube

Minikube is a tool that makes it easy to run a single-node Kubernetes cluster locally for development purposes.

On macOS (using Homebrew):

brew install minikube

On Windows (using Chocolatey):

choco install minikube

On Linux (download binary):

curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube

For other OS and detailed instructions, refer to the Minikube documentation.

3. Start Minikube

Now, let’s fire up your local Kubernetes cluster.

minikube start

This command will download the necessary components and start a single-node Kubernetes cluster. It might take a few minutes for the first run. Once started, you can check its status:

minikube status

You should see output indicating that minikube and kubelet are running.

Our First Application: A Simple Nginx Deployment

To keep things straightforward, we’ll deploy a basic Nginx web server. This will introduce you to the core Kubernetes building blocks: Pods, Deployments, and Services.

  • Pod: The smallest and simplest unit in Kubernetes. A Pod represents a single instance of a running process in your cluster. It can contain one or more containers (though typically one for simple applications) and shared resources like storage and network.
  • Deployment: A higher-level resource that manages a set of identical Pods. It ensures that a specified number of Pod replicas are running at any given time, handling scaling and rolling updates automatically.
  • Service: An abstract way to expose an application running on a set of Pods as a network service. Services define a logical set of Pods and a policy by which to access them.

Step-by-Step Deployment

Step 1: Define Your Deployment (nginx-deployment.yaml)

We’ll start by defining our Nginx Deployment. This YAML file describes the desired state of our application. Create a file named nginx-deployment.yaml and paste the following content:

# nginx-deployment.yaml
apiVersion: apps/v1          # Specifies the Kubernetes API version for Deployment objects
kind: Deployment             # The type of Kubernetes object we are creating
metadata:
  name: nginx-deployment     # Name of our Deployment
  labels:
    app: nginx               # Labels are key-value pairs used for organizing and selecting resources
spec:
  replicas: 2                # We want 2 instances (Pods) of our Nginx application
  selector:
    matchLabels:
      app: nginx             # This Deployment will manage Pods with the label 'app: nginx'
  template:                  # Defines the Pods that this Deployment will create
    metadata:
      labels:
        app: nginx           # Labels applied to the Pods
    spec:
      containers:            # Defines the containers within each Pod
      - name: nginx          # Name of the container
        image: nginx:latest  # The Docker image to use (Nginx latest version)
        ports:
        - containerPort: 80  # The port on which the container listens for traffic

Explanation:

  • We’re defining a Deployment named nginx-deployment.
  • It will ensure 2 replicas (Pods) of our application are running.
  • Each Pod will run an nginx:latest container, listening on port 80.
  • The labels are crucial for Kubernetes to link Deployments to Pods and Services.

Step 2: Apply the Deployment

Now, let’s tell Kubernetes to create our Deployment using kubectl apply. Navigate to the directory where you saved nginx-deployment.yaml.

kubectl apply -f nginx-deployment.yaml

You should see output similar to: deployment.apps/nginx-deployment created.

Verify that your Deployment and Pods are running:

kubectl get deployments
# Expected output:
# NAME             READY   UP-TO-DATE   AVAILABLE   AGE
# nginx-deployment   2/2     2            2           <some-age>

kubectl get pods
# Expected output (names will differ):
# NAME                             READY   STATUS    RESTARTS   AGE
# nginx-deployment-f57788b77-d5j7b   1/1     Running   0          <some-age>
# nginx-deployment-f57788b77-k7m9n   1/1     Running   0          <some-age>

You should see two Pods with Running status and 1/1 ready.

Step 3: Define Your Service (nginx-service.yaml)

Our Nginx Pods are running, but they’re not yet accessible from outside the cluster. That’s where a Service comes in. It provides a stable IP address and DNS name, acting as an internal load balancer. For local testing with Minikube, NodePort is a common Service type to expose your application.

Create a file named nginx-service.yaml:

# nginx-service.yaml
apiVersion: v1               # Specifies the Kubernetes API version for Service objects
kind: Service                # The type of Kubernetes object we are creating
metadata:
  name: nginx-service        # Name of our Service
spec:
  selector:
    app: nginx               # This Service will route traffic to Pods with the label 'app: nginx'
  ports:
    - protocol: TCP          # The protocol to use for this port
      port: 80               # The port the Service itself will listen on (inside the cluster)
      targetPort: 80         # The port on the Pods to which the Service will send traffic
  type: NodePort             # Exposes the Service on a port on each Node of the cluster
                             # For local testing, this allows access from outside the cluster.

Explanation:

  • We’re creating a Service named nginx-service.
  • It selects Pods with the app: nginx label, routing traffic to port 80 on those Pods.
  • The type: NodePort exposes the Service on a static port on each Node’s IP address. Minikube provides a simple way to get this external URL.

Step 4: Apply the Service

Apply your Service definition:

kubectl apply -f nginx-service.yaml

Output: service/nginx-service created.

Verify the Service is running:

kubectl get services
# Expected output:
# NAME           TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
# kubernetes     ClusterIP   10.96.0.1        <none>        443/TCP        <some-age>
# nginx-service  NodePort    10.106.182.134   <none>        80:3XXXX/TCP   <some-age>

Note the PORT(S) for nginx-service. The 3XXXX number is the NodePort that Minikube opened on your local machine.

Step 5: Access Your Application

To get the URL to access your Nginx application through the Minikube tunnel:

minikube service nginx-service --url

This command will return a URL like http://127.0.0.1:3XXXX. Copy this URL and paste it into your web browser. You should see the “Welcome to nginx!” default page. Congratulations, you’ve successfully deployed your first application on Kubernetes!

Scaling Your Application

One of Kubernetes’ most powerful features is its ability to scale applications effortlessly. Let’s say your Nginx server is experiencing high traffic and you need more instances.

You can scale your Deployment using a single command:

kubectl scale deployment nginx-deployment --replicas=5

Check your Pods again:

kubectl get pods

You’ll now see five Nginx Pods running! Kubernetes automatically created three new Pods to match your desired state.

To scale back down:

kubectl scale deployment nginx-deployment --replicas=1

Kubernetes will gracefully terminate four of your Pods, leaving only one running. This declarative approach simplifies application management significantly.

Cleaning Up

When you’re done experimenting, it’s good practice to clean up your Kubernetes resources.

First, delete the Service and Deployment:

kubectl delete -f nginx-service.yaml
kubectl delete -f nginx-deployment.yaml

Verify they are gone:

kubectl get services
kubectl get deployments

Finally, stop your Minikube cluster or delete it entirely:

minikube stop      # Stops the cluster, preserving its state for faster restarts
# OR
minikube delete    # Deletes the entire Minikube cluster and all its contents

Conclusion and Next Steps

You’ve just taken your first significant step into the world of Kubernetes! In this tutorial, you’ve learned:

  • What Kubernetes is and why it’s essential for modern application deployment.
  • How to set up a local Kubernetes environment using Minikube.
  • The fundamental concepts of Pods, Deployments, and Services.
  • How to define and deploy a containerized application (Nginx) using YAML manifests.
  • How to expose your application via a Kubernetes Service.
  • How to easily scale your application.

This beginner’s guide is just the tip of the iceberg. Kubernetes offers a vast array of features for managing complex applications, including persistent storage, advanced networking with Ingress, configuration management, and much more.

Ready for more? Here are some suggested next steps:

  • Explore more resource types: Volumes for persistent storage, ConfigMaps and Secrets for configuration.
  • Learn about Helm, the package manager for Kubernetes.
  • Dive into networking concepts like Ingress controllers.
  • Consider setting up a CI/CD pipeline using tools like GitHub Actions or GitLab CI to automate your Kubernetes deployments.
  • If you’re serious about mastering Kubernetes, start preparing for the CKA (Certified Kubernetes Administrator) exam.

What was your experience deploying your first app on Kubernetes? Share your thoughts and questions in the comments below!