1.1、假定部署环境如下:
IP | hostname | 部署实例 |
---|---|---|
172.16.xxx.101 | swarm01 | redis1、sentinel1 |
172.16.xxx.102 | swarm02 | redis2、sentinel2 |
172.16.xxx.103 | swarm02 | redis3、sentinel2 |
1.2、给节点添加标签,指定redis实例运行在哪些节点上
# 在任意一个Manager节点上执行
docker node update --label-add cluster.redis=node1 swarm01
docker node update --label-add cluster.redis=node2 swarm02
docker node update --label-add cluster.redis=node3 swarm03
注意:上面的swarm01/02/03
要修改为真实环境中的节点名称
1.2、部署架构图
1.3、创建数据卷映射目录
# 在三个服务器节点上,分别执行以下命令
mkdir -p /usr/local/redis/conf/
mkdir -p /usr/local/redis/data/
1.4、修改 docker compose编排脚本
将下面脚本中的IP172.16.xxx.xxx
替换为实际部署环境中的真实IP
,其他信息不用改
version: '3.8'
services:
redis1:
image: redis:6.2.4
environment:
TZ: "Asia/Shanghai"
ports:
- target: 6379
published: 6379
protocol: tcp
mode: host
volumes:
- /usr/local/redis/data:/data
- /usr/local/redis/conf:/conf
networks:
- net
deploy:
mode: global
placement:
constraints:
- node.labels.cluster.redis == node1
sysctls:
net.core.somaxconn: '511'
command: >
bash -c "if [ ! -f /conf/redis.conf ];then
echo 'port 6379' > /conf/redis.conf ;
echo 'bind 0.0.0.0' >> /conf/redis.conf ;
echo 'slave-announce-ip 172.16.xxx.101' >> /conf/redis.conf ;
echo 'slave-announce-port 6379' >> /conf/redis.conf ;
echo 'masterauth 123456' >> /conf/redis.conf;
echo 'requirepass 123456' >> /conf/redis.conf ;
echo 'appendonly yes' >> /conf/redis.conf ; fi &&
redis-server /conf/redis.conf"
# 行1:如果redis.conf不存在
# 行2-8:新建redis.conf文件,并写入配置信息
# 行9:启动redis实例
redis2:
image: redis:6.2.4
environment:
TZ: "Asia/Shanghai"
ports:
- target: 6379
published: 6379
protocol: tcp
mode: host
volumes:
- /usr/local/redis/data:/data
- /usr/local/redis/conf:/conf
networks:
- net
depends_on:
- redis1
deploy:
mode: global
placement:
constraints:
- node.labels.cluster.redis == node2
sysctls:
net.core.somaxconn: '511'
command: >
bash -c "if [ ! -f /conf/redis.conf ];then
echo 'port 6379' > /conf/redis.conf ;
echo 'bind 0.0.0.0' >> /conf/redis.conf ;
echo 'slave-announce-ip 172.16.xxx.102' >> /conf/redis.conf ;
echo 'slave-announce-port 6379' >> /conf/redis.conf ;
echo 'masterauth 123456' >> /conf/redis.conf;
echo 'requirepass 123456' >> /conf/redis.conf ;
echo 'replicaof 172.16.xxx.101 6379' >> /conf/redis.conf ;
echo 'appendonly yes' >> /conf/redis.conf ; fi &&
redis-server /conf/redis.conf"
redis3:
image: redis:6.2.4
environment:
TZ: "Asia/Shanghai"
ports:
- target: 6379
published: 6379
protocol: tcp
mode: host
volumes:
- /usr/local/redis/data:/data
- /usr/local/redis/conf:/conf
networks:
- net
depends_on:
- redis1
deploy:
mode: global
placement:
constraints:
- node.labels.cluster.redis == node3
sysctls:
net.core.somaxconn: '511'
command: >
bash -c "if [ ! -f /conf/redis.conf ];then
echo 'port 6379' > /conf/redis.conf ;
echo 'bind 0.0.0.0' >> /conf/redis.conf ;
echo 'slave-announce-ip 172.16.xxx.103' >> /conf/redis.conf ;
echo 'slave-announce-port 6379' >> /conf/redis.conf ;
echo 'masterauth 123456' >> /conf/redis.conf;
echo 'requirepass 123456' >> /conf/redis.conf ;
echo 'replicaof 172.16.xxx.101 6379' >> /conf/redis.conf ;
echo 'appendonly yes' >> /conf/redis.conf ; fi &&
redis-server /conf/redis.conf"
sentinel1:
image: redis:6.2.4
environment:
TZ: "Asia/Shanghai"
ports:
- target: 26379
published: 26379
protocol: tcp
mode: host
depends_on:
- redis1
- redis2
- redis3
volumes:
- /usr/local/redis/data:/data
- /usr/local/redis/conf:/conf
networks:
- net
deploy:
mode: global
placement:
constraints:
- node.labels.cluster.redis == node1
sysctls:
net.core.somaxconn: '511'
command: >
bash -c "if [ ! -f /conf/sentinel.conf ];then
echo 'port 26379' > /conf/sentinel.conf ;
echo 'sentinel announce-ip 172.16.xxx.101' >> /conf/sentinel.conf ;
echo 'sentinel monitor mymaster 172.16.xxx.101 6379 2' >> /conf/sentinel.conf ;
echo 'sentinel auth-pass mymaster 123456' >> /conf/sentinel.conf ; fi &&
redis-server /conf/sentinel.conf --sentinel"
# 行1:如果sentinel.conf不存在
# 行2-5:新建sentinel.conf文件,并写入配置信息
# 行6:启动sentinel实例
sentinel2:
image: redis:6.2.4
environment:
TZ: "Asia/Shanghai"
ports:
- target: 26379
published: 26379
protocol: tcp
mode: host
depends_on:
- redis1
- redis2
- redis3
volumes:
- /usr/local/redis/data:/data
- /usr/local/redis/conf:/conf
networks:
- net
deploy:
mode: global
placement:
constraints:
- node.labels.cluster.redis == node2
sysctls:
net.core.somaxconn: '511'
command: >
bash -c "if [ ! -f /conf/sentinel.conf ];then
echo 'port 26379' > /conf/sentinel.conf ;
echo 'sentinel announce-ip 172.16.xxx.102' >> /conf/sentinel.conf ;
echo 'sentinel monitor mymaster 172.16.xxx.101 6379 2' >> /conf/sentinel.conf ;
echo 'sentinel auth-pass mymaster 123456' >> /conf/sentinel.conf ; fi &&
redis-server /conf/sentinel.conf --sentinel"
sentinel3:
image: redis:6.2.4
environment:
TZ: "Asia/Shanghai"
ports:
- target: 26379
published: 26379
protocol: tcp
mode: host
depends_on:
- redis1
- redis2
- redis3
volumes:
- /usr/local/redis/data:/data
- /usr/local/redis/conf:/conf
networks:
- net
deploy:
mode: global
placement:
constraints:
- node.labels.cluster.redis == node3
sysctls:
net.core.somaxconn: '511'
command: >
bash -c "if [ ! -f /conf/sentinel.conf ];then
echo 'port 26379' > /conf/sentinel.conf ;
echo 'sentinel announce-ip 172.16.xxx.103' >> /conf/sentinel.conf ;
echo 'sentinel monitor mymaster 172.16.xxx.101 6379 2' >> /conf/sentinel.conf ;
echo 'sentinel auth-pass mymaster 123456' >> /conf/sentinel.conf ; fi &&
redis-server /conf/sentinel.conf --sentinel"
networks:
net:
driver: overlay
ipam:
driver: default
config:
- subnet: "192.168.1.0/24"
1.5、直接在portainer -> stack 菜单,执行以上docker compose编排脚本即可部署成功
服务实例启动成功后,每个服务器节点的文件结构如下:
[root@swarm01 redis]# pwd
/usr/local/redis
[root@swarm01 redis]# tree
.
├── conf
│ ├── redis.conf
│ └── sentinel.conf
└── data
├── appendonly.aof
└── dump.rdb