By t0th
Hi,
I have a droplet with my application but I need an auto scale, to do that do I need to setup Digital Ocean kubernetes cluster? If yes, how I can migrate my droplet to Kubernetes?
Thanks
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hey,
Migrating your application from a Droplet to a Kubernetes cluster is a good move to take advantage of the autoscaling and manage your resources efficiently.
There are a few things that you should keep in mind before starting:
https://www.digitalocean.com/community/books/digitalocean-ebook-kubernetes-for-full-stack-developers
Setting Up a Kubernetes Cluster:
doctl command-line tool.
doctl kubernetes cluster create my-k8s-cluster --region nyc1 --size s-1vcpu-2gb --count 3
my-k8s-cluster in the nyc1 region with 3 nodes of size s-1vcpu-2gb.Containerizing Your Application:
Dockerize your application by creating a Dockerfile. Here’s a basic example for a Node.js app:
FROM node:20
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "app.js"]
Build your Docker image:
docker build -t my-app:v1 .
This is just an example, if you are using a different type of tech stack, you would need to create a different Dockerfile. Feel free to share more information about it and I can try helping out with the Dockerization.
Pushing Your Docker Image:
docker tag my-app:v1 mydockerhubusername/my-app:v1
docker push mydockerhubusername/my-app:v1
Creating Kubernetes Manifests:
Define a deployment YAML file (deployment.yaml) for your application:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app-deployment
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: mydockerhubusername/my-app:v1
ports:
- containerPort: 3000
Define a service YAML file (service.yaml) to expose your application:
apiVersion: v1
kind: Service
metadata:
name: my-app-service
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 3000
selector:
app: my-app
Here is a link to the documentation about this as well:
https://docs.digitalocean.com/products/kubernetes/how-to/add-load-balancers/
Deploying Your Application:
kubectl:
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
Configuring Autoscaling:
kubectl autoscale deployment my-app-deployment --cpu-percent=50 --min=1 --max=10
my-app-deployment based on CPU usage, maintaining a target of 50% CPU usage across all pods.For more information on the autoscaling configuraiton you can take a look at the documentation here:
https://docs.digitalocean.com/products/kubernetes/how-to/autoscale/
Also make sure that your application functions correctly in the new Kubernetes environment. Test endpoints, data persistence, and performance.
Only once you’re confident in the Kubernetes deployment, update your DNS records to point to the LoadBalancer service IP.
If you encounter any hurdles or have more questions during your migration, feel free to ask. The community is here to support you!
Best,
Bobby
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.