redis集群和哨兵模式
docker
1.克隆 centos-8-2105
2.设置ip:
./ip-static
ip: 192.168.64.150
ifconfig
3.上传离线安装文件到 /root/
docker-install(有安装包可不执行下面的步骤)
4.安装docker
1.利用离线文件或者https://download.docker.com/linux/static/stable/x86_64/docker-20.10.6.tgz
在这里可以选择自己需要的版本进行下载:
https://download.docker.com/linux/static/stable/
2.离线安装工具
https://github.com/Jrohy/docker-install/
按图示下载安装工具
5.安装过程
将前面下载的以下文件放入服务器的 /root/docker-install
文件夹下:
- [docker-install]
- docker-20.10.6.tgz
- install.sh
- docker.bash
执行安装:
# 进入 docker-install 文件夹
cd docker-install
# 为 docker-install 添加执行权限
chmod +x install.sh
# 安装
./install.sh -f docker-20.10.6.tgz
6.镜像加速
由于国内网络问题,需要配置加速器来加速
修改配置文件 /etc/docker/daemon.json
下面命令直接生成文件daemon.json
cat <<EOF > /etc/docker/daemon.json
{
"registry-mirrors": [
"https://docker.mirrors.ustc.edu.cn",
"http://hub-mirror.c.163.com"
],
"max-concurrent-downloads": 10,
"log-driver": "json-file",
"log-level": "warn",
"log-opts": {
"max-size": "10m",
"max-file": "3"
},
"data-root": "/var/lib/docker"
}
EOF
之后重新启动服务:
# 重新加载docker配置
sudo systemctl daemon-reload
#重启docker服务
sudo systemctl restart docker
确认安装结果
docker info
Docker 搭建redis集群
六个Redis实例的配置文件:
redis.conf
port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
/opt/redis
/7000/redis.conf
/7001/redis.conf
/7002/redis.conf
/7003/redis.conf
/7004/redis.conf
/7005/redis.conf
mkdir /opt/redis
cd /opt/redis
mkdir 7000 7001 7002 7003 7004 7005
cat <<EOF >7000/redis.conf
port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF
cat <<EOF >7001/redis.conf
port 7001
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF
cat <<EOF >7002/redis.conf
port 7002
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF
cat <<EOF >7003/redis.conf
port 7003
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF
cat <<EOF >7004/redis.conf
port 7004
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF
cat <<EOF >7005/redis.conf
port 7005
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF
先启动6个redis实例
docker run -d --name redis7000 \
-v /opt/redis/7000/redis.conf:/redis.conf \
--net=host \
--restart=always \
redis \
redis-server /redis.conf
docker run -d --name redis7001 \
-v /opt/redis/7001/redis.conf:/redis.conf \
--net=host \
--restart=always \
redis \
redis-server /redis.conf
docker run -d --name redis7002 \
-v /opt/redis/7002/redis.conf:/redis.conf \
--net=host \
--restart=always \
redis \
redis-server /redis.conf
docker run -d --name redis7003 \
-v /opt/redis/7003/redis.conf:/redis.conf \
--net=host \
--restart=always \
redis \
redis-server /redis.conf
docker run -d --name redis7004 \
-v /opt/redis/7004/redis.conf:/redis.conf \
--net=host \
--restart=always \
redis \
redis-server /redis.conf
docker run -d --name redis7005 \
-v /opt/redis/7005/redis.conf:/redis.conf \
--net=host \
--restart=always \
redis \
redis-server /redis.conf
执行命令配置集群
# 进入容器执行集群配置命令
docker exec -it redis7000 \
redis-cli --cluster create \
192.168.64.150:7000 192.168.64.150:7001 \
192.168.64.150:7002 192.168.64.150:7003 \
192.168.64.150:7004 192.168.64.150:7005 \
--cluster-replicas 1
查看集群信息
docker exec -it redis7000 redis-cli -c -p 7000
cluster info
cluster nodes
存数据
docker exec -it redis7000 redis-cli -c -p 7000
# 7002
set a aaaaaaaaaaaaa
# 7000
set b bbbbbbbbbbb
# 7001
set c cccccccccccccccc
redis哨兵
redis主从复制结构中,当主服务器宕机,哨兵可以监控到服务宕机,从从服务器中选举产生一个新的主服务器
1.搭建主从服务器 – 一主两从
docker rm -f $(docker ps -aq)
# 启动主服务器,正常启动,没有特殊设置
docker run -d --name redis6379 --net=host --restart=always redis
docker exec -it redis6379 redis-cli
> info replication
启动两个从服务器
docker run -d --name redis6380 --net=host --restart=always redis \
redis-server --port 6380 --slaveof 192.168.64.150 6379
docker run -d --name redis6381 --net=host --restart=always redis \
redis-server --port 6381 --slaveof 192.168.64.150 6379
docker exec -it redis6379 redis-cli
> info replication
docker exec -it redis6380 redis-cli -p 6380
> info replication
docker exec -it redis6381 redis-cli -p 6381
> info replication
2.添加哨兵配置文件
mkdir /opt/sentinel/
cd /opt/sentinel/
# sentinel monitor mymaster 192.168.64.150 6379 2
# 末尾的 2 表示两台从服务器投票确认主服务器宕机,哨兵才会认为主服务器宕机
cat <<EOF >5000.conf
port 5000
sentinel monitor mymaster 192.168.64.150 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
sentinel parallel-syncs mymaster 1
EOF
cat <<EOF >5001.conf
port 5001
sentinel monitor mymaster 192.168.64.150 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
sentinel parallel-syncs mymaster 1
EOF
cat <<EOF >5002.conf
port 5002
sentinel monitor mymaster 192.168.64.150 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
sentinel parallel-syncs mymaster 1
EOF
3.启动三个哨兵
docker run -d --name sentinel5000 \
-v /opt/sentinel/5000.conf:/sentinel.conf \
--net=host \
redis redis-sentinel /sentinel.conf
docker run -d --name sentinel5001 \
-v /opt/sentinel/5001.conf:/sentinel.conf \
--net=host \
redis redis-sentinel /sentinel.conf
docker run -d --name sentinel5002 \
-v /opt/sentinel/5002.conf:/sentinel.conf \
--net=host \
redis redis-sentinel /sentinel.conf
# 进入一个哨兵容器,查看它监控的主从服务器和其他哨兵
docker exec -it sentinel5000 redis-cli -p 5000
> sentinel master mymaster
> sentinel slaves mymaster
> sentinel sentinels mymaster
4.停止主服务器,测试主服务器重新选举
# 停止主服务器
docker stop redis6379
# 在哨兵日志中查看服务器切换日志: +switch-master mymaster 192.168.64.150 6379 192.168.64.150 6381
docker logs sentinel5000
# 查看 6380 和 6381 服务器的角色变化
docker exec -it redis6380 redis-cli -p 6380
> info replication
docker exec -it redis6381 redis-cli -p 6381
> info replication
重新启动6379,不会把6379切换成主服务器,而是作为从服务器
docker start redis6379
docker exec -it redis6379 redis-cli
> info replication