设为首页 收藏本站
查看: 531|回复: 0

[经验分享] redis集群

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-5-10 09:04:35 | 显示全部楼层 |阅读模式
一、环境
系统    CentOS7.0 64位最小化安装
redis1    172.16.1.46    6379,6380
redis2    172.16.1.47    6379,6380
redis3    172.16.1.47    6379,6380
二、基础软件安装
1
2
[iyunv@redis1 ~]# yum install vim wget tree ntp net-tools lsof gcc* -y
[iyunv@redis1 ~]# yum -y install gcc openssl-devel libyaml-devel libffi-devel readline-devel zlib-devel gdbm-devel ncurses-devel gcc-c++ automake autoconf libxml2* rubygem-nokogiri



三、安装ruby
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[iyunv@redis1 ~]# yum install ruby rubygems ruby-devel -y

#删除ruby的默认源
[iyunv@redis1 ~]# gem source --remove https://rubygems.org/
https://rubygems.org/ removed from sources

#添加源
[iyunv@redis1 ~]# gem sources -a https://ruby.taobao.org
https://ruby.taobao.org added to sources

#安装rails
[iyunv@redis1 ~]# gem install rails

#安装redis和ruby的接口
[iyunv@redis1 ~]# gem install redis



四、安装redis
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#下载redis
[iyunv@redis1 ~]# wget http://download.redis.io/releases/redis-3.0.1.tar.gz
[iyunv@redis1 ~]# tar xf redis-3.0.1.tar.gz -C /usr/local/
[iyunv@redis1 ~]# ln -sv /usr/local/redis-3.0.1 /usr/local/redis
‘/usr/local/redis’ -> ‘/usr/local/redis-3.0.1’
[iyunv@redis1 ~]# cd /usr/local/redis
[iyunv@redis1 redis]# make
[iyunv@redis1 redis]# make install
[iyunv@redis1 ~]# cp /usr/local/redis/redis.conf /usr/local/redis6379.conf

#编辑6379的配置文件
[iyunv@redis1 ~]# vim /usr/local/redis/redis6379.conf
port 6379
pidfile /var/run/redis6379.pid
dbfilename dump6379.rdb
appendfilename "appendonly-6379.aof"
cluster-config-file nodes-6379.conf
cluster-node-timeout 15000
cluster-enabled yes
appendonly yes
dir /usr/local/redis/data/6379

#编辑6380的配置文件
[iyunv@redis1 ~]# cp /usr/local/redis/redis6379.conf /usr/local/redis/redis6380.conf
[iyunv@redis1 ~]# sed -i 's#6379#6380#g' /usr/local/redis/redis6380.conf

#创建相应的目录
[iyunv@redis1 ~]# mkdir -p /usr/local/redis/data/{6379,6380}

#创建6379的启动脚本
[iyunv@redis1 ~]# cat /etc/init.d/redis6379
#!/bin/bash
# chkconfig: 2345 50 30
#
# description: Redis service
#
#Script:Redis command
   
Redisserver=/usr/local/bin/redis-server
Rediscli=/usr/local/bin/redis-cli
Redisconf=/usr/local/redis/redis6379.conf
   
function_start()
{
    printf "start redis-server..."
    $Redisserver $Redisconf &>/dev/null  &
    if [ $? -eq 0 ];then
        echo "runing"
    fi
}
   
function_stop()
{
    printf "stop redis-server..."
    $Rediscli -p 6379 shutdown
    if [ $? -eq 0 ];then
        echo "stop"
    fi
}
   
function_restart()
{
    function_start
    function_stop
}
   
function_kill()
{
    killall redis-server
}
   
function_status()
{
    a=`ps -A|grep "redis-server\>" -c`
    if [ $a -ge 1 ];then
        echo -e "The Redis is [\e[0;32;5m runing \e[0m]"
    else
        echo -e "The Redis is [\e[0;31;5m not run \e[0m]"
    fi
}
   
