DEV Community

Cover image for How to deploy on Kubernetes with Docker Desktop
Olalekan Oladiran
Olalekan Oladiran

Posted on

How to deploy on Kubernetes with Docker Desktop

Requirements

Introduction

n this section, you’ll learn how to deploy your application to a fully functional Kubernetes environment using Docker Desktop. This local Kubernetes setup enables you to test, debug, and validate your workloads before deploying them to production.

Create a Kubernetes YAML file

In the cloned repository's directory, create a file named docker-node-kubernetes.yaml. Open the file in an IDE or text editor and add the following contents. Replace DOCKER_USERNAME/REPO_NAME with your Docker username and the name of the repository that you created in How to configure CI/CD for your Node.js application.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: docker-nodejs-demo
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      todo: web
  template:
    metadata:
      labels:
        todo: web
    spec:
      containers:
        - name: todo-site
          image: DOCKER_USERNAME/REPO_NAME
          imagePullPolicy: Always
---
apiVersion: v1
kind: Service
metadata:
  name: todo-entrypoint
  namespace: default
spec:
  type: NodePort
  selector:
    todo: web
  ports:
    - port: 3000
      targetPort: 3000
      nodePort: 30001
Enter fullscreen mode Exit fullscreen mode

Image description

This YAML file defines two Kubernetes objects (separated by ---):

  • Deployment Object
    Manages a scalable set of identical pods, currently configured for a single replica. The pod template specifies one container running the application image built by GitHub Action in How to configure CI/CD for your Node.js application.

  • NodePort Service
    Provides external network access by routing traffic from port 30001 on your host to port 3000 on the application pods, enabling connectivity to your deployed application.

Deploy and check your application

  1. In a terminal, navigate to where you created docker-node-kubernetes.yaml and deploy your application to Kubernetes.

kubectl apply -f docker-node-kubernetes.yaml

Image description

  1. Make sure everything worked by listing your deployments.

kubectl get deployments

Image description
This indicates all one of the pods you asked for in your YAML are up and running. Do the same check for your services.

kubectl get services

Image description
In addition to the default kubernetes service, you can see your todo-entrypoint service, accepting traffic on port 30001/TCP.

  1. Open a browser and visit your app at localhost:30001. You should see your application. Image description
  2. Run the following command to tear down your application.

kubectl delete -f docker-node-kubernetes.yaml

Image description

Heroku

Amplify your impact where it matters most — building exceptional apps.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

DEV shines when you're signed in, unlocking a customized experience with features like dark mode!

Okay