We'll first create the MongoDB deployment because the application expects the database's presence. A deployment object creates the desired number of pods indicated by our replica count. Notice the label given to the pods that are created. The Kubernetes system manages the pods, the deployment, and their linking to their corresponding services via label selectors. Navigate to /Chapter01/mysite-gke/db-deployment.yml:
Use kubectl to deploy the deployment to the cluster:
$ kubectl create -f db-deployment.yml
You can view the deployments using the command:
$ kubectl get deployments
The pods created by the deployment can be viewed using the command:
$ kubectl get pods
To present the MongoDB pods to the application layer, we'll need to create a service. A service exposes a single static IP address to the underlying set of pods. Navigate to /Chapter01/mysite-gke/db-service.yml:
apiVersion: v1
kind: Service
metadata:
labels:
name: mongo
name: mongo
spec:
ports:
- port: 27017
targetPort: 27017
selector:
name: mongo #The key-value pair is matched with the label on the deployment
The kubectl command to create a service is:
$ kubectl create -f db-service.yml
You can view the status of the creation using the commands:
$ kubectl get services
$ kubectl describe service mongo
We'll repeat the same process for the Node.js application. For the deployment, we'll choose to have two replicas of the application pod to serve the web requests. Navigate to /Chapter01/mysite-gke/web-deployment.yml and update the <Project ID> in the image item:
Finally, we'll create the service to manage the application pods. Navigate to /Chapter01/mysite-gke/web-service.yml:
apiVersion: v1 kind: Service
metadata:
name: mysite
labels:
name: mysite
spec:
type: LoadBalancer
ports:
- port: 80 #The application is exposed to the external world on port 80
targetPort: http-server
protocol: TCP
selector:
name: mysite
To create the service execute the below command:
$ kubectl create -f web-service.yml
Get the external IP of the mysite service and open it in a browser to view the application:
$ kubectl get services
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes 10.27.240.1 <none> 443/TCP 49m mongo 10.27.246.117 <none> 27017/TCP 30m mysite 10.27.240.33 1x4.1x3.38.164 80:30414/TCP 2m
After the service is created, the External IP will be unavailable for a short period; you can retry after a few seconds. The Google Cloud Console has a rich interface to view the cluster components, in addition to the Kubernetes dashboard. In case of any errors, you can view the logs and verify the configurations on the Console. The Workloads submenu of GKE provides details of Deployments, the Discovery & load balancing submenu gives us all the services created.