redis僞集羣腳本

  1         #安裝redis僞集羣腳本,先把redis-4.0.9.tar.gz redis-3.2.2.gem及啓動腳本放在/data1/redis-cluster目錄下,而後執行該腳本便可 
  2         #!/bin/bash
  3         set -e
  4         #獲取redis本機ip
  5         #ip=`hostname -I`
  6         ip=`ifconfig | grep inet | grep 10. | awk '{print $2}'`
  7         workspace=/data1/redis-cluster  #根據需求自定義  想在哪一個目錄安裝就寫哪一個目錄
  8         if [ ! -d $workspace ];then
  9             mkdir -p $workspace
 10         fi
 11         #下載並解壓安裝redis
 12         cd $workspace
 13         wget http://download.redis.io/releases/redis-4.0.9.tar.gz
 14         tar zxvf redis-4.0.9.tar.gz -C $workspace
 15         cd $workspace/redis-4.0.9
 16         make  &&  make install PREFIX=$workspace
 17         
 18         #配置redis
 19         cd $workspace
 20         mv bin redis-01
 21         cp $workspace/redis-4.0.9/redis.conf redis-01
 22         sed -i '69s/127.0.0.1/'$ip'/' redis-01/redis.conf
 23         sed -i '92,158s/6379/7001/' redis-01/redis.conf
 24         sed -i '136s/no/yes/' redis-01/redis.conf
 25         sed -i '672s/no/yes/' redis-01/redis.conf
 26         sed -i '814s/#//' redis-01/redis.conf
 27         sed -i '822s/#//' redis-01/redis.conf
 28         sed -i '828s/#//' redis-01/redis.conf
 29         
 30         
 31         cp -r redis-01 redis-02
 32         cp -r redis-01 redis-03
 33         cp -r redis-01 redis-04
 34         cp -r redis-01 redis-05
 35         cp -r redis-01 redis-06
 36         
 37         sed -i '92,158s/7001/7002/' redis-02/redis.conf
 38         sed -i '92,158s/7001/7003/' redis-03/redis.conf
 39         sed -i '92,158s/7001/7004/' redis-04/redis.conf
 40         sed -i '92,158s/7001/7005/' redis-05/redis.conf
 41         sed -i '92,158s/7001/7006/' redis-06/redis.conf
 42         
 43         #啓動腳本
 44         echo "#/bin/bash
 45         
 46         cd $workspace/redis-01  
 47         ./redis-server redis.conf  
 48         
 49         cd $workspace/redis-02
 50         ./redis-server redis.conf
 51         
 52         cd $workspace/redis-03
 53         ./redis-server redis.conf
 54         
 55         cd $workspace/redis-04
 56         ./redis-server redis.conf
 57         
 58         cd $workspace/redis-05
 59         ./redis-server redis.conf
 60         
 61         cd $workspace/redis-06
 62         ./redis-server redis.conf
 63         " >$workspace/all-start.sh
 64         
 65         #中止腳本
 66         echo "#/bin/bash
 67         redis-cli -h $ip -p 7001  shutdown
 68         redis-cli -h $ip -p 7002  shutdown
 69         redis-cli -h $ip -p 7003  shutdown
 70         redis-cli -h $ip -p 7004  shutdown
 71         redis-cli -h $ip -p 7005  shutdown
 72         redis-cli -h $ip -p 7006  shutdown
 73         ">$workspace/all-stop.sh
 74 
 75         #自啓動腳本  注:EOF先後都不該有空格或其餘符號
 76         cat > /etc/init.d/redis <<EOF
