Back to blog

Kubernetes for Beginners: Deploying and Scaling Your First Microservice Cluster

When your application outgrows a single server, managing container deployment, networking, and scaling can become a nightmare. This is where Kubernetes (K8s) comes in. Kubernetes is an open-source system for automating deployment, scaling, and management of containerized applications.

Let us explore K8s fundamental concepts and deploy our first microservice.

1. Core Concepts

Before we write YAML files, let us understand K8s core building blocks:

  • Pod: The smallest deployable unit in Kubernetes. A Pod runs one or more containers (usually one) sharing network and storage.
  • Deployment: A controller that defines the desired state for your Pods (e.g., "run 3 copies of this pod"). It automatically manages updates and rollbacks.
  • Service: Since Pods are ephemeral and their IP addresses change, a Service provides a stable network endpoint to access a set of Pods.

2. Setting Up a Local Cluster

To practice, you can run a single-node Kubernetes cluster on your laptop using tools like Minikube or Kind, or enable Kubernetes directly inside Docker Desktop.

Ensure your command line tool is working by typing:

kubectl cluster-info

3. Creating Your First Deployment

Let us write a YAML file to define our application deployment. We will call it app-deployment.yaml. This deployment runs three instances of an Nginx web server.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: webserver
  template:
    metadata:
      labels:
        app: webserver
    spec:
      containers:
      - name: nginx
        image: nginx:1.25-alpine
        ports:
        - containerPort: 80

Apply this configuration to your cluster:

kubectl apply -f app-deployment.yaml

You can view the running pods using:

kubectl get pods

4. Exposing the Service

To make our web server accessible from outside the cluster, we need to create a Service. Let us write a file called app-service.yaml.

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

Apply the service configuration:

kubectl apply -f app-service.yaml

Now, you can access the Nginx web server by navigating to http://localhost:30080 in your web browser.

5. Scaling Up

Need more capacity? Scaling your cluster is as simple as running a single command:

kubectl scale deployment/nginx-deployment --replicas=5

Kubernetes will instantly start spin up two new Pods to match your requested count.

Conclusion

Kubernetes may seem intimidating, but once you master Pods, Deployments, and Services, you have the foundational knowledge to run and scale production-grade microservice architectures on any cloud platform.