服务器编排
ip | 端口 | 用途 |
---|
192.168.92.129 | 8848 | nacos-cluster01 | 192.168.92.129 | 3306 | mysql | 192.168.92.129 | 80 | nginx | 192.168.92.132 | 8848 | nacos-cluster02 | 192.168.92.133 | 8848 | nacos-cluster03 |
一、mysql
1. 创建挂载目录
mkdir -p /app/mysql/data
mkdir -p /app/mysql/conf
mkdir -p /app/mysql/sql
2. 赋予权限
chmod -R 777 /app/mysql/
3. 运行容器
docker run --name mysql5.7 -d \
-p 3306:3306 \
-v /app/mysql/conf:/etc/my.cnf.d \
-v /app/mysql/logs:/logs \
-v /app/mysql/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
--restart always \
--privileged=true \
mysql:5.7
4. 创建数据库
创建nacos_config,
5. 初始化脚本
数据库初始化脚本: https://github.com/alibaba/nacos/blob/develop/distribution/conf/nacos-mysql.sql
二、nacos-cluster
2.1. 192.168.92.129
docker run -d \
-e MODE=cluster \
-e NACOS_APPLICATION_PORT=8848 \
-e NACOS_SERVERS=192.168.92.129:8848,192.168.92.132:8848,192.168.92.133:8848 \
-e SPRING_DATASOURCE_PLATFORM=mysql \
-e MYSQL_SERVICE_HOST=192.168.92.129 \
-e MYSQL_SERVICE_PORT=3306 \
-e MYSQL_SERVICE_USER=root \
-e MYSQL_SERVICE_PASSWORD=123456 \
-e MYSQL_SERVICE_DB_NAME=nacos_config \
-e NACOS_SERVER_IP=192.168.92.129 \
-p 8848:8848 \
--name nacos-cluster8848 \
--restart always \
nacos/nacos-server:1.4.2
2.2. 192.168.92.132
docker run -d \
-e MODE=cluster \
-e NACOS_APPLICATION_PORT=8848 \
-e NACOS_SERVERS=192.168.92.129:8848,192.168.92.132:8848,192.168.92.133:8848 \
-e SPRING_DATASOURCE_PLATFORM=mysql \
-e MYSQL_SERVICE_HOST=192.168.92.129 \
-e MYSQL_SERVICE_PORT=3306 \
-e MYSQL_SERVICE_USER=root \
-e MYSQL_SERVICE_PASSWORD=123456 \
-e MYSQL_SERVICE_DB_NAME=nacos_config \
-e NACOS_SERVER_IP=192.168.92.132 \
-p 8848:8848 \
--name nacos-cluster8848 \
--restart always \
nacos/nacos-server:1.4.2
2.3. 192.168.92.133
docker run -d \
-e MODE=cluster \
-e NACOS_APPLICATION_PORT=8848 \
-e NACOS_SERVERS=192.168.92.129:8848,192.168.92.132:8848,192.168.92.133:8848 \
-e SPRING_DATASOURCE_PLATFORM=mysql \
-e MYSQL_SERVICE_HOST=192.168.92.129 \
-e MYSQL_SERVICE_PORT=3306 \
-e MYSQL_SERVICE_USER=root \
-e MYSQL_SERVICE_PASSWORD=123456 \
-e MYSQL_SERVICE_DB_NAME=nacos_config \
-e NACOS_SERVER_IP=192.168.92.133 \
-p 8848:8848 \
--name nacos-cluster8848 \
--restart always \
nacos/nacos-server:1.4.2
docker run -d \
-e MODE=cluster \
-e NACOS_APPLICATION_PORT=8846 \
-e NACOS_SERVERS=192.168.92.129:8846,192.168.92.129:8847,192.168.92.129:8848 \
-e SPRING_DATASOURCE_PLATFORM=mysql \
-e MYSQL_SERVICE_HOST=192.168.92.129 \
-e MYSQL_SERVICE_PORT=3306 \
-e MYSQL_SERVICE_USER=root \
-e MYSQL_SERVICE_PASSWORD=123456 \
-e MYSQL_SERVICE_DB_NAME=nacos_config \
-e NACOS_SERVER_IP=192.168.92.129 \
-p 8846:8846 \
--name nacos8846 \
--restart always \
nacos/nacos-server:1.4.2
常见坑点:官网使用的是NACOS_SERVER_PORT,对于nacos/nacos-server:1.3.2镜像,自定义Nacos端口号的时候,不要使用NACOS_SERVER_PORT,要使用NACOS_APPLICATION_PORT!因为conf/application.properties里写了。
三、nginx
由Nginx进行转发(nginx下部署了3台Nacos集群)配置文件自行修改
3.1. 运行临时容器
-目的,为了复制nginx容器内部的配置文件
mkdir /app/nginx/conf/ /app/nginx/dist -p
chmod 777 /app/nginx -R
docker run \
-d \
--name nginx-tmp \
-p 80:80 \
nginx:1.21.4
3.2. 配置拷贝
docker cp nginx-tmp:/etc/nginx/conf.d/default.conf /app/nginx/conf/default.conf
docker cp nginx-tmp:/usr/share/nginx/html/index.html /app/nginx/dist/index.html
3.3. 删除临时容器
docker rm -f nginx-tmp
3.4. 添加配置
在/app/nginx/conf/挂载目录下新建 nacos.conf
cd /app/nginx/conf/
vim nacos.conf
添加以下内容:
upstream nacos {
server 192.168.92.129:8848 weight=1 max_fails=2 fail_timeout=10s;
server 192.168.92.132:8848 weight=1 max_fails=2 fail_timeout=10s;
server 192.168.92.133:8848 weight=1 max_fails=2 fail_timeout=10s;
}
server{
listen 80;
server_name 192.168.92.129;
location / {
proxy_pass http://nacos;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
add_header X-Cache $upstream_cache_status;
add_header Cache-Control no-cache;
}
}
3.5. 运行容器
这是在咱们生产真正的nginx容器
docker run \
-d \
--name nginx \
-p 80:80 \
-v /app/nginx/dist:/usr/share/nginx/html:ro \
-v /app/nginx/conf:/etc/nginx/conf.d \
--privileged=true \
--restart always \
nginx:1.21.4
四、验证和实战
4.1. nacos控制台
http://192.168.92.129:8848/nacos http://192.168.92.132:8848/nacos http://192.168.92.133:8848/nacos
4.2. nginx验证
这是nginx端口是80 http://192.168.92.129/nacos/
4.3. 项目配置
server:
port: 8000
spring:
cloud:
nacos:
discovery:
service: order2-serv
server-addr: 192.168.92.129:80
|