Deploy NodeJS app to Kubernetes using docker-compose — part-2
As we have discussed earlier in the last post, we did convert compose file to the manifest file using Kompose, if not please take a look at it as it is mandatory for this tutorial.
After the conversion of the docker-compose file the kubemanifest.yaml would look like this.
This creates the service as well as deployment files for Kubernetes. There are some changes to be made to serve this server and client to talk to the external world rather than internal communication.
We have to just add type LoadBalancer .
one more thing to note is, as we are not pushing the docker images to docker hub there is one more thing that is to be added is imagePullPolicy
to Never
.This tells K8s not to pull images, instead use local images. finally, the file would look like this.
compare this with the previous file and notice the change. changes are only made to services deployment will remain the same. Great! we are done with the hardest part i.e creating the service and deployment file.
Now we can start our minikube. this is a simple 1 node development Kubernetes cluster. we can start that by minikube start
the command thus it would look like follows.
Minikube has started and you can now list all nodes, pods, and services by using kubectl get no
kubectl get po
kubectl get svc
respectively.
Initially, it would look like this.
there is one more step to follow as we are not push our images to any registery we are building and using local images. we need to perform couple more steps.
minikube has its own docker demon and thus we have to connect to that demon and build our images inside that environment. to do that firstly execute
eval $(minikube -p minikube docker-env)
this will attach minikube docker demon to your local terminal and now without closing that terminal execute the next command from the root directory
cd client && docker build --tag client:latest .
this will build image inside minikube demon similarly build server image
cd server && docker build --tag server:latest .
Now get into the project directory TrainingDemo and execute this command.
kubectl apply -f kubemanifest.yaml
just make sure you are in the root directory of the project and it would look like.
thus our services and deployment are created now when you hit pods and services you can see pods and their states and run
minikube tunnel
this command does the magic all the LoadBalancer type services are now exposed on the defined port. the server on 8080 and client on 3000. you can now access them on localhost
That's all. if you want to know more about deploying it on Digital Ocean or Linode or any cloud providers stay tuned. if you have any issues you can reach me out on Linkedin or Instagram