- Google Cloud Platform Cookbook
- Legorie Rajan PS
- 553字
- 2021-08-27 19:13:25
Creating a replicated deployment for the application and MongoDB
Follow these steps:
- To create an external disk, we'll use the following command:
$ gcloud compute disks create --size 1GB mongo-disk \
--zone us-east1-c
- 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:
apiVersion: apps/v1beta1
kind: Deployment metadata: name: mongo-deployment spec: replicas: 1 template: metadata: labels: name: mongo spec: containers: - image: mongo name: mongo ports: - name: mongo containerPort: 27017 hostPort: 27017 volumeMounts: - name: mongo-persistent-storage mountPath: /data/db volumes: - name: mongo-persistent-storage gcePersistentDisk: pdName: mongo-disk #The created disk name fsType: ext4
You can refer to the following link for more information on Kubernetes objects: https://kubernetes.io/docs/user-guide/walkthrough/k8s201/.
- 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:
apiVersion: apps/v1beta1 kind: Deployment metadata: name: mysite-app labels: name: mysite spec: replicas: 2 template: metadata: labels: name: mysite spec: containers: - image: gcr.io/<Project ID>/mysite name: mysite ports: - name: http-server containerPort: 8080 #KeystoneJS app is exposed on port 8080
- Use kubectl to create the deployment:
$ kubectl create -f web-deployment.yml
- 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.
推薦閱讀
- Hands-On Intelligent Agents with OpenAI Gym
- Mastering Matplotlib 2.x
- 大數(shù)據(jù)技術(shù)基礎(chǔ)
- Mastercam 2017數(shù)控加工自動(dòng)編程經(jīng)典實(shí)例(第4版)
- 工業(yè)機(jī)器人現(xiàn)場編程(FANUC)
- 網(wǎng)絡(luò)安全與防護(hù)
- Blender 3D Printing by Example
- 教育機(jī)器人的風(fēng)口:全球發(fā)展現(xiàn)狀及趨勢(shì)
- 大數(shù)據(jù):引爆新的價(jià)值點(diǎn)
- 傳感器原理及實(shí)用技術(shù)
- Data Analysis with R(Second Edition)
- Mastering MongoDB 4.x
- Web滲透技術(shù)及實(shí)戰(zhàn)案例解析
- 運(yùn)動(dòng)控制器及數(shù)控系統(tǒng)的工程應(yīng)用
- 數(shù)據(jù)庫技術(shù)及應(yīng)用