In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
How to deploy the SpringBoot application to K8S, for this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
Abstract
To deploy a complex microservice project to K8S, we must first learn to deploy a single SpringBoot application.
Push image to Docker Hub
Previously, we all built our own image warehouse, but this time we upload the image to Docker Hub in a different way.
First of all, we have to sign up for a Docker Hub account, Docker Hub address: https://hub.docker.com/
To deploy the application using the previous mall-tiny-fabric project, modify the pom.xml file first, mainly to add the authentication information of Docker Hub and modify the image prefix, as shown below
Http://192.168.5.94:2375 macrodocker xxx macrodocker/$ {project.name}: ${project.version}
After the modification is completed, use the package command to package the image to the Linux server, and then use the docker:push command to push the image to Docker Hub:
After the push is successful, you can see the image in Docker Hub.
Application deployment
Next we will deploy the application to K8S, including the deployment of SpringBoot applications and the deployment of MySQL.
Deploy MySQL
First, add the configuration file mysql-deployment.yaml to create the Deployment. For specific instructions, please refer to the notes.
ApiVersion: apps/v1 kind: Deployment metadata: # specify the name of Deployment name: mysql-deployment # specify the label of Deployment labels: app: mysql spec: # specify the number of copies of Pod created replicas: 1 # define how to find Pod selector: # Management label Pod matchLabels: app: mysql # specify the template to create Pod: Metadata: # tag Pod labels: app:mysql # Pod template specification spec: containers:-name: mysql # specify container image image: mysql:5.7 # specify open port ports:-containerPort: 3306 # set the environment variable env:-name: MYSQL_ROOT_PASSWORD value: root # use the storage volume volumeMounts: # Mount the storage volume to the container internal path-mountPath: / var/log/mysql name: log-volume-mountPath: / var/lib/mysql name: data-volume-mountPath: / etc/mysql name: conf-volume # define the storage volume volumes:-name: log-volume # the path of the hostPath type storage volume on the host hostPath: path: / home/docker/mydata/mysql/log # create type: DirectoryOrCreate-name: data-volume hostPath: path: / home/docker/mydata/mysql/data type: DirectoryOrCreate-name: conf-volume hostPath: path: / home/docker/mydata/mysql/conf type: DirectoryOrCreate when the directory does not exist
Create a Deployment by applying a configuration file
Kubectl apply-f mysql-deployment.yaml
After running successfully, query Deployment and find that mysql-deployment is ready.
[macro@linux-local k8s] $kubectl get deployments NAME READY UP-TO-DATE AVAILABLE AGE mysql-deployment 1 + 1 1 38 s nginx-volume-deployment 2 + 2 2 2 6d5h
If you want other Pod to access the MySQL through the service name, you need to create a Service and add a profile mysql-service.yaml to create the Service.
ApiVersion: v1 kind: Service metadata: # defines the service name. Other Pod can use the service name as the domain name to access name: mysql-service spec: # specify the service type Expose the service type through the static port on Node: NodePort # manage Pod selector: app: mysql ports:-name: http protocol: TCP port: 3306 targetPort: 3306 # Node on Pod selector: app: mysql ports:-name: TCP port with the label mysql nodePort: 30306
Create a Service by applying a configuration file
Kubectl apply-f mysql-service.yaml
After running successfully, query Service and find that mysql-service has been exposed on port 30306 of Node.
[macro@linux-local k8s] $kubectl get services NAME TYPE CLUSTER-IP EXTERNAL-IP PORT (S) AGE kubernetes ClusterIP 10.96.0.1 443/TCP 7d23h mysql-service NodePort 10.107.189.51 3306:30306/TCP 7s nginx-service NodePort 10.101.171.181 80:30080/TCP 6d2h
After deployment, you need to create a new mall database and import related tables, table address: https://github.com/macrozheng/mall-learning/blob/master/document/sql/mall.sql
Here is a relatively simple way to import the database, create a connection through Navicat, and first configure a SSH channel
Then we can access the database in Minikube as if we were accessing the database on the Linux server, just add the database IP and port in Minikube directly.
Deploy SpringBoot applications
First add the configuration file mall-tiny-fabric-deployment.yaml to create the Deployment, where we can override the default configuration in the SpringBoot through the environment variable
ApiVersion: apps/v1 kind: Deployment metadata: name: mall-tiny-fabric-deployment labels: app: mall-tiny-fabric spec: replicas: 1 selector: matchLabels: app: mall-tiny-fabric template: metadata: labels: app: mall-tiny-fabric spec: containers:-name: mall-tiny-fabric # specify the address of the image in Docker Hub Image: macrodocker/mall-tiny-fabric:0.0.1-SNAPSHOT ports:-containerPort: 8080 env: # specify database connection address-name: spring.datasource.url value: jdbc:mysql://mysql-service:3306/mall?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai # specified day Log file path-name: logging.path value: / var/logs volumeMounts:-mountPath: / var/logs name: log-volume volumes:-name: log-volume hostPath: path: / home/docker/mydata/app/mall-tiny-fabric/logs type: DirectoryOrCreate
Create a Deployment by applying a configuration file
Kubectl apply-f mall-tiny-fabric-deployment.yaml
We can view the startup log of the application through the kubectl logs command
[macro@linux-local k8s] $kubectl get pods NAME READY STATUS RESTARTS AGE mall-tiny-fabric-deployment-8684857dff-pnz2t 1 Running 0 47s mysql-deployment-5dccc96ccf-sfxvg 1 Running 0 25m nginx-volume-deployment-6f6c89976d-nv2rn 1 Running 4 6d6h nginx-volume -deployment-6f6c89976d-tmhc5 1 Running 4 6d5h [macro@linux-local k8s] $kubectl logs-f mall-tiny-fabric-deployment-8684857dff-pnz2t
If you want to access the SpringBoot application from the outside, you need to create a Service and add a configuration file mall-tiny-fabric-service.yaml to create the Service
Static port on apiVersion: v1 kind: Service metadata: name: mall-tiny-fabric-service spec: type: NodePort selector: app: mall-tiny-fabric ports:-name: http protocol: TCP port: 8080 targetPort: 8080 # Node nodePort: 30180
Create a Service by applying a configuration file
Kubectl apply-f mall-tiny-fabric-service.yaml
At this time, the service has been exposed to port 30180 of Node.
[macro@linux-local k8s] $kubectl get services NAME TYPE CLUSTER-IP EXTERNAL-IP PORT (S) AGE kubernetes ClusterIP 10.96.0.1 443/TCP 7d23h mall-tiny-fabric-service NodePort 10.100.112.84 8080:30180/TCP 5s mysql-service NodePort 10.107.189.51 3306:30306/TCP 13m nginx-service NodePort 10.101.171.181 80:30080/TCP 6d2h
On the Linux server, we can access the Swagger page of the next project through the curl command, but we can only see a string of HTML code returned.
Curl $(minikube ip): 30180/swagger-ui.html
External access to the application
Because the K8S Node installed with Minikube is in the intranet environment of the Linux server and cannot be accessed directly from the outside, we need to install a Nginx reverse proxy to access it.
First of all, we need to install Nginx, and friends who are not familiar with Nginx can refer to this article directly: "there must be some wonderful uses of Nginx that you don't know!"
Add a Nginx configuration file after installation. Here my configuration path is / mydata/nginx/conf/conf.d/, which is used to access the mall-tiny.macrozheng.com domain name to the SpringBoot application in K8S. Proxy_pass is the path used by the above curl.
Server {listen 80; server_name mall-tiny.macrozheng.com; # modify domain name location / {proxy_set_header Host $host:$server_port; proxy_pass http://192.168.49.2:30180; # to proxy service address index index.html index.htm;} error_page 502 503 504 / 50x.html Location = / 50x.html {root / usr/share/nginx/html;}}
Restart the Nginx service, then modify the native host file that accesses the Linux server, and add the following record
192.168.5.94 mall-tiny.macrozheng.com
After that, you can directly access the SpringBoot application on K8S on the local computer, access address: http://mall-tiny.macrozheng.com/swagger-ui.html
Through the operation of deploying SpringBoot applications to K8S, we can find that there are many similarities between deploying on K8S and deploying on Docker. Many of the scripts used for deployment on K8S can be translated directly into scripts that used Docker Compose before, very similar. If you have used Docker before, then you can easily use K8S!
This is the answer to the question about how to deploy the SpringBoot application to K8S. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.