redis叢集搭建以及可能出現的問題
redis
redis下載安裝
cd tmp
mkdir frank
cd frank
mkdir redis
cd redis
wget http://download.redis.io/releases/redis-4.0.1.tar.gz
tar xzf redis-4.0.1.tar.gz
cd redis-4.0.1
make && make install
redis檢視,關閉,解除安裝
- 檢視
ps aux|grep redis
- 關閉
redis-cli shutdown
or redis-cli -h 127.0.0.1 -p 6379 shutdown
or kill -9 10654
- 解除安裝
rm -rf /usr/local/bin/redis*
rm -rf /tmp/frank/redis/redis-4.0.1
啟動redis服務
redis-server
啟動redis客戶端
redis-cli or redis-cli -h 127.0.0.1 -p 6379
ruby
安裝需要的Package
# yum -y install gcc
# yum -y install gcc-c++
# yum -y install zlib zlib-devel
# yum -y install readline readline-devel
# yum -y install openssl openssl-devel
最好是 #yum install gcc gcc-c++ openssl* readline* ncurses* zlib* libxml* libjpeg* libpng* libxslt* libtool*
ruby下載安裝
cd tmp
mkdir frank
cd frank
mkdir ruby
cd ruby
wget https://cache.ruby-lang.org/pub/ruby/2.4/ruby-2.4.2.tar.gz
tar xzf ruby-2.4.2.tar.gz
cd ruby-2.4 .2
./configure
make && make install
安裝redis介面,gem未ruby下的一個工具包
gem install redis
1.問題1
ERROR: Loading command: install (LoadError)
cannot load such file -- zlib
ERROR: While executing gem ... (NoMethodError)
undefined method `invoke_with_build_args' for nil:NilClass
解決 :
# 進入ruby原始碼資料夾
# 安裝ruby自身提供的zlib包
cd ext/zlib
ruby ./extconf.rb
make
make install
搭建redis叢集
1。環境
redis 採用 redis-3.2.1 版本。
伺服器CentOS7.0 兩臺 (IP:116.196.65.198),(IP:47.52.41.245)
2。在兩臺伺服器上,下載安裝redis在tmp/frank/redis目錄下
cd /tmp
mkdir frank
cd frank
mkdir redis
wget http://download.redis.io/releases/redis-3.2.1.tar.gz
tar -zxvf redis-3.2.1.tar.gz
cd redis-3.2.1
make && make install
3。將 redis安裝目錄下面的src目錄下面的redis-trib.rb 複製到 /usr/local/bin 目錄下
cd src
cp redis-trib.rb /usr/local/bin/
4。建立 Redis 節點
分別在兩臺伺服器上redis安裝目錄下建立 redis_cluster 目錄;
mkdir redis_cluster
在116.196.65.198伺服器上面的redis_cluster目錄下面建立名為7000、7001、7002的目錄,並將 redis.conf 拷貝到這三個目錄中
cd redis_cluster
mkdir 7000 7001 7002
cd ..
cp redis.conf redis_cluster/7000
cp redis.conf redis_cluster/7001
cp redis.conf redis_cluster/7002
分別修改這三個配置檔案,修改如下內容
port 7000 //埠7000,7002,7003 protected-mode no //關掉保護模式3.2之後 不然會報Sorry, can't connect to node
#bind 本機ip //預設ip為127.0.0.1,在3.2之後要註釋掉 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日誌開啟 有需要就開啟,它會每次寫操作都記錄一條日誌
接著在另外一臺機器上(47.52.41.245),的操作重複以上三步,只是把目錄改為7003、7004、7005,對應的配置檔案也按照這個規則修改即可
5。啟動各個節點
#第一臺機器上執行即116.196.65.198
redis-server redis_cluster/7000/redis.conf
redis-server redis_cluster/7001/redis.conf
redis-server redis_cluster/7002/redis.conf
#另外一臺機器上執行即47.52.41.245
redis-server redis_cluster/7003/redis.conf
redis-server redis_cluster/7004/redis.conf
redis-server redis_cluster/7005/redis.conf
6。檢查 redis 啟動情況
ps -ef | grep redis
root 2138 1 0 13:57 ? 00:00:01 redis-server *:7003 [cluster]
root 2142 1 0 13:57 ? 00:00:01 redis-server *:7004 [cluster]
root 2146 1 0 13:57 ? 00:00:01 redis-server *:7005 [cluster]
root 2185 2119 0 14:20 pts/0 00:00:00 grep --color=auto redis
netstat -tnlp | grep redis
tcp 0 0 0.0.0.0:17003 0.0.0.0:* LISTEN 2138/redis-server *
tcp 0 0 0.0.0.0:17004 0.0.0.0:* LISTEN 2142/redis-server *
tcp 0 0 0.0.0.0:17005 0.0.0.0:* LISTEN 2146/redis-server *
tcp 0 0 0.0.0.0:7003 0.0.0.0:* LISTEN 2138/redis-server *
tcp 0 0 0.0.0.0:7004 0.0.0.0:* LISTEN 2142/redis-server *
tcp 0 0 0.0.0.0:7005 0.0.0.0:* LISTEN 2146/redis-server *
tcp6 0 0 :::17003 :::* LISTEN 2138/redis-server *
tcp6 0 0 :::17004 :::* LISTEN 2142/redis-server *
tcp6 0 0 :::17005 :::* LISTEN 2146/redis-server *
tcp6 0 0 :::7003 :::* LISTEN 2138/redis-server *
tcp6 0 0 :::7004 :::* LISTEN 2142/redis-server *
tcp6 0 0 :::7005 :::* LISTEN 2146/redis-server *
7。建立叢集
Redis 官方提供了 redis-trib.rb 這個工具,就在解壓目錄的 src 目錄中,第三步中已將它複製到 /usr/local/bin 目錄中,可以直接在命令列中使用了。使用下面這個命令即可完成安裝
redis-trib.rb create --replicas 1 116.196.65.198:7000 116.196.65.198:7001 116.196.65.198:7002 47.52.41.245:7003 47.52.41.245:7004 47.52.41.245:7005
其中,前三個 ip:port 為第一臺機器的節點,剩下三個為第二臺機器。
這個工具是用 ruby 實現的,所以需要安裝 ruby。安裝命令如下:
yum -y install ruby ruby-devel rubygems rpm-build
gem install redis
或者按上面的流程安裝ruby
之後再執行 redis-trib.rb 命令,會出現如下提示:
redis-trib.rb create --replicas 1 116.196.65.198:7000 116.196.65.198:7001 116.196.65.198:7002 47.52.41.245:7003 47.52.41.245:7004 47.52.41.245:7005
###出現下面這些代表成功
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
116.196.65.198:7000
47.52.41.245:7003
116.196.65.198:7001
Adding replica 47.52.41.245:7004 to 116.196.65.198:7000
Adding replica 116.196.65.198:7002 to 47.52.41.245:7003
Adding replica 47.52.41.245:7005 to 116.196.65.198:7001
M: 33d50636b5235de9970de2da75901b642d2b14d2 116.196.65.198:7000
slots:0-5460 (5461 slots) master
M: 668397aba571ece85532b1eb1fccb42e4e33b1f2 116.196.65.198:7001
slots:10923-16383 (5461 slots) master
S: 908430b2bf63669898e9eaef79dd6c1b33c8c57a 116.196.65.198:7002
replicates cc86a24f3896ad7530e2687cf52582912f74b661
M: cc86a24f3896ad7530e2687cf52582912f74b661 47.52.41.245:7003
slots:5461-10922 (5462 slots) master
S: 3a0b27e2e34e72430ce41bb78f6d207579175306 47.52.41.245:7004
replicates 33d50636b5235de9970de2da75901b642d2b14d2
S: ca60de73e15c19a9aa45da508d270a5781a76724 47.52.41.245:7005
replicates 668397aba571ece85532b1eb1fccb42e4e33b1f2
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 116.196.65.198:7000)
M: 33d50636b5235de9970de2da75901b642d2b14d2 116.196.65.198:7000
slots:0-5460 (5461 slots) master
M: 668397aba571ece85532b1eb1fccb42e4e33b1f2 116.196.65.198:7001
slots:10923-16383 (5461 slots) master
M: 908430b2bf63669898e9eaef79dd6c1b33c8c57a 116.196.65.198:7002
slots: (0 slots) master
replicates cc86a24f3896ad7530e2687cf52582912f74b661
M: cc86a24f3896ad7530e2687cf52582912f74b661 47.52.41.245:7003
slots:5461-10922 (5462 slots) master
M: 3a0b27e2e34e72430ce41bb78f6d207579175306 47.52.41.245:7004
slots: (0 slots) master
replicates 33d50636b5235de9970de2da75901b642d2b14d2
M: ca60de73e15c19a9aa45da508d270a5781a76724 47.52.41.245:7005
slots: (0 slots) master
replicates 668397aba571ece85532b1eb1fccb42e4e33b1f2
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
8。叢集驗證
在第一臺機器上連線叢集的7002埠的節點,在另外一臺連線7005節點,連線方式為 redis-cli -h 116.196.65.198 -c -p 7002 ,加引數 -C 可連線到叢集,因為上面 redis.conf 將 bind 改為了ip地址,所以 -h 引數不可以省略。
####連結116.196.65.198:7002存入 鍵jiqun 值chenggong
[[email protected] redis-3.2.1]# redis-cli -h 116.196.65.198 -c -p 7002
116.196.65.198:7002> set jiqun chenggong
-> Redirected to slot [8235] located at 47.52.41.245:7003
OK
47.52.41.245:7003>
####連結47.52.41.245:700取出 鍵jiqun 的值
[[email protected] redis-3.2.1]# redis-cli -h 47.52.41.245 -c -p 7005
47.52.41.245:7005> get jiqun
-> Redirected to slot [8235] located at 47.52.41.245:7003
"chenggong"
47.52.41.245:7003>
9。可能出現問題
- Sorry, can’t connect to node
解決方案:
首先檢查伺服器埠是否開發,阿里,京東等伺服器需要在安全組裡面開發
埠,直接在伺服器裡面用命令開發埠是無效的。其次在redis.config配置檔案裡面關閉保護模式和註釋掉bin
- [ERR] Node 172.168.63.202:7001 is not empty. Either the nodealready knows other nodes (check with CLUSTER NODES) or contains some
解決方案:
1)、將需要新增的節點下aof、rdb等本地備份檔案刪除;
2)、同時將新Node的叢集配置檔案刪除,即:刪除你redis.conf裡面cluster-config-file所在的檔案;
3)、再次新增新節點如果還是報錯,則登入新Node,./redis-cli –h x –p對資料庫進行清除:
116.196.65.198:7000> flushdb #清空當前資料庫
4),重啟伺服器,或者重啟redis
- Waiting for the cluster to join 一直等待
解決方案:
1、伺服器的防火牆是否允許叢集匯流排埠通過
redis叢集不僅需要開通redis客戶端連線的埠,而且需要開通叢集匯流排埠,叢集匯流排埠為redis客戶端連線的埠 + 10000
如上面用到redis埠為6379,7000,7001,7002,7003,7004,7005,則叢集匯流排埠為16379,17000,17001,17002,17003,17004,17005,故,所有伺服器的點需要開通redis的客戶端連線埠和叢集匯流排埠。如果伺服器有安全組安全組也要開通,沒有隻要開通伺服器本身的
##centos7.3伺服器
systemctl status firewalld.service #防火牆狀態
systemctl start firewalld.service #防火牆開啟
systemctl disable firewalld.service #防火牆不可用
firewall-cmd --zone=public --add-port=80/tcp --permanent #開啟埠
命令含義:
--zone #作用域
--add-port=80/tcp #新增埠,格式為:埠/通訊協議
--permanent #永久生效,沒有此引數重啟後失效
重啟防火牆
firewall-cmd --reload