1.环境确认
a.系统版本查看
[hadoop@p168 ~]$ cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
b.安装依赖
[hadoop@p168 ~]$ sudo yum -y install tcl ruby tree
2.安装redis 3.0.6
[hadoop@p168 ~]#cd /usr/local/
[hadoop@p168 ~]#tar xzf redis-stable.tar.gz
[hadoop@p168 ~]#cd redis-stable
[hadoop@p168 ~]#make
[hadoop@p168 ~]#make test
[hadoop@p168 ~]#make install
3.建立节点配置目录
[hadoop@p168 ~]#mkdir /etc/redis
[hadoop@p168 ~]#mkdir /etc/redis/7000
4.建立节点配置文件
a.配置7000节点,先复制redis-server与初始配置文件redis.conf,然后修改redis.conf,使其适合集群
[hadoop@p168 ~]#cp /usr/local/redis-stable/src/redis-server /usr/local/redis-stable/redis.conf /etc/redis/7000
修改redis.conf,主要修改内容如下:
[hadoop@p168 ~]#vim /etc/redis/7000/redis.conf
#/etc/redis/7000/redis.conf
port 7000
daemonize yes
pidfile /var/run/redis_7000.pid
cluster-enabled yes
cluster-config-file nodes.conf
logfile "/var/log/redisd7000.log"
dir /etc/redis/7000/
cluster-node-timeout 5000
appendonly yes
b.复制其他节点配置文件
#生成7001配置,其他类似
cp -r 7000 7001
sed -i "s/7000/7001/g" 7001/redis.conf
cat 7001/redis.conf |grep 7001
5.配置节点开机启动
修改redisd7000的启动脚本,7001~7005类似
[hadoop@p168 ~]#cp /usr/local/redis-stable/utils/redis_init_script /etc/init.d/redisd7000
[hadoop@p168 ~]#vim /etc/init.d/redisd7000
#!/bin/sh
#chkconfig: 2345 90 10
#description: Redis is a persistent key-value database
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
REDISPORT=7000
#EXEC=/usr/local/bin/redis-server
EXEC=/etc/redis/${REDISPORT}/redis-server
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid
#CONF="/etc/redis/${REDISPORT}.conf"
CONF="/etc/redis/${REDISPORT}/redis.conf"
主要修改内容包括如下三点:
a.加入前三行即
#!/bin/sh
#chkconfig: 2345 90 10
#description: Redis is a persistent key-value database
b.修改端口,即
REDISPORT=7000
c.指定EXEC与CONF即
EXEC=/etc/redis/${REDISPORT}/redis-server
CONF="/etc/redis/${REDISPORT}/redis.conf"
其他的不动,略
复制生成其他启动脚本
#生成7001脚本,其他类似
cp /etc/init.d/redisd7000 /etc/init.d/redisd7001
sed -i "s/7000/7001/g" /etc/init.d/redisd7001
cat /etc/init.d/redisd7001 |grep 7001
#设置为开机自启动服务器,其他服务类似
[hadoop@p168 ~]#chkconfig redisd7000 on
确认配置文件,重启前应该是如下状态
redisd7000
redisd7001
redisd7002
redisd7003
redisd7004
redisd7005
/etc/redis
├── 7000
│ ├── redis.conf
│ └── redis-server
├── 7001
│ ├── redis.conf
│ └── redis-server
├── 7002
│ ├── redis.conf
│ └── redis-server
├── 7003
│ ├── redis.conf
│ └── redis-server
├── 7004
│ ├── redis.conf
│ └── redis-server
├── 7005
│ ├── redis.conf
│ └── redis-server
注:以上目录是各服务器的redis-server可执行程序与其配置文件,若没有tree命令,先 yum install -y tree
[hadoop@p168 ~]# reboot
重启以后服务应该就启动了,若有需要可以用如下命令来启动或停止服务了
service redisd7000 start
service redisd7000 stop
6.安装redis接口
#rubygems.org被墙,换ruby.taobao.org
gem install redis
7.建立集群
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.1.161:7000
192.168.1.161:7001
192.168.1.161:7002
Adding replica 192.168.1.161:7003 to 192.168.1.161:7000
Adding replica 192.168.1.161:7004 to 192.168.1.161:7001
Adding replica 192.168.1.161:7005 to 192.168.1.161:7002
M: 5f62052fb63b330ae39e35e35fdb5cd5d3f3d013 192.168.1.161:7000
slots:0-5460 (5461 slots) master
M: 925478713e7a73c986dc0c07c01dd7bd63c20f47 192.168.1.161:7001
slots:5461-10922 (5462 slots) master
M: d81817cb1d01695e552ea2fde2a5ac62772d2a08 192.168.1.161:7002
slots:10923-16383 (5461 slots) master
S: 26906db48fba3321374c9d1fa0f3b55730c49ae1 192.168.1.161:7003
replicates 5f62052fb63b330ae39e35e35fdb5cd5d3f3d013
S: 94077768fe3ff601722f2970c65b1604c6e714df 192.168.1.161:7004
replicates 925478713e7a73c986dc0c07c01dd7bd63c20f47
S: 9259eff3d7d40449830e2de9561e6c77a490f4e0 192.168.1.161:7005
replicates d81817cb1d01695e552ea2fde2a5ac62772d2a08
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.161:7000)
M: 5f62052fb63b330ae39e35e35fdb5cd5d3f3d013 192.168.1.161:7000
slots:0-5460 (5461 slots) master
M: 925478713e7a73c986dc0c07c01dd7bd63c20f47 192.168.1.161:7001
slots:5461-10922 (5462 slots) master
M: d81817cb1d01695e552ea2fde2a5ac62772d2a08 192.168.1.161:7002
slots:10923-16383 (5461 slots) master
M: 26906db48fba3321374c9d1fa0f3b55730c49ae1 192.168.1.161:7003
slots: (0 slots) master
replicates 5f62052fb63b330ae39e35e35fdb5cd5d3f3d013
M: 94077768fe3ff601722f2970c65b1604c6e714df 192.168.1.161:7004
slots: (0 slots) master
replicates 925478713e7a73c986dc0c07c01dd7bd63c20f47
M: 9259eff3d7d40449830e2de9561e6c77a490f4e0 192.168.1.161:7005
slots: (0 slots) master
replicates d81817cb1d01695e552ea2fde2a5ac62772d2a08
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
8.确认集群状态
>>> Performing Cluster Check (using node 192.168.1.161:7000)
M: e3b5e66e28c015552625b7f1b52538da98d16c96 192.168.1.161:7000
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 6dacc2604f22692a9291e3e6e82bee492fbb0817 192.168.1.161:7003
slots: (0 slots) slave
replicates e3b5e66e28c015552625b7f1b52538da98d16c96
M: 5aa111c270091b474feaf0f11aa8a2e3ff2a5ccc 192.168.1.161:7001
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 8252ac10b98a2f8d78c6f1b488610b8f789548db 192.168.1.161:7005
slots: (0 slots) slave
replicates 6aa2e0cb813a6ed451437ef4203aa669d42ac50c
S: 1e179fa5c399dba3eed3e592c5855cf600a648a5 192.168.1.161:7004
slots: (0 slots) slave
replicates 5aa111c270091b474feaf0f11aa8a2e3ff2a5ccc
M: 6aa2e0cb813a6ed451437ef4203aa669d42ac50c 192.168.1.161:7002
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.
有错误的话,修复集群
root 26530 13340 0 12:07 pts/1 00:00:03 ./7000/redis-server *:7000 [cluster]
root 26914 26538 0 12:07 pts/3 00:00:03 ./7001/redis-server *:7001 [cluster]
root 27430 13649 0 12:10 pts/2 00:00:03 ./7002/redis-server *:7002 [cluster]
root 27441 26922 0 12:10 pts/4 00:00:03 ./7003/redis-server *:7003 [cluster]
root 27732 27448 0 12:10 pts/5 00:00:03 ./7004/redis-server *:7004 [cluster]
root 28029 27740 0 12:11 pts/6 00:00:02 ./7005/redis-server *:7005 [cluster]
root 28572 5882 0 12:23 pts/0 00:00:00 redis-cli -c -p 7000
root 30085 28867 0 12:35 pts/7 00:00:00 grep --color=auto redis
tcp 0 0 0.0.0.0:17000 0.0.0.0:* LISTEN 26530/./7000/redis-
tcp 0 0 0.0.0.0:17001 0.0.0.0:* LISTEN 26914/./7001/redis-
tcp 0 0 0.0.0.0:17002 0.0.0.0:* LISTEN 27430/./7002/redis-
tcp 0 0 0.0.0.0:17003 0.0.0.0:* LISTEN 27441/./7003/redis-
tcp 0 0 0.0.0.0:17004 0.0.0.0:* LISTEN 27732/./7004/redis-
tcp 0 0 0.0.0.0:17005 0.0.0.0:* LISTEN 28029/./7005/redis-
tcp 0 0 0.0.0.0:7000 0.0.0.0:* LISTEN 26530/./7000/redis-
tcp 0 0 0.0.0.0:7001 0.0.0.0:* LISTEN 26914/./7001/redis-
tcp 0 0 0.0.0.0:7002 0.0.0.0:* LISTEN 27430/./7002/redis-
tcp 0 0 0.0.0.0:7003 0.0.0.0:* LISTEN 27441/./7003/redis-
tcp 0 0 0.0.0.0:7004 0.0.0.0:* LISTEN 27732/./7004/redis-
tcp 0 0 0.0.0.0:7005 0.0.0.0:* LISTEN 28029/./7005/redis-
tcp6 0 0 :::17000 :::* LISTEN 26530/./7000/redis-
tcp6 0 0 :::17001 :::* LISTEN 26914/./7001/redis-
tcp6 0 0 :::17002 :::* LISTEN 27430/./7002/redis-
tcp6 0 0 :::17003 :::* LISTEN 27441/./7003/redis-
tcp6 0 0 :::17004 :::* LISTEN 27732/./7004/redis-
tcp6 0 0 :::17005 :::* LISTEN 28029/./7005/redis-
tcp6 0 0 :::7000 :::* LISTEN 26530/./7000/redis-
tcp6 0 0 :::7001 :::* LISTEN 26914/./7001/redis-
tcp6 0 0 :::7002 :::* LISTEN 27430/./7002/redis-
tcp6 0 0 :::7003 :::* LISTEN 27441/./7003/redis-
tcp6 0 0 :::7004 :::* LISTEN 27732/./7004/redis-
tcp6 0 0 :::7005 :::* LISTEN 28029/./7005/redis-
9.打开防火墙端口
success
[<a href="mailto:root@p161" target="_blank">root@p161 ~]#firewall-cmd --reload
success
10.访问测试
127.0.0.1:7000> 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:1
cluster_stats_messages_sent:1248
cluster_stats_messages_received:1248
127.0.0.1:7000> cluster nodes
6dacc2604f22692a9291e3e6e82bee492fbb0817 192.168.1.161:7003 slave e3b5e66e28c015552625b7f1b52538da98d16c96 0 1451399924770 4 connected
5aa111c270091b474feaf0f11aa8a2e3ff2a5ccc 192.168.1.161:7001 master - 0 1451399923768 2 connected 5461-10922
8252ac10b98a2f8d78c6f1b488610b8f789548db 192.168.1.161:7005 slave 6aa2e0cb813a6ed451437ef4203aa669d42ac50c 0 1451399922766 6 connected
1e179fa5c399dba3eed3e592c5855cf600a648a5 192.168.1.161:7004 slave 5aa111c270091b474feaf0f11aa8a2e3ff2a5ccc 0 1451399925772 5 connected
e3b5e66e28c015552625b7f1b52538da98d16c96 192.168.1.161:7000 myself,master - 0 0 1 connected 0-5460
6aa2e0cb813a6ed451437ef4203aa669d42ac50c 192.168.1.161:7002 master - 0 1451399926776 3 connected 10923-16383
127.0.0.1:7000>
11.python测试
先安装pip install redis-py-cluster
注意在集群中 pip install redis安装的redis会报错!!!
原文链接:https://www.f2er.com/linux/403233.htmlfrom rediscluster import StrictRedisCluster
startup_nodes = [{"host": "192.168.1.161","port": "7000"}]
rc = StrictRedisCluster(startup_nodes=startup_nodes,decode_responses=True)
rc.set("blog","http://blog.csdn.net/dgatiger")
print rc.get('blog')