試驗機(jī)操作系統(tǒng):CentOS Linux release 7.5.1804 (Core)
下載Redis
下載地址:https://redis.io/download
下載版本:redis-4.0.1.tar.gz
[root@Centos7-1 ~]# wget http://download.redis.io/releases/redis-4.0.1.tar.gz
安裝gcc與psmisc
yum install gcc psmisc
解壓tar包
[root@Centos7-1 ~]# tar zxvf redis-4.0.1.tar.gz
[root@Centos7-1 ~]# mv redis-4.0.1 /usr/local/rediscd /soft
編譯安裝
[root@Centos7-1 ~]#cd /usr/local/redis
[root@Centos7-1 redis]#make MALLOC=libc
[root@Centos7-1 redis]#make & make install
說明:如果在make的時候不指定內(nèi)存管理方式,會報錯的:
zmalloc.h:50:31: 致命錯誤:jemalloc/jemalloc.h:沒有那個文件或目錄。
malloc是管理內(nèi)存碎片的。
使用systemctl管理服務(wù)
[root@Centos7-1 redis]# vim /lib/systemd/system/redis.service
[Unit] Description=Redis After=network.target [Service] ExecStart=/usr/local/bin/redis-server /usr/local/redis/redis.conf --daemonize no ExecStop=/usr/local/bin/redis-cli -h 127.0.0.1 -p 6379 shutdown [Install] WantedBy=multi-user.target
* [Unit] 表示這是基礎(chǔ)信息
* Description 是描述
* After 是在那個服務(wù)后面啟動,一般是網(wǎng)絡(luò)服務(wù)啟動后啟動
* [Service] 表示這里是服務(wù)信息
* ExecStart 是啟動服務(wù)的命令
* ExecStop 是停止服務(wù)的指令
* [Install] 表示這是是安裝相關(guān)信息
* WantedBy 是以哪種方式啟動:multi-user.target表明當(dāng)系統(tǒng)以多用戶方式(默認(rèn)的運(yùn)行級別)啟動時,這個服務(wù)需要被自動運(yùn)行。
刷新systemctl配置
[root@Centos7-1 redis]#systemctl daemon-reload
啟動reids
[root@Centos7-1 redis]#systemctl start redis; systemctl enable redis
[root@Centos7-1 redis]#systemctl status redis
===========================================
Redis集群安裝配置
redis集群最小是3主3從。
192.168.1.196 CentOS Linux release 7.5.1804 (Core)
192.168.1.127 CentOS Linux release 7.5.1804 (Core)
192.168.1.210 CentOS Linux release 7.5.1804 (Core)
192.168.1.46 CentOS Linux release 7.5.1804 (Core)
192.168.1.172 CentOS Linux release 7.5.1804 (Core)
192.168.1.173 CentOS Linux release 7.5.1804 (Core)
在一臺機(jī)器上裝好redis,然后拷貝到其他服務(wù)器上去。
安裝ruby
創(chuàng)建redis集群需要用Ruby運(yùn)行redis-trib.rb
[root@Centos7-1 redis]# yum install centos-release-scl-rh -y
[root@Centos7-1 redis]# yum install rh-ruby23 rh-ruby23-ruby-devel rubygems rpm-build -y
[root@Centos7-1 redis]# scl enable rh-ruby23 bash
1.移除現(xiàn)有的鏡像
[root@Centos7-1 redis]# gem sources --remove https://rubygems.org/
2.使用淘寶鏡像
[root@Centos7-1 redis]# gem sources -a https://ruby.taobao.org/
3.驗證當(dāng)前ruby版本
[root@Centos7-1 redis]# gem sources -l
[root@Centos7-1 redis]# gem install redis
修改redis.conf
打開redis.conf,把下面列出來的配置項全部注釋,完了拷貝下面7條配置到redis.conf文件末尾
port 6379
bind 192.168.1.196
daemonize yes
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 15000
pidfile /var/run/redis_6379.pid
把每臺機(jī)器上的redis.conf中的bind修改成對于的IP
啟動redis
每臺都要啟動
[root@Centos7-1 redis]# systemctl restart redis
啟動集群
[root@Centos7-1 redis]# /usr/local/redis/src/redis-trib.rb create --replicas 1 192.168.1.196:6379 192.168.1.127:6379 192.168.1.210:6379 192.168.1.46:6379 192.168.1.172:6379 192.168.1.173:6379
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.1.196:6379
192.168.1.127:6379
192.168.1.210:6379
Adding replica 192.168.1.172:6379 to 192.168.1.196:6379
Adding replica 192.168.1.173:6379 to 192.168.1.127:6379
Adding replica 192.168.1.46:6379 to 192.168.1.210:6379
M: 8f665799d22539e4fb817c2bbbd2762fba8a452d 192.168.1.196:6379
slots:0-5460 (5461 slots) master
M: 124c4d42b375bc0eb9341ec350c85eb178037a5c 192.168.1.127:6379
slots:5461-10922 (5462 slots) master
M: 17a0cfe1dddcbaff42d678dbd887409ad8059011 192.168.1.210:6379
slots:10923-16383 (5461 slots) master
S: 9d0a59860f1aa7affceaa1a2e7f22c3bd16ecf87 192.168.1.46:6379
replicates 17a0cfe1dddcbaff42d678dbd887409ad8059011
S: 2fb0663db810e5eef383ceb75022c69a10a73cfa 192.168.1.172:6379
replicates 8f665799d22539e4fb817c2bbbd2762fba8a452d
S: fd072f90fd19fbe56e133dd3893d7854f6749358 192.168.1.173:6379
replicates 124c4d42b375bc0eb9341ec350c85eb178037a5c
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 192.168.1.196:6379)
M: 8f665799d22539e4fb817c2bbbd2762fba8a452d 192.168.1.196:6379
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 2fb0663db810e5eef383ceb75022c69a10a73cfa 192.168.1.172:6379
slots: (0 slots) slave
replicates 8f665799d22539e4fb817c2bbbd2762fba8a452d
M: 124c4d42b375bc0eb9341ec350c85eb178037a5c 192.168.1.127:6379
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: fd072f90fd19fbe56e133dd3893d7854f6749358 192.168.1.173:6379
slots: (0 slots) slave
replicates 124c4d42b375bc0eb9341ec350c85eb178037a5c
S: 9d0a59860f1aa7affceaa1a2e7f22c3bd16ecf87 192.168.1.46:6379
slots: (0 slots) slave
replicates 17a0cfe1dddcbaff42d678dbd887409ad8059011
M: 17a0cfe1dddcbaff42d678dbd887409ad8059011 192.168.1.210:6379
slots:10923-16383 (5461 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
連接redis集群
[root@localhost redis]# src/redis-cli -h 192.168.1.196 -c -p 6379
必須要加:-c參數(shù)
在192.168.1.196節(jié)點(diǎn)上寫一條數(shù)據(jù):
[root@Centos7-1 redis]# redis-cli -h 192.168.1.196 -c -p 6379
192.168.1.196:6379> set key test
-> Redirected to slot [12539] located at 192.168.1.210:6379
OK
192.168.1.210:6379> get key
"test"
192.168.1.210:6379> set key2 test2
-> Redirected to slot [4998] located at 192.168.1.196:6379
OK
192.168.1.196:6379> get key2
"test2"
可以看到插入鍵值在196機(jī)器上,獲取鍵值的時候,自動跳到其他機(jī)器上
獲取集群信息
192.168.1.210:6379> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:3
cluster_stats_messages_ping_sent:1624
cluster_stats_messages_pong_sent:1665
cluster_stats_messages_meet_sent:3
cluster_stats_messages_sent:3292
cluster_stats_messages_ping_received:1662
cluster_stats_messages_pong_received:1627
cluster_stats_messages_meet_received:3
cluster_stats_messages_received:3292
查看集群節(jié)點(diǎn)
192.168.1.210:6379> cluster nodes
124c4d42b375bc0eb9341ec350c85eb178037a5c 192.168.1.127:6379@16379 master - 0 1530001192724 2 connected 5461-10922
9d0a59860f1aa7affceaa1a2e7f22c3bd16ecf87 192.168.1.46:6379@16379 slave 17a0cfe1dddcbaff42d678dbd887409ad8059011 0 1530001190720 4 connected
8f665799d22539e4fb817c2bbbd2762fba8a452d 192.168.1.196:6379@16379 master - 0 1530001195729 1 connected 0-5460
17a0cfe1dddcbaff42d678dbd887409ad8059011 192.168.1.210:6379@16379 myself,master - 0 1530001190000 3 connected 10923-16383
2fb0663db810e5eef383ceb75022c69a10a73cfa 192.168.1.172:6379@16379 slave 8f665799d22539e4fb817c2bbbd2762fba8a452d 0 1530001196732 5 connected
fd072f90fd19fbe56e133dd3893d7854f6749358 192.168.1.173:6379@16379 slave 124c4d42b375bc0eb9341ec350c85eb178037a5c 0 1530001194727 6 connected
如果重啟服務(wù)器后,再次執(zhí)行redis-trib.rb會報錯如下錯誤:
[ERR] Node 192.168.1.80:6379 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
解決方法:
刪除dump.rdb和nodes-6379.conf這兩個文件,然后重啟redis服務(wù),最后啟動集群。