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

How to configure anti-hotlinking for nginx website service (recommended)

1. Principle of Hotlinking 1.1 Web page preparati...

jQuery implements the drop-down box for selecting the place of residence

The specific code for using jQuery to implement t...

Vue batch update dom implementation steps

Table of contents Scene Introduction Deep respons...

Pure CSS to achieve a single div regular polygon transformation

In the previous article, we introduced how to use...

In-depth explanation of the principle of MySQL Innodb index

introduction Looking back four years ago, when I ...

Detailed explanation of JavaScript function introduction

Table of contents Function Introduction function ...

A simple method to implement Linux timed log deletion

Introduction Linux is a system that can automatic...

Manjaro installation CUDA implementation tutorial analysis

At the end of last year, I replaced the opensuse ...

VMware and CentOS system installation method to reset the root password

Today's Tasks 1. Choice of Linux distribution...

How to use javascript to do simple algorithms

Table of contents 1 Question 2 Methods 3 Experime...

Ubuntu 16.04 64-bit compatible with 32-bit programs in three steps

Step 1: Confirm the architecture of your system d...

Correct steps to install Nginx in Linux

Preface If you are like me, as a hard-working Jav...

HTML table tag tutorial (34): row span attribute ROWSPAN

In a complex table structure, some cells span mul...

Learn about JavaScript closure functions in one article

Table of contents Variable Scope The concept of c...