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

Implementation of webpack code fragmentation

Table of contents background CommonsChunkPlugin s...

JavaScript to achieve calendar effect

This article shares the specific code for JavaScr...

JavaScript to achieve drop-down menu effect

Use Javascript to implement a drop-down menu for ...

JavaScript history object explained

Table of contents 1. Route navigation 2. History ...

Detailed usage of Linux text search command find

The find command is mainly used to find directori...

CSS3 transition to achieve underline example code

This article introduces the sample code of CSS3 t...

Detailed explanation of Excel parsing and exporting based on Vue

Table of contents Preface Basic Introduction Code...

CentOS 8.0.1905 installs ZABBIX 4.4 version (verified)

Zabbix Server Environment Platform Version: ZABBI...

CSS implementation code for drawing triangles (border method)

1. Implement a simple triangle Using the border i...

An article to help you learn CSS3 picture borders

Using the CSS3 border-image property, you can set...

SQL statements in Mysql do not use indexes

MySQL query not using index aggregation As we all...

Detailed explanation of JSON.parse and JSON.stringify usage

Table of contents JSON.parse JSON.parse Syntax re...

Summary of the top ten problems of MySQL index failure

Table of contents background 1. The query conditi...

Linux hardware configuration command example

Hardware View Commands system # uname -a # View k...

How to install php7 + nginx environment under centos6.6

This article describes how to install php7 + ngin...