服务器IP | 端口 | 服务 | 用途 |
---|---|---|---|
192.168.4.114 |
6443 | k8s-master | k8s管理端 |
192.168.4.114 192.168.4.115 192.168.4.118 |
8080 | admin | 商城后台服务 |
8401 | atuh | 认证中心 | |
8101 | monitor | 监控中心 | |
8085 | portalk | 商城前台服务 | |
8081 | search | 商城后台搜索 | |
8201 | gateway | 微服务网关 | |
192.168.4.119 |
80 | nginx | 前端发展 |
3306 | mysql | 数据库 | |
6379 | redis | 数据库 | |
27017 | nogodb | 数据库 | |
9200 | es | ELK | |
8000 | portainer | 容器可视化 | |
5672 | rabbitmq | 消息队列 | |
8848 | nacos | 注册中心 | |
192.168.4.120 |
2376 | mvn | 镜像构建https端口 |
443 | harbor | 镜像仓库 |
cd /opt/
wget -c https://github.com/macrozheng/mall-swarm/archive/refs/tags/v1.0.2.zip
yum install unzip -y
unzip mall-swarm-1.0.2.zip
yum -y remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-selinux \
docker-engine-selinux \
docker-ce-cli \
docker-engine
#查看还有没有存在的docker组件
#rpm -qa|grep docker
#有则通过命令 yum -y remove XXX 来删除,比如:
#yum remove docker-ce-cli
#获取docker-ce的yum源
yum -y install wget
wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
#获取epel源
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum -y install epel-release
#显示docker-ce所有可安装版本:
#yum list docker-ce --showduplicates | sort -r
#安装指定docker版本
sudo yum install docker-ce-18.06.3.ce-3.el7 -y
#启动docker并设置docker开机启动
systemctl enable docker
systemctl start docker
docker ps
#确认镜像目录是否改变
docker info |grep "Docker Root Dir"
#创建镜像加速
cat >>/etc/docker/daemon.json <
#下载Docker Compose
curl -L https://get.daocloud.io/docker/compose/releases/download/1.24.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
#修改该文件的权限为可执行
chmod +x /usr/local/bin/docker-compose
#查看是否已经安装成功
docker-compose --version
images
# 运行的是mysql5.7的镜像
image: mysql:5.7
container_name
# 容器名称为mysql
container_name: mysql
ports
# 将宿主机的3306端口映射到容器的3306端口
ports:
- 3306:3306
volumes
# 将外部文件挂载到myql容器中
volumes:
- /mydata/mysql/log:/var/log/mysql
- /mydata/mysql/data:/var/lib/mysql
- /mydata/mysql/conf:/etc/mysql
environment
# 设置mysqlroot帐号密码的环境变量
environment:
- MYSQL_ROOT_PASSWORD=root
links
# 可以以database为域名访问服务名称为db的容器
links:
- db:database
# -d表示在后台运行
docker-compose up -d
##停止所有相关容器:
docker-compose stop
#列出所有容器信息:
docker-compose ps
Docker Compose将所管理的容器分为三层,工程、服务及容器。docker-compose.yml中定义所有服务组成了一个工程,services节点下即为服务,服务之下为容器。容器与容器直之间可以以服务名称为域名进行访问,比如在mall-tiny-docker-compose服务中可以通过jdbc:mysql://db:3306这个地址来访问db这个mysql服务。
#实例说明
2. version: '3'
3. services:
4. # 指定服务名称
5. db:
6. # 指定服务使用的镜像
7. image: mysql:5.7
8. # 指定容器名称
9. container_name: mysql
10. # 指定服务运行的端口
11. ports:
12. - 3306:3306
13. # 指定容器中需要挂载的文件
14. volumes:
15. - /mydata/mysql/log:/var/log/mysql
16. - /mydata/mysql/data:/var/lib/mysql
17. - /mydata/mysql/conf:/etc/mysql
18. # 指定容器的环境变量
19. environment:
20. - MYSQL_ROOT_PASSWORD=root
21. # 指定服务名称
22. mall-tiny-docker-compose:
23. # 指定服务使用的镜像
24. image: mall-tiny/mall-tiny-docker-compose:0.0.1-SNAPSHOT
25. # 指定容器名称
26. container_name: mall-tiny-docker-compose
27. # 指定服务运行的端口
28. ports:
29. - 8080:8080
30. # 指定容器中需要挂载的文件
31. volumes:
32. - /etc/localtime:/etc/localtime
33. - /mydata/app/mall-tiny-docker-compose/logs:/var/logs
注意:如果遇到mall-tiny-docker-compose服务无法连接到mysql,需要在mysql中建立mall数据库,同时导入mall.sql脚本。
数据库脚本mall.sql:
基础服务部署docker-compose
mkdir -p /opt/docker-compose
cd /opt/docker-compose/
version: '3'
services:
mysql:
image: mysql:5.7
container_name: mysql
command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
restart: always
environment:
MYSQL_ROOT_PASSWORD: root #设置root帐号密码
ports:
- 3306:3306
volumes:
- /mydata/mysql/data/db:/var/lib/mysql #数据文件挂载
- /mydata/mysql/data/conf:/etc/mysql/conf.d #配置文件挂载
- /mydata/mysql/log:/var/log/mysql #日志文件挂载
redis:
image: redis:5
container_name: redis
command: redis-server --appendonly yes
volumes:
- /mydata/redis/data:/data #数据文件挂载
ports:
- 6379:6379
nginx:
image: nginx:1.10
container_name: nginx
volumes:
- /mydata/nginx/conf:/etc/nginx #配置文件挂载
- /mydata/nginx/html:/usr/share/nginx/html #静态资源根目录挂载
- /mydata/nginx/logs:/var/log/nginx #日志文件挂载
ports:
- 80:80
rabbitmq:
image: rabbitmq:3.7.15-management
container_name: rabbitmq
volumes:
- /mydata/rabbitmq/data:/var/lib/rabbitmq #数据文件挂载
- /mydata/rabbitmq/log:/var/log/rabbitmq #日志文件挂载
ports:
- 5672:5672
- 15672:15672
elasticsearch:
image: elasticsearch:7.6.2
container_name: elasticsearch
user: root
environment:
- "cluster.name=elasticsearch" #设置集群名称为elasticsearch
- "discovery.type=single-node" #以单一节点模式启动
- "ES_JAVA_OPTS=-Xms512m -Xmx512m" #设置使用jvm内存大小
volumes:
- /mydata/elasticsearch/plugins:/usr/share/elasticsearch/plugins #插件文件挂载
- /mydata/elasticsearch/data:/usr/share/elasticsearch/data #数据文件挂载
ports:
- 9200:9200
- 9300:9300
logstash:
image: logstash:7.6.2
container_name: logstash
environment:
- TZ=Asia/Shanghai
volumes:
- /mydata/logstash/logstash.conf:/usr/share/logstash/pipeline/logstash.conf #挂载logstash的配置文件
depends_on:
- elasticsearch #kibana在elasticsearch启动之后再启动
links:
- elasticsearch:es #可以用es这个域名访问elasticsearch服务
ports:
- 4560:4560
- 4561:4561
- 4562:4562
- 4563:4563
kibana:
image: kibana:7.6.2
container_name: kibana
links:
- elasticsearch:es #可以用es这个域名访问elasticsearch服务
depends_on:
- elasticsearch #kibana在elasticsearch启动之后再启动
environment:
- "elasticsearch.hosts=http://es:9200" #设置访问elasticsearch的地址
ports:
- 5601:5601
mongo:
image: mongo:4.2.5
container_name: mongo
volumes:
- /mydata/mongo/db:/data/db #数据文件挂载
ports:
- 27017:27017
nacos-registry:
image: nacos/nacos-server:1.3.0
container_name: nacos-registry
environment:
- "MODE=standalone"
ports:
- 8848:8848
docker-compose -f docker-compose-env.yml up -d
应用业务部署docker-compose
应用 | 说明 |
---|---|
mall-monitor | 监控中心 |
mall-gateway | 微服务网关 |
mall-auth | 认证中心 |
mall-admin | 商城后台服务 |
mall-portal | 商城前台服务 |
mall-search | 商城搜索服务 |
一共6个应用服务需要打包成Docker镜像,具体如何打包可以参考使用Maven插件为SpringBoot应用构建Docker镜像 。需要注意的是如果打包过程中遇到找不到mall-common或mall-mbg模块,需要先按顺序将这些模块install到本地maven仓库再进行打包。
version: '3'
services:
mall-admin:
image: mall/mall-admin:1.0-SNAPSHOT
container_name: mall-admin
ports:
- 8080:8080
volumes:
- /mydata/app/mall-admin/logs:/var/logs
- /etc/localtime:/etc/localtime
environment:
- 'TZ="Asia/Shanghai"'
external_links:
- mysql:db #可以用db这个域名访问mysql服务
- nacos-registry:nacos-registry #可以用nacos-registry这个域名访问nacos服务
mall-search:
image: mall/mall-search:1.0-SNAPSHOT
container_name: mall-search
ports:
- 8081:8081
volumes:
- /mydata/app/mall-search/logs:/var/logs
- /etc/localtime:/etc/localtime
environment:
- 'TZ="Asia/Shanghai"'
external_links:
- elasticsearch:es #可以用es这个域名访问elasticsearch服务
- mysql:db #可以用db这个域名访问mysql服务
- nacos-registry:nacos-registry #可以用nacos-registry这个域名访问nacos服务
mall-portal:
image: mall/mall-portal:1.0-SNAPSHOT
container_name: mall-portal
ports:
- 8085:8085
volumes:
- /mydata/app/mall-portal/logs:/var/logs
- /etc/localtime:/etc/localtime
environment:
- 'TZ="Asia/Shanghai"'
external_links:
- redis:redis #可以用redis这个域名访问redis服务
- mongo:mongo #可以用mongo这个域名访问mongo服务
- mysql:db #可以用db这个域名访问mysql服务
- rabbitmq:rabbit #可以用rabbit这个域名访问rabbitmq服务
- nacos-registry:nacos-registry #可以用nacos-registry这个域名访问nacos服务
mall-auth:
image: mall/mall-auth:1.0-SNAPSHOT
container_name: mall-auth
ports:
- 8401:8401
volumes:
- /mydata/app/mall-auth/logs:/var/logs
- /etc/localtime:/etc/localtime
environment:
- 'TZ="Asia/Shanghai"'
external_links:
- nacos-registry:nacos-registry #可以用nacos-registry这个域名访问nacos服务
mall-gateway:
image: mall/mall-gateway:1.0-SNAPSHOT
container_name: mall-gateway
ports:
- 8201:8201
volumes:
- /mydata/app/mall-gateway/logs:/var/logs
- /etc/localtime:/etc/localtime
environment:
- 'TZ="Asia/Shanghai"'
external_links:
- redis:redis #可以用redis这个域名访问redis服务
- nacos-registry:nacos-registry #可以用nacos-registry这个域名访问nacos服务
mall-monitor:
image: mall/mall-monitor:1.0-SNAPSHOT
container_name: mall-monitor
ports:
- 8101:8101
volumes:
- /mydata/app/mall-monitor/logs:/var/logs
- /etc/localtime:/etc/localtime
environment:
- 'TZ="Asia/Shanghai"'
external_links:
- nacos-registry:nacos-registry #可以用nacos-registry这个域名访问nacos服务
启动docker-compose注意:
组件 | 版本号 |
---|---|
Mysql | 5.7 |
Redis | 5.0 |
MongoDb | 4.2.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 pull mysql:5.7
docker pull redis:5.0
docker pull nginx:1.10
docker pull rabbitmq:3.7.15-management
docker pull elasticsearch:7.6.2
docker pull kibana:7.6.2
docker pull logstash:7.6.2
docker pull mongo:4.2.5
docker pull nacos/nacos-server:1.3.0
# 改变设置
sysctl -w vm.max_map_count=262144
# 使之立即生效
sysctl -p
# 创建目录
mkdir -p /mydata/elasticsearch/data/
# 创建并改变该目录权限
chmod 777 /mydata/elasticsearch/data
登录github
密码:马坝五二一$
#需要安装中文分词器IKAnalyzer,并重新启动。
docker exec -it elasticsearch /bin/bash
#此命令需要在容器中运行
elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.6.2/elasticsearch-analysis-ik-7.6.2.zip
docker restart elasticsearch
# 创建目录之后将nginx.conf文件上传到该目录下面
mkdir -p /mydata/nginx/conf
nginx配置ginx.conf文件
#下载nginx1.10的docker镜像
docker pull nginx:1.10
#先运行一次容器(为了拷贝配置文件)
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/
#修改文件名称:
rm -rf /mydata/nginx/conf/
mv /mydata/nginx/nginx/ /mydata/nginx/conf
#终止并删除容器:
docker stop nginx
docker rm nginx
注意:此时已经将Nginx的配置已将导出到持久化目录,重新部署Nginx即可正常启动。
# 创建好配置文件目录
mkdir -p /mydata/logstash
# 进入容器使用如下命令安装插件
docker exec -it logstash /bin/bash
logstash-plugin install logstash-codec-json_lines
exit
#重启
docker restart logstash
logstash.conf配置详情
cat >>/mydata/logstash/logstash.conf< "server"
host => "0.0.0.0"
port => 4560
codec => json_lines
type => "debug"
}
tcp {
mode => "server"
host => "0.0.0.0"
port => 4561
codec => json_lines
type => "error"
}
tcp {
mode => "server"
host => "0.0.0.0"
port => 4562
codec => json_lines
type => "business"
}
tcp {
mode => "server"
host => "0.0.0.0"
port => 4563
codec => json_lines
type => "record"
}
}
filter{
if [type] == "record" {
mutate {
remove_field => "port"
remove_field => "host"
remove_field => "@version"
}
json {
source => "message"
remove_field => ["message"]
}
}
}
output {
elasticsearch {
hosts => "192.168.4.119:9200"
index => "mall-%{type}-%{+YYYY.MM.dd}"
}
}
EOF
#下载源码
wget -c https://github.com/macrozheng/mall-swarm/archive/refs/tags/v1.0.2.zip
yum install unzip lrzsz -y
rz
cd /opt
unzip mall-swarm-master.zip
#将mall.sql文件拷贝到mysql容器的/目录下:
docker cp /opt/ mall-swarm-1.0.2/document/sql/mall.sql mysql:/
进入mysql容器并执行如下操作:
##进入mysql容器
docker exec -it mysql /bin/bash
#连接到mysql服务
mysql -uroot -proot --default-character-set=utf8
#创建远程访问用户
grant all privileges on *.* to 'reader' @'%' identified by '123456';
#创建mall数据库
create database mall character set utf8;
#使用mall数据库
use mall;
#导入mall.sql脚本
source /mall.sql;
exit
#退出容器
exit
#进入mysql
docker exec -it mysql /bin/bash
mysql -uroot -proot --default-character-set=utf8
#修改root帐号的权限,使得任何ip都能访问:
grant all privileges on *.* to 'root'@'%';
flush privileges;
exit
#进入容器并开启管理功能:
docker exec -it rabbitmq /bin/bash
rabbitmq-plugins enable rabbitmq_management
exit
docker restart rabbitmq
访问:http://192.168.4.119:15672/
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UvnVTTlq-1681213938000)(http://m.qpic.cn/psc?/V53GU2po3e4Vc929i8d01BP45G32VjvB/ruAMsa53pVQWN7FLK88i5rvcmOCSIWSdd6V3jg7KEGM7eLgtJvZJNKxknWsQm0JfeBMuPjMQ*O6fP1Em6s6tHDetvaiAstTf57Zh9Hn7UCo!/b&bo=PgKOAD4CjgADFzI!&rf=viewer_4&t=5#pic_center)]
(http://m.qpic.cn/psc?/V53GU2po3e4Vc929i8d01BP45G32VjvB/ruAMsa53pVQWN7FLK88i5rvcmOCSIWSdd6V3jg7KEGM7eLgtJvZJNKxknWsQm0JfeBMuPjMQ*O6fP1Em6s6tHDetvaiAstTf57Zh9Hn7UCo!/b&bo=PgKOAD4CjgADFzI!&rf=viewer_4&t=5#pic_center)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-iziMFgSu-1681213715283)(http://m.qpic.cn/psc?/V53GU2po3e4Vc929i8d01BP45G32VjvB/ruAMsa53pVQWN7FLK88i5rvcmOCSIWSdd6V3jg7KEGM7eLgtJvZJNKxknWsQm0JfeBMuPjMQ*O6fP1Em6s6tHDetvaiAstTf57Zh9Hn7UCo!/b&bo=PgKOAD4CjgADFzI!&rf=viewer_4&t=5)]
输入账号密码并登录:guest guest
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OdhlVy4P-1681213715287)(http://m.qpic.cn/psc?/V53GU2po3e4Vc929i8d01BP45G32VjvB/ruAMsa53pVQWN7FLK88i5rvcmOCSIWSdd6V3jg7KEGNMZdlrkHyb.Z1piHR4kaSAvw3kXqNGlj.w0aztjX90XY0r9.zGiM2Ja2l8mSPdY!/b&bo=PgJLAT4CSwEDFzI!&rf=viewer_4&t=5)]
创建帐号并设置其角色为管理员:mall mall
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8tonXsEa-1681213715288)(http://m.qpic.cn/psc?/V53GU2po3e4Vc929i8d01BP45G32VjvB/ruAMsa53pVQWN7FLK88i5syHFThBnru1qq1h.GWNYsqWXCSWBpJySalNN2F2RvVmQ6mM8PGQ7lNzZhbn01qelauOmpB*DZN5otCnEMnFw!/b&bo=PgLHAT4CxwEDFzI!&rf=viewer_4&t=5)]
创建一个新的虚拟host为:/mall
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-264r07bQ-1681213715288)(http://m.qpic.cn/psc?/V53GU2po3e4Vc929i8d01BP45G32VjvB/ruAMsa53pVQWN7FLK88i5syHFThBnru1qq1h.GWNYuifQGpS2L8obqtRO7NLs6IifIaozrPzKjA5ve7Vosl5exv0LPYnk8Sra5ghRaZbc!/b&bo=PgKYAD4CmAADFzI!&rf=viewer_4&t=5)]
点击/mall用户进入用户配置页面
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7r3Yitkd-1681213715288)(http://m.qpic.cn/psc?/V53GU2po3e4Vc929i8d01BP45G32VjvB/ruAMsa53pVQWN7FLK88i5qulVCkBQiVOnkvnEEL.m4DT8ncWtYJHfG5ln5LV5tk5vD.QWdBLEjmLfVj3*TnDjWSjvG16bmlS8bqVLBG19BU!/b&bo=PgLSAD4C0gADFzI!&rf=viewer_4&t=5)]
给mall用户配置该虚拟host的权限
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1BTwOBht-1681213715289)(http://m.qpic.cn/psc?/V53GU2po3e4Vc929i8d01BP45G32VjvB/ruAMsa53pVQWN7FLK88i5qulVCkBQiVOnkvnEEL.m4Awz11gNc7DVZz27esr6X3UMJxAB6JOOa1KN7SwiI5DBHNB7E8D8yNCE679d7szM!/b&bo=PgI.AT4CPgEDFzI!&rf=viewer_4&t=5)]
创建mall用户给/mall主机授期结果如图所示。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HzUUvBbe-1681213715289)(http://m.qpic.cn/psc?/V53GU2po3e4Vc929i8d01BP45G32VjvB/ruAMsa53pVQWN7FLK88i5r0SPNyPsWTUhUCVVn0k4.I2YjM.tKH**VTuAwxT*.TmSzH1jnVxgUgKrtskY4yonIkKvNgqEgtrxcVVJRhznlo!/b&bo=PgLsAD4C7AADFzI!&rf=viewer_4&t=5)]
【阿里云使用方式】http://www.macrozheng.com/mall/deploy/mall_deploy_windows.html#%E5%88%9B%E5%BB%BA%E5%AD%98%E5%82%A8%E7%A9%BA%E9%97%B4
#关闭防火墙
systemctl stop firewalld
systemctl enable firewalld
#开启防火墙
firewall-cmd --zone=public --add-port=端口/tcp --permanent
firewall-cmd --reload
#启动
docker-compose -f docker-compose-app.yml up -d
#停止
docker-compose -f docker-compose-env.yml down
#查看
[root@k8s-node-04 docker-compose]# docker-compose -f docker-compose-env.yml ps
Name Command State Ports
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
elasticsearch /usr/local/bin/docker-entr ... Up 0.0.0.0:9200->9200/tcp, 0.0.0.0:9300->9300/tcp
kibana /usr/local/bin/dumb-init - ... Up 0.0.0.0:5601->5601/tcp
logstash /usr/local/bin/docker-entr ... Up 0.0.0.0:4560->4560/tcp, 0.0.0.0:4561->4561/tcp, 0.0.0.0:4562->4562/tcp, 0.0.0.0:4563->4563/tcp, 5044/tcp,
9600/tcp
mongo docker-entrypoint.sh mongod Up 0.0.0.0:27017->27017/tcp
mysql docker-entrypoint.sh mysql ... Up 0.0.0.0:3306->3306/tcp, 33060/tcp
nacos-registry bin/docker-startup.sh Up 0.0.0.0:8848->8848/tcp
nginx nginx -g daemon off; Up 443/tcp, 0.0.0.0:80->80/tcp
rabbitmq docker-entrypoint.sh rabbi ... Up 15671/tcp, 0.0.0.0:15672->15672/tcp, 25672/tcp, 4369/tcp, 5671/tcp, 0.0.0.0:5672->5672/tcp
redis docker-entrypoint.sh redis ... Up 0.0.0.0:6379->6379/tcp
Portainer 是一款轻量级的应用,它提供了图形化界面,用于方便的管理Docker环境,包括单机环境和集群环境,下面我们将用Portainer来管理Docker容器中的应用。
【官网地址】
【可视化参考地址】
【找回密码】
【Portainer使用】
#汉化
mkdir -p /mydata/portainer/data /mydata/portainer/public
cd /mydata/portainer/
wget https://labx.me/dl/4nat/public.zip
unzip public.zip
#部署
docker pull portainer/portainer
docker run -p 9000:9000 -p 8000:8000 --name portainer \
--restart=always \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /mydata/portainer/data:/data \
-v /mydata/portainer/public:/public \
-d portainer/portainer
注意:8000是监听,9000是对外暴露端口。
访问:http://192.168.4.119:9000
查看: