Redis教程

在三台服务器上搭建redis集群

本文主要是介绍在三台服务器上搭建redis集群,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Redis集群

参考-->redis

分别在三台主机上创建如下文件和文件夹

mkdir -p /usr/local/docker/redis
cd /usr/local/docker/redis
touch docker-compose.yaml
vim docker-compose.yaml
192.168.86.58的docker-compose.yaml

version: '3'
services:
  master:
    image: redis
    container_name: redis-master
    restart: always
    command: redis-server --port 6379 --requirepass test1234 --masterauth test1234 --appendonly yes
    ports:
      - 6379:6379
    privileged: true
    volumes:
      - /usr/local/docker/redis/data:/data
      - /usr/local/docker/redis/conf/:/usr/local/etc/redis/

192.168.86.19的docker-compose.yaml

version: '3'
services:
  redis-slave1:
    image: redis
    container_name: redis-slave1
    restart: always
    command: redis-server --slaveof 192.168.86.58 6379 --port 6379 --requirepass test1234 --masterauth test1234 --appendonly yes
    ports:
      - 6379:6379
    privileged: true
    volumes:
      - /usr/local/docker/redis/data:/data
      - /usr/local/docker/redis/conf/:/usr/local/etc/redis/

192.168.86.43的docker-compose.yaml

version: '3'
services:
  redis-slave2:
    image: redis
    container_name: redis-slave2
    restart: always
    command: redis-server --slaveof 192.168.86.58 6379 --port 6379 --requirepass test1234 --masterauth test1234 --appendonly yes
    ports:
      - 6379:6379
    privileged: true
    volumes:
      - /usr/local/docker/redis/data:/data
      - /usr/local/docker/redis/conf/:/usr/local/etc/redis/

参数解释:
privileged: true 权限
requirepass test1234 访问本机需要的密码
masterauth test1234 作为从机访问主机的密码
appendonly yes 是否持久化

docker-compose up -d 后台启动
后面可以用docker ps查看容器是否正常启动
docker-compose down 关闭

进入docker exec -it redis-master bash
输入redis-cli

如果你更改了端口号,在进入集群的时候,要使用redis-cli -p 端口号(如果是6379就不用加-p)

在输入info

哨兵模式

在/usr/local/docker/redis这个目录下创建新的文件夹sentinel
mkdir sentinel
cd sentinel
touch sentinel.conf
touch docker-compose.yml

192.168.86.63的sentinel.conf编写:(其他两台服务器也可以使用26379,因为从属不同的服务器)

# bind 127.0.0.1 192.168.1.1
# protected-mode no
port 26379
#daemonize yes
#pidfile /var/run/redis-sentinel.pid
logfile "sentinel.log"
sentinel monitor redis-master 192.168.86.58 6379 2
sentinel auth-pass redis-master test1234
sentinel down-after-milliseconds redis-master 10000

sentinel announce-ip "192.168.86.58"
sentinel announce-port 26379

sentinel deny-scripts-reconfig yes

其他两台的配置文件和上面的一样

docker-compose.yaml文件
192.168.86.58

version: '3'
services:
  redis-sentinel-1:
    image: redis
    container_name: redis-sentinel-1
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    ports:
      - 26379:26379
	privileged: true
    volumes:
      - /usr/local/docker/redis/sentinel:/usr/local/etc/redis/

192.168.86.19

version: '3'
services:
  redis-sentinel-2:
    image: redis
    container_name: redis-sentinel-2
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    ports:
      - 26379:26379
	privileged: true
    volumes:
      - /usr/local/docker/redis/sentinel:/usr/local/etc/redis/

192.168.86.43

version: '3'
services:
  redis-sentinel-3:
    image: redis
    container_name: redis-sentinel-3
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    ports:
      - 26379:26379
	privileged: true
    volumes:
      - /usr/local/docker/redis/sentinel:/usr/local/etc/redis/	

启动哨兵:docker-compose up -d

进入哨兵的容器中:
docker exec -it 容器名称 bash
进去集群: redis-cli -p 26379(你设置的端口,我设置的是26379)
输入:info查看当前的配置信息

这篇关于在三台服务器上搭建redis集群的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!