Springboot-Kubernetes-Docker- Goolge Cloud Platform
Kubernetes is an open source project which can run in many different environments, from laptops to high-availability multi-node clusters, from public clouds to on-premise deployments, from virtual machines to bare metal. We Will deploy a simple Java web-based application (using Spring Boot) to Kubernetes running on Container Engine.
Goal is to run your web application with as a replicated application running on Kubernetes. You take code that we have developed on our machine, turn it into a Docker container image, and then run that image on Container Engine.
If you don't already have a Google Account (Gmail or Google Apps), you must create one. Sign-in to Google Cloud Platform console (console.cloud.google.com) and create a new project:
-
gcloud auth list -
gcloud config list project -
gcloud config set project <PROJECT_ID>
-
sudo update-alternatives --config javac -
sudo update-alternatives --config java
-
git clone https://github.com/spring-guides/gs-spring-boot.git -
cd gs-spring-boot/complete
./mvnw -DskipTests spring-boot:run
./mvnw -DskipTests package
touch Dockerfile
FROM openjdk:8
COPY target/gs-spring-boot-0.1.0.jar /app.jar
EXPOSE 8080/tcp
ENTRYPOINT ["java", "-jar", "/app.jar"]
docker build -t gcr.io/PROJECT_ID/hello-java:v1 .
Once this completes (it will take some time to download and extract everything) you can test the image locally with the following command which will run a Docker container as a daemon on port 8080 from your newly-created container image:
docker run -ti --rm -p 8080:8080 gcr.io/PROJECT_ID/hello-java:v1
Now that the image works as intended you can push it to the Google Container Registry, a private repository for your Docker images accessible from every Google Cloud project (but also from outside Google Cloud Platform) :
gcloud docker -- push gcr.io/PROJECT_ID/hello-java:v1
Please note that we need to enable the google container registry before moving forward.
gcloud container clusters create hello-java-cluster \
--num-nodes 2 \
--machine-type n1-standard-1 \
--zone us-central1-c
kubectl run hello-java \
--image=gcr.io/PROJECT_ID/hello-java:v1 \
--port=8080
kubectl get deployments
kubectl get pods
kubectl expose deployment hello-java --type=LoadBalancer
To find the publicly-accessible IP address of the service, simply request kubectl to list all the cluster services:
kubectl get services
http://<EXTERNAL_IP>:8080
kubectl scale deployment hello-java --replicas=3
kubectl get deployment
- Navigate to
/gs-spring-boot/complete/src/main/java/hello/HelloController.java - Introduce some change
./mvnw -DskipTests package
docker build -t gcr.io/PROJECT_ID/hello-java:v2 .
gcloud docker -- push gcr.io/PROJECT_ID/hello-java:v2
You can use kubectl set image command to ask Kubernetes to deploy the new version of your application across the entire cluster one instance at a time with rolling update:
kubectl set image deployment/hello-java \
hello-java=gcr.io/PROJECT_ID/hello-java:v2
kubectl rollout undo deployment/hello-java