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.
VirtualBox
$ brew cask install virtualbox
System Preferences> Security & Privacy.
(Forgot to take a screenshot)minikube
$ brew cask install minikube
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
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.

#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.
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.
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.
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.
Look up the URL.
$ minikube service hellojavakubernetes --url
http://192.168.99.100:31830
Access the displayed URL

It was displayed correctly. I was able to proceed without getting stuck so far.
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.
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.
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.
[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