Detailed tutorial on deploying Springboot or Nginx using Kubernetes

Detailed tutorial on deploying Springboot or Nginx using Kubernetes

1 Introduction

After "Maven deploys Springboot to Docker repository with one click to prepare for automation", Springboot 's Docker image is ready and can run successfully on Docker . It's time to put it on Kubernetes and run it. This is very simple, just one yaml file.

2 One-click deployment of Springboot

2.1 Prepare the YAML file

Once the image file is prepared, it is very easy to deploy it to Kubernetes . All you need is a file in yaml format, which can describe the components you need, such as Deployment , Service , Ingress , etc. The definition is as follows:

apiVersion: apps/v1
kind: Deployment
metadata:
 name: pkslow-springboot-deployment
spec:
 selector:
 matchLabels:
  app: springboot
 replicas: 2
 template:
 metadata:
  labels:
  app: springboot
 spec:
  containers:
  - name: springboot
   image: pkslow/springboot-mongo:0.0.6
   ports:
   - containerPort: 8080

---
apiVersion: v1
kind: Service
metadata:
 labels:
 app: springboot
 name: pkslow-springboot-service
spec:
 ports:
 - port: 8080
  name: springboot-service
  protocol: TCP
  targetPort: 8080
  nodePort: 30080
 selector:
 app: springboot
 type: NodePort

Kind : Type, including Deployment , Service , Pod , Ingress , etc., which is very rich;

metadata : used to define some component information, such as name, label, etc.

labels : Label function, very useful for selecting associations; but label do not provide uniqueness, so combinations can be used to select;

nodePort : For services that need to be exposed externally, there are three ways: NodePorts , LoadBalancer , and Ingress . NodePorts is used here. It should be noted that the default port range is [3000-32767] . If other ranges are required, the relevant parameters need to be modified.

2.2 Deployment through kubectl command

Once the yaml file is ready, you can deploy it with the following command:

$ kubectl create -f pksow-springboot.yaml 
deployment.apps/pkslow-springboot-deployment created
service/pkslow-springboot-service created

The console log shows that Deployment and Service were successfully created. View Dashboard as follows:

Access the Web service: http://localhost:30080/user

Check it out via the command line:

$ kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
pkslow-springboot-deployment 2/2 2 2 8m2s

$ kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 10m
pkslow-springboot-service NodePort 10.102.218.119 <none> 8080:30080/TCP 8m7s

$ kubectl get pod
NAME READY STATUS RESTARTS AGE
pkslow-springboot-deployment-68dffc6795-874tp 1/1 Running 0 8m15s
pkslow-springboot-deployment-68dffc6795-89xww 1/1 Running 0 8m15s

So far, we have successfully released Springboot to Kubernetes .

2.3 Try killing a pod?

The smallest management element Kubernetes is not a container, but Pod .

Let's try deleting a Pod and see what happens?

$ kubectl delete pod pkslow-springboot-deployment-68dffc6795-89xww
pod "pkslow-springboot-deployment-68dffc6795-89xww" deleted

$ kubectl get pod
NAME READY STATUS RESTARTS AGE
pkslow-springboot-deployment-68dffc6795-874tp 1/1 Running 0 13m
pkslow-springboot-deployment-68dffc6795-gpw67 1/1 Running 0 46s

It can be found that after deleting another Pod , a new Pod will be automatically generated for us, which can improve the high availability of the entire service.

2.4 Try killing a container?

Let's explore what happens if we kill a container instance.

$ docker ps
$ docker rm -f 57869688a226
57869688a226

$ docker ps

After experimenting, we found that after killing a container, a new container instance will be automatically generated for us. Pod will not change or be regenerated.

2.5 Rapid Pod Expansion

When the number of user requests increases suddenly and the service cannot cope with it, the number of Pod needs to be increased. Just modify replicas in the yaml configuration file and update it to replicas: 4 . Then execute the following command:

$ kubectl apply -f pksow-springboot.yaml

Check Dashboard and you will see that two more Pod have been added to the original two.

3 One-click deployment of Nginx

If you don't have a Springboot image, you can use the official Nginx image. The yaml file is as follows:

apiVersion: apps/v1
kind: Deployment
metadata:
 name: nginx-deployment
spec:
 selector:
 matchLabels:
  app: nginx
 replicas: 3
 template:
 metadata:
  labels:
  app: nginx
 spec:
  containers:
  - name: nginx
   image: nginx:1.19.0
   ports:
   - containerPort: 80

---
apiVersion: v1
kind: Service
metadata:
 labels:
 app: nginx
 name: nginx-service
spec:
 ports:
 - port: 80
  name: nginx-service1
  protocol: TCP
  targetPort: 80
  nodePort: 30000
 - port: 81
  name: nginx-service2
  protocol: TCP
  targetPort: 80
  nodePort: 30001
 selector:
 app: nginx
 type: NodePort

Execute the deployment command:

$ kubectl apply -f nginx-deployment-scale.yaml 
deployment.apps/nginx-deployment created
service/nginx-service created

View Dashboard as follows:

Access the service at http://localhost:30000/ or http://localhost:30001. Because we set up two.

4 Conclusion

This article uses two examples to give you a perceptual understanding of Kubernetes , which is very helpful for a deeper understanding of the concepts later. We will discuss the principles and details later.

This is the end of this article about deploying Springboot or Nginx with Kubernetes. For more information about deploying Springboot or Nginx with Kubernetes, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • The whole process of Nginx configuration hot loading in Kubernetes

<<:  Detailed explanation of the underlying implementation of descending index, a new feature of MySQL 8

>>:  JavaScript to achieve fancy carousel effect

Recommend

Linux operation and maintenance basics httpd static web page tutorial

Table of contents 1. Use the warehouse to create ...

Analyze how to automatically generate Vue component documentation

Table of contents 1. Current situation 2. Communi...

The magic of tbody tag speeds up the display of table content

You must have saved other people’s web pages and l...

js to implement verification code interference (dynamic)

This article example shares the specific code of ...

Detailed method of using goaccess to analyze nginx logs

Recently I want to use goaccess to analyze nginx ...

WeChat applet scroll-view realizes left and right linkage

This article shares the specific code for WeChat ...

How to view Docker container application logs

docker attach command docker attach [options] 容器w...

CentOS7.5 installation tutorial of MySQL

1. First check whether the system has mysql insta...

How to use CSS to achieve two columns fixed in the middle and adaptive

1. Use absolute positioning and margin The princi...

Summary of four ways to loop through an array in JS

This article compares and summarizes four ways of...

Some wonderful uses of URL objects in JavaScript

Table of contents Preface Parsing parameters Modi...

Vue implements a simple shopping cart example

This article shares the specific code of Vue to i...

Html Select uses the selected attribute to set the default selection

Adding the attribute selected = "selected&quo...

Three useful codes to make visitors remember your website

Three useful codes to help visitors remember your...