Docker环境安装
安装yum-utils:
yum install -y yum-utils device-mapper-persistent-data lvm2
为yum源添加docker仓库位置:
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
安装docker:
yum install docker-ce
启动docker:
systemctl start docker
MySQL安装
下载MySQL5.7的docker镜像:
docker pull mysql:5.7
使用如下命令启动MySQL服务:
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
进入运行MySQL的docker容器:
dockerexec -it mysql /bin/bash
使用MySQL命令打开客户端:
mysql -uroot -proot --default-character-set=utf8
创建mall数据库:
createdatabase mall characterset utf8
安装上传下载插件,并将document/sql/mall.sql上传到Linux服务器上:
yum -y install lrzsz
将mall.sql文件拷贝到mysql容器的/目录下:
dockercp /mydata/mall.sql mysql:/
将sql文件导入到数据库:
use mall;source /mall.sql;
创建一个reader:123456帐号并修改权限,使得任何ip都能访问:
grantallprivilegeson*.*to'reader'@'%' identified by'123456';
#Redis安装
下载Redis7的docker镜像:
docker pull redis:7
使用如下命令启动Redis服务:
docker run -p 6379:6379 --name redis \
-v /mydata/redis/data:/data \
-d redis:7 redis-server --appendonly yes
进入Redis容器使用redis-cli命令进行连接:
dockerexec -it redis redis-cli
![](https://i-blog.csdnimg.cn/blog_migrate/edf8f5ec877e9f67c77c96685bdda3cc.png)
Nginx安装
下载Nginx1.22的docker镜像:
docker pull nginx:1.22
先运行一次容器(为了拷贝配置文件):
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.22
将容器内的配置文件拷贝到指定目录:
docker container cp nginx:/etc/nginx /mydata/nginx/
修改文件名称:
mv nginx conf
终止并删除容器:
docker stop nginx
dockerrm nginx
使用如下命令启动Nginx服务:
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.22
#RabbitMQ安装
下载rabbitmq3.9-management的docker镜像:
docker pull rabbitmq:3.9-management
使用如下命令启动RabbitMQ服务:
docker run -p 5672:5672 -p 15672:15672 --name rabbitmq \
-d rabbitmq:3.9-management
开启防火墙:
firewall-cmd --zone=public --add-port=15672/tcp --permanent
firewall-cmd --reload
访问地址查看是否安装成功:http://192.168.3.101:15672
![](https://i-blog.csdnimg.cn/blog_migrate/4eb3f593e7d12d2b619bc68dd1b0c499.png)
输入账号密码并登录:guest guest
创建帐号并设置其角色为管理员:mall mall
![](https://i-blog.csdnimg.cn/blog_migrate/0ed4b290e681386ddbd651bf8739c592.png)
创建一个新的虚拟host为:/mall
![](https://i-blog.csdnimg.cn/blog_migrate/4dc2fe8d95fde08c662255a68cae0b33.png)
点击mall用户进入用户配置页面
![](https://i-blog.csdnimg.cn/blog_migrate/4438cc5bd2b7ba4ce72fe95ba534e590.png)
给mall用户配置该虚拟host的权限
![](https://i-blog.csdnimg.cn/blog_migrate/1351f5e073a77e3c33f377997ded5c6c.png)
#Elasticsearch安装
下载Elasticsearch7.17.3的docker镜像:
docker pull elasticsearch:7.17.3
1
修改虚拟内存区域大小,否则会因为过小而无法启动:
sysctl -w vm.max_map_count=262144
1
使用如下命令启动Elasticsearch服务,内存小的服务器可以通过ES_JAVA_OPTS来设置占用内存大小:
docker run -p 9200:9200 -p 9300:9300 --name elasticsearch \
-e "discovery.type=single-node"\
-e "cluster.name=elasticsearch"\
-e "ES_JAVA_OPTS=-Xms512m -Xmx1024m"\
-v /mydata/elasticsearch/plugins:/usr/share/elasticsearch/plugins \
-v /mydata/elasticsearch/data:/usr/share/elasticsearch/data \
-d elasticsearch:7.17.3
1
2
3
4
5
6
7
启动时会发现/usr/share/elasticsearch/data目录没有访问权限,只需要修改/mydata/elasticsearch/data目录的权限,再重新启动即可;
chmod777 /mydata/elasticsearch/data/
1
安装中文分词器IKAnalyzer,注意下载与Elasticsearch对应的版本,下载地址:https://github.com/medcl/elasticsearch-analysis-ik/releases
![](https://i-blog.csdnimg.cn/blog_migrate/21fe2ae6c408dc804b196047a875c969.png)
下载完成后解压到Elasticsearch的/mydata/elasticsearch/plugins目录下;
![](https://i-blog.csdnimg.cn/blog_migrate/4a9f314f6bf1e7d9d756bcbb88ea98aa.png)
重新启动服务:
docker restart elasticsearch
1
开启防火墙:
firewall-cmd --zone=public --add-port=9200/tcp --permanent
firewall-cmd --reload
1
2
访问会返回版本信息:http://192.168.3.101:9200
{"name":"708f1d885c16","cluster_name":"elasticsearch","cluster_uuid":"mza51wT-QvaZ5R0NmE183g","version":{"number":"7.17.3","build_flavor":"default","build_type":"docker","build_hash":"5ad023604c8d7416c9eb6c0eadb62b14e766caff","build_date":"2022-04-19T08:11:19.070913226Z","build_snapshot":false,"lucene_version":"8.11.1","minimum_wire_compatibility_version":"6.8.0","minimum_index_compatibility_version":"6.0.0-beta1"},"tagline":"You Know, for Search"}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#Logstash安装
下载Logstash7.17.3的docker镜像:
docker pull logstash:7.17.3
1
修改Logstash的配置文件logstash.conf中output节点下的Elasticsearch连接地址为es:9200,配置文件地址:https://github.com/macrozheng/mall/blob/master/document/elk/logstash.conf
output {
elasticsearch {
hosts => "es:9200"
index => "mall-%{type}-%{+YYYY.MM.dd}"
}
}
1
2
3
4
5
6
创建/mydata/logstash目录,并将Logstash的配置文件logstash.conf拷贝到该目录;
mkdir /mydata/logstash
1
使用如下命令启动Logstash服务;
docker run --name logstash -p 4560:4560 -p 4561:4561 -p 4562:4562 -p 4563:4563 \
--link elasticsearch:es \
-v /mydata/logstash/logstash.conf:/usr/share/logstash/pipeline/logstash.conf \
-d logstash:7.17.3
1
2
3
4
进入容器内部,安装json_lines插件。
logstash-plugin install logstash-codec-json_lines
1
#Kibana安装
下载Kibana7.17.3的docker镜像:
docker pull kibana:7.17.3
1
使用如下命令启动Kibana服务:
docker run --name kibana -p 5601:5601 \
--link elasticsearch:es \
-e "elasticsearch.hosts=http://es:9200"\
-d kibana:7.17.3
开启防火墙:
firewall-cmd --zone=public --add-port=5601/tcp --permanent
firewall-cmd --reload
访问地址进行测试:http://192.168.3.101:5601
![](https://i-blog.csdnimg.cn/blog_migrate/435eb5e03419a83eb50fcc6c9cc4781a.png)
#MongoDB安装
下载MongoDB4的docker镜像:
docker pull mongo:4
使用docker命令启动:
docker run -p 27017:27017 --name mongo \
-v /mydata/mongo/db:/data/db \
-d mongo:4
#Docker全部环境安装完成
所有下载镜像文件:
REPOSITORY TAG IMAGE ID CREATED SIZE
redis 7 604d80444252 2 days ago 117MB
nginx 1.22 f9c88cc1c21a 2 weeks ago 142MB
elasticsearch 7.17.3 3c91aa69ae06 8 weeks ago 613MB
kibana 7.17.3 4897f4b8b6ee 8 weeks ago 797MB
logstash 7.17.3 dd4291c803f4 8 weeks ago 774MB
mongo 4 1c0f1e566fec 5 months ago 438MB
rabbitmq 3.9-management 6c3c2a225947 6 months ago 253MB
mysql 5.7 7faa3c53e6d6 3 years ago 373MB
nginx 1.10 0346349a1a64 5 years ago 182MB
java 8 d23bdf5b1b1b 5 years ago 643MB
所有运行在容器里面的应用:
![](https://i-blog.csdnimg.cn/blog_migrate/c7daf6413290f61b943cac557c9a3d4c.png)
#SpringBoot应用部署
#构建所有Docker镜像并上传
修改项目根目录下的pom.xml中的docker.host属性:
<properties><docker.host>http://192.168.3.101:2375</docker.host></properties>
如果项目根目录的pom.mxl中docker-maven-plugin的<executions>节点被注释掉了就打开注释,使项目在打包时直接构建Docker镜像;
![](https://i-blog.csdnimg.cn/blog_migrate/441094fe80626aa1eb64894d2ab5329a.png)
直接双击根项目mall的package命令可以一次性打包所有应用的Docker镜像;
![](https://i-blog.csdnimg.cn/blog_migrate/e2130822b4d3ecd3944ffea92ff75d5c.png)
REPOSITORY TAG IMAGE ID CREATED SIZE
mall/mall-portal 1.0-SNAPSHOT c668ea32b405 24 hours ago 712MB
mall/mall-search 1.0-SNAPSHOT 627d1ebbc21b 24 hours ago 745MB
mall/mall-admin 1.0-SNAPSHOT 2300aca75270 24 hours ago 725MB
#部署mall-admin
docker run -p 8080:8080 --name mall-admin \
--link mysql:db \
--link redis:redis \
-v /etc/localtime:/etc/localtime \
-v /mydata/app/admin/logs:/var/logs \
-d mall/mall-admin:1.0-SNAPSHOT
注意:如果想使用Logstash收集日志的话,需要将应用容器连接到Logstsh,添加如下配置即可;
--link logstash:logstash \
#部署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 --permanent
firewall-cmd --zone=public --add-port=8081/tcp --permanent
firewall-cmd --zone=public --add-port=8085/tcp --permanent
firewall-cmd --reload
#访问接口进行测试
mall-admin的api接口文档地址:http://192.168.3.101:8080/swagger-ui/
![](https://i-blog.csdnimg.cn/blog_migrate/6afd103a7faf8d0af2bb01c1c923a45a.png)
mall-search的api接口文档地址:http://192.168.3.101:8081/swagger-ui/
![](https://i-blog.csdnimg.cn/blog_migrate/426a0aa21f4ec37af2825b961bbc8d83.png)
mall-portal的api接口文档地址:http://192.168.3.101:8085/swagger-ui/
![](https://i-blog.csdnimg.cn/blog_migrate/120b1c7245d4a7d1f6797bda3a2d491d.png)