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.