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
}
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
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
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 ~]
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
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