【云安全最佳实践】搭建redis集群

2022-10-19 19:13:50 浏览数 (2)

一、基础概览

1、redis集群最少需要6个节点,且redis集群下只有db0,不支持多db。

2、开放端口

节点访问:6391-6393

集群连接:16391-16393

二、准备容器挂载的目录

1.创建根目录

代码语言:shell复制
mkdir /server
cd /server

2.创建容器目录。然后给每个节点创建对应的目录和配置文件

服务器1 192.168.79.131 init.sh

代码语言:shell复制
for port in $(seq 6391 6393); 
do 
mkdir -p /server/redis-cluster/redis-node-${port}/{conf,data}
touch /server/redis-cluster/redis-node-${port}/conf/redis.conf
cat << EOF >/server/redis-cluster/redis-node-${port}/conf/redis.conf
#端口
port ${port} 
bind 0.0.0.0
#启用集群模式
cluster-enabled yes 
cluster-config-file nodes.conf
#超时时间
cluster-node-timeout 5000
# 集群连接地址及端口
cluster-announce-ip 192.168.79.131
cluster-announce-port ${port}
cluster-announce-bus-port 1${port}
appendonly yes
#集群加密
masterauth 123456
requirepass 123456
EOF
done

服务器2 192.168.79.132 init.sh

代码语言:shell复制
for port in $(seq 6391 6393); 
do 
mkdir -p /server/redis-cluster/redis-node-${port}/{conf,data}
touch /server/redis-cluster/redis-node-${port}/conf/redis.conf
cat << EOF >/server/redis-cluster/redis-node-${port}/conf/redis.conf
#端口
port ${port} 
bind 0.0.0.0
#启用集群模式
cluster-enabled yes 
cluster-config-file nodes.conf
#超时时间
cluster-node-timeout 5000
# 集群连接地址及端口
cluster-announce-ip 192.168.79.132
cluster-announce-port ${port}
cluster-announce-bus-port 1${port}
protected-mode no
appendonly yes
#集群加密
masterauth 123456
requirepass 123456
EOF
done

三、使用docker-compose.yml运行redis容器

代码语言:yaml复制
version: '3'

services:
 redis1:
  container_name: redis1
  image: redis:7.0.5
  network_mode: host
  restart: always  
  volumes:
   - /server/redis-cluster/redis-node-6391/conf/redis.conf:/etc/redis/redis.conf
   - /server/redis-cluster/redis-node-6391/data:/data
  environment:
   - TZ=Asia/Shanghai
   - LANG=en_US.UTF-8
  ports:
    - '6391:6379'     #服务端口
    - '16391:16379'   #集群端口
  command: ["redis-server", "/etc/redis/redis.conf"]
  privileged: true    #环境变量
  

 redis2:
  container_name: redis2
  image: redis:7.0.5
  network_mode: host
  restart: always
  volumes:
   -  /server/redis-cluster/redis-node-6392/conf/redis.conf:/etc/redis/redis.conf
   -  /server/redis-cluster/redis-node-6392/data:/data
  environment:
   - TZ=Asia/Shanghai
   - LANG=en_US.UTF-8
  ports:
    - '6392:6379'     #服务端口
    - '16392:16379'   #集群端口
  command: ["redis-server", "/etc/redis/redis.conf"]
  privileged: true    #环境变量

 redis3:
  container_name: redis3
  image: redis:7.0.5
  network_mode: host
  restart: always
  volumes:
   - /server/redis-cluster/redis-node-6393/conf/redis.conf:/etc/redis/redis.conf
   - /server/redis-cluster/redis-node-6393/data:/data
  environment:
   - TZ=Asia/Shanghai
   - LANG=en_US.UTF-8
  ports:
    - '6393:6379'     #服务端口
    - '16393:16379'   #集群端口
  command: ["redis-server", "/etc/redis/redis.conf"]
  privileged: true    #环境变量

分别在两台服务器上执行

代码语言:shell复制
docker-compose up -d

四、组建redis集群

随便进一台服务器进行配置

进入某一个容器

代码语言:shell复制
docker exec -it redis1 /bin/bash

组建集群

代码语言:shell复制
redis-cli -p 6391 -a 123456 --cluster create 192.168.79.131:6391 192.168.79.131:6392 192.168.79.131:6393 192.168.79.132:6391 192.168.79.132:6392 192.168.79.132:6393 --cluster-replicas 1
image.pngimage.png

输入yes即可

五、查看集群信息

主从关系

代码语言:shell复制
redis-cli -p 6391 -a 123456

#查看节点

cluster nodes
image.pngimage.png

0 人点赞