CLOUD/IaC

[Redis/Shell Programming] 쉘 스크립트로 Redis Master - Slave - Sentinel 구성하기

alsruds 2023. 3. 28. 12:14

◎ 가상머신 7대 준비 CentOS 8

▷ Master : 200.200.200.156

 Slave : 200.200.200.157 / 200.200.200.158 / 200.200.200.159

 Sentinel : 200.200.200.160 / 200.200 200.161 / 200.200.200.162

 

 

● mastercmd.sh

#!/bin/bash

yum install -y redis & wait
sed -i 's/bind 127.0.0.1/bind 0.0.0.0/g' /etc/redis.conf
sed -i 's/daemonize no/daemonize yes/g' /etc/redis.conf
sed -i 's/# min-replicas-to-write 3/min-replicas-to-write 1/g' /etc/redis.conf
sed -i 's/# min-replicas-max-lag 10/min-replicas-max-lag 10/g' /etc/redis.conf
sed -i 's/# requirepass foobared/requirepass qwer1234/g' /etc/redis.conf
systemctl restart redis
systemctl stop firewalld

 

● slavecmd.sh

#!/bin/bash

yum install -y redis & wait
sed -i 's/bind 127.0.0.1/bind 0.0.0.0/g' /etc/redis.conf
sed -i 's/daemonize no/daemonize yes/g' /etc/redis.conf
sed -i 's/# replicaof <masterip> <masterport>/replicaof 200.200.200.156 6379/g' /etc/redis.conf
sed -i 's/# masterauth <master-password>/masterauth qwer1234/g' /etc/redis.conf
sed -i 's/# requirepass foobared/requirepass qwer1234/g' /etc/redis.conf
systemctl restart redis
systemctl stop firewalld

 

● sentinelcmd.sh

#!/bin/bash

yum install -y redis & wait
sed -i 's/# bind 127.0.0.1 192.168.1.1/bind 0.0.0.0/g' /etc/redis-sentinel.conf
sed -i 's/daemonize no/daemonize yes/g' /etc/redis-sentinel.conf
sed -i 's/sentinel monitor mymaster 127.0.0.1 6379 2/sentinel monitor mymaster 200.200.200.156 6379 2/g' /etc/redis-sentinel.conf
sed -i 's/# sentinel auth-pass <master-name> <password>/sentinel auth-pass mymaster qwer1234/g' /etc/redis-sentinel.conf
systemctl restart redis-sentinel
systemctl stop firewalld

 

ex01.sh (실행할 파일)

#!/bin/bash

servers="
200.200.200.156:master
200.200.200.157:slave
200.200.200.158:slave
200.200.200.159:slave
200.200.200.160:sentinel
200.200.200.162:sentinel
200.200.200.161:sentinel
"

for server in $servers
do

        if [ ${server:16:22} = master ]
        then
                sshpass -p 'qwer1234' ssh root@${server:0:15} < mastercmd.sh
                echo $server
                echo "-----------------"
        elif [ ${server:16:21} = slave ]
        then
                sshpass -p 'qwer1234' ssh root@${server:0:15} < slavecmd.sh
                echo $server
                echo "-----------------"
        else
                sshpass -p 'qwer1234' ssh root@${server:0:15} < sentinelcmd.sh
                echo $server
                echo "-----------------"
        fi
done

 

● 확인

# 실행
sh ex01.sh