This is a simple code example of performing a Blue/Green and a Canary deployment of a Docker-packaged Spring Boot Microservice to Google Kubernetes Engine.

It’s just the basics to get you started in 10 minutes.

Checking out the source

You can check out the source here.

It’s a basic Microservice that echoes a message that you pass.

Bash
git clone https://github.com/johndobie-blog/spring-boot-gke

All of the instructions are also here

https://github.com/johndobie-blog/spring-boot-gke

Run Standalone

Start the Microservice locally and pass it a test message to echo.

Bash
mvn clean spring-boot:run

curl http://localhost:8080/echo?message=testmessage
Host[192.168.0.13] : testmessage

Run In Docker Locally

Create the image and then run the service in docker.

Bash
 docker image build --tag="spring-boot/echo" .
 docker run -p 8080:8080 -t "spring-boot/echo"    

Run In Google Shell

Bash
https://console.cloud.google.com/cloudshell/open
?git_repo=https://github.com/johndobie-blog/spring-boot-ke.git
&page=editor
&open_in_editor=README.md

mvn clean spring-boot:run

Run In Google Shell with Docker

Bash
https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/johndobie-blog/spring-boot-gke.git&page=editor&open_in_editor=README.md

mvn clean install
docker image build --tag="spring-boot/echo" .
docker run -p 8080:8080 -t "spring-boot/echo"    

Run With Kubernetes

Open Google Shell

Bash
https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/johndobie-blog/spring-boot-gke.git&page=editor&open_in_editor=README.md

Create and set the project

Bash
gcloud projects create spring-boot-kubernetes-echo --name="Spring Boot Echo on GKE"
gcloud config set project spring-boot-kubernetes-echo

export PROJECT_ID=”$(gcloud config get-value project -q)”

gcloud projects list
gcloud beta billing projects link spring-boot-kubernetes-echo --billing-account 003146-4D6988-3DC95C

Create the Docker Image

Bash
gcloud services enable containerregistry.googleapis.com

docker build -t echo-container-image .
docker tag echo-container-image gcr.io/spring-boot-kubernetes-echo/echo:v1
docker push gcr.io/spring-boot-kubernetes-echo/echo:v1

Create the Kubernetes cluster

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

gcloud container clusters create echo-kubernetes-cluster \
  --num-nodes 4 \
  --machine-type n1-standard-1 \
  --zone us-central1-c

Deploy The Application

Bash
cd src/main/docker
kubectl apply -f deploy-echo-v1.yaml
or   
kubectl create deployment echo-deployment --image=gcr.io/spring-boot-kubernetes-echo/echo:v1

kubectl get deployments 
kubectl get pods

Create a load balancer and expose the service

Bash
kubectl expose deployment echo-deployment 
        --type=LoadBalancer --port 80 --target-port 8080

kubectl get svc
NAME              TYPE           CLUSTER-IP      EXTERNAL-IP    PORT(S)        AGE
echo-deployment   LoadBalancer   10.59.247.132   35.222.75.46   80:31383/TCP   80s

Use the external address to access the service 

http://35.222.75.46/echo?message=test

Scale the pods up or down

Bash
kubectl scale deployment echo-deployment --replicas=3
kubectl get pods
kubectl scale deployment echo-deployment --replicas=1
kubectl get pods

Tear The Project Down

Bash
gcloud projects delete spring-boot-kubernetes-echo -q

All of the instructions are in the readme above.