redis 集群部署

#!/usr/bin/env bash

ip1=192.168.122.152
ip2=192.168.122.161
ip3=192.168.122.65

network(){
ping -c2 www.baidu.com &>/dev/null && echo "network is ok" ||echo " network is notok";exit

}
# 写个用于自动生成密钥对的函数
auto_keygen(){
rpm -qa |grep expect && echo "expect yi anzhuang" ||yum -y install expect
/usr/bin/expect<<EOF
set timeout 30
spawn ssh-keygen
expect {
        ".ssh/id_rsa)"       { send    "\n";  exp_continue }
        "Overwrite (y/n)?"   { send    "y\n"; exp_continue }
        "no passphrase):"    { send    "\n";  exp_continue }
        "again:"             { send    "\n";  exp_continue }
}
expect eof
EOF
}
#ssh-keygen -t rsa -P "" -f /root/.ssh/id_rsa
# 写个自动免密登录的函数
send_key(){
/usr/bin/expect <<EOF
spawn ssh-copy-id $1@$2
expect {
        "*(yes/no)?" { send "yes\n"; exp_continue }
        "*password:*" { send "$3\n"; exp_continue } 
}
expect eof
EOF
}

cat >ips.txt <<EOF
root 192.168.122.65 1
root 192.168.122.161 1
EOF
# 循环一个存放 user ip 的文件,并且把每个传递给 函数
copy_pub(){
old_ifs=$IFS
IFS=$'\n'
for num in $(cat ips.txt)
do
    a=$(echo $num | awk '{print $1}')
    b=$(echo $num | awk '{print $2}')
    c=$(echo $num | awk '{print $3}')
    send_key $a $b $c
done
export IFS=$old_ifs
}


# 定义一个变量,其值是当前用户的公钥文件
pub_key_file=$HOME/.ssh/id_rsa.pub
# 假如公钥文件不存在,说明需要创建密钥对
if [ ! -f ${pub_key_file}  ];then
    auto_keygen
fi


copy_pub

host_info(){
cat >>/etc/hosts <<EOF
192.168.122.152 clus-01
192.168.122.161 clus-02
192.168.122.65 clus-03
EOF
}
host_info
send_key(){
/usr/bin/expect <<EOF
spawn scp /etc/hosts root@$i:/etc/hosts
expect {
        "*(yes/no)?" { send "yes\n" }
}
expect eof
EOF
}


for i in {clus-03,clus-02}
do
    send_key
done

cat >> /etc/security/limits.conf << EOF
* soft nofile 102400
* hard nofile 102400
EOF


echo "net.core.somaxconn = 32767" >> /etc/sysctl.conf

echo "vm.overcommit_memory=1" >> /etc/sysctl.conf
sysctl -p

echo never > /sys/kernel/mm/transparent_hugepage/enabled


for y in {clus-02,clus-03}
do
    scp /etc/security/limits.conf root@$y:/etc/security/limits.conf
    ssh root@$y 'echo "net.core.somaxconn = 32767" >> /etc/sysctl.conf'
    ssh root@$y 'echo "vm.overcommit_memory=1" >> /etc/sysctl.conf'
    ssh root@$y "sysctl -p"
    ssh root@$y 'echo never > /sys/kernel/mm/transparent_hugepage/enabled'
done



install_gcc(){
    yum -y install gcc glibc glibc-kernheaders  glibc-common glibc-devel make
    yum -y install centos-release-scl
    yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
#    scl enable devtoolset-9 bash
    source /opt/rh/devtoolset-9/enable
    echo "source /opt/rh/devtoolset-9/enable" >>/root/.bashrc
}
install_redis(){
    
    wget http://download.redis.io/releases/redis-6.0.8.tar.gz
    tar -zxvf redis-6.0.8.tar.gz -C /usr/local
    mv /usr/local/redis-6.0.8 /usr/local/redis
    cd /usr/local/redis
    make && make all

}
redis_MS(){
    mkdir -p /redis/{6001,6002}/{conf,data,log}
    cd /redis
cat >6001/conf/redis.conf <<eof
bind 0.0.0.0
protected-mode no
port 6001
dir /redis/6001/data
cluster-enabled yes
cluster-config-file /redis/6001/conf/nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes
pidfile /redis/6001/redis.pid
logfile /redis/6001/log/redis.log
eof
sed 's/6001/6002/g' /redis/6001/conf/redis.conf > /redis/6002/conf/redis.conf
}
start_redis(){
    /usr/local/redis/src/redis-server /redis/6001/conf/redis.conf
    /usr/local/redis/src/redis-server /redis/6002/conf/redis.conf

}

install_gcc
install_redis
redis_MS
start_redis

for w in {clus-02,clus-03}
do
    scp /root/redis_cluster2.sh root@"$w":/root/
    ssh root@"$w" "bash /root/redis_cluster2.sh" $>/root/test.txt
done


redis-cli --cluster create "$ip1":6001 "$ip1":6002 "$ip2":6001 "ip2":6002 "$ip3":6001 "$ip3":6002 --cluster-replicas 1

此脚本放在/root/redis_cluster2.sh
[root@redis-server1 ~]# cat /root/redis_cluster2.sh
#!/usr/bin/env bash
#make install redis
network(){
    ping -c2 www.baidu.com &>/dev/null
    if [ $? -ne 0 ];then
        echo "network is error...."
        exit
     
    fi
    
}

test(){
[ $? -ne 0 ] && echo "excution error....";exit

}

install_gcc(){
    yum -y install gcc glibc glibc-kernheaders  glibc-common glibc-devel make
    yum -y install centos-release-scl
    yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
#    scl enable devtoolset-9 bash
    source /opt/rh/devtoolset-9/enable
    echo "source /opt/rh/devtoolset-9/enable" >>/root/.bashrc
}
install_redis(){
    
    wget http://download.redis.io/releases/redis-6.0.8.tar.gz
    tar -zxvf redis-6.0.8.tar.gz -C /usr/local
    mv /usr/local/redis-6.0.8 /usr/local/redis
    cd /usr/local/redis
    make && make all

}
redis_MS(){
    mkdir -p /redis/{6001,6002}/{conf,data,log}
    cd /redis
cat >6001/conf/redis.conf <<eof
bind 0.0.0.0
protected-mode no
port 6001
dir /redis/6001/data
cluster-enabled yes
cluster-config-file /redis/6001/conf/nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes
pidfile /redis/6001/redis.pid
logfile /redis/6001/log/redis.log
eof
sed 's/6001/6002/g' /redis/6001/conf/redis.conf > /redis/6002/conf/redis.conf
}
start_redis(){
    /usr/local/redis/src/redis-server /redis/6001/conf/redis.conf
    /usr/local/redis/src/redis-server /redis/6002/conf/redis.conf

}


network
install_gcc
install_redis
redis_MS
start_redis

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值