[IaC] SSH 사용 Redis (Master-Slave-Sentinel)

이정훈·2023년 3월 27일
0

IaC

목록 보기
1/8
post-thumbnail
  • 대화형 명령어 vi편집기와 같은
sed 's/[패턴1]/[패턴2]/g' [파일] : 패턴1을 패턴2로 변경 
  • master_commands.sh
#!/bin/bash

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
  • slave_commands.sh
#!/bin/bash

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 192.168.100.10 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
  • sentinel_commands.sh
#!/bin/bash

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 192.168.100.10 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
  • commands.sh
#!/bin/bash

yum install -y redis & wait
systemctl stop firewalld
setenforce 0
  • redis-modify.sh
servers="
200.200.200.21:master
200.200.200.22:slave
200.200.200.23:slave
200.200.200.24:slave
200.200.200.25:sentinel
200.200.200.26:sentinel
200.200.200.27:sentinel
"

for server in $servers
do
        server_ip=`echo $server | awk -F":" '{print $1}'`
        server_name=`echo $server | awk -F":" '{print $2}'`
        echo $server_ip
        echo $server_name

        sshpass -p 'qwer1234' ssh root@$server_ip < commands.sh

        if [ $server_name = "master" ]
        then
                sshpass -p 'qwer1234' ssh root@$server_ip < master_commands.sh
                echo $server_ip
                echo '-------------------------'
        elif [ $server_name = "slave" ]
        then
                sshpass -p 'qwer1234' ssh root@$server_ip < slave_commands.sh
                echo $server_ip
                echo '-------------------------'
        elif [ $server_name = "sentinel" ]
        then
                sshpass -p 'qwer1234' ssh root@$server_ip < sentienl_commands.sh
                echo $server_ip
                echo '-------------------------'
        fi
done
profile
싱숭생숭늉

0개의 댓글