77
#!/bin/sh 78 # 79 # chkconfig: 2345 70 20 80 # description: Redis-cluster autostart 81 . /etc/init.d/functions 82 83 case "$1" in 84 start) 85 /bin/bash /data1/redis-cluster/all-start.sh 86 ;; 87 stop) 88 /bin/bash /data1/redis-cluster/all-stop.sh 89 ;; 90 *) 91 echo "Usage: $0 (start|stop)" 92 ;; 93 esac 94 EOF 95 96 #啓動redis 97 chmod 741 $workspace/*.sh 98 $workspace/all-start.sh 99 sleep 3 100 #安裝redis-trib所需的 ruby腳本 101 cp $workspace/redis-4.0.9/src/redis-trib.rb $workspace/redis-trib.rb 102 103 wget https://rubygems.org/downloads/redis-3.2.2.gem 104 yum install -y ruby rubygems 105 gem install redis-3.2.2.gem 106 107 yum install -y expect 108 #配置集羣 109 expect -c " 110 cd $workspace; 111 spawn ./redis-trib.rb create --replicas 1 $ip:7001 $ip:7002 $ip:7003 $ip:7004 $ip:7005 $ip:7006; 112 expect "configuration" { send \"yes\r\"; } 113 expect eof 114 " 115 cp $workspace/redis-01/redis-cli /usr/bin/redis-cli 116 echo -e "\nredis-cluster is ok " 117 chkconfig redis on 118

 

 

啓動腳本redis

單實例設置自啓動腳本

一、複製redis啓動腳本vim

redis啓動腳本通常在redis根目錄的utils,若是不知道路徑,能夠先查看路徑centos

[root@slj-redis data1]#  find / -name redis_init_scriptruby

/data1/redis-cluster/redis-4.0.9/utils/redis_init_scriptbash

 

複製啓動腳本到/etc/init.d/redis文件中centos7

cp /data1/redis-cluster/redis-4.0.9/utils/redis_init_script /etc/init.d/redis-alonespa

 

二、更改redis-alone腳本code

 

首先添加以下倆行server

# chkconfig: 2345 90 10blog

# description: Redis is a persistent key-value database

 

其次更改EXEC   CLIEXEC  CONF 爲實際所安裝目錄便可

REDISPORT=6379

EXEC=/data1/redis-alone/bin/redis-server

CLIEXEC=/data1/redis-alone/bin/redis-cli

 

PIDFILE=/var/run/redis_${REDISPORT}.pid

CONF="/data1/redis-alone/redis.conf"

 

 

chkconfig redis-alone on

 

 

centos7

[Unit]
Description=Redis
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking

ExecStart=/usr/local/redis/src/redis-server /usr/local/redis/redis.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/usr/bin/redis-cli -h 127.0.0.1 -p 6379 shutdown



[Install]
WantedBy=multi-user.target

 

 

集羣設置自啓動

 

 

先寫好集羣啓動腳本

[root@slj-redis redis-cluster]# cat all-start.sh

cd /data1/redis-cluster/redis-01 

./redis-server redis.conf 

 

cd /data1/redis-cluster/redis-02

./redis-server redis.conf

 

cd /data1/redis-cluster/redis-03

./redis-server redis.conf

 

cd /data1/redis-cluster/redis-04

./redis-server redis.conf

 

cd /data1/redis-cluster/redis-05

./redis-server redis.conf

 

cd /data1/redis-cluster/redis-06

./redis-server redis.conf

 

 

關閉腳本

[root@slj-redis redis-cluster]# vim all-stop.sh

 

redis-cli -h 10.253.0.1 -p 7001  shutdown

redis-cli -h 10.253.0.1 -p 7002  shutdown

redis-cli -h 10.253.0.1 -p 7003  shutdown

redis-cli -h 10.253.0.1 -p 7004  shutdown

redis-cli -h 10.253.0.1 -p 7005  shutdown

redis-cli -h 10.253.0.1 -p 7006  shutdown

 

 

 

[root@slj-redis init.d]# vim redis-cluster

#!/bin/sh

#

# chkconfig: 2345 70 20

# description: Redis-cluster autostart

. /etc/init.d/functions

 

 

case "$1" in

    start)

        /bin/bash /data1/redis-cluster/all-start.sh

        ;;

    stop)

        /bin/bash /data1/redis-cluster/all-stop.sh

        ;;

    *)

        echo "Usage: $0 (start|stop)"

        ;;

esac

 

 

chkconfig redis-cluster on
相關文章
相關標籤/搜索