docker-compose通过shell脚本一键安装redis集群
in Docker with 0 comment

docker-compose通过shell脚本一键安装redis集群

in Docker with 0 comment

目标

之前我在通过docker安装redis集群的时候,按照网上的教程一步一步的来安装,我也写过我安装成功的案例,如有需要,请看下文,但是docker虽然简化了一些繁琐的步骤,但不可避免的还是有一些步骤需要自己手动写命令,所以我就写了个shell脚本,励志通过一个命令即可完成redis集群的部署

Docker Compose部署Redis Cluster

一键安装redis集群

1 编写shell脚本

#!/bin/bash

# 创建redis挂载目录
echo "step 1 -> 创建redis安装位置------"
mkdir -p /usr/local/docker/volumes/redis-cluster
cd /usr/local/docker/volumes/redis-cluster

echo "step 2 -> 创建redis-cluster.tmpl模板------"
cat <<'EOF'> redis-cluster.tmpl
port ${PORT}
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip ${CLUSTER_ANNOUNCE_IP}
cluster-announce-port ${PORT}
cluster-announce-bus-port 1${PORT}
EOF

#for port in `seq 6391 6396`; do \
#firewall-cmd --zone=public --add-port=${port}/tcp --permanent
#done

echo "step 3 -> 创建redis数据配置挂载目录------"

CLUSTER_ANNOUNCE_IP=127.0.0.1
echo ${CLUSTER_ANNOUNCE_IP}

for port in `seq 6391 6396`; do \
mkdir -p ./${port}/conf \
&& PORT=${port} CLUSTER_ANNOUNCE_IP=${CLUSTER_ANNOUNCE_IP}  envsubst < ./redis-cluster.tmpl > ./${port}/conf/redis.conf \
&& mkdir -p ./${port}/data; \
done

echo "step 4 -> 创建redis docker-compose.yaml 模板------"
cat <<EOF > docker-compose.yaml
version: '3'
services:
  redis-6391:
    image: redis:latest
    container_name: redis-6391
    command: redis-server /usr/local/etc/redis/redis.conf --appendonly yes
    privileged: true
    restart: always
    volumes:
      - /usr/local/docker/volumes/redis-cluster/6391/data:/data
      - /usr/local/docker/volumes/redis-cluster/6391/conf/redis.conf:/usr/local/etc/redis/redis.conf
    ports:
      - 6391:6391
      - 16391:16391
  redis-6392:
    image: redis:latest
    container_name: redis-6392
    command: redis-server /usr/local/etc/redis/redis.conf --appendonly yes
    privileged: true
    restart: always
    volumes:
      - /usr/local/docker/volumes/redis-cluster/6392/data:/data
      - /usr/local/docker/volumes/redis-cluster/6392/conf/redis.conf:/usr/local/etc/redis/redis.conf
    ports:
      - 6392:6392
      - 16392:16392
  redis-6393:
    image: redis:latest
    container_name: redis-6393
    command: redis-server /usr/local/etc/redis/redis.conf --appendonly yes
    privileged: true
    restart: always
    volumes:
      - /usr/local/docker/volumes/redis-cluster/6393/data:/data
      - /usr/local/docker/volumes/redis-cluster/6393/conf/redis.conf:/usr/local/etc/redis/redis.conf
    ports:
      - 6393:6393
      - 16393:16393
  redis-6394:
    image: redis:latest
    container_name: redis-6394
    command: redis-server /usr/local/etc/redis/redis.conf --appendonly yes
    privileged: true
    restart: always
    volumes:
      - /usr/local/docker/volumes/redis-cluster/6394/data:/data
      - /usr/local/docker/volumes/redis-cluster/6394/conf/redis.conf:/usr/local/etc/redis/redis.conf
    ports:
      - 6394:6394
      - 16394:16394
  redis-6395:
    image: redis:latest
    container_name: redis-6395
    command: redis-server /usr/local/etc/redis/redis.conf --appendonly yes
    privileged: true
    restart: always
    volumes:
      - /usr/local/docker/volumes/redis-cluster/6395/data:/data
      - /usr/local/docker/volumes/redis-cluster/6395/conf/redis.conf:/usr/local/etc/redis/redis.conf
    ports:
      - 6395:6395
      - 16395:16395
  redis-6396:
    image: redis:latest
    container_name: redis-6396
    command: redis-server /usr/local/etc/redis/redis.conf --appendonly yes
    privileged: true
    restart: always
    volumes:
      - /usr/local/docker/volumes/redis-cluster/6396/data:/data
      - /usr/local/docker/volumes/redis-cluster/6396/conf/redis.conf:/usr/local/etc/redis/redis.conf
    ports:
      - 6396:6396
      - 16396:16396
EOF

echo "docker-compose redis 生成成功!"

echo "step 5 -> 运行docker-compose 部署启动redis容器------"
# 运行docker-compose启动redis容器
docker-compose -f docker-compose.yaml up -d

exist=$(docker inspect --format '{{.State.Running}}' redis-6391)

if [[${exist}!='true']];
then
    sleep 3000
else
    echo 'redis容器启动成功!'
    IP_RESULT=""
    CONTAINER_IP=""
    for port in `seq 6391 6396`;
    do
    #CONTAINER_IP=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' redis-${port})
    IP_RESULT=${IP_RESULT}${CLUSTER_ANNOUNCE_IP}":"${port}" "
    done
fi
echo "获取redis容器ip和端口号:" ${IP_RESULT}

echo "step 6 -> redis 执行集群指令------"
docker run --rm -it inem0o/redis-trib create --replicas 1 ${IP_RESULT}

注意
CLUSTER_ANNOUNCE_IP:要换成你的公网ip,就是你可以访问到的ip,
同时执行脚本前,先开放6391~6396 以及16391~16396端口

2 以下脚本是为了测试集群以及重新部署使用

#!/bin/bash
for port in `seq 6391 6396`; do
docker stop "redis-"${port}
sleep 1
docker rm "redis-"${port}
done
#!/bin/bash
docker exec -it redis-6391 redis-cli -p 6391 -a 123456

当你执行show_cluster_info.sh,再输入cluster nodes 或者 cluster info

懒人模式

如果你觉得上述步骤觉得还是有点麻烦可以下载我上传到github的代码

redis-cluster

总结

程序员的天性就是懒,我的目标就是解放双手,点一下就安装成功,岂不爽哉~