- 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.
推薦閱讀
- Oracle SOA Governance 11g Implementation
- Visual C# 2008開發(fā)技術(shù)詳解
- 21天學(xué)通C#
- OpenStack Cloud Computing Cookbook(Second Edition)
- C語言開發(fā)技術(shù)詳解
- ESP8266 Home Automation Projects
- 中國戰(zhàn)略性新興產(chǎn)業(yè)研究與發(fā)展·工業(yè)機(jī)器人
- INSTANT Munin Plugin Starter
- Mastering Ansible(Second Edition)
- PowerMill 2020五軸數(shù)控加工編程應(yīng)用實(shí)例
- 基于Proteus的單片機(jī)應(yīng)用技術(shù)
- Unreal Development Kit Game Design Cookbook
- 漢字錄入技能訓(xùn)練
- 單片機(jī)C51應(yīng)用技術(shù)
- 大數(shù)據(jù):從基礎(chǔ)理論到最佳實(shí)踐