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

一、基础概览

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

2、开放端口

节点访问:6391-6393

集群连接:16391-16393

二、准备容器挂载的目录

1.创建根目录

mkdir /server
cd /server

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

服务器1 192.168.79.131 init.sh

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

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容器

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    #环境变量

分别在两台服务器上执行

docker-compose up -d

四、组建redis集群

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

进入某一个容器

docker exec -it redis1 /bin/bash

组建集群

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

输入yes即可

五、查看集群信息

主从关系

redis-cli -p 6391 -a 123456

#查看节点

cluster nodes

未经允许不得转载:木盒主机 » 【云安全最佳实践】搭建redis集群

赞 (0)

相关推荐

    暂无内容!