Shell腳本一分鐘解決redis批量部署


批量部署一組redis服務,端口從8001到8009
當前環境:Centos6

第一步 編譯redis

[root@ localhost ~]# vim redis_install.sh
#!/bin/bash

yum install -y nmap unzip wget lsof xz net-tools gcc make gcc-c++

wget http://download.redis.io/releases/redis-5.0.6.tar.gz
tar zxvf redis-5.0.6.tar.gz
cd redis-5.0.6
make

mkdir  -p /opt/redis/bin/
cp src/redis-benchmark /opt/redis/bin/
cp src/redis-check-aof /opt/redis/bin/
cp src/redis-check-dump /opt/redis/bin/
cp src/redis-cli /opt/redis/bin/
cp src/redis-sentinel /opt/redis/bin/
cp src/redis-server /opt/redis/bin/

cd ..
#cp conf/redis.conf /opt/redis/redis.conf
cat >/opt/redis/redis.conf<<EOF
daemonize yes
port 7121
timeout 60
loglevel warning
databases 16
EOF

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

/opt/redis/bin/redis-server /opt/redis/redis.conf

echo "ok"

#sysctl vm.overcommit_memory=1 
##或執行
#echo vm.overcommit_memory=1 >>/proc/sys/vm/overcommit_memory

##釋放內存
#sync && echo 3 > /proc/sys/vm/drop_caches

##測試連接
#/opt/redis/bin/redis-cli -h 127.0.0.1 -p 7121 
#/opt/redis/bin/redis-cli -h 127.0.0.1 -p 7121 -a Msxxsdsdsfaqqqqqq

運行並查看

[root@ localhost ~]# sh redis_install.sh
6301:C 20 Sep 2019 10:54:36.218 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
6301:C 20 Sep 2019 10:54:36.218 # Redis version=5.0.6, bits=64, commit=00000000, modified=0, pid=6301, just started
6301:C 20 Sep 2019 10:54:36.218 # Configuration loaded
ok
......
[root@ localhost ~]# ps aux | grep redis
root       6302  0.0  0.7 152428  7792 ?        Ssl  10:54   0:00 /opt/redis/bin/redis-server *:7121               
root       6460  0.0  0.0 103316   840 pts/2    S+   10:57   0:00 grep --color=auto redis

第二步 拷貝redis執行文件,修改配置文件,並啓動。

[root@ localhost ~]# vim redis_start.sh
#!/bin/bash

num1=$2
num2=$3
n=$2
case "$1" in

start)
### start
for ((n=$num1;n<=$num2;n++))
do
echo "port:$n ok"
/opt/redis/bin/redis-server /opt/redis/redis_$n/redis.conf
done
### start
 ;;

 stop)
### stop
for ((n=$2;n<=$3;n++))
do
echo "port:$n ok"
ps -ef | grep redis | grep $n | grep -v grep | grep -v sh | awk '{print $2}' | xargs kill
done
echo ok
### stop
 ;;

 restart)
echo restart
 ;;
 
 install)
### install
for ((n=$num1;n<=$num2;n++))
do
echo "port:$n ok"
mkdir -p /opt/redis/redis_$n
cp /opt/redis/redis.conf  /opt/redis/redis_$n/
sed -i "s/7121/$n/g"  /opt/redis/redis_$n/redis.conf
/opt/redis/bin/redis-server /opt/redis/redis_$n/redis.conf
done
### install
 ;;

 uninstall)
echo uninstall
 ;;

 *)
 echo "Usage: $SCRIPTNAME {start|stop|restart|install|uninstall}" >&2
 exit 3
 ;;
esac

運行並查看

[root@ localhost ~]# sh redis_start.sh install 8001 8009
port:8001 ok
6311:C 20 Sep 2019 10:56:11.542 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
6311:C 20 Sep 2019 10:56:11.542 # Redis version=5.0.6, bits=64, commit=00000000, modified=0, pid=6311, just started
6311:C 20 Sep 2019 10:56:11.542 # Configuration loaded
port:8002 ok
6319:C 20 Sep 2019 10:56:11.567 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
6319:C 20 Sep 2019 10:56:11.567 # Redis version=5.0.6, bits=64, commit=00000000, modified=0, pid=6319, just started
6319:C 20 Sep 2019 10:56:11.567 # Configuration loaded
......

[root@ localhost ~]# ps aux | grep redis
root       6302  0.0  0.7 152428  7792 ?        Ssl  10:54   0:00 /opt/redis/bin/redis-server *:7121               
root       6312  0.0  0.7 152428  7788 ?        Ssl  10:56   0:00 /opt/redis/bin/redis-server *:8001                          
root       6320  0.0  0.7 152428  7788 ?        Ssl  10:56   0:00 /opt/redis/bin/redis-server *:8002                          
root       6328  0.0  0.7 152428  7792 ?        Ssl  10:56   0:00 /opt/redis/bin/redis-server *:8003                          
root       6336  0.7  0.7 152428  7792 ?        Ssl  10:56   0:00 /opt/redis/bin/redis-server *:8004                          
root       6344  0.0  0.7 152428  7788 ?        Ssl  10:56   0:00 /opt/redis/bin/redis-server *:8005                          
root       6352  0.2  0.7 152428  7792 ?        Ssl  10:56   0:00 /opt/redis/bin/redis-server *:8006                          
root       6360  0.0  0.7 152428  7788 ?        Ssl  10:56   0:00 /opt/redis/bin/redis-server *:8007                          
root       6368  0.0  0.7 152428  7792 ?        Ssl  10:56   0:00 /opt/redis/bin/redis-server *:8008                          
root       6376  0.0  0.7 152428  7788 ?        Ssl  10:56   0:00 /opt/redis/bin/redis-server *:8009                          
root       6383  0.0  0.0 103316   840 pts/2    S+   10:56   0:00 grep --color=auto redis

