一、安装docker、ruby环境
#安装所需的软件包
yum install -y yum-utils
device-mapper-persistent-data
lvm2
#设置稳定的存储库
yum-config-manager
--add-repo
https://download.docker.com/linux/centos/docker-ce.repo
#安装docker ce(docker社区版)
yum install docker-ce
#启动docker
systemctl start docker
#获取redis镜像(最新版为4.0.9)
docker pull redis
#在docker库获取ruby镜像
docker pull ruby
二、部署redis集群
#创建集群目录
mkdir /opt/cluster/redis
cd /opt/cluster/redis
#创建模板文件redis-cluster.tmpl,内容如下
port ${PORT}
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 10000
cluster-announce-ip 123.*.*.*
cluster-announce-port ${PORT}
cluster-announce-bus-port 1${PORT}
appendonly yes
#生成conf和data目标,并生成配置信息
for port in `seq 6370 6375`; do
mkdir -p ./${port}/conf
&& PORT=${port} envsubst < ./redis-cluster.tmpl > ./${port}/conf/redis.conf
&& mkdir -p ./${port}/data;
done
#创建6个redis容器
for port in `seq 6370 6375`; do
docker run -d -ti -p ${port}:${port} -p 1${port}:1${port}
-v /opt/cluster/redis/${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf
-v /opt/cluster/redis/${port}/data:/data
--restart always --name redis-${port} --net bridge
-m 100M --memory-swap=100M -h redis${port}
--sysctl net.core.somaxconn=1024 redis redis-server /usr/local/etc/redis/redis.conf;
done
#通过启动ruby来实现集群
echo yes | docker run -i --rm --net bridge ruby sh -c '
gem install redis
&& wget http://download.redis.io/redis-stable/src/redis-trib.rb
&& ruby redis-trib.rb create --replicas 1
'"$(for port in `seq 6370 6375`; do
echo -n "$(docker inspect --format '{{ (index .NetworkSettings.Networks "bridge").IPAddress }}' "redis-${port}")":${port} ' ' ;
done)"