最近在重新整理了一下Docker相关环境的搭建、准备工作,在此记录一下
一、Nacos的安装
关于安装官网文档已经很详细了,参照官网即可,这里说一下需要注意的几个问题。
Nacos安装文档github.com- Nacos本身采用的内存数据库,在机器重启后相关配置都没了,所以需要通过Mysql来持久化。官方镜像自带的mysql版本为5.6,如果需要高版本的需要注意一下,自己下载源码通过example中的cluster-hostname.yaml进行生成镜像即可。但是需要稍微改动一下:
注意:
version: "3"
services:
nacos1:
hostname: nacos1
container_name: nacos1
image: nacos/nacos-server:latest
volumes:
#官方例子中是基于mysql5.6的,现在改为mysql8需要加入最新的驱动。
- ./plugins/mysql/:/home/nacos/plugins/mysql/
- ./cluster-logs/nacos1:/home/nacos/logs
- ./init.d/custom.properties:/home/nacos/init.d/custom.properties
ports:
- "8848:8848"
- "9555:9555"
env_file:
- ../env/nacos-hostname.env
restart: always
depends_on:
- mysql
nacos2:
hostname: nacos2
image: nacos/nacos-server:latest
container_name: nacos2
volumes:
#官方例子中是基于mysql5.6的,现在改为mysql8需要加入最新的驱动。
- ./plugins/mysql/:/home/nacos/plugins/mysql/
- ./cluster-logs/nacos2:/home/nacos/logs
- ./init.d/custom.properties:/home/nacos/init.d/custom.properties
ports:
- "8849:8848"
env_file:
- ../env/nacos-hostname.env
restart: always
depends_on:
- mysql
nacos3:
hostname: nacos3
image: nacos/nacos-server:latest
container_name: nacos3
volumes:
#官方例子中是基于mysql5.6的,现在改为mysql8需要加入最新的驱动。
- ./plugins/mysql/:/home/nacos/plugins/mysql/
- ./cluster-logs/nacos3:/home/nacos/logs
- ./init.d/custom.properties:/home/nacos/init.d/custom.properties
ports:
- "8850:8848"
env_file:
- ../env/nacos-hostname.env
restart: always
depends_on:
- mysql
mysql:
container_name: mysql
image: nacos/nacos-mysql:8
env_file:
- ../env/mysql.env
volumes:
- ./mysql:/var/lib/mysql
ports:
- "3306:3306"
二、Nginx的安装配置
由于Nacos采用集群方式运行的话,需要通过Nginx进行负载均衡。
1、 下载nginx镜像
docker pull nginx
2、 运行:目的:为了获取配置文件 nginx.conf
docker run --name nginx-test -p 7777:80 -d nginx
3、获取nginx.conf
docker cp nginx-test:/etc/nginx/nginx.conf /home/local
4、修改配置文件
vim /home/local/nginx.conf
在nginx.conf中http标签中加入如下代码:
upstream nacos {
server 192.168.102.17:8845 weight=1 max_fails=2 fail_timeout=10s;
server 192.168.102.17:8846 weight=1 max_fails=2 fail_timeout=10s;
server 192.168.102.17:8847 weight=1 max_fails=2 fail_timeout=10s;
}
server{
listen 8848;
server_name 192.168.102.17;
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;
}
}
5、运行
docker run -p 80:80 --net host --name mynginx -v /home/local/nginx.conf:/etc/nginx/nginx.conf --privileged=true -d nginx
现在可以在需要注册的地方,配置为:http://192.168.102.17:8848
6、遇到的问题
1.端口配置没有用
解决:
一开始run运行镜像时,没有加 - -net host ,导致docker里面的容器的ip无法识别
2.配置后访问502
upstream里面的ip+端口应该与nacos里面的一致
3.访问404
访问路径: http://192.168.102.17:8848/nacos