Mall-swarm (6.1K +star) address: github.com/macrozheng/…
Abstract
Because K8S is so popular at present, we have been saying that we would give the Mall project a K8S deployment plan, this time it comes! After updating the K8S series of tutorials, it was time to put the Mall Swarm project to the test. If you have experience deploying the project under Docker and have already followed my series of K8S tutorials, you can easily get started!
Server Planning
Let’s talk about server planning first. We used to deploy all the services on a single machine before, but this time we use two servers to deploy.
-
Basic server (192.168.3.101) : Used to deploy dependent services of mall-swarm, including MySql, Redis, Elasticsearch and other services irrelevant to applications. Docker is used to deploy these services.
-
Application server (192.168.3.102) : used to deploy mall-swarm application services, including mall-admin, mall-portal, and mall-search, in K8S mode.
Image packaging and push
To facilitate deployment, we uploaded all the images of the mall-Swarm application to Docker Hub.
- First modify the project root directory
pom.xml
File;
<properties>
<! -- Change your Docker service remote access address -->
<docker.host>http://192.168.3.101:2375</docker.host>
</properties>
Copy the code
- Package all the images to the Linux server using the Maven plugin, using the files directly under the root project
package
Command can be;
- Change the names of all mirror labels and the names of local mirror labels to those of remote mirror labels.
Docker tag mall/mall-gateway: 1.0-snapshot Macrodocker /mall-gateway: 1.0-snapshot Docker tag mall/mall-auth: 1.0-snapshot Macrodocker /mall-auth: 1.0-snapshot Docker Tag mall/mall-monitor: 1.0-snapshot MacroDocker /mall-monitor: 1.0-snapshot Docker tag mall/mall-admin: 1.0-snapshot Macrodocker /mall-admin: 1.0-snapshot Docker tag mall/mall-portal: 1.0-snapshot Macrodocker /mall-portal: 1.0-snapshot Docker tag mall/mall-search: 1.0-snapshot Macrodocker /mall-search: 1.0-snapshotCopy the code
- Query the information after the modification
macrodocker
The related image is displayed as follows,macrodocker
Is our warehouse address on Docker Hub;
[root@local-linux ~]# docker images |grep macrodockerMacrodocker /mall-auth 1.0-SNAPSHOT 72df5f91f2d7 9 minutes ago 699MB macrodocker/mall-gateway 1.0-SNAPSHOT 4055dfc1e601 9 30 MINUTES ago 70 MB Macrodocker /mall-monitor 1.0-SNAPSHOT 492d9bb4375c 9 minutes ago 70 MB Macrodocker/mall-Portal Macrodocker /mall-search 1.0-snapshot f0d0d80c590f 10 minutes ago 734MB Macrodocker /mall-admin 1.0-snapshot 15737ce903a9 11 minutes ago 715MBCopy the code
- Then push the image to Docker Hub. If you don’t want to push the image by yourself, you can use the image I have uploaded to the image.
# Log in to Docker Hub
docker login
Push to remote repositoryDocker push MacroDocker /mall-gateway: 1.0-snapshot Docker push MacroDocker/mall-Auth: 1.0-snapshot Docker push Macrodocker /mall-monitor: 1.0-snapshot Docker push MacroDocker /mall-admin: 1.0-snapshot docker push Macrodocker/mall - portal: 1.0 the SNAPSHOT docker push macrodocker/mall - search: 1.0 the SNAPSHOTCopy the code
Base Server Deployment
We still use Docker to deploy dependent services, and the dependent services required by mall-swarm are as follows. It is more convenient to install Docker Compose script. Please refer to “Deploying SpringBoot Application with Docker Compose” for Docker Compose usage.
component | The version number |
---|---|
Mysql | 5.7 |
Redis | 5.0 |
MongoDb | 4.3.5 |
RabbitMq | 3.7.15 |
Nginx | 1.10 |
Elasticsearch | 7.6.2 |
Logstash | 7.6.2 |
Kibana | 7.6.2 |
Nacos | 1.3.0 |
- Docker Compose script is provided in this project. You can directly run the following command: github.com/macrozheng/…
docker-compose -f docker-compose-env.yml up -d
Copy the code
-
Some system components cannot be started. For details, see “Mall Deployment in Linux environment (Based on Docker Compose)”.
-
After the deployment is complete, the following information is displayed when you view the services running in the Docker.
[root@local-linux ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES FEF5695319D1 NACOS/NACOS-Server :1.3.0"Bin/docker - startup...."3 months ago Up 6 hours 0.0.0.0:8848->8848/ TCP nacos-registry e7EC37Fcafda ElasticSearch :7.6.2"/ usr/local/bin/dock..."3 months ago Up 6 hours 0.0.0.0:9200->9200/ TCP, 0.0.0.0:9300->9300/ TCP ElasticSearch 427F0176c426 mongo:4.2.5"Docker - entrypoint. S..."3 months ago Up 6 hours 0.0.0.0:27017->27017/ TCP mongo 5618ED50942A redis: 4 months ago Up 6 hours 0.0.0.0:27017->27017/ TCP mongo 5618ED50942a redis:5"Docker - entrypoint. S..."3 months ago Up 6 hours 0.0.0.0:6379->6379/ TCP redis 1744F412c6F4 RabbitMQ :3.7.15- Management"Docker - entrypoint. S..."3 months ago Up 6 hours 4369/ TCP, 5671/ TCP, 0.0.0.0:5672->5672/ TCP, 15671/ TCP, 25672/ TCP, 0.0.0.0:15672 - > 15672 / TCP rabbitmq 6 b26f63dfad6 mysql: 5.7"Docker - entrypoint. S..."3 months ago Up 6 hours 0.0.0.0:3306->3306/ TCP, 33060/ TCP mysqlCopy the code
Application Server Deployment
We will deploy all the application services in Mall-Swarm on K8S and use Rancher for visual management.
Install the Rancher
- First, you need to install Rancher, first download Rancher Docker image;
Docker pull rancher/rancher: v2.5 - headCopy the code
- Run the Rancher service in the Docker container after downloading.
Docker run -p 80:80-p 443:443 --name rancher \ --privileged \ --restart= solved-stopped \ -d rancher/rancher:v2.5-headCopy the code
- Once Rancher has started successfully, you can access its home page at http://192.168.3.102
Modify the Nacos configuration
All the configurations in the project config directory are added to Nacos. Since the application services are deployed to K8S, the relevant configurations need to be modified.
- The configuration information added to the Nacos display the information as follows, after Nacos access address: http://192.168.3.101:8848/nacos/index.html
- Modify the
mall-admin-prod.yaml
MySql > alter MySql > Redis--link
In the form of, you need to change the IP address for access.
spring:
datasource:
url: JDBC: mysql: / / 192.168.3.101:3306 / mall? useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: root
password: root
redis:
host: 192.1683.101. # Redis server address
database: 0 # Redis database index (default 0)
port: 6379 # Redis server connection port
password: # do not set password
Copy the code
- Modify the
mall-gateway-prod.yaml
Configuration, modify the Redis connection address and JWT publicKey access address (when the service is created in K8S, you can access by the service name);
spring:
redis:
host: 192.1683.101. # Redis server address
database: 0 # Redis database index (default 0)
port: 6379 # Redis server connection port
password: # do not set password
security:
oauth2:
resourceserver:
jwt:
jwk-set-uri: 'http://mall-gateway-service:8201/mall-auth/rsa/publicKey'
Copy the code
- Modify the
mall-portal-prod.yaml
Change the connection addresses of MySql, MongoDb, Redis, and RabbitMq.
spring:
datasource:
url: JDBC: mysql: / / 192.168.3.101:3306 / mall? useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: root
password: root
data:
mongodb:
host: 192.1683.101.
port: 27017
database: mall-port
redis:
host: 192.1683.101. # Redis server address
database: 0 # Redis database index (default 0)
port: 6379 # Redis server connection port
password: # do not set password
rabbitmq:
host: 192.1683.101.
port: 5672
virtual-host: /mall
username: mall
password: mall
publisher-confirms: true Set to true if callback is required for asynchronous messages
Copy the code
- Modify the
mall-search-prod.yaml
Change the connection address of MySql and Elasticsearch.
spring:
datasource:
url: JDBC: mysql: / / 192.168.3.101:3306 / mall? useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: root
password: root
elasticsearch:
rest:
uris: http://192.168.3.101:9200
Copy the code
Deploy the application using Rancher
When creating Deployment using Rancher, if the image download is too slow and times out, you can manually download it into the Rancher container.
- First inside the Rancher container;
docker exex -it rancher /bin/bash
Copy the code
- through
crictl
Command to download the application image. The download process is slow and there is no progress bar. You need to wait patiently.
K3s crictl pull Macrodocker /mall-gateway:1.0-SNAPSHOT k3s crictl pull Macrodocker/mall-Auth :1.0-SNAPSHOT k3s crictl pull Macrodocker /mall-monitor: 1.0-snapshot k3s crictl pull Macrodocker /mall-admin: 1.0-snapshot k3s crictl pull Macrodocker/mall-Portal: 1.0-snapshot k3s crictl pull MacroDocker /mall-search: 1.0-snapshotCopy the code
- After downloading, the image is displayed as follows.
root@ae85f823208f:/var/lib/rancher# k3s crictl images |grep macrodockerDocker. IO/macrodocker/mall - 1.0 - the SNAPSHOT admin 15737 ce903a94 308 MB docker. IO/macrodocker/mall - 1.0 - the SNAPSHOT auth 72 df5f91f2d74 293 MB docker. IO/macrodocker/mall - 1.0 the SNAPSHOT gateway 4055 dfc1e6016 301 MB Docker. IO/macrodocker/mall - 1.0 - the SNAPSHOT monitor 492 d9bb4375c6 291 MB docker. IO/macrodocker/mall - portal 1.0 - the SNAPSHOT 8 dd79675f40c1 312 MB docker. IO/macrodocker/mall - 1.0 - search the SNAPSHOT f0d0d80c590f1 325 MBCopy the code
- With the images downloaded, we can use Rancher to visually create Deployment, as shown here
YAML
The way to create;
- Direct transfer project
k8s
In foldermall-admin-deployment.yaml
Copy the contents of the file;
- Use it later
mall-admin-service.yaml
File create Service;
- Here are just
mall-admin
For example, the Deployment and Service are created for an application. For other applications, the creation process is basically the same. The scripts are stored in the K8S folder of the project.
Results show
After deploying all the application services using Rancher, let’s take a look at the effect.
- First look at all the deployments you created;
- View all created services.
- If you want to see your app’s startup logs, look in the Pod list.
- Since the application service is deployed inside the Rancher container and cannot be accessed directly, we can use the Nginx reverse proxy to access it. The Nginx service runs on
2080
Port;
docker run -p 2080:2080 --name nginx \
-v /mydata/nginx/html:/usr/share/nginx/html \
-v /mydata/nginx/logs:/var/log/ nginx \ - v/mydata/nginx/conf: / etc/nginx \ - d nginx: 1.10Copy the code
- To obtain the IP address of the Rancher container, run the following command:
[root@linux-local ~]# docker inspect rancher |grep IPAddress
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.2"."IPAddress": "172.17.0.2".Copy the code
- After creating the Nginx container, add the configuration file
api.conf
That will beapi.macrozheng.com
Domain name access is reversely proxy to K8Small-gateway-service
Serve up;
server { listen 2080; server_name api.macrozheng.com; {proxy_set_header Host $Host :$server_port; Proxy_pass http://172.17.0.2:30201; # change the proxy server address to index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; }}Copy the code
- Then modify the local host file for accessing Linux servers and add the following records.
192.168.3.102 api.macrozheng.com
Copy the code
- Instead of using Nginx to reverse proxy the container will be created
mall-gateway-service
Can be mapped to the port of
docker run -p 80:80 -p 443:443 -p 8201:30201 --name rancher \ --privileged \ --restart=unless-stopped \ -d The rancher/rancher: v2.5 - headCopy the code
- It is accessible via Nginx
mall-swarm
Interface document, access address:api.macrozheng.com:2080/doc.html
conclusion
By deploying the mall-swarm project to K8S, we can find that K8S is not as difficult as expected. Many things in K8S are connected with Docker! When we hear that a new technology is replacing an old one, we often wonder if the old technology we learned is obsolete and useless. In fact, we don’t have to worry, often new technology is based on the old technology, and those who can learn the old technology will be more easily master the new technology!
K8S series tutorials
Once again recommend a wave of my K8S series of tutorials, K8S combat to see these on the right!
- K8S is so hot! Wouldn’t it be nice to play with it for 10 minutes?
- Since the K8S, the project updates without downtime!
- I deployed the SpringBoot application to K8S and it looks like Docker!
- Goodbye command line! K8S foolproof installation, graphical management really sweet!
- Look at someone else’s open source project documentation, that is called a friendly!
- It is said that only high-end machines can run K8S.
- Goodbye Docker! 5 minutes for containerd!
Project source code address
Github.com/macrozheng/…