Try running Spring Boot on Kubernetes

about I was a little interested in Kubernetes while reading SoftwareDesign 2018/3, so I tried it. I was wondering if there were many things I was addicted to, but I was able to proceed smoothly from environment construction to deployment and scale.

environment

Installation

VirtualBox

$ brew cask install virtualbox

minikube

$ brew cask install minikube

Operation check

Launch minikube

$ minikube start
Starting local Kubernetes v1.8.0 cluster...
Starting VM...
Getting VM IP address...
Moving files into cluster...
Setting up certs...
Connecting to cluster...
Setting up kubeconfig...
Starting cluster components...
Kubectl is now configured to use the cluster.
Loading cached images from config file.

You can see that minikube is created and started on VirtualBox

スクリーンショット 2018-02-24 17.28.48.png

Creating a deployment

# deployment
$ kubectl run hello-minikube --image=k8s.gcr.io/echoserver:1.4 --port=8080
deployment "hello-minikube" created

$ kubectl expose deployment hello-minikube --type=NodePort
service "hello-minikube" exposed

$ kubectl get pod
NAME                              READY     STATUS    RESTARTS   AGE
hello-minikube-7844bdb9c6-88hl4   1/1       Running   0          2m

If you check the dashboard in this state

#Launch the dashboard
$ minikube dashboard

Deployments and pods are created. スクリーンショット 2018-02-24 17.35.13.png

#URL confirmation
$ minikube service hello-minikube --url

$ curl $(minikube service hello-minikube --url)
CLIENT VALUES:
client_address=172.17.0.1
command=GET
real path=/
・
・
・
#Service removal
$ kubectl delete service hello-minikube
service "hello-minikube" deleted

#Deploy deployment
$ kubectl delete deployment hello-minikube
deployment "hello-minikube" deleted

#minikube end
$ minikube stop
Stopping local Kubernetes cluster...
Machine stopped.

Prepare Docker environment for Spring Boot

This time we will use the completed version of the official tutorial. https://spring.io/guides/gs/spring-boot-docker/

Clone the starter

$ git clone https://github.com/spring-guides/gs-spring-boot-docker.git
#Use the finished version
$ cd gs-spring-boot-docker/complete
$ ./gradlew build docker
・
・
・
:processTestResources NO-SOURCE
:testClasses
:test
:check
:build
:dockerClean UP-TO-DATE
:dockerPrepare
:docker

BUILD SUCCESSFUL

Total time: 1 mins 37.278 secs

Try to move

$ docker images
REPOSITORY                       TAG                 IMAGE ID            CREATED             SIZE
springio/gs-spring-boot-docker   latest              d486fdb38288        4 minutes ago       116MB

$ docker run -p 8080:8080 -t springio/gs-spring-boot-docker

Spring Boot has started on docker.

スクリーンショット 2018-02-24 21.11.02.png

Run on kubernetes

Setting environment variables

$ minikube start
$ eval $(minikube docker-env)
#In this state, you will not be able to connect to docker from your local PC temporarily, but if you restart the terminal, the settings will disappear and you will be restored.

Create a service configuration file with the following contents.

kubernetes-service.yaml


apiVersion: v1
kind: Service
metadata:
  name: hellojavakubernetes
  labels:
    app: hellojavakubernetes
    tier: backend
spec:
  type: NodePort
  ports:
    # the port that this service should serve on
  - port: 8080
  selector:
    app: hellojavakubernetes
    tier: backend

Service registration

$ kubectl create -f kubernetes-service.yaml

The hellojavakubernetes service has been registered.

スクリーンショット 2018-02-24 21.43.07.png

Create a deployment configuration file with the following contents.

kubernetes-deployment.yaml


apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: hellojavakubernetes
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: hellojavakubernetes
        tier: backend
    spec:
      containers:
      - name: hellojavakubernetes
        image: springio/gs-spring-boot-docker
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        ports:
        - containerPort: 8080

Try deploying.

$ kubectl create -f kubernetes-deployment.yaml
deployment "hellojavakubernetes" created

$ kubectl get deployment
NAME                  DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
hellojavakubernetes   1         1         1            1           3m

A deployment called hellojavakubernetes has been created and a pod has also been created.

スクリーンショット 2018-02-24 21.54.37.png

