Docker Compose 单机部署Redis+Senti
更新:HHH   时间:2023-1-7


卸载旧版本docker uninstall old version

sudo yum remove docker \

                  docker-client \

                  docker-client-latest \

                  docker-common \

                  docker-latest \

                  docker-latest-logrotate \

                  docker-logrotate \

                  docker-engine

安装repo install docker repo

sudo yum install -y yum-utils \

  device-mapper-persistent-data \

  lvm2

sudo yum-config-manager \

--add-repo \

    https://download.docker.com/linux/centos/docker-ce.repo

安装社区版本install Docker Engine-Community

sudo yum install -y docker-ce docker-ce-cli containerd.io --skip-broken

设置服务启动和测试镜像

sudo systemctl start docker

sudo systemctl enable docker

sudo docker run hello-world

安装git和下载代码

sudo yum install -y git

git clone https://github.com/AliyunContainerService/redis-cluster

cd redis-cluster

检查docker compose文件

cat docker-compose.yml

master:

image: redis:3

slave:

image: redis:3

command: redis-server --slaveof redis-master 6379

links:

- master:redis-master

sentinel:

build: sentinel

environment:

- SENTINEL_DOWN_AFTER=5000

- SENTINEL_FAILOVER=5000

links:

- master:redis-master

- slave

在模板中定义了下面一系列服务

  • master: Redis master
  • slave: Redis slave
  • sentinel: Redis Sentinel

cat sentinel.conf

# Example sentinel.conf can be downloaded from http://download.redis.io/redis-stable/sentinel.conf

port 26379

dir /tmp

sentinel monitor mymaster redis-master 6379 $SENTINEL_QUORUM

sentinel down-after-milliseconds mymaster $SENTINEL_DOWN_AFTER

sentinel parallel-syncs mymaster 1

sentinel failover-timeout mymaster $SENTINEL_FAILOVER

并以sentinel模式启动容器。其配置文件如下,其中包含了sentinel对名为"mymaster"的集群的监控配置

注意:

  • slave和sentinel容器初始化配置的Redis master节点主机名为"redis-master",这里我们利用了Docker容器连接的别名机制来连接master和sentinel/slave容器实例
  • 由于我们会部署3个Sentinel,我们把sentinel的"quorum"设置为2,只有两个sentinel同意故障切换,才会真正切换相应的redis master节点。

sudo yum install -y docker-compose

sudo docker-compose up -d

容我上个厕所

。。。。。。。。。。。

厕所回来都好了

docker ps

怎么只有2个哨兵,扩展到3个

docker-compose scale sentinel=3

顺大便扩容2个slave redis

docker-compose scale slave=2

看下目前跑的内容

docker ps|sort -k2

改一个测试redis宕机的脚本

vi test.sh

MASTER_IP=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' rediscluster_master_1)

SLAVE_IP=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' rediscluster_slave_1)

SENTINEL_IP=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' rediscluster_sentinel_1)

echo Redis master: $MASTER_IP

echo Redis Slave: $SLAVE_IP

echo ------------------------------------------------

echo Initial status of sentinel

echo ------------------------------------------------

docker exec rediscluster_sentinel_1 redis-cli -p 26379 info Sentinel

echo Current master is

docker exec rediscluster_sentinel_1 redis-cli -p 26379 SENTINEL get-master-addr-by-name mymaster

echo ------------------------------------------------

echo Stop redis master

docker pause rediscluster_master_1

echo Wait for 10 seconds

sleep 10

echo Current infomation of sentinel

docker exec rediscluster_sentinel_1 redis-cli -p 26379 info Sentinel

echo Current master is

docker exec rediscluster_sentinel_1 redis-cli -p 26379 SENTINEL get-master-addr-by-name mymaster

echo ------------------------------------------------

echo Restart Redis master

docker unpause rediscluster_master_1

sleep 5

echo Current infomation of sentinel

docker exec rediscluster_sentinel_1 redis-cli -p 26379 info Sentinel

echo Current master is

docker exec rediscluster_sentinel_1 redis-cli -p 26379 SENTINEL get-master-addr-by-name mymaster

./test.sh

发现redis master 进行了切换


但是一台主机的3个redis和3个sentinel 不能算高可用,需要将redis和sentinel部署到不同区域的多个主机中

返回云计算教程...