Deployment of mall in Linux environment (based on Docker container)
This article mainly explains the deployment of mall in the Linux environment in the form of graphics and text, involving the installation of Mysql, Redis, Nginx, RabbitMQ, Elasticsearch, Mongodb, and SpringBoot application deployment in a Docker container, based on CenterOS7.6.
Docker environment installation
Install yum-utils:
yum install -y yum-utils device-mapper-persistent-data lvm2
Add docker warehouse location for yum source:
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
Install docker:
yum install docker-ce
Start docker:
systemctl start docker
Mysql installation
Download the docker image of mysql5.7:
docker pull mysql:5.7
Use the docker command to start:
docker run -p 3306:3306 --name mysql \-v /mydata/mysql/log:/var/log/mysql \-v /mydata/mysql/data:/var/lib/mysql \-v /mydata/mysql/conf:/etc/mysql \-e MYSQL_ROOT_PASSWORD=root \-d mysql:5.7
Parameter Description
-p 3306:3306: Map port 3306 of the container to port 3306 of the host
-v /mydata/mysql/conf:/etc/mysql: Hang the configuration folder to the host
-v /mydata/mysql/log:/var/log/mysql: mount the log folder to the host
-v /mydata/mysql/data:/var/lib/mysql/: mount the data folder to the host
-e MYSQL ROOT PASSWORD=root: Initialize the password of the root user
Enter the docker container running mysql:
docker exec -it mysql /bin/bash
Use the mysql command to open the client:
mysql -uroot -proot --default-character-set=utf8
Create mall database:
create database mall character set utf8
Install the upload and download plug-in, and upload the docment/sql/mall.sql to the Linux server:
yum -y install lrzsz
Copy the mall.sql file to the / directory of the mysql container:
docker cp /mydata/mall.sql mysql:/
Import the sql file into the database:
use mall;source /mall.sql;
Create a reader account and modify permissions so that any ip can access:
grant all privileges on *.* to 'reader' @'%' identified by '123456';
Redis installation
Download the docker image of redis3.2:
docker pull redis:3.2
Use the docker command to start:
docker run -p 6379:6379 --name redis \-v /mydata/redis/data:/data \-d redis:3.2 redis-server --appendonly yes
Enter the redis container and use the redis-cli command to connect:
docker exec -it redis redis-cli
Nginx installation
Download the docker image of nginx1.10:
docker pull nginx:1.10
Copy nginx configuration from the container
Run the container once (in order to copy the configuration file):
docker run -p 80:80 --name nginx \-v /mydata/nginx/html:/usr/share/nginx/html \-v /mydata/nginx/logs:/var/log/nginx \-d nginx:1.10
将容器内的配置文件拷贝到指定目录:
docker container cp nginx:/etc/nginx /mydata/nginx/
修改文件名称:
mv nginx conf
终止并删除容器:
docker stop nginxdocker rm nginx
使用docker命令启动:
docker run -p 80:80 --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.10
RabbitMQ安装
下载rabbitmq3.7.15的docker镜像:
docker pull rabbitmq:3.7.15
使用docker命令启动:
docker run -d --name rabbitmq \--publish 5671:5671 --publish 5672:5672 --publish 4369:4369 \--publish 25672:25672 --publish 15671:15671 --publish 15672:15672 \rabbitmq:3.7.15
进入容器并开启管理功能:
docker exec -it rabbitmq /bin/bashrabbitmq-plugins enable rabbitmq_management
开启防火墙:
firewall-cmd --zone=public --add-port=15672/tcp --permanentfirewall-cmd --reload
访问地址查看是否安装成功:http://192.168.3.101:15672/
输入账号密码并登录:guest guest
创建帐号并设置其角色为管理员:mall mall
创建一个新的虚拟host为:/mall
点击mall用户进入用户配置页面
给mall用户配置该虚拟host的权限
Elasticsearch安装
下载elasticsearch6.4.0的docker镜像:
docker pull elasticsearch:6.4.0
修改虚拟内存区域大小,否则会因为过小而无法启动:
sysctl -w vm.max_map_count=262144
使用docker命令启动:
docker run -p 9200:9200 -p 9300:9300 --name elasticsearch \-e "discovery.type=single-node" \-e "cluster.name=elasticsearch" \-v /mydata/elasticsearch/plugins:/usr/share/elasticsearch/plugins \-v /mydata/elasticsearch/data:/usr/share/elasticsearch/data \-d elasticsearch:6.4.0
启动时会发现/usr/share/elasticsearch/data目录没有访问权限,只需要修改/mydata/elasticsearch/data目录的权限,再重新启动。
chmod 777 /mydata/elasticsearch/data/
安装中文分词器IKAnalyzer,并重新启动:
docker exec -it elasticsearch /bin/bash#此命令需要在容器中运行elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.4.0/elasticsearch-analysis-ik-6.4.0.zipdocker restart elasticsearch
开启防火墙:
firewall-cmd --zone=public --add-port=9200/tcp --permanentfirewall-cmd --reload
访问会返回版本信息:http://192.168.3.101:9200/
kibana安装
下载kibana6.4.0的docker镜像:
docker pull kibana:6.4.0
使用docker命令启动:
docker run --name kibana -p 5601:5601 \--link elasticsearch:es \-e "elasticsearch.hosts=http://es:9200" \-d kibana:6.4.0
开启防火墙:
firewall-cmd --zone=public --add-port=5601/tcp --permanentfirewall-cmd --reload
访问地址进行测试:http://192.168.3.101:5601
Mongodb安装
下载mongo3.2的docker镜像:
docker pull mongo:3.2
使用docker命令启动:
docker run -p 27017:27017 --name mongo \-v /mydata/mongo/db:/data/db \-d mongo:3.2
Docker全部环境安装完成
所有下载镜像文件:
所有运行在容器里面的应用:
SpringBoot应用部署
构建所有Docker镜像并上传
打开pom.xml中使用docker插件的注释:
修改dockerHost为你自己的docker服务器地址:
构建镜像并上传:
部署mall-admin
docker run -p 8080:8080 --name mall-admin \--link mysql:db \-v /etc/localtime:/etc/localtime \-v /mydata/app/admin/logs:/var/logs \-d mall/mall-admin:1.0-SNAPSHOT
注意
:CenterOS7.2版本需要加入此行,否则容器时区和宿主机无法同步
-v /etc/timezone:/etc/timezone \
部署mall-search
docker run -p 8081:8081 --name mall-search \--link elasticsearch:es \--link mysql:db \-v /etc/localtime:/etc/localtime \-v /mydata/app/search/logs:/var/logs \-d mall/mall-search:1.0-SNAPSHOT
部署mall-port
docker run -p 8085:8085 --name mall-portal \--link mysql:db \--link redis:redis \--link mongo:mongo \--link rabbitmq:rabbit \-v /etc/localtime:/etc/localtime \-v /mydata/app/portal/logs:/var/logs \-d mall/mall-portal:1.0-SNAPSHOT
开启防火墙
firewall-cmd --zone=public --add-port=8080/tcp --permanentfirewall-cmd --zone=public --add-port=8081/tcp --permanentfirewall-cmd --zone=public --add-port=8085/tcp --permanentfirewall-cmd --reload
访问接口进行测试
mall-admin的api接口文档地址:http://192.168.3.101:8080/swagger-ui.html
mall-search的api接口文档地址:http://192.168.3.101:8081/swagger-ui.html
mall-portal的api接口文档地址:http://192.168.3.101:8085/swagger-ui.html