第三步 批量關閉redis端口 8001到8009

[root@ localhost ~]# vim port_stop.sh
#!/bin/bash

num1=$2
num2=$3
n=$2
case "$1" in

start)
### start
for ((n=$num1;n<=$num2;n++))
do
echo "port:$n ok"
/opt/redis/bin/redis-server /opt/redis/redis_$n/redis.conf
done
### start
 ;;

 stop)
### stop
for ((n=$2;n<=$3;n++))
do
echo "port:$n ok"
ps -ef | grep redis | grep $n | grep -v grep | grep -v sh | awk '{print $2}' | xargs kill
done
echo ok
### stop
 ;;

 restart)
echo restart
 ;;
 
 install)
### install
for ((n=$num1;n<=$num2;n++))
do
echo "port:$n ok"
mkdir -p /opt/redis/redis_$n
cp /opt/redis/redis.conf  /opt/redis/redis_$n/
sed -i "s/7121/$n/g"  /opt/redis/redis_$n/redis.conf
/opt/redis/bin/redis-server /opt/redis/redis_$n/redis.conf
done
### install
 ;;

 uninstall)
echo uninstall
 ;;

 *)
 echo "Usage: $SCRIPTNAME {start|stop|restart|install|uninstall}" >&2
 exit 3
 ;;
esac

運行並查看

[root@ localhost ~]# sh port_stop.sh stop 8001 8009
port:8001 ok
port:8002 ok
port:8003 ok
port:8004 ok
port:8005 ok
port:8006 ok
port:8007 ok
port:8008 ok
port:8009 ok
ok
[root@ localhost ~]# ps aux | grep redis
root       6302  0.0  0.7 152428  7792 ?        Ssl  10:54   0:00 /opt/redis/bin/redis-server *:7121               
root       6460  0.0  0.0 103316   840 pts/2    S+   10:57   0:00 grep --color=auto redis

第四步 批量啓動redis端口 8001到8009

[root@ localhost ~]# vim port_start.sh
#!/bin/bash

num1=$2
num2=$3
n=$2
case "$1" in

start)
### start
for ((n=$num1;n<=$num2;n++))
do
echo "port:$n ok"
/opt/redis/bin/redis-server /opt/redis/redis_$n/redis.conf
done
### start
 ;;

 stop)
### stop
for ((n=$2;n<=$3;n++))
do
echo "port:$n ok"
ps -ef | grep redis | grep $n | grep -v grep | grep -v sh | awk '{print $2}' | xargs kill
done
echo ok
### stop
 ;;

 restart)
echo restart
 ;;
 
 install)
### install
for ((n=$num1;n<=$num2;n++))
do
echo "port:$n ok"
mkdir -p /opt/redis/redis_$n
cp /opt/redis/redis.conf  /opt/redis/redis_$n/
sed -i "s/7121/$n/g"  /opt/redis/redis_$n/redis.conf
/opt/redis/bin/redis-server /opt/redis/redis_$n/redis.conf
done
### install
 ;;

 uninstall)
echo uninstall
 ;;

 *)
 echo "Usage: $SCRIPTNAME {start|stop|restart|install|uninstall}" >&2
 exit 3
 ;;
esac

運行並查看

[root@ localhost ~]# sh port_start.sh start 8001 8009
port:8001 ok
6463:C 20 Sep 2019 10:58:27.828 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
6463:C 20 Sep 2019 10:58:27.828 # Redis version=5.0.6, bits=64, commit=00000000, modified=0, pid=6463, just started
6463:C 20 Sep 2019 10:58:27.828 # Configuration loaded
port:8002 ok
......
[root@ localhost ~]# ps aux | grep redis
root       6302  0.0  0.7 152428  7792 ?        Ssl  10:54   0:00 /opt/redis/bin/redis-server *:7121               
root       6464  0.0  0.7 152428  7792 ?        Ssl  10:58   0:00 /opt/redis/bin/redis-server *:8001                          
root       6466  0.0  0.7 152428  7788 ?        Ssl  10:58   0:00 /opt/redis/bin/redis-server *:8002                          
root       6471  0.0  0.7 152428  7788 ?        Ssl  10:58   0:00 /opt/redis/bin/redis-server *:8003                          
root       6476  0.0  0.7 152428  7788 ?        Ssl  10:58   0:00 /opt/redis/bin/redis-server *:8004                          
root       6484  0.0  0.7 152428  7792 ?        Ssl  10:58   0:00 /opt/redis/bin/redis-server *:8005                          
root       6489  0.0  0.7 152428  7788 ?        Ssl  10:58   0:00 /opt/redis/bin/redis-server *:8006                          
root       6494  0.0  0.7 152428  7788 ?        Ssl  10:58   0:00 /opt/redis/bin/redis-server *:8007                          
root       6499  0.0  0.7 152428  7792 ?        Ssl  10:58   0:00 /opt/redis/bin/redis-server *:8008                          
root       6501  6.3  0.7 152428  7788 ?        Ssl  10:58   0:00 /opt/redis/bin/redis-server *:8009                          
root       6509  0.0  0.0 103316   840 pts/2    S+   10:58   0:00 grep --color=auto redis
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章