Getting Started with Kubernetes Deployments and Services

Getting Started with Kubernetes Deployments and Services

In this beginner's guide, we will walk through the key Kubernetes concepts and resources involved in deploying and updating web apps.

Happy new month everyone! Kubernetes has become the leading platform for deploying and managing containerized web applications. In this beginner's guide, we will walk through the key Kubernetes concepts and resources involved in deploying and updating web apps. We will also discuss the different deployment strategies that can be used to deploy web applications on Kubernetes.

Introduction

When deploying web applications on Kubernetes, two main resources are used:

  • Deployments - Used to manage multiple instances of a containerized web app.

  • Services - Expose deployments to external traffic.

We will cover how these resources work together to run and expose web apps. We will also discuss common deployment strategies like rolling updates, blue-green, and canary deployments.

By the end of this guide, you will understand:

  • The role of deployments and services

  • How to create and update deployments

  • Strategies like rolling updates to deploy new versions

  • How to expose apps externally using services

Let's get started! The diagram below gives a high-level overview of the steps we'll be going through.

K8s Workflow

Deployments in Kubernetes

A deployment controllers manages a set of identical pods (containers) that make up your application.

When you create a deployment, you specify the container image and the number of replicas (pods) you want to run. Kubernetes will spin up the pods and make sure the specified number are kept running.

For example, a simple deployment might create 3 replicas of an application container. If one of the pods goes down, Kubernetes will automatically re-create it to match the desired state.

Deployments allow you to easily scale up or down your application by changing the replica count. They also support rolling updates to push new versions of your application code.

Here is an example deployment manifest:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-webapp
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-webapp
  template:
    metadata:
      labels:
        app: my-webapp
    spec:
      containers:
      - name: my-webapp
        image: nginx:1.7.9
        ports:
        - containerPort: 80

This will create a deployment named my-webapp that runs 3 replicas of the nginx:1.7.9 image exposed on port 80.

Services in Kubernetes

While deployments run containers inside pods, services expose your deployments so they can be accessed from outside the cluster.

There are different types of Kubernetes services. The main ones are:

  • ClusterIP - Exposes pods only within the cluster.

  • NodePort - Makes deployments accessible via node ports.

  • LoadBalancer - Provisions a cloud load balancer to route traffic.

For example, you may run a ClusterIP service to keep your app internal, and a NodePort service to allow external access.

Here is an example service manifest:

  apiVersion: v1
  kind: Service
  metadata:
    name: my-webapp-service
  spec:
    selector:
      app: my-webapp
    type: NodePort  
    ports:
      - port: 80
        targetPort: 80
        nodePort: 30007

This exposes our my-webapp deployment on port 30007 of every cluster node.

With deployments and services, you have the basic resources to run and expose containerized web applications on Kubernetes.

Next we will look at deployment strategies...

Deployment Strategies

There are several common strategies used to deploy new versions of your application:

Rolling Updates

The rolling update is the default strategy used if you apply a change to your Kubernetes deployment manifest.

It works by slowly replacing old pods with new ones a few at a time, rather than stopping everything at once. This allows changes to be applied without downtime.

The rollout pauses if too many pods become unavailable, ensuring availability. Rollbacks can also be performed.

Blue-Green Deployment

With blue-green deployments, instead of changing the existing deployment directly, you deploy the new version alongside the old version.

Once the new version is up and tested, you switch over by pointing the service to the new deployment. Traffic immediately shifts over to the new version.

This makes rollbacks as easy as switching the service back to the old deployment if issues arise.

Canary Deployment

With canary deployments, you slowly roll out a new version to a small subset of users to test it out before rolling it out to everybody.

This allows you to catch any issues with a new version early with minimal impact. Traffic is gradually shifted over time until all users have the new version.

Deploying a Web App on Kubernetes

Now that we have covered the key concepts, let's walk through a simple example of deploying a web application.

K8S Web Deployment

We will deploy the free open-source Cowsay app which displays a cow saying messages.

Step 1 - Create the deployment

First we create a deployment manifest cowsay.yaml:

  apiVersion: apps/v1
  kind: Deployment
  metadata:
    name: cowsay
  spec:
    replicas: 3
    selector:
      matchLabels:
        app: cowsay
    template:
      metadata:
        labels:
          app: cowsay
      spec:
        containers:
        - name: cowsay
          image: docker/getting-started
          ports:
          - containerPort: 80

This will deploy 3 replicas of the cowsay image exposed on port 80.

Step 2 - Create the service

Now we create a service manifest cowsay-service.yaml to expose the deployment:

  apiVersion: v1
  kind: Service
  metadata:
    name: cowsay-service
  spec:
    selector:
      app: cowsay
    ports:
      - port: 80
        targetPort: 80

This is a simple ClusterIP service to expose the cowsay internally.

Step 3 - Deploy the app

We can now deploy the app using kubectl:

  kubectl apply -f cowsay.yaml
  kubectl apply -f cowsay-service.yaml

That's it! The cowsay app is deployed and exposed on your Kubernetes cluster. Run the following commands to see deployment, services and pods that were created.

  kubectl get deployments
  kubectl get services
  kubectl get pods

From here you could update the deployment or expose it via a NodePort to make it externally accessible.

Summary

In this guide we covered:

  • Deployments - For running a number of identical pod replicas

  • Services - For exposing deployments internally and externally

  • Common deployment strategies like rolling updates and blue-green

  • Walkthrough of deploying a sample app on Kubernetes

We discussed how deployments can be used to manage multiple instances of a pod, how services can be used to expose web applications to the outside world, and the different deployment strategies that can be used to deploy web applications on Kubernetes. By understanding these concepts, you can begin to deploy your own web applications on Kubernetes.

This should give you a solid foundational understanding of running web applications on Kubernetes.

For more hands-on practice, I recommend checking out the Kubernetes documentation and trying out examples locally using minikube.

In the next article, we will explore how to scale and monitor web applications on Kubernetes. We will cover the different ways to scale your applications, and the tools and techniques required to monitor your applications. We will also discuss the different metrics that can be used to monitor your applications, and the different tools that can be used to visualize these metrics.

I hope you have enjoyed this article. If you have any questions, please feel free to reach out to me on Twitter. or LinkedIn. You can also check out my other articles on Dev.to. Thanks for reading!

Buy me a coffee here.

image

References