- Consulting
- Training
- Partners
- About Us
x
Google Kubernetes Engine is a fully managed Kubernetes cluster offered by Google Cloud Platform.
GKE offers two modes of the operational cluster: Standard and Autopilot.
In this blog, we are going to deploy a highly available application on GKE with a step-by-step guide.
Step 1: Activate GCP Cloud Shell by clicking on the icon from the right top of the console
It will activate the Cloud Shell as per below:
Note: The Project has already been selected as we open Cloudshell from web browser. Use gcloud config set project [PROJECT_ID] to change project.
Step 2: Create a ct-demo repository for storing container images in the Docker repository
1 2 3 4 |
$ gcloud artifacts repositories create ct-demo \ --repository-format=docker \ --location=asia-south1 \ --description="Docker repository" |
Step 3: Create a Dockerfile
1 2 3 4 5 6 7 8 |
$ mkdir ct-demo && cd ct-demo $ cat <<EOF > Dockerfile FROM tomcat:8.5.73-jdk8-corretto ADD ROOT.war /usr/local/tomcat/webapps/ EXPOSE 8080 CMD ["catalina.sh", "run"] |
Step 4: Upload your WAR application in CloudShell
Step 5: Create Docker image from Dockerfile in the same directory where WAR file is uploaded
1 2 |
$ docker build -t asia-south1-docker.pkg.dev/${PROJECT_ID}/ct-demo/demo-app:v1 . $ docker images |
Output:
1 2 |
REPOSITORY TAG IMAGE ID CREATED SIZE asia-south1-docker.pkg.dev/my-project/ct-demo/demo-app v1 25cfadb1bf28 10 seconds ago 154 MB |
Note: The PROJECT_ID is environment variable to store Project ID information. Instead of env variable you can also use paste Project ID directly in above command
Step 6: Push Docker Image in Artifact repository created in Step 2
1 2 3 |
$ gcloud auth configure-docker asia-south1-docker.pkg.dev $ docker push asia-south1-docker.pkg.dev/${PROJECT_ID}/ct-demo/demo-app:v1 |
Step 7: Create GKE Cluster
1 2 3 4 5 6 7 8 |
$ gcloud config set compute/zone asia-south1-a $ gcloud container clusters create demo-cluster #It will take few minutes for cluster to come up. #verify cluster by running command below $ kubectl get nodes |
Step 8: Create deployment.yml from below textbox and run command
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
apiVersion: apps/v1 kind: Deployment metadata: name: demoapp spec: replicas: 1 template: metadata: name: demoapp labels: app: demoapp spec: containers: - image: asia-south1-docker.pkg.dev/my-project /ct-demo/demo-app:v1 name: demoapp-pod readinessProbe: httpGet: path: /rest/health port: 8080 ports: #Container port number - containerPort: 8080 env: #Set the environment variable for the container, and you can connect mysql with the tomcat - name: JAVA_OPTS value: “-dDB” selector: matchLabels: app: demoapp |
1 |
$ kubectl create -f deployment.yml |
Step 9: Create service.yml from below textbox and run command
1 2 3 4 5 6 7 8 9 10 11 12 13 |
apiVersion: v1 kind: Service metadata: name: nodeport-svc-demo spec: type: NodePort selector: app: demoapp ports: - name: demo-port protocol: TCP port: 80 targetPort: 8080 |
1 |
$ kubectl create -f service.yml |
Step 10: Upload SSL to cloud shell and add SSL into secrets that Ingress controller will use for SSL-termination
1 2 3 |
$ kubectl create secret tls demo-ssl-cert \ --cert=path/to/cert/file \ --key=path/to/key/file |
Step 11: Create ingress.yml from below textbox and run the command
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: demo-ingress-lb spec: tls: - secretName: demo-ssl-cert rules: - http: paths: - pathType: ImplementationSpecific backend: service: name: nodeport-svc-demoa port: name: demo-port |
1 |
$ kubectl create -f ingress.yml |
Step 12: Review all the resources and test your application by requesting an External IP of the ingress controller
1 2 3 |
$ kubectl get all $ kubectl get ingress |
Output:
1 2 |
NAME CLASS HOSTS ADDRESS PORTS AGE demo-ingress-lb <none> * 31.4.62.179 80, 443 44s |
To Create HorizontalPodAutoscaler on deployment on the basis of CPU usage
1 |
$ kubectl autoscale deployment demo-app --cpu-percent=80 --min=1 --max=5 |
Keep it up! You have deployed a highly available and scalable Tomcat application on GKE
I hope you find this blog helpful for your Kubernetes (GKE) journey
Please do not forget to delete resources to avoid incurring charges to your Google account using the below commands
1 2 3 4 5 6 7 8 9 10 |
# Delete Ingress $ kubectl delete ingress demo-ingress-lb # Delete Cluster $ gcloud container clusters delete demo-cluster --zone asia-south1-a # Delete you container image repository $ gcloud artifacts docker images delete \ asia-south1-docker.pkg.dev/${PROJECT_ID}/ct-demo/demo-app:v1 \ --delete-tags --quiet |
Kubernetes works on the same design principles that run popular Google services and provides the same advantages as automatic management, monitoring, and liveness probes for application containers, automatic scaling, rolling updates, and more. When running your applications on a cluster, you use Google’s 10+ years of technology and experience to run production workloads in containers.
For detailed information on Kubernetes works, here is a blog on Beginner’s Guide with Real-world Illustration
We here at CloudThat are the official Google Cloud Partner, AWS Advanced Consulting Partner, Microsoft gold partner, helping people develop knowledge on cloud and help their businesses aim for higher goals using best in industry cloud computing practices and expertise. We are on a mission to build a robust cloud computing ecosystem by disseminating knowledge on technological intricacies within the cloud space. Our blogs, webinars, case studies, and white papers enable all the stakeholders in the cloud computing sphere.
Feel free to drop a comment or any queries that you have regarding Google Cloud Platform, Google Kubernetes Engine, consulting requirements and we will get back to you quickly. To get started, go through our Expert Advisory page and Managed Services Package that is CloudThat’s offerings.
Voiced by Amazon Polly |
CloudThat is a leading provider of cloud training and consulting services, empowering individuals and organizations to leverage the full potential of cloud computing. With a commitment to delivering cutting-edge expertise, CloudThat equips professionals with the skills needed to thrive in the digital era.
Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!
Click to Comment