Sling Academy
Home/DevOps/How to Use Kubernetes on DigitalOcean

How to Use Kubernetes on DigitalOcean

Last updated: January 31, 2024

Introduction

Kubernetes has become an essential tool for DevOps professionals, thanks to its powerful management of containerized applications across a cluster of machines. DigitalOcean offers a managed Kubernetes service that simplifies cluster setup, scaling, and management. In this tutorial, we will guide you through setting up and using Kubernetes on DigitalOcean.

Before diving into the setup process, make sure you have a DigitalOcean account, the doctl command-line tool installed, and kubectl, the Kubernetes command-line tool. It’s worth noting that you’ll also need Docker configured on your local machine to manage containers.

Step-by-Step Instructions

Step 1: Create a Kubernetes Cluster on DigitalOcean

doctl kubernetes cluster create my-k8s-cluster --region nyc1 --size s-2vcpu-2gb --count 3

This command creates a cluster named my-k8s-cluster with three nodes in the nyc1 region. Each node will be a s-2vcpu-2gb droplet. Wait for the cluster creation process to complete.

Step 2: Setting Up the Kubectl Context

doctl kubernetes cluster kubeconfig save my-k8s-cluster

This command saves the cluster configuration to kubectl, meaning you can now interact with your Kubernetes cluster using kubectl commands.

Step 3: Deploying an Application

Create a file called nginx-deployment.yaml and add the following code:

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

Apply the deployment using:

kubectl apply -f nginx-deployment.yaml

Step 4: Exposing Your Application

To make your application accessible from the internet, you’ll need to expose it via a service. Create a file called nginx-service.yaml with the contents:

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  type: LoadBalancer
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80

Apply this manifest file using:

kubectl apply -f nginx-service.yaml

After a few minutes a public IP will be assigned to your service, and you should be able to access your application.

Step 5: Scaling Your Application

If you need to scale your application to accommodate for load, you can easily do so:

kubectl scale deployment nginx-deployment --replicas=4

This will update the number of replicas for your deployment to four.

Step 6: Updating Your Application

To update your deployment to a new version of the image:

kubectl set image deployment/nginx-deployment nginx=nginx:1.16.1

This updates the nginx image to version 1.16.1. Kubernetes will smoothly roll out the change ensuring zero downtime.

Step 7: Monitoring and Logs

kubectl also allows you to monitor your deployments and view logs:

kubectl get pods
kubectl logs <POD-NAME>

This provides insight into your cluster’s operations and any potential issues.

Step 8: Cleaning Up

To delete your cluster:

doctl kubernetes cluster delete my-k8s-cluster

This will remove all resources associated with the cluster in DigitalOcean, ensure you do this cautiously to prevent data loss.

Conclusion

In this tutorial, we’ve covered the basics of setting up a Kubernetes cluster on DigitalOcean and outlined a few common tasks to get you started. Be sure to explore the DigitalOcean Kubernetes documentation for more advanced topics such as setting up a CI/CD pipeline, persistent storage, and configuring network policies.

Next Article: Understanding Kubernetes Architecture: A Beginner’s Guide

Previous Article: How to Set Up a Kubernetes Cluster on AWS

Series: Kubernetes Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide