Kubernetes Engine

Create a Kubernetes cluster and deploy a container.

Kubernetes Engine is a secured and managed Kubernetes service so you can deploy containerized application in an enterprise/production-grade Kubernetes cluster with a click of a button.

Getting Started

Clone

cd $HOME
git clone https://github.com/saturnism/jvm-helloworld-by-example
cd jvm-helloworld-by-example/helloworld-springboot-tomcat

Build

./mvnw package

Containerize

Enable API

Enable the Container Registry API so that you can push container images to Container Registry.

gcloud services enable containerregistry.googleapis.com

Jib

Use Jib to containerize the application:

PROJECT_ID=$(gcloud config get-value project)
./mvnw compile com.google.cloud.tools:jib-maven-plugin:2.4.0:build \
-Dimage=gcr.io/${PROJECT_ID}/helloworld

Learn different ways to containerize a Java application in the Container Image section.

Create Cluster

Enable API

gcloud services enable compute.googleapis.com
gcloud services enable container.googleapis.com

Create Cluster

Create a VPC-native Kubernetes Engine cluster.

gcloud container clusters create helloworld-cluster \
--num-nodes 2 \
--enable-ip-alias \
--scopes=cloud-platform \
--network=default \
--machine-type n1-standard-1

See Compute Engine Machine Types documentation for a list of Machine Types and the associated CPU/Memory resources.

Cluster Credentials

Kubernetes credentials are automatically retrieved and stored in your $HOME/.kube/config file. If you need to re-retrieve the credentials:

gcloud container clusters get-credentials helloworld-cluster

Deploy

PROJECT_ID=$(gcloud config get-value project)
kubectl create deployment helloworld \
--image=gcr.io/${PROJECT_ID}/helloworld

Check that the container is deployed:

kubectl get pods

Expose

You can expose this one service using a single Network (L4) Load Balancer:

kubectl create service loadbalancer helloworld --tcp=8080:8080

A Network (L4) Load Balancer is the easiest way to expose a single service for a demo. For production environment, you likely will need to use a HTTP Load Balancer instead.

Connect

Find the Load Balancer's External IP address:

kubectl get services helloworld

Initially, it may display that the External IP is <pending>.

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
helloworld LoadBalancer ... <pending> 8080:32414/TCP ...

Re-check until the External IP is assigned.

Then connect with curl:

EXTERNAL_IP=$(kubectl get svc helloworld \
-ojsonpath='{.status.loadBalancer.ingress[0].ip}')
curl http://${EXTERNAL_IP}:8080

Learn More