Redis 5.0 Cluster叢集帶認證及客戶端連線
Redis在3.0版正式引入redis-cluster叢集這個特性。Redis叢集是一個提供在多個Redis間節點間共享資料的程式集。Redis叢集是一個分散式(distributed)、容錯(fault-tolerant)的Redis記憶體K/V服務,叢集可以使用的功能是普通單機Redis所能使用的功能的一個子集(subset),比如Redis叢集並不支援處理多個keys的命令,因為這需要在不同的節點間移動資料,從而達不到像Redis那樣的效能,在高負載的情況下可能會導致不可預料的錯誤。還有比如set裡的並集(unions)和交集(intersections)操作,就沒有實現。通常來說,那些處理命令的節點獲取不到鍵值的所有操作都不會被實現。在將來,使用者或許可以通過使用MIGRATE COPY命令,在叢集上用計算節點(Computation Nodes) 來執行多鍵值的只讀操作, 但Redis叢集本身不會執行復雜的多鍵值操作來把鍵值在節點間移來移去。Redis叢集不像單機版本的Redis那樣支援多個資料庫,叢集只有資料庫0,而且也不支援SELECT命令。Redis叢集通過分割槽來提供一定程度的可用性,在實際環境中當某個節點宕機或者不可達的情況下繼續處理命令。
Redis叢集的優點:
無中心架構,分散式提供服務。資料按照slot儲存分佈在多個redis例項上。增加slave做standby資料副本,用於failover,使叢集快速恢復。實現故障auto failover,節點之間通過gossip協議交換狀態資訊;投票機制完成slave到master角色的提升。支援線上增加或減少節點。降低硬體成本和運維成本,提高系統的擴充套件性和可用性。
Redis叢集的缺點:
client實現複雜,驅動要求實現smart client,快取slots mapping資訊並及時更新。目前僅JedisCluster相對成熟,異常處理部分還不完善,比如常見的“max redirect exception”。客戶端的不成熟,影響應用的穩定性,提高開發難度。節點會因為某些原因發生阻塞(阻塞時間大於clutser-node-timeout),被判斷下線。這種failover是沒有必要,sentinel也存在這種切換場景。
#hosts檔案配置
cat >> /etc/hosts << EOF 192.168.5.65 redis65 192.168.5.66 redis65 EOF
一、搭建redis5.0叢集
從redis 3.0之後版本支援redis-cluster叢集,redis-4.0.0開始支援module,redis-5.0.0開始支援類似於kafka那樣的訊息佇列,Redis-Cluster採用無中心結構,每個節點儲存資料和整個叢集狀態,每個節點都和其他所有節點連線。這樣就可以很好的保證redis的高可用性,下面就來部署個Redis Cluster,在兩臺伺服器上部署6個redis節點
IP和埠配置檔名
192.168.5.65:600165_6001/redis.conf
192.168.5.65:600265_6002/redis.conf
192.168.5.65:600365_6003/redis.conf
192.168.5.66:600166_6001/redis.conf
192.168.5.66:600266_6002/redis.conf
192.168.5.66:600366_6003/redis.conf
1、修改系統引數
#修改最大可開啟檔案數
cat >> /etc/security/limits.conf << EOF * soft nofile 102400 * hard nofile 102400 EOF
#TCP監聽佇列大小
echo "net.core.somaxconn = 32767" >> /etc/sysctl.conf sysctl -p
#OOM相關:vm.overcommit_memory
echo "vm.overcommit_memory=1" >> /etc/sysctl.conf sysctl -p
#開啟核心的“Transparent Huge Pages (THP)”特性
echo never > /sys/kernel/mm/transparent_hugepage/enabled
#請將“echo never > /sys/kernel/mm/transparent_hugepage/enabled”加入到檔案/etc/rc.local中
2、安裝redis並配置redis-cluster
[root@redis65 /]# cd /opt [root@redis65 /]# wget http://download.redis.io/releases/redis-5.0.0.tar.gz [root@redis65 /]# tar -zxvf redis-5.0.0.tar.gz [root@redis65 /]# cd redis-5.0.0/ [root@redis65 /]# make [root@redis65 /]# make install PREFIX=/usr/local/redis-cluster
#建立例項目錄
[root@redis65 redis-5.0.0]# mkdir -p /usr/local/redis-cluster/{65_6001,65_6002,65_6003} [root@redis66 redis-5.0.0]# mkdir -p /usr/local/redis-cluster/{66_6001,66_6002,66_6003}
#配置官方配置檔案,去掉#開頭的和空格行
cat redis.conf |grep -v ^# |grep -v ^$
#redis65 6001配置檔案
[root@redis65 /]#cd /usr/local/redis-cluster [root@redis65 redis-cluster]# cat >> 65_6001/redis.conf << EOF bind 0.0.0.0 protected-mode no port 6001 daemonize no dir /usr/local/redis-cluster/65_6001 cluster-enabled yes cluster-config-file /usr/local/redis-cluster/65_6001/nodes.conf cluster-node-timeout 5000 appendonly yes daemonize yes pidfile /usr/local/redis-cluster/65_6001/redis.pid logfile /usr/local/redis-cluster/65_6001/redis.log EOF
#redis65 6002配置檔案
[root@redis65 redis-cluster]# sed 's/6001/6002/g' 65_6001/redis.conf > 65_6002/redis.conf
#redis65 6003配置檔案
[root@redis65 redis-cluster]# sed 's/6001/6003/g' 65_6001/redis.conf > 65_6003/redis.conf
#寫一個啟動指令碼start-redis-cluster.sh
[root@redis65 /]#cat /usr/local/redis-cluster/start-redis-cluster.sh
#!/bin/sh REDIS_HOME=/usr/local/redis-cluster $REDIS_HOME/bin/redis-server $REDIS_HOME/65_6001/redis.conf $REDIS_HOME/bin/redis-server $REDIS_HOME/65_6002/redis.conf $REDIS_HOME/bin/redis-server $REDIS_HOME/65_6003/redis.conf
chmod +x /usr/local/redis-cluster/start-redis-cluster.sh /usr/local/redis-cluster/start-redis-cluster.sh
#redis66 6001配置檔案
[root@redis66 /]# cd /usr/local/redis-cluster [root@redis66 redis-cluster]# cat >> 66_6001/redis.conf << EOF bind 0.0.0.0 protected-mode no port 6001 daemonize no dir /usr/local/redis-cluster/66_6001 cluster-enabled yes cluster-config-file /usr/local/redis-cluster/66_6001/nodes.conf cluster-node-timeout 5000 appendonly yes daemonize yes pidfile /usr/local/redis-cluster/66_6001/redis.pid logfile /usr/local/redis-cluster/66_6001/redis.log EOF
#redis66 6002配置檔案
[root@redis66 redis-cluster]# sed 's/6001/6002/g' 66_6001/redis.conf > 66_6002/redis.conf
#redis66 6003配置檔案
[root@redis66 redis-cluster]# sed 's/6001/6003/g' 66_6001/redis.conf > 66_6003/redis.conf
[root@redis66 /]#cat /usr/local/redis-cluster/start-redis-cluster.sh
#!/bin/sh REDIS_HOME=/usr/local/redis-cluster $REDIS_HOME/bin/redis-server $REDIS_HOME/66_6001/redis.conf $REDIS_HOME/bin/redis-server $REDIS_HOME/66_6002/redis.conf $REDIS_HOME/bin/redis-server $REDIS_HOME/66_6003/redis.conf
chmod +x /usr/local/redis-cluster/start-redis-cluster.sh
#啟動redis
/usr/local/redis-cluster/start-redis-cluster.sh
#檢視redis程序啟動狀態
ps -ef | grep redis
#建立redis cluster,如果只是想快速建立和啟動redis叢集,可使用redis官方提供的指令碼create-cluster,注意redis-5.0.0版本開始才支援“--cluster”
cd /usr/local/redis-cluster/bin ./redis-cli --cluster create 192.168.5.65:6001 192.168.5.65:6002 192.168.5.65:6003 192.168.5.66:6001 192.168.5.66:6002 192.168.5.66:6003 --cluster-replicas 1
如果配置項cluster-enabled的值不為yes,則執行時會報錯“[ERR] Node 192.168.5.65:6001 is not configured as a cluster node.”。這個時候需要先將cluster-enabled的值改為yes,然後重啟redis-server程序,之後才可以重新執行redis-cli建立叢集。
redis-cli的引數說明:
1) create
表示建立一個redis叢集。
2) --cluster-replicas 1
表示為叢集中的每一個主節點指定一個從節點,即一比一的複製
#檢視redis程序是否已切換為叢集狀態(cluster)
ps -ef|grep redis
#停止redis例項,直接使用kill命令即可
kill -9 15025
#命令列工具redis-cli
[root@redis65 /]# ln -s /usr/local/redis-cluster/bin/redis-cli /bin/redis-cli [root@redis65 bin]# redis-cli -c -p 6001
#檢視叢集中的節點:
127.0.0.1:6001> cluster nodes
127.0.0.1:6002> set name 65-7001 -> Redirected to slot [5798] located at 192.168.5.66:6001 OK 192.168.5.66:6001> quit [root@redis65 bin]# redis-cli -c -p 6003 127.0.0.1:6003> get name -> Redirected to slot [5798] located at 192.168.5.66:6001 "65-7001"
[root@redis65 bin]# redis-cli -h 192.168.5.66 -p 6002
#檢查節點狀態
[root@redis65 bin]# redis-cli --cluster check 192.168.5.66:6001
#檢視叢集資訊
[root@redis65 bin]# redis-cli -c -p 6003 127.0.0.1:6003> cluster info
#給redis cluster叢集加上認證,登入到redis節點執行下面的操作
[root@redis65 /]# redis-cli -h 192.168.5.65 -p 6001 -c > config set masterauth zxc789 > config set requirepass zxc789 > auth zxc789 > config rewrite
#各個節點都完成上面的3條config操作,重啟redis各節點,看下各節點的redis.conf,可以發現最後多了3行內容
[root@redis65 /]# killall redis-server [root@redis65 /]# /usr/local/redis-cluster/start-redis-cluster.sh [root@redis66 ~]# cat /usr/local/redis-cluster/66_6001/redis.conf
#加了認證的redis登入
redis-cli -h 192.168.5.65 -p 6001 -c -a 'zxc789'
二、安裝php的redis擴充套件
1、在現有的web伺服器上安裝php的redis擴充套件
[root@web33 ~]# cd /opt [root@web33 opt]# wget -c -t 0 https://github.com/owlient/phpredis/archive/master.zip [root@web33 opt]# unzip master.zip [root@web33 opt]# cd phpredis-master/ [root@web33 phpredis-master]# /usr/local/php/bin/phpize [root@web33 phpredis-master]# ./configure --with-php-config=/usr/local/php/bin/php-config [root@web33 phpredis-master]# make && make install
#這裡進行make的時候報錯
2、那是因為最新的 phpredis 分了幾大分支,針對最新的PHP穩定發行版 php7 有專門為php7的分支,所以我們從github拉下phpredis 原始碼 需要切換到PHP7的分支 首先git clone phpredis下來
[root@web33 ~]# cd /opt [root@web33 ~]# git clone https://github.com/nicolasff/phpredis [root@web33 ~]# git checkout php7 [root@web33 opt]# cd phpredis [root@web33 phpredis]# /usr/local/php/bin/phpize [root@web33 phpredis]# ./configure --with-php-config=/usr/local/php/bin/php-config [root@web33 phpredis]# make && make install
3、修改php.ini加上“extension=redis.so”
vi /usr/local/php/etc/php.ini extension = "redis.so"
4、重啟php-fpm
/etc/init.d/php-fpm restart
#用phpinfo驗證下redis擴充套件是否安裝成功
5、單例項redis通過php連線測試
<?php //連線192.168.5.65的Redis服務 $redis = new Redis(); $redis->connect('192.168.5.65', 6001); $redis->auth('zxc789'); //redis認證 echo "Connection to server sucessfully"; //檢視服務是否執行 echo "Server is running: " . $redis->ping(); ?>
#執行指令碼,輸出結果為:
Connection to server sucessfully
Server is running: PONG
6、Java操作Redis cluster叢集可使用jredis,PHP要操作redis cluster叢集有兩種方式:
1)使用phpredis擴充套件,這是個c擴充套件,效能更高,但是這個方案參考資料很少
2)使用predis,純php開發,使用了名稱空間,需要php5.3+,靈活性高,我這裡用的是predis,下載地址https://github.com/nrk/predis
[root@web33 tmp]# git clone https://github.com/nrk/predis.git #將predis放到網站根目錄下 [root@web33 tmp]# mv predis /data/www/predis [root@web33 tmp]# cd /data/www/
[root@web33 www]# cat predis.php
<?php require 'predis/autoload.php';//引入predis相關包 //redis例項 $servers = array( 'tcp://192.168.5.65:6001', 'tcp://192.168.5.65:6002', 'tcp://192.168.5.65:6003', 'tcp://192.168.5.66:6001', 'tcp://192.168.5.66:6002', 'tcp://192.168.5.66:6003', ); $options = ['cluster' =>'redis','parameters' => [ 'password' => 'zxc789' ]]; $client = new Predis\Client($servers,$options); $client->set('name1', '1111111'); $client->set('name2', '2222222'); $client->set('name3', '3333333'); $name1 = $client->get('name1'); $name2 = $client->get('name2'); $name3 = $client->get('name3'); var_dump($name1, $name2, $name3);die; ?>