Table of Contents
- Prerequisite
- Build your Docker image locally
- Push your docker image to DockerHub
- Start the minikube
- Create Kubernetes Deployment resource
- Create deployment in Kubernetes
- Create Kubernetes service
- Create service in Kubernetes
- Get Kubernetes Service Details
- Accessing the service
- References
we have a dockerized NodeJS application and we will try to run that application in Minikube (Kubernetes)
Prerequisite
- Nodejs application which can be dockerized.
- Dockerize your Nodejs application
- Minikube should be installed on your machine
- Blog : Minikube installation
- YouTube : Minikube installation
- Kubectl should be installed on your machine
- Blog: Kubectl installation
- YouTube: Kubectl installation
- Dockerhub connectivity from your local machine
- Blog: Push your docker image to DockerHub
Build your Docker image locally
we are using the Nodejs sample app so we will give the name to the docker image as selftuts/node-sample-app
- selftuts is my DockerHub username. you need to put your DockerHub username.
- node-sample-app is the image name that will be published to DockerHub and other people will pull using this name
- If you check the Dockerfile then you will find that Docker container will expose port 9005 to the external world
docker build -t selftuts/node-sample-app .
Push your docker image to DockerHub
once you have created the docker image locally then you need to publish the image to DockerHub.
When we do the pod deployment in Minikube then we need to download the docker image. This image will be downloaded from DockerHub so we need to push the image to DockerHub
docker push selftuts/node-sample-app
Start the minikube
minikube start
Create Kubernetes Deployment resource
For deploying the pod in Kubernetes we need to create a Deployment resource file. Create a file as node-sample-app-deployment.yaml
Then using kubectl we can do the deployment in the Kubernetes cluster
apiVersion: apps/v1
kind: Deployment
metadata:
name: node-sample-app
spec:
replicas: 1
selector:
matchLabels:
app: node-sample-app
template:
metadata:
labels:
app: node-sample-app
spec:
containers:
- name: node-sample-app
image: selftuts/node-sample-app:latest
ports:
- containerPort: 9005
env:
- name: PORT
value: '9005'
Create deployment in Kubernetes
kubectl apply -f node-sample-app-deployment.yaml
This will create two things in Kubernetes
- Deployment
- Pod creation in Kubernetes
Fetch deployment using the below command
kubectl get deployments
➜ nodejs kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
node-sample-app 1/1 1 1 2d15h
Fetch pods using the below command
kubectl get pods
➜ nodejs kubectl get pods
NAME READY STATUS RESTARTS AGE
node-sample-app-5b7c68b964-4fpnm 1/1 Running 0 2d15h
Create Kubernetes service
When we create pods in the Kubernetes ecosystem then they are local to Kubernetes and are not accessible from outside.
we need to create a Kubernetes service that will point to those pods.
Create a node-sample-app-service.yaml
apiVersion: v1
kind: Service
metadata:
name: node-sample-app
spec:
selector:
app: node-sample-app
type: LoadBalancer
ports:
- protocol: TCP
port: 9005
targetPort: 9005
Create service in Kubernetes
kubectl apply -f node-sample-app-service.yaml
Get Kubernetes Service Details
kubectl get service
➜ nodejs kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 443/TCP 9d
node-sample-app LoadBalancer 10.99.105.113 9005:32094/TCP 4s
Accessing the service
First, access the the Minikube IP
minikube ip
Then access the port which is binded with the external world
minikube service node-sample-app --url