case "$1" in
        start)
                function_start
                ;;
        stop)
                function_stop
                ;;
        restart)
                function_stop
                function_start
                ;;
        kill)
                function_kill
                ;;
        status)
                function_status
                ;;
              *)
              echo "Usage: /etc/init.d/redis {start|stop|restart|kill|status}"
               
esac
   
exit

#赋予执行权限
[iyunv@redis1 ~]# chmod +x /etc/init.d/redis6379
[iyunv@redis1 ~]# /etc/init.d/redis6379 start
start redis-server...runing
[iyunv@redis1 ~]# netstat -tunlp |grep redis
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      5631/redis-server *

#创建6380的启动脚本
[iyunv@redis1 ~]# cat /etc/init.d/redis6380
#!/bin/bash
# chkconfig: 2345 50 30
#
# description: Redis service
#
#Script:Redis command
   
Redisserver=/usr/local/bin/redis-server
Rediscli=/usr/local/bin/redis-cli
Redisconf=/usr/local/redis/redis6380.conf
   
function_start()
{
    printf "start redis-server..."
    $Redisserver $Redisconf &>/dev/null  &
    if [ $? -eq 0 ];then
        echo "runing"
    fi
}
   
function_stop()
{
    printf "stop redis-server..."
    $Rediscli -p 6380 shutdown
    if [ $? -eq 0 ];then
        echo "stop"
    fi
}
   
function_restart()
{
    function_start
    function_stop
}
   
function_kill()
{
    killall redis-server
}
   
function_status()
{
    a=`ps -A|grep "redis-server\>" -c`
    if [ $a -ge 1 ];then
        echo -e "The Redis is [\e[0;32;5m runing \e[0m]"
    else
        echo -e "The Redis is [\e[0;31;5m not run \e[0m]"
    fi
}
   
case "$1" in
        start)
                function_start
                ;;
        stop)
                function_stop
                ;;
        restart)
                function_stop
                function_start
                ;;
        kill)
                function_kill
                ;;
        status)
                function_status
                ;;
              *)
              echo "Usage: /etc/init.d/redis {start|stop|restart|kill|status}"
               
esac
   
exit

#赋予执行权限并启动6380
[iyunv@redis1 ~]# chmod +x /etc/init.d/redis6380
[iyunv@redis1 ~]# /etc/init.d/redis6380 start
start redis-server...runing
[iyunv@redis1 ~]# netstat -tunlp |grep redis
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      5646/redis-server *
tcp        0      0 0.0.0.0:6380            0.0.0.0:*               LISTEN      5652/redis-server *

#其余2台执行同样的操作



五、配置集群
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#在任意一台机器上操作
[iyunv@redis1 ~]# /usr/local/redis/src/redis-trib.rb create --replicas 1 172.16.1.46:6379 172.16.1.46:6380 172.16.1.47:6379 172.16.1.47:6380 172.16.1.48:6379 172.16.1.48:6380
>>> Creating cluster
Connecting to node 172.16.1.46:6379: OK
Connecting to node 172.16.1.46:6380: OK
Connecting to node 172.16.1.47:6379: OK
Connecting to node 172.16.1.47:6380: OK
Connecting to node 172.16.1.48:6379: OK
Connecting to node 172.16.1.48:6380: OK
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
172.16.1.46:6379
172.16.1.47:6379
172.16.1.48:6379
Adding replica 172.16.1.47:6380 to 172.16.1.46:6379
Adding replica 172.16.1.46:6380 to 172.16.1.47:6379
Adding replica 172.16.1.48:6380 to 172.16.1.48:6379
M: 3c2279ff6333b98a233ebf541e2b75981d27751f 172.16.1.46:6379
   slots:0-5460 (5461 slots) master
S: eb0b79b22797c14172c8f0f117a4839d6825ea1f 172.16.1.46:6380
   replicates dd122d63def65b8bc62719cabbb468f7976b1b73
M: dd122d63def65b8bc62719cabbb468f7976b1b73 172.16.1.47:6379
   slots:5461-10922 (5462 slots) master
S: c8136c436cec8146dbbbd1ff2e5468d1dd5dd195 172.16.1.47:6380
   replicates 3c2279ff6333b98a233ebf541e2b75981d27751f
