Question

Migrate droplet to kubernetes

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


Submit an answer


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!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
March 2, 2024

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:

  1. Understanding Kubernetes Concepts: It’s crucial to grasp core Kubernetes concepts such as Pods, Deployments, Services, and Namespaces. These are fundamental to orchestrating your application in a Kubernetes environment. Here is a free eBook by DigitalOcean to get you started with Kubernetes:

https://www.digitalocean.com/community/books/digitalocean-ebook-kubernetes-for-full-stack-developers

  1. Setting Up a Kubernetes Cluster:

    • Create a Kubernetes cluster in DigitalOcean: You can do this via the DigitalOcean Control Panel or by using the doctl command-line tool.
      doctl kubernetes cluster create my-k8s-cluster --region nyc1 --size s-1vcpu-2gb --count 3
      
    • This command creates a cluster named my-k8s-cluster in the nyc1 region with 3 nodes of size s-1vcpu-2gb.
  2. 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.

  3. Pushing Your Docker Image:

    • Tag and push the image to a container registry, such as Docker Hub or DigitalOcean Container Registry.
      docker tag my-app:v1 mydockerhubusername/my-app:v1
      docker push mydockerhubusername/my-app:v1
      
  4. 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/

  5. Deploying Your Application:

    • Apply your Kubernetes manifests using kubectl:
      kubectl apply -f deployment.yaml
      kubectl apply -f service.yaml
      
  6. Configuring Autoscaling:

    • Set up Horizontal Pod Autoscaler to enable autoscaling based on CPU usage:
      kubectl autoscale deployment my-app-deployment --cpu-percent=50 --min=1 --max=10
      
    • This command autoscales the 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

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

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

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.