sudo apt remove docker #卸载原先的版本
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
sudo apt-get install ca-certificates curl gnupg
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
echo "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
"$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
sudo apt install docker.io
Docker
sudo apt install docker-compose
docker-compose
docker pull redis
sudo docker pull redis #拉取redis镜像
mkdir redis
sudo mkdir redis
sudo mkdir redis/data
sudo mkdir redis/conf
#在下面目录里创建master.conf和sentinel.conf文件
ubuntu@i-c2t0v2su:/mnt/redis/conf$
master.conf里面填写:
bind 0.0.0.0
port 6379
requirepass Jiaowu@2023
masterauth Jiaowu@2023
dbfilename dump.rdb
dir "/tmp"
appendonly no
protected-mode no
daemonize no
loglevel debug
sentinel.conf里面填写
port 26379
daemonize no
pidfile "/var/run/redis-sentinel.pid"
logfile ""
dir "/tmp"
sentinel monitor mymaster 172.24.121.37 6379 2
sentinel auth-pass mymaster Jiaowu@2023
sentinel down-after-milliseconds mymaster 3000
sentinel deny-scripts-reconfig yes
#在下面目录里创建slave1conf和sentinel.conf文件
ubuntu@i-c2t0v2su:/mnt/redis/conf$
slave1.conf里面填写:
replicaof 172.24.121.37 6379
masterauth centos
bind 0.0.0.0
port 6380
requirepass centos
dbfilename dump.rdb
dir "/tmp"
appendonly no
protected-mode no
daemonize no
loglevel debug
sentinel.conf里面填写
port 26379
daemonize no
pidfile "/var/run/redis-sentinel.pid"
logfile ""
dir "/tmp"
sentinel monitor mymaster 172.24.121.37 6379 2
sentinel auth-pass mymaster Jiaowu@2023
sentinel down-after-milliseconds mymaster 3000
sentinel deny-scripts-reconfig yes
#在下面目录里创建slave2.conf和sentinel.conf文件
ubuntu@i-c2t0v2su:/mnt/redis/conf$
slave2.conf里面填写:
replicaof 192.168.88.132 6379
masterauth centos
bind 0.0.0.0
port 6381
requirepass centos
dbfilename dump.rdb
dir "/tmp"
appendonly no
protected-mode no
daemonize no
loglevel debugentinel deny-scripts-reconfig yes
sentinel.conf里面填写
port 26379
daemonize no
pidfile "/var/run/redis-sentinel.pid"
logfile ""
dir "/tmp"
sentinel monitor mymaster 172.24.121.37 6379 2
sentinel auth-pass mymaster Jiaowu@2023
sentinel down-after-milliseconds mymaster 3000
sentinel deny-scripts-reconfig yes
sudo docker run -p 6379:6379 --name redis-master -v /mnt/redis/conf:/etc/redis -v /mnt/redis/data:/data --privileged=true -d redis redis-server /etc/redis/master.conf
sudo docker run -p 6379:6379 --name redis-slave1 -v /mnt/redis/conf:/etc/redis -v /mnt/redis/data:/data --privileged=true -d redis redis-server /etc/redis/slave1.conf
sudo docker run -p 6379:6379 --name redis-slave2 -v /mnt/redis/conf:/etc/redis -v /mnt/redis/data:/data --privileged=true -d redis redis-server /etc/redis/slave2.conf
sudo docker run -p 26379:26379 --name redis-sentinel -v /mnt/redis/conf:/etc/redis --privileged=true -d redis redis-sentinel /etc/redis/sentinel.conf
docker: Error response from daemon: Conflict. The container name "/redis-sentinel" is already in use by container "318cf7161baf2952ec0a28726a3ed1ebe7160255a148488b537b35038e97c792". You have to remove (or rename) that container to be able to reuse that name.
See 'docker run --help'.
上面错误可以使用下面命令
#删除容器
sudo docker rm redis-sentinel
#主节点相关命令
sudo docker start redis-master
sudo docker restart redis-master
sudo docker stop redis-master
sudo docker kill redis-master
#从节点相关命令
sudo docker start redis-slave1
sudo docker restart redis-slave1
sudo docker stop redis-slave1
sudo docker kill redis-slave1
#哨兵相关命令
sudo docker start redis-sentinel
sudo docker restart redis-sentinel
sudo docker stop redis-sentinel
sudo docker kill redis-sentinel
# 查看容器进程
sudo docker ps
#杀掉容器进程
sudo docker stop 7d1 && sudo docker rm 7d1
# 进入客户端
sudo docker exec -it redis-master /bin/bash
sudo docker exec -it redis-slave1 /bin/bash
sudo docker exec -it redis-slave2 /bin/bash
redis-cli -p 6379 -a Jiaowu@2023
查看是否设置开机启动
systemctl list-unit-files | grep docker
显示enabled说明docker服务设置了开机启动
显示disabled说明docker服务没有设置开机启动
设置开机启动
systemctl enable docker.service
Docker容器设置自动启动
1、启动相关应用 比如:Mysql、Nginx、Redis,具体如果安装启动,我这里就不多说,我主要讲解一下已经启动的应用容器如何设置自动启动。
2、设置容器自启
docker update --restart=always 容器名称
例如:sudo docker update --restart=always redis-slave2
redis哨兵模式下,当master宕机之后,哨兵sentinel会自动将某一slave升级成新的master,原master重启之后应该自动成为新master的slave节点。但我们发现,原master重启之后,知道新的master是谁,但是却无法建立连接,主要表现在,原master中使用info命令查看节点信息时,发现Replication->master_link_status: down。
原因
我们的redis节点本身是配置了密码的,即配置文件中requirepass配置项值不为空,所以各节点进行主从配置的时候,配置文件中需要通过masterauth配置项指定连接master的密码。但当原master宕机,sentinel重新选举出新master时,sentinel只会往各节点配置文件replicaof配置项中写入新的master地址,但是masterauth配置项却不会更新,所以我们只需要通过原master配置文件中masterauth配置项指定连接新master的密码即可。
解决
原master配置文件中写入以下配置(123456是新master密码)
masterauth 123456
建议
搭建redis哨兵时,如果所在网络安全,各节点可以不设置密码,如果必须设置密码,建议各节点密码设置成相同,且在初始配置时各节点配置文件中就加上masterauth配置项。