哨兵模式
- 创建部署目录
mkdir redis_sentinel/{node1..node3}/{etc,data,log,sentineldata}
cd redis_sentinel
- 创建YML
cat > docker-compose-redis-sentinel.yml << EOF
version: '3.1'
services:
redis_sentinel_node1:
image: redis:5.0.7
container_name: redis_sentinel_node1
restart: always
user: root
privileged: true
network_mode: host
volumes:
- /etc/localtime:/etc/localtime:ro
- ./node1/etc/redis.conf:/redis/etc/redis.conf
- ./node1/data:/redis/data
- ./node1/log:/redis/log
ports:
- 7101:7101
command: redis-server /redis/etc/redis.conf
logging:
options:
max-size: '100m'
max-file: '10'
redis_sentinel_node2:
image: redis:5.0.7
container_name: redis_sentinel_node2
restart: always
network_mode: host
volumes:
- /etc/localtime:/etc/localtime:ro
- ./node2/etc/redis.conf:/redis/etc/redis.conf
- ./node2/data:/redis/data
- ./node2/log:/redis/log
ports:
- 7102:7102
user: root
privileged: true
command: redis-server /redis/etc/redis.conf
logging:
options:
max-size: '100m'
max-file: '10'
redis_sentinel_node3:
image: redis:5.0.7
container_name: redis_sentinel_node3
restart: always
network_mode: host
volumes:
- /etc/localtime:/etc/localtime:ro
- ./node3/etc/redis.conf:/redis/etc/redis.conf
- ./node3/data:/redis/data
- ./node3/log:/redis/log
ports:
- 7103:7103
user: root
privileged: true
command: redis-server /redis/etc/redis.conf
logging:
options:
max-size: '100m'
max-file: '10'
redis_sentinel_sentinel1:
image: redis:5.0.7
container_name: redis_sentinel_sentinel1
restart: always
network_mode: host
volumes:
- /etc/localtime:/etc/localtime:ro
- ./node1/etc/sentinel.conf:/redis/etc/sentinel.conf
- ./node1/sentineldata:/redis/data
- ./node1/log:/redis/log
ports:
- 17101:17101
user: root
privileged: true
command: redis-sentinel /redis/etc/sentinel.conf
logging:
options:
max-size: '100m'
max-file: '10'
redis_sentinel_sentinel2:
image: redis:5.0.7
container_name: redis_sentinel_sentinel2
restart: always
network_mode: host
volumes:
- /etc/localtime:/etc/localtime:ro
- ./node2/etc/sentinel.conf:/redis/etc/sentinel.conf
- ./node2/sentineldata:/redis/data
- ./node2/log:/redis/log
ports:
- 17102:17102
user: root
privileged: true
command: redis-sentinel /redis/etc/sentinel.conf
logging:
options:
max-size: '100m'
max-file: '10'
redis_sentinel_sentinel3:
image: redis:5.0.7
container_name: redis_sentinel_sentinel3
restart: always
network_mode: host
volumes:
- /etc/localtime:/etc/localtime:ro
- ./node3/etc/sentinel.conf:/redis/etc/sentinel.conf
- ./node3/sentineldata:/redis/data
- ./node3/log:/redis/log
ports:
- 17103:17103
user: root
privileged: true
command: redis-sentinel /redis/etc/sentinel.conf
logging:
options:
max-size: '100m'
max-file: '10'
EOF
- 创建配置文件
redis主节点配置文件
cat > node1/etc/redis.conf << EOF
# 监听端口
port 7101
# 绑定地址
bind 0.0.0.0
# Redis密码
requirepass Pwd@123
appendonly yes
# 持久化文件存放目录
dir /redis/data
# 日志文件
logfile /redis/log/redis.log
# 让redis后台运行
daemonize no
# 关闭保护模式
protected-mode no
# 数据库数量
databases 16
# 内存限制
maxmemory 4G
masterauth Pwd@123
EOF
cat > node1/etc/sentinel.conf << EOF
port 17101
daemonize no
protected-mode no
pidfile "/var/run/redis-sentinel.pid"
logfile "/redis/log/sentinel.log"
dir "/redis/data"
# 不允许使用SENTINEL SET设置notification-script和client-reconfig-script。
sentinel deny-scripts-reconfig yes
# 指定主节点IP地址和端口,并且指定当有2台哨兵认为主机挂了,则对主机进行容灾切换。
sentinel monitor ceshi 192.168.10.10 7101 2
# 当在Redis实例中开启了requirepass,这里就需要提供密码。
sentinel auth-pass ceshi Pwd@123
# 这里设置了主机多少秒无响应,则认为挂了。
sentinel down-after-milliseconds ceshi 3000
# 主备切换时,最多有多少个slave同时对新的master进行同步,这里设置为默认的1。
sentinel parallel-syncs ceshi 1
# 故障转移的超时时间,这里设置为三分钟。
sentinel failover-timeout ceshi 180000
# 是否允许通过执行脚本来重新配置自身("yes"时,Sentinel节点将拒绝通过执行脚本来重新配置自身)
sentinel config-epoch ceshi yes
EOF
从节点配置文件(node2)
cat > node2/etc/redis.conf << EOF
# 监听端口
port 7102
# 绑定地址
bind 0.0.0.0
# Redis密码
requirepass Pwd@123
# 开启数据持久化(根据需求开启)
appendonly yes
# 持久化文件存放目录
dir /redis/data
# 日志文件
logfile /redis/log/redis.log
# 让redis后台运行
daemonize no
# 关闭保护模式
protected-mode no
# 数据库数量
databases 16
# 内存限制
maxmemory 4G
masterauth Pwd@123
replicaof 192.168.10.10 7101
EOF
cat > node2/etc/sentinel.conf << EOF
port 17102
daemonize no
protected-mode no
pidfile "/var/run/redis-sentinel.pid"
logfile "/redis/log/sentinel.log"
dir "/redis/data"
sentinel deny-scripts-reconfig yes
sentinel monitor ceshi 192.168.10.10 7101 2
sentinel auth-pass ceshi Pwd@123
sentinel down-after-milliseconds ceshi 3000
sentinel parallel-syncs ceshi 1
sentinel failover-timeout ceshi 180000
sentinel config-epoch ceshi yes
EOF
从节点配置文件(node3)
cat > node3/etc/redis.conf << EOF
# 监听端口
port 7103
# 绑定地址
bind 0.0.0.0
# Redis密码
requirepass Pwd@123
# 开启数据持久化(根据需求开启)
appendonly yes
# 持久化文件存放目录
dir /redis/data
# 日志文件
logfile /redis/log/redis.log
# 让redis后台运行
daemonize no
# 关闭保护模式
protected-mode no
# 数据库数量
databases 16
# 内存限制
maxmemory 4G
masterauth Pwd@123
replicaof 192.168.10.10 7101
EOF
cat > node3/etc/sentinel.conf << EOF
port 17103
daemonize no
protected-mode no
pidfile "/var/run/redis-sentinel.pid"
logfile "/redis/log/sentinel.log"
dir "/redis/data"
sentinel deny-scripts-reconfig yes
sentinel monitor ceshi 192.168.10.10 7101 2
sentinel auth-pass ceshi Pwd@123
sentinel down-after-milliseconds ceshi 3000
sentinel parallel-syncs ceshi 1
sentinel failover-timeout ceshi 180000
sentinel config-epoch ceshi yes
EOF
- 启动节点
docker-compose -f docker-compose-redis-sentinel.yml up -d
集群模式
1.创建YAML
cat > docker-compose-redis-cluster.yml << EOF
version: '3.1'
services:
redis_cluster_node1:
image: redis:5.0.5
container_name: redis_cluster_node1
restart: always
user: root
privileged: true
network_mode: host
volumes:
- /etc/localtime:/etc/localtime:ro
- ./redis/node1/etc/redis.conf:/redis/etc/redis.conf
- ./redis/node1/data:/redis/data
- ./redis/node1/log:/redis/log
ports:
- 7001:7001
- 17001:17001
command: redis-server /redis/etc/redis.conf
logging:
options:
max-size: '100m'
max-file: '10'
redis_cluster_node2:
image: redis:5.0.5
container_name: redis_cluster_node2
restart: always
network_mode: host
volumes:
- /etc/localtime:/etc/localtime:ro
- ./redis/node2/etc/redis.conf:/redis/etc/redis.conf
- ./redis/node2/data:/redis/data
- ./redis/node2/log:/redis/log
ports:
- 7002:7002
- 17002:17002
user: root
privileged: true
command: redis-server /redis/etc/redis.conf
logging:
options:
max-size: '100m'
max-file: '10'
redis_cluster_node3:
image: redis:5.0.5
container_name: redis_cluster_node3
restart: always
network_mode: host
volumes:
- /etc/localtime:/etc/localtime:ro
- ./redis/node3/etc/redis.conf:/redis/etc/redis.conf
- ./redis/node3/data:/redis/data
- ./redis/node3/log:/redis/log
ports:
- 7003:7003
- 17003:17003
user: root
privileged: true
command: redis-server /redis/etc/redis.conf
logging:
options:
max-size: '100m'
max-file: '10'
redis_cluster_node4:
image: redis:5.0.5
container_name: redis_cluster_node4
restart: always
network_mode: host
volumes:
- /etc/localtime:/etc/localtime:ro
- ./redis/node4/etc/redis.conf:/redis/etc/redis.conf
- ./redis/node4/data:/redis/data
- ./redis/node4/log:/redis/log
ports:
- 7004:7004
- 17004:17004
user: root
privileged: true
command: redis-server /redis/etc/redis.conf
logging:
options:
max-size: '100m'
max-file: '10'
redis_cluster_node5:
image: redis:5.0.5
container_name: redis_cluster_node5
restart: always
network_mode: host
volumes:
- /etc/localtime:/etc/localtime:ro
- ./redis/node5/etc/redis.conf:/redis/etc/redis.conf
- ./redis/node5/data:/redis/data
- ./redis/node5/log:/redis/log
ports:
- 7005:7005
- 17005:17005
user: root
privileged: true
command: redis-server /redis/etc/redis.conf
logging:
options:
max-size: '100m'
max-file: '10'
redis_cluster_node6:
image: redis:5.0.5
container_name: redis_cluster_node6
restart: always
network_mode: host
volumes:
- /etc/localtime:/etc/localtime:ro
- ./redis/node6/etc/redis.conf:/redis/etc/redis.conf
- ./redis/node6/data:/redis/data
- ./redis/node6/log:/redis/log
ports:
- 7006:7006
- 17006:17006
user: root
privileged: true
command: redis-server /redis/etc/redis.conf
logging:
options:
max-size: '100m'
max-file: '10'
EOF
2.创建集群创建脚本及配置文件
- 集群配置文件创建
mkdir redis/{node1..node6}/{etc,data,log}
cat > redis/{node1..node6}/etc/redis.conf << EOF
# 监听端口
port 7001
# 绑定地址
bind 0.0.0.0
# Redis密码
requirepass Pwd@123
# 开启集群功能
cluster-enabled yes
# 集群的配置文件名称,不需要我们创建,由redis自己维护
cluster-config-file nodes.conf
# 节点心跳失败的超时时间
cluster-node-timeout 5000
# 开启数据持久化
appendonly yes
# 持久化文件存放目录
dir /redis/data
# 日志文件
logfile /redis/log/redis.log
# 不让redis后台运行
daemonize no
# 注册的实例ip(避免多网卡时IP冲突) #设置为本机IP
cluster-announce-ip 192.168.10.10
cluster-announce-port 7001
cluster-announce-bus-port 17001
# 关闭保护模式
protected-mode no
# 数据库数量(集群模式只有一个db
0库所以此配置无效注释掉)
# databases 16
# 内存限制(单节点内存 3主3从集群内存为4*3=12G)
maxmemory 4G
masterauth Pwd@123
EOF
- 修改配置文件脚本
cat > redis/edietc.sh << EOF
localip=`ifconfig eth0|awk NR==2|awk '{print $2}'`
set -i 's/7001/7001/g' node1/etc/redis.conf
set -i 's/7001/7002/g' node2/etc/redis.conf
set -i 's/7001/7003/g' node3/etc/redis.conf
set -i 's/7001/7004/g' node4/etc/redis.conf
set -i 's/7001/7005/g' node5/etc/redis.conf
set -i 's/7001/7006/g' node6/etc/redis.conf
sed -i 's/192.168.10.10/${localip}/g' node1/etc/redis.conf
sed -i 's/192.168.10.10/${localip}/g' node2/etc/redis.conf
sed -i 's/192.168.10.10/${localip}/g' node3/etc/redis.conf
sed -i 's/192.168.10.10/${localip}/g' node4/etc/redis.conf
sed -i 's/192.168.10.10/${localip}/g' node5/etc/redis.conf
sed -i 's/192.168.10.10/${localip}/g' node6/etc/redis.conf
EOF
sh redis/edietc.sh
- 创建集群创建脚本
cat > redis/createcluster.sh << EOF
localip=`ifconfig eth0|awk NR==2|awk '{print $2}'`
docker exec -it redis_cluster_node1 redis-cli -a Weihu12345 --cluster create --cluster-replicas 1 ${localip}:7001 ${localip}:7002 ${localip}:7003 ${localip}:7004 ${localip}:7005 ${localip}:7006
EOF
3.启动服务
docker-compose -f docker-compose-redis-cluster.yml up -d
4.创建集群
sh redis/createcluster.sh