redis哨兵、集群、docker

 

Dockerfile

# 基于Alpine Linux的Redis镜像
# FROM alpine:latest
FROM harbor.sjzt.com/pub/openjdk:8-jre

COPY redis-7.4.1.zip /redis-7.4.1.zip

# 暴露Redis端口
EXPOSE 6379

# 复制自定义配置文件(如果需要) 需删除原有的redis.conf才能COPY
RUN unzip /redis-7.4.1.zip 
COPY redis.conf /redis-7.4.1/redis/redis.conf
# 后续命令都会在/redis-7.4.1/redis目录下执行
WORKDIR /redis-7.4.1/redis

# 运行Redis服务器
CMD ./redis-server redis.conf

 

构建容器
docker build -t xmh-redis:7.4.1 .
运行容器
docker run -d -p 6379:6379 --name my-redis-container xmh-redis:7.4.1

docker ps -a | grep redis
docker logs -f --tail 300 3446e62252b4
docker stop 0cad3753b186 
docker rm 0cad3753b186 
docker exec -it 0cad3753b186 bash 进入容器

docker run -it --rm xmh-redis:7.4.1 which redis-server

  

 https://redis.io/docs/latest/commands/cluster-addslotsrange/ redis官方命令

$ tar xzf redis-7.4.1.tar.gz
$ cd redis-7.4.1
make # 编译
make install  # 可以不做此操作 安装Redis到系统 redis解压包可以删除了
make uninstall # 卸载Redis

cd src
mkdir redis
cp redis-server ../redis
cp redis-benchmark ../redis
cp redis-cli ../redis
cp redis.conf redis   redis.conf 在外层
cd redis
redis.conf配置如下  如果外部机器需要连接,127.0.0.1需替换为真实ip
requirepass 设置密码    
slaveof xx.xx.xx.xx 6379 在从节点上配置挂接的主节点
masterauth your_master_password 从机连接主机的密码
#bind localhost 注释掉 (代码连接需要)
#protected-mode yes 注释掉 (代码连接需要)
6380的节点配置中全部改为6380
启动Redis服务。
nohup ./redis-server redis.conf &
nohup ./redis-server redis6380.conf &
nohup ./redis-server redis6381.conf &
然后用客户端测试一下是否启动成功。
$ ./redis-cli
./redis-cli -p 6380


sentinel auth-pass mymaster 123  如果服务有密码需用密码连接
port 26379  
daemonize yes  Sentinel会在当前终端前台运行,终端关闭则进程终止
sentinel monitor mymaster xx.xx.xx.xx 6379 2
1主2从共3个redis,需要3个哨兵,对于端口为26380和26381的sentinel 只需修改端口号,其他不动
nohup ./redis-sentinel sentinel.conf &
nohup ./redis-sentinel sentinel26380.conf &
nohup ./redis-sentinel sentinel26381.conf &
./redis-cli -p 26379
127.0.0.1:26379> info sentinel
主节点,使用如下命令查看主从节点状态:
127.0.0.1:6379> info replication


集群模式: 某个主节点下线后,从节点会接替主节点的槽位变为主节点,旧的主节点再点启动后变为从节点
cluster-enabled yes
6380的节点配置中全部改为6380
pidfile /var/run/redis_6380.pid
cluster-config-file nodes-6380.conf
dbfilename dump6380.rdb
appendfilename "appendonly6380.aof"
masterauth 123 设置主节点密码 都要设置,主节点下线后再次启动变为从节点
#bind localhost 注释掉 (代码连接需要)
#protected-mode yes 注释掉 (代码连接需要)
redis.conf文件复制六份名为:cluster6379.conf、cluster6380.conf、cluster6381.conf cluster6382.conf cluster6383.conf、cluster6384.conf
nohup ./redis-server cluster6379.conf &
nohup ./redis-server cluster6380.conf &
nohup ./redis-server cluster6381.conf &
nohup ./redis-server cluster6382.conf &
nohup ./redis-server cluster6383.conf &
nohup ./redis-server cluster6384.conf &
./redis-cli -p 6379
连接其余节点 需要单个添加,不能全部复制粘贴5个   127.0.0.1换成具体的地址,否则外部链接报错
cluster meet 172.30.0.103 6380
cluster meet 172.30.0.103 6381
cluster meet 172.30.0.103 6382
cluster meet 172.30.0.103 6383
cluster meet 172.30.0.103 6384
cluster nodes
CLUSTER SLOTS  查看所有槽位分配情况
./redis-cli -p 6379 CLUSTER ADDSLOTSRANGE 0 5461
./redis-cli -p 6380 CLUSTER ADDSLOTSRANGE 5462 10922
./redis-cli -p 6381 cluster addslotsrange 10923 16383
其他3个节点设置为上面3个节点的从节点
127.0.0.1:6382>cluster replicate 8f3c09d20ffbaed7f056abf24191b5913a8b75f8
127.0.0.1:6383>cluster replicate 72466aa56df311ec74f3519d6cb2875e7de8e2b1
127.0.0.1:6384>cluster replicate 2ef91de4a27eec61333f6f4de607a84dc1bc4bc9
连接集群
./redis-cli -c -h 172.30.0.103 -p 6383

cluster nodes
0ee69ce755af6a81f245f5cc57a5027deb677e0c 127.0.0.1:6383@16383 slave 72466aa56df311ec74f3519d6cb2875e7de8e2b1 0 1754279334393 7 connected
2ef91de4a27eec61333f6f4de607a84dc1bc4bc9 127.0.0.1:6381@16381 master - 0 1754279329371 2 connected 10923-16383
72466aa56df311ec74f3519d6cb2875e7de8e2b1 127.0.0.1:6380@16380 master - 0 1754279334000 7 connected 5462-10922
8f3c09d20ffbaed7f056abf24191b5913a8b75f8 127.0.0.1:6379@16379 myself,master - 0 0 5 connected 0-5461
03222241ffac9b86365febaf994fa43014a6ac56 127.0.0.1:6382@16382 slave 8f3c09d20ffbaed7f056abf24191b5913a8b75f8 0 1754279335397 5 connected
9d23c68db1d417bedb973dae1711cc5344396f1c 127.0.0.1:6384@16384 slave 2ef91de4a27eec61333f6f4de607a84dc1bc4bc9 0 1754279333000 2 connected

./redis-cli -c -h 172.30.0.103 -p 6379

  

# 基于Ubuntu镜像构建
FROM ubuntu:22.04
# 安装编译依赖
RUN apt-get update && apt-get install -y \
    gcc \
    make \
    wget \
    && rm -rf /var/lib/apt/lists/*
# 设置工作目录
WORKDIR /usr/local/src
# 下载Redis源码(以6.2.6版本为例)
RUN wget http://download.redis.io/releases/redis-6.2.6.tar.gz && \
    tar xzf redis-6.2.6.tar.gz && \
    rm redis-6.2.6.tar.gz
# 进入Redis源码目录并编译
WORKDIR /usr/local/src/redis-6.2.6
RUN make  # 编译Redis
RUN make install  # 安装Redis到系统
# 创建Redis配置文件目录
RUN mkdir -p /etc/redis
# 复制默认配置文件
RUN cp redis.conf /etc/redis/
# 暴露Redis端口
EXPOSE 6379
# 启动Redis服务器
CMD ["redis-server", "/etc/redis/redis.conf"]

 

posted @ 2025-08-03 20:02  XUMT111  阅读(9)  评论(0)    收藏  举报