In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you the relevant knowledge points about how to deploy SpringBoot applications to K8S. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.
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.
Next we will deploy the application to K8S, including the deployment of SpringBoot applications and the deployment of MySQL.
Deploy MySQL
First of all, add the configuration file mysql-deployment.yaml to create Deployment. For specific instructions, please refer to the notes.
ApiVersion: apps/v1kind: Deploymentmetadata: # specify the name of Deployment name: mysql-deployment # specify the label of Deployment labels: app: mysqlspec: # specify the number of copies of Pod created replicas: 1 # define how to find Pod selector: # Management label app Pod matchLabels: app: mysql # specify the template for creating Pod template: metadata: # to Pod marked with app:mysql labels: app:mysql # Pod template specification spec: containers:-name: mysql # specify container image image: mysql:5.7 # specify open port ports:-containerPort: 3306 # set environment variable Env:-name: MYSQL_ROOT_PASSWORD value: root # Mount the storage volume to the internal path of the container using volumeMounts: # 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 # hostPath type storage volume path on the host hostPath: path: / home/docker/mydata/mysql/log # create type when the directory does not exist: 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
Create Deployment; by applying configuration files create Deployment by applying configuration files
Kubectl apply-f mysql-deployment.yaml
After running successfully, query Deployment and find that mysql-deployment is ready.
[macro@linux-local k8s] $kubectl get deploymentsNAME READY UP-TO-DATE AVAILABLE AGEmysql-deployment 1 38snginx-volume-deployment 1 1 1 38snginx-volume-deployment 2 2 2 6d5h
If you want the rest of the Pod to access MySQL through the service name, you need to create the Service and add the configuration file mysql-service.yaml to create the Service
ApiVersion: v1kind: Servicemetadata: # defines the service name, and the rest of the Pod can be accessed through the service name as the domain name name: mysql-servicespec: # specify the service type, and expose the service type: NodePort # through the static port on Node: Pod selector: app: mysql ports:-name: http protocol: TCP port: 3306 targetPort: 3306 # Node static port nodePort: 30306 on the management tag app: mysql
Create Service by applying configuration files
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 servicesNAME TYPE CLUSTER-IP EXTERNAL-IP PORT (S) AGEkubernetes ClusterIP 10.96.0.1 443/TCP 7d23hmysql-service NodePort 10.107.189.51 3306:30306/TCP 7snginx-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: 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 configure a SSH channel first
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.
Deploy SpringBoot applications
First, add the configuration file mall-tiny-fabric-deployment.yaml to create Deployment, where we can override the default configuration in SpringBoot through environment variables
ApiVersion: apps/v1kind: Deploymentmetadata: name: mall-tiny-fabric-deployment labels: app: mall-tiny-fabricspec: replicas: 1 selector: matchLabels: app: mall-tiny-fabric template: metadata: labels: app: mall-tiny-fabricspec: containers:-name: mall-tiny-fabric # specify the image address image: macrodocker/mall- in Docker Hub Tiny-fabric:0.0.1-SNAPSHOT ports:-containerPort: 8080 env: # specify the database connection address-name: spring.datasource.url value: jdbc:mysql://mysql-service:3306/mall?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai # specify the 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 Deployment by applying configuration files
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 podsNAME READY STATUS RESTARTS AGEmall-tiny-fabric-deployment-8684857dff-pnz2t 1 Running 0 47smysql-deployment-5dccc96ccf-sfxvg 1 Running 0 25mnginx-volume-deployment-6f6c89976d-nv2rn 1 25mnginx-volume-deployment-6f6c89976d-nv2rn 1 Running 4 6d6hnginxMub VolumeMutual 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 outside, you need to create Service and add the configuration file mall-tiny-fabric-service.yaml to create Service
Static port nodePort: 30180 on apiVersion: v1kind: Servicemetadata: name: mall-tiny-fabric-servicespec: type: NodePort selector: app: mall-tiny-fabric ports:-name: http protocol: TCP port: 8080 targetPort: 8080 # Node
Create Service by applying configuration files
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 servicesNAME TYPE CLUSTER-IP EXTERNAL-IP PORT (S) AGEkubernetes ClusterIP 10.96.0.1 443/TCP 7d23hmall-tiny-fabric-service NodePort 10.100.112.84 8080:30180/TCP 5smysql-service NodePort 10.107 . 189.51 3306:30306/TCP 13mnginx-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 agent to access it.
First we need to install Nginx
Add a Nginx configuration file after the installation is completed. Here my configuration path is / mydata/nginx/conf/conf.d/, which is used to transfer the mall-tiny.macrozheng.com domain name access agent to the SpringBoot application in K8S, and 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 agent 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, and then modify the native host file that accesses the Linux server, adding 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
These are all the contents of the article "how to deploy SpringBoot applications to K8S". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
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.