redis install cluster 集群安装
2024-07-04redis install cluster 集群安装redis.io/releases
#!/bin/sh
#set -x
REDIS_HOME=/jesong/redis
REDIS_CONF=${REDIS_HOME}/conf
REDIS_DATA=${REDIS_HOME}/data
REDIS_LOG=${REDIS_HOME}/logs
REDIS_RUN=${REDIS_HOME}/run
CC=/etc/init.d/create-cluster.sh
mkdir -p ${REDIS_HOME}/{conf,data,logs,run}
passwd=$(head -c 500 /dev/urandom | tr -dc [:alnum:]| head -c10)
passwd=Bjmr0cakP7Zw
IP=$(ifconfig eth0|grep inet|awk '{print $2}'|head -1)
ver=redis-7.0.4
wget -c http://download.redis.io/releases/${ver}.tar.gz -P untar
#wget -c http://www.easyliao.com/tar/${ver}.tar.gz
tar zxf untar/${ver}.tar.gz
cd ${ver}
make
make install
PORT=9100
NODES=6
ENDPORT=$((PORT+NODES))
while [[ $((PORT < ENDPORT)) != "0" ]]; do
PORT=$((PORT+1));
file=${REDIS_CONF}/$PORT.conf;
/bin/cp ./redis.conf $file;
sed -i "s/^bind 127.0.0.1/bind ${IP}/g" $file;
sed -i "s/^protected-mode yes/protected-mode no/g" $file;
sed -i "s/^port 6379/port $PORT/g" $file;
sed -i "s/^#.*cluster-enabled.*\$/cluster-enabled yes/" $file;
sed -i "s/^daemonize no/daemonize yes/" $file;
sed -i "s/^# maxmemory .*\$/maxmemory 1g/" $file;
sed -i "s/^stop-writes-on-bgsave-error yes/stop-writes-on-bgsave-error no/" $file;
sed -i "s,^dir.*\$,dir ${REDIS_DATA}," $file;
sed -i "s/^#.*masterauth.*\$/masterauth $passwd/g" $file;
sed -i "s/^#.*requirepass foobared.*\$/requirepass $passwd/g" $file;
sed -i "s/^stop-writes-on-bgsave-error yes.*\$/stop-writes-on-bgsave-error no/" $file;
sed -i "s,^#.*cluster-config-file.*\$,cluster-config-file nodes-${PORT}.conf," $file;
sed -i "s,^pidfile \/var\/run\/redis_6379.pid,pidfile ${REDIS_RUN}\/redis_${PORT}.pid," $file;
sed -i "s,^dbfilename dump.rdb,dbfilename dump${PORT}.rdb,g" $file;
sed -i "s,^logfile \"\",logfile ${REDIS_LOG}\/${PORT}.log,g" $file;
sed -i "s,^appendfilename \"appendonly.aof\",appendfilename \"appendonly${PORT}.aof\",g" $file;
#sed -i "s/^#.*save.*\$/save 3600 1/" $file;
echo $file;
done
##start
cat << EOT > $REDIS_HOME/start_all_redis.sh
#!/bin/sh
#####start
exefile=/usr/local/bin/redis-server
confdir=/jesong/redis/conf
PORT=9100
NODES=6
ENDPORT=\$((PORT+NODES))
while [ \$((PORT < ENDPORT)) != "0" ]; do
PORT=\$((PORT+1))
echo \$PORT
\${exefile} \${confdir}/\${PORT}.conf
done
EOT
###shut
cat << EOF > $REDIS_HOME/shutdown_all_redis.sh
#!/bin/sh
#export REDISCLI_AUTH=\$passwd
#/usr/local/bin/redis-cli -h \${IP} -p 9101 shutdown save 2>/dev/null
#####start
IP=\$(ifconfig eth0|grep inet|awk '{print \$2}'|head -1)
exefile=/usr/local/bin/redis-cli
confdir=/jesong/redis/conf
PORT=9100
NODES=6
ENDPORT=\$((PORT+NODES))
export REDISCLI_AUTH=$passwd
while [ \$((PORT < ENDPORT)) != "0" ]; do
PORT=\$((PORT+1))
echo \$PORT
\${exefile} -h \${IP} -p \${PORT} shutdown save 2>/dev/null
done
EOF
cat << EOF > $REDIS_HOME/create_cluster_redis.sh
#!/bin/sh
IP=\$(ifconfig eth0|grep inet|awk '{print \$2}'|head -1)
REPLICAS=1
PORT=9100
NODES=6
ENDPORT=\$((PORT+NODES))
HOSTS=""
while [ \$((PORT < ENDPORT)) != "0" ]; do
PORT=\$((PORT+1))
HOSTS="\$HOSTS \$IP:\$PORT"
done
export REDISCLI_AUTH=$passwd
redis-cli --cluster create \$HOSTS --cluster-replicas \$REPLICAS
exit 0
EOF
cat << EOF > $REDIS_HOME/conn_cluster_redis.sh
#!/bin/sh
IP=\$(ifconfig eth0|grep inet|awk '{print \$2}'|head -1)
export REDISCLI_AUTH=$passwd
redis-cli -c -h \$IP -p 9101
EOF
cat << EOF > $REDIS_HOME/2create_cluster_redis.sh
#!/bin/sh
#IP=\$(ifconfig eth0|grep inet|awk '{print $2}'|head -1)
iplist=(
10.28.29.6
10.28.29.7
10.28.29.8
)
REPLICAS=1
HOSTS=""
for IP in \${iplist[@]};do
PORT=9100
NODES=2
ENDPORT=\$((PORT+NODES))
while [ \$((PORT < ENDPORT)) != "0" ]; do
PORT=\$((PORT+1))
HOSTS="\$HOSTS \$IP:\$PORT"
done
done
#echo \${HOSTS#,}
echo \${HOSTS}
export REDISCLI_AUTH=$passwd
redis-cli --cluster create \${HOSTS} --cluster-replicas \$REPLICAS --cluster-yes --no-auth-warning
exit 0
EOF
cat << EOF > $REDIS_HOME/3get_cluster_redis.sh
#!/bin/sh
#IP=\$(ifconfig eth0|grep inet|awk '{print $2}'|head -1)
iplist=(
10.28.29.6
10.28.29.7
10.28.29.8
)
REPLICAS=1
HOSTS=""
for IP in \${iplist[@]};do
PORT=9100
NODES=2
ENDPORT=\$((PORT+NODES))
while [ \$((PORT < ENDPORT)) != "0" ]; do
PORT=\$((PORT+1))
HOSTS="\$HOSTS \$IP:\$PORT"
done
done
#echo \${HOSTS#,}
echo \${HOSTS}
echo REDISCLI_AUTH=$passwd
#redis-cli --cluster create \${HOSTS} --cluster-replicas \$REPLICAS --cluster-yes --no-auth-warning
exit 0
EOF