概述
Nacos ApiSix docker集群环境搭建(docker-compose方式)
集群流程(我采用SLB做负载均衡,也可自行配置nginx做负载):
nginx负载均衡的配置参考如下(仅供参考):
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_iso8601] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream apisix{
ip_hash;
server 12.10.8.92:9000 weight=1;
server 12.10.8.93:9000 weight=2;
server 12.10.8.93:9000 weight=3;
}
upstream nacos{
ip_hash;
server 12.10.8.92:8848 weight=1;
server 12.10.8.93:8848 weight=2;
server 12.10.8.94:8848 weight=3;
}
server {
listen 8080;
server_name localhost;
#charset koi8-r;
if ($time_iso8601 ~ '(d{4}-d{2}-d{2})') {
set $time $1;
}
access_log logs/$time.access.log main;
#access_log logs/host.access.log main;
location / {
proxy_pass http://apisix/;
#proxy_set_header Host $host;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 8082 ;
client_max_body_size 20M;
proxy_read_timeout 600s;
#charset koi8-r;
if ($time_iso8601 ~ '(d{4}-d{2}-d{2})') {
set $time $1;
}
access_log logs/$time.access.log main;
#access_log logs/host.access.log main;
location / {
proxy_pass http://nacos/;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
环境安装:
IP地址 | 需安装服务(仅限docker方式) | 备注 |
12.10.8.92 | Docker+docker-compose+Apisix+nacos,etcd, dashboard, Grafana, prometheus | 红色为必装服务 |
12.10.8.93 | Docker+docker-compose+Apisix+nacos,etcd,dashboard,Grafana, prometheus等 | 红色为必装服务 |
12.10.8.94 | Docker+docker-compose+Apisix+nacos,etcd,dashboard,Grafana, prometheus等 | 红色为必装服务 |
安装docker:https://blog.csdn.net/BThinker/article/details/123358697
安装docker-compose: https://blog.csdn.net/pushiqiang/article/details/78682323
Docker 配置:
mkdir -p /etc/docker/
vim /etc/docker/daemon.json
#需要配置docker和docker-compose的默认网段,避免网段冲突,daemon.json添加以下内容。
{ "registry-mirrors": ["https://hub-mirror.c.163.com"],
"bip": "192.168.0.1/24",
"default-address-pools": [{"base":"10.10.0.0/16","size":24}]
}
docke-compose方式安装APISIX:
创建安装目录:
mkdir -p /data/soft/
cd /data/soft/
通过 git 命令克隆 apisix-docker 仓库:
git clone https://github.com/apache/apisix-docker.git
cd apisix-docker/example
vim docker-compose.yml
然后在docker-compose.yml里配置etcd集群(由于网上无任何docker安装方式的apisix集群配置,下面配置都是自行研究而来,如有错误还请指正)。
这里遇到一个问题,官方配置apache/apisix:latest拉取的版本和etcd版本由冲突,需要修改docker-compose.yml下APISIX镜像的版本,如:apache/apisix:3.0.0-centos
apisix:
image: apache/apisix:3.0.0-centos
restart: always
volumes:
- ./apisix_log:/usr/local/apisix/logs
- ./apisix_conf/config.yaml:/usr/local/apisix/conf/config.yaml:ro
depends_on:
- etcd
##network_mode: host
ports:
- "9180:9180/tcp"
- "9080:9080/tcp"
- "9091:9091/tcp"
- "9443:9443/tcp"
- "9092:9092/tcp"
networks:
apisix:
节点1 ETCD集群配置:
etcd:
image: bitnami/etcd:3.4.15
restart: always
volumes:
- etcd_data:/bitnami/etcd
environment:
ETCD_NAME: "etcd1"
ETCD_ENABLE_V2: "true"
ALLOW_NONE_AUTHENTICATION: "yes"
ETCD_INITIAL_CLUSTER_STATE: "new"
ETCD_ADVERTISE_CLIENT_URLS: "http://12.10.8.92:2379"
ETCD_INITIAL_ADVERTISE_PEER_URLS: "http://12.10.8.92:2380"
ETCD_LISTEN_CLIENT_URLS: "http://0.0.0.0:2379"
ETCD_INITIAL_CLUSTER_TOKEN: "etcd-cluster"
ETCD_LISTEN_PEER_URLS: "http://0.0.0.0:2380"
ETCD_INITIAL_CLUSTER: "etcd1=http://12.10.8.92:2380,etcd2=http://12.10.8.93:2380,etcd3=http://12.10.8.94:2380"
ports:
- "2379:2379/tcp"
- "2380:2380/tcp"
networks:
apisix:
节点2 ETCD集群配置:
etcd:
image: bitnami/etcd:3.4.15
restart: always
volumes:
- etcd_data:/bitnami/etcd
environment:
ETCD_NAME: "etcd2"
ETCD_ENABLE_V2: "true"
ALLOW_NONE_AUTHENTICATION: "yes"
ETCD_INITIAL_CLUSTER_STATE: "new"
ETCD_ADVERTISE_CLIENT_URLS: "http://12.10.8.93:2379"
ETCD_INITIAL_ADVERTISE_PEER_URLS: "http://12.10.8.93:2380"
ETCD_LISTEN_CLIENT_URLS: "http://0.0.0.0:2379"
ETCD_INITIAL_CLUSTER_TOKEN: "etcd-cluster"
ETCD_LISTEN_PEER_URLS: "http://0.0.0.0:2380"
ETCD_INITIAL_CLUSTER: "etcd1=http://12.10.8.92:2380,etcd2=http://12.10.8.93:2380,etcd3=http://12.10.8.94:2380"
ports:
- "2379:2379/tcp"
- "2380:2380/tcp"
networks:
apisix:
节点3 ETCD集群配置:
etcd:
image: bitnami/etcd:3.4.15
restart: always
volumes:
- etcd_data:/bitnami/etcd
environment:
ETCD_NAME: "etcd3"
ETCD_ENABLE_V2: "true"
ALLOW_NONE_AUTHENTICATION: "yes"
ETCD_INITIAL_CLUSTER_STATE: "new"
ETCD_ADVERTISE_CLIENT_URLS: "http://12.10.8.94:2379"
ETCD_INITIAL_ADVERTISE_PEER_URLS: "http://12.10.8.94:2380"
ETCD_LISTEN_CLIENT_URLS: "http://0.0.0.0:2379"
ETCD_INITIAL_CLUSTER_TOKEN: "etcd-cluster"
ETCD_LISTEN_PEER_URLS: "http://0.0.0.0:2380"
ETCD_INITIAL_CLUSTER: "etcd1=http://12.10.8.92:2380,etcd2=http://12.10.8.93:2380,etcd3=http://12.10.8.94:2380"
ports:
- "2379:2379/tcp"
- "2380:2380/tcp"
networks:
apisix:
注意:各个节点etcd的2379和2380需要互通。ETCD_LISTEN_PEER_URLS和ETCD_LISTEN_CLIENT_URLS的IP最好是0.0.0.0如果按网上或者官方的配置会导致启动失败。
配置APISIX的配置文件(三个节点都需要修改):
/data/soft/apisix-docker/example/apisix_conf/config.yaml
修改config.yaml的etcd如下:
etcd:
host:
- "http://12.10.8.92:2379"
- "http://12.10.8.93:2379"
- "http://12.10.8.94:2379"
# multiple etcd address
prefix: "/apisix" # apisix configurations prefix
timeout: 30 # 30 seconds
配置dashboard的配置文件(三个节点都需要修改):
/data/soft/apisix-docker/example/dashboard_conf/conf.yaml
etcd:
endpoints:
- "http://12.10.8.92:2379"
- "http://12.10.8.93:2379"
- "http://12.10.8.94:2379"
配置完成,启动APISIX(三个节点都需要启动):
cd /data/soft/apisix-docker/example
docker-compose -p docker-apisix up -d
安装完成后,你可以在运行 Docker 的宿主机上执行 curl 命令访问 Admin API,根据返回数据判断 APISIX 是否成功启动。X-API-KEY在apisix_conf/config.yaml配置文件里可找到。
# 注意:请在运行 Docker 的宿主机上执行 curl 命令。
curl "http://127.0.0.1:9180/apisix/admin/services/" -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1'
如果返回数据如下所示,则表示 APISIX 成功启动:
{
"count":0,
"node":{
"key":"/apisix/services",
"nodes":[],
"dir":true
}
}
验证etcd集群:
- 进入etcd 容器内:
-
docker exec -it ***** /bin/bash
- 运行下面命令查看节点状态
-
etcdctl member list
- 显示以下内容etcd集群成功:
b99e84a8f163a77, started, etcd1, http://12.10.8.92:2380, http://12.10.8.92:2379, false
78d14fd665ac2577, started, etcd2, http://12.10.8.93:2380, http://12.10.8.93:2379, false
d72f728605b522c2, started, etcd3, http://12.10.8.94:2380, http://12.10.8.94:2379, false
验证APISIX集群:
1.后台查看docker服务日志看看是否有报错
Docker logs docker-apisix_apisix_1
2.登录:http://12.10.8.92:9000/serverinfo 三个节点能正常显示则代表集群安装成功。
3.停止其中一个节点的容器服务:
docker-compose -p docker-apisix stop
查看其它容器的etcd日志,发现已经在重新选举:
#集群测试验证(停止其中一台node后会重新选举主席)
raft2023/01/09 00:50:52 INFO: b99e84a8f163a77 [term: 18] received a MsgVote message with higher term from d72f728605b522c2 [term: 19]
raft2023/01/09 00:50:52 INFO: b99e84a8f163a77 became follower at term 19
raft2023/01/09 00:50:52 INFO: b99e84a8f163a77 [logterm: 18, index: 80, vote: 0] cast MsgVote for d72f728605b522c2 [logterm: 18, index: 80] at term 19
raft2023/01/09 00:50:52 INFO: raft.node: b99e84a8f163a77 lost leader 78d14fd665ac2577 at term 19
raft2023/01/09 00:50:52 INFO: raft.node: b99e84a8f163a77 elected leader d72f728605b522c2 at term 19
2023-01-09 00:50:52.680807 W | rafthttp: lost the TCP streaming connection with peer 78d14fd665ac2577 (stream MsgApp v2 reader)
2023-01-09 00:50:52.681175 W | rafthttp: lost the TCP streaming connection with peer 78d14fd665ac2577 (stream Message reader)
2023-01-09 00:50:52.683499 E | rafthttp: failed to dial 78d14fd665ac2577 on stream MsgApp v2 (peer 78d14fd665ac2577 failed to find local node b99e84a8f163a77)
2023-01-09 00:50:52.683509 I | rafthttp: peer 78d14fd665ac2577 became inactive (message send to peer failed)
启动刚刚停止的容器。
APISIX集群搭建完成。
Nacos集群搭建:
官网的集群搭建步骤不适合我们本地,所以我自己写了docker-compose.yml。官网的配置步骤不够明确,所以不作为参考。我是直接把nacos 的docker-compose配置写在了 apisix 的docker-compose.yml里面,为了方便不再重新创建新的docker-compose。
修改config.yaml,把nacos集成到apisix(三个节点都需要配置)
在文件 /data/soft/apisix-docker/example/apisix_conf/config.yaml
中添加以下配置(域名下来后host只需要配置一个,如:https://nacos.qx.com):
discovery:
nacos:
host:
- "http://12.10.8.92:8848"
- "http://12.10.8.93:8848"
- "http://12.10.8.94:8848"
prefix: "/nacos/v1/"
fetch_interval: 30 # default 30 sec
weight: 200 # default 100
timeout:
connect: 3000 # default 2000 ms
send: 3000 # default 2000 ms
read: 6000 # default 5000 ms
修改docker-compose.yml(三个节点都需要配置),NACOS_SERVER_IP 需要改为当前宿主机IP
在/data/soft/apisix-docker/example/docker-compose.yml中services下添加以下配置:
nacos:
image: nacos/nacos-server:v2.2.0
container_name: nacos
environment:
- MODE=cluster # 集群模式
- NACOS_SERVERS=12.10.8.92:8848,12.10.8.93:8848,12.10.8.94:8848 # IP1,IP2是对应的宿主服务器IP,PORT1,PORT2是对应的nacos服务端口
- PREFER_HOST_MODE=ip
- NACOS_SERVER_IP=12.10.8.92 # 服务器外网
- SPRING_DATASOURCE_PLATFORM=mysql
- MYSQL_SERVICE_HOST=***** # mysql数据库对应的ip
- MYSQL_SERVICE_PORT=3306 # mysql数据库对应的端口
- MYSQL_SERVICE_DB_NAME=*** # mysql数据库
- MYSQL_SERVICE_USER=**# mysql数据库对应的用户名
- MYSQL_SERVICE_PASSWORD=***# mysql数据库对应的密码
- MYSQL_SERVICE_DB_PARAM=characterEncoding=utf8&connectTimeout=10000&socketTimeout=10000&autoReconnect=true&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true # mysql8数据库必须配置参数否则报错
- MYSQL_DATABASE_NUM=1 # 数据源为1个
volumes:
- ./nacos/nacos_log:/home/nacos/logs # 日志的挂在
ports:
- "8848:8848"
- "9848:9848"
- "9849:9849"
- "7848:7848"
restart: always
networks:
apisix:
apisix-dashboard:
image: apache/apisix-dashboard:2.15.0-alpine
restart: always
volumes:
- ./dashboard_conf/conf.yaml:/usr/local/apisix-dashboard/conf/conf.yaml
ports:
- "9000:9000"
networks:
apisix:
初始化nacos SQL:
在官网指定版本,如releases v2.2,下找到最新的SQL初始化文件,一般在conf目录下面:
https://github.com/alibaba/nacos/releases/download/2.2.0/nacos-server-2.2.0.zip,
在nacos数据库执行SQL初始化数据。如果没有执行这一步会导致启动报错。
启动容器服务(三个节点都需要):
docker-compose -p docker-apisix up -d
验证集群:http://12.10.8.92:8848/nacos/
用户密码:nacos/nacos
Nacos Docker 快速开始
遇到的问题:
Nacos
- NACOS DOCKER 官方镜像启动失败报错 nacos Error creating bean with name 'memoryMonitor' defined in URL,需要初始化SQL,和配置数据库连接参数,因为我的是mysql8,所以需要配置。
- nacos节点状态正常,但是添加服务各种报错。Fail to get leader of group naming_instance_metadata,无法选举的报错,后面查看日志发现是端口 - "9849:9849" - "7848:7848" 未开放,未映射,官网和网上文档也并未提及。
APISIX
1.更新apisix集群需要删除数据,并重新创建文件夹,否则集群无法启动
rm -rf /var/lib/docker/volumes/docker-apisix_etcd_data
mkdir -p /var/lib/docker/volumes/docker-apisix_etcd_data
2. ETCD_LISTEN_PEER_URLS和ETCD_LISTEN_CLIENT_URLS的IP最好是0.0.0.0如果按网上或者官方的配置会导致etcd启动失败。
3. 官方配置apache/apisix:latest拉取的版本和etcd版本由冲突,需要修改docker-compose.yml下APISIX镜像的版本,如:apache/apisix:3.0.0-centos
4. 各个节点服务最好在几分钟内都能启动
5. docker-compose.yml下etcd的2380端口也需要映射出来,不然集群无法启动。
6. ETCD_LISTEN_CLIENT_URLS 按网上或者官方的配置会报错etcd bind: cannot assign requeste d address,IP需设置为0.0.0.0
7. apisix [error]failed to set upstream: no valid upstream node 接口服务没有配置好,网上文档有问题,端口是9180,不是9080。
8. etcd_ cluster 的版本不对导致集群启动失败,需要更新etcd_cluster的版本。
参考资料:
https://blog.51cto.com/waringid/5845100
https://nacos.io/zh-cn/docs/v2/quickstart/quick-start.html
https://www.bookstack.cn/read/apisix-3.0-zh/7c42ab4f372353b1.md
最后
以上就是寂寞黑裤为你收集整理的Nacos+ApiSix docker 集群环境搭建(docker-compose方式)Nacos ApiSix docker集群环境搭建(docker-compose方式)docke-compose方式安装APISIX:配置APISIX的配置文件(三个节点都需要修改):Nacos集群搭建:初始化nacos SQL:遇到的问题:参考资料:的全部内容,希望文章能够帮你解决Nacos+ApiSix docker 集群环境搭建(docker-compose方式)Nacos ApiSix docker集群环境搭建(docker-compose方式)docke-compose方式安装APISIX:配置APISIX的配置文件(三个节点都需要修改):Nacos集群搭建:初始化nacos SQL:遇到的问题:参考资料:所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复