M: a0cf20f0ea29760dfdb3e4748417efaefc0c37f0 172.16.1.48:6379
   slots:10923-16383 (5461 slots) master
S: c6df03cde4424b2b09752c89d236ee1cd4834709 172.16.1.48:6380
   replicates a0cf20f0ea29760dfdb3e4748417efaefc0c37f0
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 172.16.1.46:6379)
M: 3c2279ff6333b98a233ebf541e2b75981d27751f 172.16.1.46:6379
   slots:0-5460 (5461 slots) master
M: eb0b79b22797c14172c8f0f117a4839d6825ea1f 172.16.1.46:6380
   slots: (0 slots) master
   replicates dd122d63def65b8bc62719cabbb468f7976b1b73
M: dd122d63def65b8bc62719cabbb468f7976b1b73 172.16.1.47:6379
   slots:5461-10922 (5462 slots) master
M: c8136c436cec8146dbbbd1ff2e5468d1dd5dd195 172.16.1.47:6380
   slots: (0 slots) master
   replicates 3c2279ff6333b98a233ebf541e2b75981d27751f
M: a0cf20f0ea29760dfdb3e4748417efaefc0c37f0 172.16.1.48:6379
   slots:10923-16383 (5461 slots) master
M: c6df03cde4424b2b09752c89d236ee1cd4834709 172.16.1.48:6380
   slots: (0 slots) master
   replicates a0cf20f0ea29760dfdb3e4748417efaefc0c37f0
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

#检查集群状态
[iyunv@redis1 ~]# /usr/local/redis/src/redis-trib.rb check 127.0.0.1:6379
Connecting to node 127.0.0.1:6379: OK
Connecting to node 172.16.1.48:6379: OK
Connecting to node 172.16.1.48:6380: OK
Connecting to node 172.16.1.47:6379: OK
Connecting to node 172.16.1.47:6380: OK
Connecting to node 172.16.1.46:6380: OK
>>> Performing Cluster Check (using node 127.0.0.1:6379)
M: 3c2279ff6333b98a233ebf541e2b75981d27751f 127.0.0.1:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: a0cf20f0ea29760dfdb3e4748417efaefc0c37f0 172.16.1.48:6379
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: c6df03cde4424b2b09752c89d236ee1cd4834709 172.16.1.48:6380
   slots: (0 slots) slave
   replicates a0cf20f0ea29760dfdb3e4748417efaefc0c37f0   
M: dd122d63def65b8bc62719cabbb468f7976b1b73 172.16.1.47:6379
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: c8136c436cec8146dbbbd1ff2e5468d1dd5dd195 172.16.1.47:6380
   slots: (0 slots) slave
   replicates 3c2279ff6333b98a233ebf541e2b75981d27751f
S: eb0b79b22797c14172c8f0f117a4839d6825ea1f 172.16.1.46:6380
   slots: (0 slots) slave
   replicates dd122d63def65b8bc62719cabbb468f7976b1b73
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

#测试集群,添加一个值
[iyunv@redis1 ~]# /usr/local/bin/redis-cli -c -p 6379
127.0.0.1:6379> set a a
-> Redirected to slot [15495] located at 172.16.1.48:6379        #可以看到值添加到了172.16.1.48:6379这个节点上
OK
172.16.1.48:6379> get a
"a"

#关闭172.16.1.48:6379这个节点
[iyunv@redis3 redis]# /etc/init.d/redis6379 stop
stop redis-server...stop
[iyunv@redis3 redis]# netstat -tunlp |grep 6379

#再次连接到集群上
[iyunv@redis1 ~]# /usr/local/bin/redis-cli -c -p 6379
127.0.0.1:6379> get a
-> Redirected to slot [15495] located at 172.16.1.48:6380
"a"            #这里能看到之前设置的值,从172.16.1.48:6380拿到结果了

#集群配置到此结束



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-214948-1-1.html 上篇帖子: redis底层数据结构之adlist 下篇帖子: Redis键值相关命令
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表