Look up the URL.

$ minikube service hellojavakubernetes --url
http://192.168.99.100:31830

Access the displayed URL スクリーンショット 2018-02-24 22.04.06.png

It was displayed correctly. I was able to proceed without getting stuck so far.

Application scaling

Since it's a big deal, I'll try scaling.

$ kubectl get deployments
NAME                  DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
hellojavakubernetes   1         1         1            1           17h

#Specify replicas
$ kubectl scale --replicas=2 -f kubernetes-deployment.yaml 
deployment "hellojavakubernetes" scaled

#The number of deployments has increased to 2.
$ kubectl get deployments
NAME                  DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
hellojavakubernetes   2         2         2            2           17h

You can see that the number of pods has increased to two.

スクリーンショット 2018-02-25 15.48.27.png

Next, try changing from the configuration file. Change the replicas of the kubernetes-deployment.yaml created above to 3.

#Apply settings
$ kubectl apply -f kubernetes-deployment.yaml 
Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply
deployment "hellojavakubernetes" configured

$ kubectl get deployments
NAME                  DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
hellojavakubernetes   3         3         3            3           17h

I was able to scale this as well.

Clean up

finish

$ kubectl delete service,deployment hellojavakubernetes
$ minikube stop
$ minikube delete

It seems that there are still many things to investigate in order to operate it in production, but I got the impression that Kubernetes itself is quite complete.

Reference page

[minikube] https://github.com/kubernetes/minikube

[Spring Boot with Docker] https://spring.io/guides/gs/spring-boot-docker/

[getting started] https://www.bluefyre.io/getting-started-springboot-kubernetes/

Recommended Posts

Try running Spring Boot on Kubernetes
Try Spring Boot on Mac
Spring Boot on Microsoft Azure
Try using Spring Boot Security
gRPC on Spring Boot with grpc-spring-boot-starter
Create Spring Boot development environment on Vagrant
Spring Boot environment construction memo on mac
Try running Word2vec model on AWS Lambda
Try using Spring Boot with VS Code
Try running a Kubernetes Job from Java
Try Spring Boot 1 (Environment construction ~ Tomcat startup)
Challenge Spring Boot
Spring Boot Form
Spring Boot Memorandum
gae + spring boot
Try running MPLS-VPN with FR Routing on Docker
Try running ScalarDB on WSL Ubuntu (Environment Construction)
Try running OSPF with FR Routing on Docker
Try LDAP authentication with Spring Security (Spring Boot) + OpenLDAP
Try to implement login function with Spring Boot
Try to automate migration with Spring Boot Flyway
Deploy a Spring Boot application on Elastic Beanstalk
Try using DI container with Laravel and Spring Boot
SPRING BOOT learning record 01
Spring Boot + Heroku Postgres
Try DisplayLink on Ubuntu 20.04
Reinstall Kubernetes on Ubuntu 19.10
Try running an app made with Quarkus on Heroku
Spring boot memo writing (1)
Try OpenLiteSpeed on CentOS8
First Spring Boot (DI)
SPRING BOOT learning record 02
Spring Boot2 cheat sheet
Spring Boot exception handling
Spring Boot Servlet mapping
Spring boot development-development environment-
Spring Boot learning procedure
Learning Spring Boot [Beginning]
Try Dependency Inversion Principle with Multiple Spring Boot Projects
Spring boot memo writing (2)
Try running Spring Cloud Config for the time being
Spring Boot 2.2 Document Summary
Try gRPC in Spring Boot & Spring Cloud project (Mac OS)
[Spring Boot] DataSourceProperties $ DataSourceBeanCreationException
Try actions on GitHub [actions]
Deploy the Spring Boot project to Tomcat on XAMPP
Spring Boot 2.3 Application Availability
Spring boot tutorials Topics
Download with Spring Boot
Try using Spring JDBC
Try hitting the zip code search API with Spring Boot
Set context-param in Spring Boot
Hello World with Spring Boot
Implement GraphQL with Spring Boot
Spring Boot tutorial task schedule
Spring 5 & Spring Boot 2 Hands-on preparation procedure
Get started with Spring boot
Try Docker on Windows 10 Home
Hello World with Spring Boot!
Spring Boot 2 multi-project in Gradle
Run LIFF with Spring Boot