Redis簡單叢集搭建及(error)NOAUTH Authentication required等問題的解決方法
一、redisz主從叢集最少需要6個節點
首先我們既然要搭建叢集,那麼master節點至少要3個,slave節點也是3個,為什麼呢?這是因為一個redis叢集如果要對外提供可用的服務,那麼叢集中必須要有過半的master節點正常工作。基于這個特性,如果想搭建一個能夠允許 n 個master節點挂掉的叢集,那麼就要搭建2n+1個master節點的叢集。(感覺和Zookeeper的vote機制差不多)
如:
2個master節點,挂掉1個,則1不過半,則叢集down掉,無法使用,容錯率為0
3個master節點,挂掉1個,2>1,還可以正常運作,容錯率為1
4個master節點,挂掉1個,3>1,還可以正常運作,但是當挂掉2個時,2=2,不過半,容錯率依然為1
如果建立叢集時設定slave為1個
--cluster-replicas 1
則當總節點少于6個時會有如下報錯:
[[email protected] 7000]# redis-cli --cluster create 0.0.0.0:7000 0.0.0.0:7001 0.0.0.0:7002 --cluster-replicas 1 -a wzy123
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
*** ERROR: Invalid configuration for cluster creation.
*** Redis Cluster requires at least 3 master nodes.
*** This is not possible with 3 nodes and 1 replicas per node.
*** At least 6 nodes are required.
是以叢集搭建至少需要6個節點哦~
二、redis叢集開始搭建
首先尼,由于條件限制,我隻有一台伺服器,是以節點ip都一緻,隻是端口不同,有條件的朋友可以多ip哦。
我們先在redis根目錄下
mkdir redis_cluster
并在redis_cluster目錄下分别建立7000、7001、7002、7003、7004、7005,并将redis.conf檔案cp到這幾個檔案夾下
[[email protected] redis_cluster]# pwd
/usr/local/redis/redis_cluster
[[email protected] redis_cluster]# ll
total 24
drwxr-xr-x 2 root root 4096 Jul 17 21:43 7000
drwxr-xr-x 2 root root 4096 Jul 17 16:36 7001
drwxr-xr-x 2 root root 4096 Jul 17 16:36 7002
drwxrwxr-x 2 root root 4096 Jul 17 16:37 7003
drwxrwxr-x 2 root root 4096 Jul 17 16:37 7004
drwxrwxr-x 2 root root 4096 Jul 17 16:38 7005
cp redis.conf redis_cluster/7000
cp redis.conf redis_cluster/7001
cp redis.conf redis_cluster/7002
cp redis.conf redis_cluster/7003
cp redis.conf redis_cluster/7004
cp redis.conf redis_cluster/7005
然後修改各個檔案夾中的redis.conf
port 7000 //端口7000,7002,7003
bind 本機ip //預設ip為127.0.0.1 需要改為其他節點機器可通路的ip 否則建立叢集時無法通路對應的端口,無法建立叢集,我這裡是注釋掉了,不知道0.0.0.0行不行
daemonize yes //redis背景運作
pidfile /var/run/redis_7000.pid //pidfile檔案對應7000,7001,7002
cluster-enabled yes //開啟叢集 把注釋#去掉
cluster-config-file nodes_7000.conf //叢集的配置 配置檔案首次啟動自動生成 7000,7001,7002
cluster-node-timeout 15000 //請求逾時 預設15秒,可自行設定
appendonly yes //aof日志開啟 有需要就開啟,它會每次寫操作都記錄一條日志
然後分别啟動這6個節點,也可以寫個shell一鍵搞定~
[[email protected] redis]# cat startRedis.sh
redis-server /usr/local/redis/redis_cluster/7000/redis.conf
redis-server /usr/local/redis/redis_cluster/7001/redis.conf
redis-server /usr/local/redis/redis_cluster/7002/redis.conf
redis-server /usr/local/redis/redis_cluster/7003/redis.conf
redis-server /usr/local/redis/redis_cluster/7004/redis.conf
redis-server /usr/local/redis/redis_cluster/7005/redis.conf
[[email protected] redis]# ./startRedis.sh
[[email protected] redis]# ps -ef|grep redis
root 6304 1 0 21:44 ? 00:00:00 redis-server 0.0.0.0:7000 [cluster]
root 6306 1 0 21:44 ? 00:00:00 redis-server 0.0.0.0:7001 [cluster]
root 6308 1 0 21:44 ? 00:00:00 redis-server 0.0.0.0:7002 [cluster]
root 6310 1 0 21:44 ? 00:00:00 redis-server 0.0.0.0:7003 [cluster]
root 6318 1 0 21:44 ? 00:00:00 redis-server 0.0.0.0:7004 [cluster]
root 6320 1 0 21:44 ? 00:00:00 redis-server 0.0.0.0:7005 [cluster]
root 6345 4985 0 21:44 pts/0 00:00:00 grep --color=auto redi
所有節點啟動好後,輸入以下指令建立叢集,注意ip哦,是你的本地ip
注意:如果輸入指令後沒有反應,那可能是防火牆的關系,關掉防火牆就好了
[[email protected] redis]# redis-cli --cluster create 114.116.35.252:7000 114.116.35.252:7001 114.116.35.252:7002 114.116.35.252:7003 114.116.35.252:7004 114.116.35.252:7005 --cluster-replicas 1
如果加了密碼,那麼建立叢集時就需要加-a wzy123參數
[[email protected] redis]# redis-cli --cluster create 114.116.35.252:7000 114.116.35.252:7001 114.116.35.252:7002 114.116.35.252:7003 114.116.35.252:7004 114.116.35.252:7005 --cluster-replicas 1 -a wzy123
不然會報以下錯誤
[ERR] Node 114.116.35.252:7000 NOAUTH Authentication required.
輸入建立叢集的指令後會出現以下提示,注意Can I set the above configuration? (type 'yes' to accept): yes,該處請輸入yes,不然好像配置設定不了哈希槽(别人說的,沒試)
[[email protected] redis_cluster]# redis-cli --cluster create 114.116.35.252:7000 114.116.35.252:7001 114.116.35.252:7002 114.116.35.252:7003 114.116.35.252:7004 114.116.35.252:7005 --cluster-replicas 1 -a wzy123
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 114.116.35.252:7003 to 114.116.35.252:7000
Adding replica 114.116.35.252:7004 to 114.116.35.252:7001
Adding replica 114.116.35.252:7005 to 114.116.35.252:7002
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 4e571c020d1f2cca020132a9adfdea2a367da21d 114.116.35.252:7000
slots:[0-5460] (5461 slots) master
M: 838382153a78260e274c1d2d11a105dd3986a223 114.116.35.252:7001
slots:[5461-10922] (5462 slots) master
M: e86d01e92214015304461a104a9f14e3cedc7829 114.116.35.252:7002
slots:[10923-16383] (5461 slots) master
S: 22b1f3d83f068973c6e8a5d0b9e87c0c1b950594 114.116.35.252:7003
replicates 838382153a78260e274c1d2d11a105dd3986a223
S: 5248474e122d745b7e929a2705da210d3d150b4c 114.116.35.252:7004
replicates e86d01e92214015304461a104a9f14e3cedc7829
S: b3d20a419df22b4c9f4fe14c1fda22c2920c5c11 114.116.35.252:7005
replicates 4e571c020d1f2cca020132a9adfdea2a367da21d
Can I set the above configuration? (type 'yes' to accept): yes
輸完yes後,會出現如下提示,[OK] All 16384 slots covered.說明成功啦
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
.....
>>> Performing Cluster Check (using node 0.0.0.0:7000)
M: 4e571c020d1f2cca020132a9adfdea2a367da21d 114.116.35.252:7000
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: b3d20a419df22b4c9f4fe14c1fda22c2920c5c11 114.116.35.252:7005
slots: (0 slots) slave
replicates 4e571c020d1f2cca020132a9adfdea2a367da21d
M: e86d01e92214015304461a104a9f14e3cedc7829 114.116.35.252:7002
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 22b1f3d83f068973c6e8a5d0b9e87c0c1b950594 114.116.35.252:7003
slots: (0 slots) slave
replicates 838382153a78260e274c1d2d11a105dd3986a223
M: 838382153a78260e274c1d2d11a105dd3986a223 114.116.35.252:7001
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 5248474e122d745b7e929a2705da210d3d150b4c 114.116.35.252:7004
slots: (0 slots) slave
replicates e86d01e92214015304461a104a9f14e3cedc7829
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
驗證一下
[[email protected] redis_cluster]# redis-cli -c -p 7000
127.0.0.1:7000> auth wzy123
OK
127.0.0.1:7000> cluster nodes
b3d20a419df22b4c9f4fe14c1fda22c2920c5c11 114.116.35.252:[email protected] slave 4e571c020d1f2cca020132a9adfdea2a367da21d 0 1563378113000 6 connected
e86d01e92214015304461a104a9f14e3cedc7829 114.116.35.252:[email protected] master - 0 1563378115000 3 connected 10923-16383
22b1f3d83f068973c6e8a5d0b9e87c0c1b950594 114.116.35.252:[email protected] slave 838382153a78260e274c1d2d11a105dd3986a223 0 1563378116490 4 connected
838382153a78260e274c1d2d11a105dd3986a223 114.116.35.252:[email protected] master - 0 1563378114486 2 connected 5461-10922
5248474e122d745b7e929a2705da210d3d150b4c 114.116.35.252:[email protected] slave e86d01e92214015304461a104a9f14e3cedc7829 0 1563378115488 5 connected
4e571c020d1f2cca020132a9adfdea2a367da21d 114.116.35.252:[email protected] myself,master - 0 1563378114000 1 connected 0-5460
114.116.35.252:7000> set qwe 111
OK
114.116.35.252:7000>exit
[[email protected] 7000]# redis-cli -c -p 7003
114.116.35.252:7003> auth wzy123
OK
114.116.35.252:7003> get qwe
-> Redirected to slot [757] located at 127.0.0.1:7000
(error) NOAUTH Authentication required.
114.116.35.252:7000> auth wzy123
OK
127.0.0.1:7000> get qwe
"111"
127.0.0.1:7000>