Deploy your Node.js application
Prerequisites
- Complete all the previous sections of this guide, starting with Containerize a Node.js application.
- Turn on Kubernetes in Docker Desktop.
Overview
In this section, you'll deploy your containerized Node.js application to a local Kubernetes cluster using Docker Desktop. You'll create a Kubernetes manifest that describes how the application should run, including the application deployment, the PostgreSQL database, and the services that connect them.
Create a Kubernetes manifest
Create a new file called nodejs-docker-example-kubernetes.yaml in your project root:
Before applying the manifest, replace DOCKER_USERNAME in the server deployment's image field with your Docker Hub username.
Deploy to Kubernetes
Apply the manifest to your local Kubernetes cluster:
$ kubectl apply -f nodejs-docker-example-kubernetes.yaml
You should see output confirming that the resources were created:
namespace/nodejs-docker-example created
configmap/app-config created
persistentvolumeclaim/postgres-pvc created
deployment.apps/postgres created
service/postgres created
deployment.apps/server created
service/server created
Then create the database secret from your password file:
$ kubectl create secret generic app-secrets \
--namespace nodejs-docker-example \
--from-file=db-password=db/password.txt
Verify the deployment
Check that your pods are running:
$ kubectl get pods -n nodejs-docker-example
Wait until all pods show Running in the STATUS column. Then verify your services:
$ kubectl get services -n nodejs-docker-example
Access the application
Use port forwarding to access the application from your local machine:
$ kubectl port-forward -n nodejs-docker-example service/server 3000:3000
Open a new terminal and make a request to the application:
$ curl http://localhost:3000
{"message":"Hello World"}
You can also create a hero:
$ curl -X POST http://localhost:3000/heroes/ \
-H 'Content-Type: application/json' \
-d '{"name": "my hero", "secret_name": "austing", "age": 12}'
Clean up
When you're done testing, remove the deployment:
$ kubectl delete -f nodejs-docker-example-kubernetes.yaml
Summary
In this section, you deployed your containerized Node.js application to Kubernetes. You created a manifest that defines the application and database deployments, applied it to a local cluster, and verified the application is accessible.
Related information: