您的位置:首页 > 数据库 > Redis

redis安装及cluster集群环境搭建

2017-08-03 12:14 891 查看
===========================redis安装===========================
1.下载redis包redis-3.2.1.tar.gz
2.将redis-3.2.1.tar.gz包拷贝到/usr/local下,并解压
3.修改文件夹名为redis

[root@localhost local]# mv redis-3.2.1 redis
4.切换到redis目录下

[root@localhost local]# cd redis
5.编译安装(指定安装路径)

[root@localhost redis]#make PREFIX=/usr/local/redis install
6.等待安装完毕之后,执行bin文件夹下的redis-server启动文件即可启动redis

[root@localhost redis]# bin/redis-server

61294:C 25 Jul 14:12:14.583 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo

61294:C 25 Jul 14:12:14.583 # Redis version=4.0.0, bits=64, commit=00000000, modified=0, pid=61294, just started

61294:C 25 Jul 14:12:14.583 # Warning: no config file specified, using the default config. In order to specify a config file use bin/redis-server /path/to/redis.conf
_._
_.-``__ ''-._

_.-`` `. `_. ''-._ Redis 4.0.0 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._

( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 61294
`-._ `-._ `-./ _.-' _.-'

|`-._`-._ `-.__.-' _.-'_.-'|

| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'

|`-._`-._ `-.__.-' _.-'_.-'|

| `-._`-._ _.-'_.-' |

`-._ `-._`-.__.-'_.-' _.-'

`-._ `-.__.-' _.-'

`-._ _.-'
`-.__.-'

61294:M 25 Jul 14:12:14.585 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

61294:M 25 Jul 14:12:14.585 # Server initialized

61294:M 25 Jul 14:12:14.585 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1'
for this to take effect.

61294:M 25 Jul 14:12:14.585 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled'
as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.

61294:M 25 Jul 14:12:14.585 * Ready to accept connections

7.修改redis.conf配置文件, daemonize yes 以后端模式启动。
[root@localhost redis]# bin/redis-server redis.conf &

---------------------------------------------分割线-----------------------------------------------------

==========================redis集群============================
集群采用的2台服务器,每台服务器上开启3个redis节点集群。

1.ruby环境
    redis集群管理工具redis-trib.rb依赖ruby环境,首先需要安装ruby环境:
    安装ruby
      yum install ruby
      yum install rubygems

安装ruby和redis的接口程序

gem install redis(内网环境安装不了)

到网上下载redis-3.2.1.gem包,使用gem命令安装

[root@localhost ~]# gem install /home/app/redis-3.2.1.gem
2.集群环境创建
172.19.2.159搭建:

1)在/usr/local下输入mkdir redis_cluster //创建集群目录

2)在 redis_cluster文件夹下创建 6379,6380,6381 三个文件夹

3)将/usr/local/reids下面的redis.conf配置文件分别给6379,6380,6381
三个文件夹拷贝一份

[root@localhost redis_cluster]# cp /usr/local/redis/redis.conf ./6379

4)修改6379,6380,6381
三个文件夹下的配置文件:

daemonize yes //设置redis后台运行

pidfile /var/run/redis_6379.pid
//pidfile文件对应修改为6379,6380,6381

port 6379 //使用端口6379,6380,6381

cluster-enabled yes //开启集群

cluster-config-file nodes_6379.conf
//集群的配置 配置文件首次启动自动生成 6379,6380,6381

cluster-node-timeout 5000 //请求超时 设置5秒够了

bind 172.19.2.159 //修改绑定地址,设置为本机的虚拟IP,集群的时候需要用到

masterauth sound_box123 //设置master密码

requirepass sound_box123 //设置登陆验证密码

5)修改gems客户端密码

vim /usr/lib/ruby/gems/1.8/gems/redis-3.2.1/lib/redis/client.rb

修改 password=“sound_box123”

6)切换到redis安装目录下的bin文件夹,启动所有节点

[root@localhost bin]# ./redis-server /usr/local/redis_cluster/6379/redis.conf

[root@localhost bin]# ./redis-server /usr/local/redis_cluster/6380/redis.conf

[root@localhost bin]# ./redis-server /usr/local/redis_cluster/6381/redis.conf

查看所有节点状态:

[root@localhost src]# ps -ef|grep redis

root 78784 1 0 11:19 ? 00:00:00 ./redis-server 172.19.2.159:6379 [cluster]

root 78788 1 0 11:19 ? 00:00:00 ./redis-server 172.19.2.159:6380 [cluster]

root 78792 1 0 11:19 ? 00:00:00 ./redis-server 172.19.2.159:6381 [cluster]

172.19.2.158搭建:
搭建方法与172.19.2.159一样,只是分配端口的时候使用6382、6383、6384三个端口。

3.redis集群启动:
登陆到172.19.2.159服务器,进入到/usr/local/redis/src目录下,执行命令:
./redis-trib.rb create --replicas 1 172.19.2.159:6379 172.19.2.159:6380 172.19.2.159:6381 172.19.2.158:6382 172.19.2.158:6383 172.19.2.158:6384

解释下, --replicas  1  表示 自动为每一个master节点分配一个slave节点    上面有6个节点,程序会按照一定规则生成 3个master(主)3个slave(从)
    前面已经提醒过的 防火墙一定要开放监听的端口,否则会创建失败。
[root@localhost src]# ./redis-trib.rb create --replicas 1 172.19.2.159:6379 172.19.2.159:6380 172.19.2.159:6381 172.19.2.158:6382 172.19.2.158:6383 172.19.2.158:6384
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
172.19.2.158:6382
172.19.2.159:6379
172.19.2.158:6383
Adding replica 172.19.2.159:6380 to 172.19.2.158:6382
Adding replica 172.19.2.158:6384 to 172.19.2.159:6379
Adding replica 172.19.2.159:6381 to 172.19.2.158:6383
M: 76f3069d3efc460f2fb0b84e3ac06e9ccb79f265 172.19.2.159:6379
slots:5461-10922 (5462 slots) master
S: 1b593d385f04afd0503c43b88740dbfc3669cd03 172.19.2.159:6380
replicates 41f093d1363a9d9b6ffc7ee68abf5a243713d668
S: 73c6fa6ddd9ed9040a9594b58c8e0a9183b12ba3 172.19.2.159:6381
replicates ea4a71b51f893699f92d94a7b438708042070dee
M: 41f093d1363a9d9b6ffc7ee68abf5a243713d668 172.19.2.158:6382
slots:0-5460 (5461 slots) master
M: ea4a71b51f893699f92d94a7b438708042070dee 172.19.2.158:6383
slots:10923-16383 (5461 slots) master
S: 5fafc112f5ef474707151651ec337639cd5c64c9 172.19.2.158:6384
replicates 76f3069d3efc460f2fb0b84e3ac06e9ccb79f265
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.19.2.159:6379)
M: 76f3069d3efc460f2fb0b84e3ac06e9ccb79f265 172.19.2.159:6379
slots:5461-10922 (5462 slots) master
M: 1b593d385f04afd0503c43b88740dbfc3669cd03 172.19.2.159:6380
slots: (0 slots) master
replicates 41f093d1363a9d9b6ffc7ee68abf5a243713d668
M: 73c6fa6ddd9ed9040a9594b58c8e0a9183b12ba3 172.19.2.159:6381
slots: (0 slots) master
replicates ea4a71b51f893699f92d94a7b438708042070dee
M: 41f093d1363a9d9b6ffc7ee68abf5a243713d668 172.19.2.158:6382
slots:0-5460 (5461 slots) master
M: ea4a71b51f893699f92d94a7b438708042070dee 172.19.2.158:6383
slots:10923-16383 (5461 slots) master
M: 5fafc112f5ef474707151651ec337639cd5c64c9 172.19.2.158:6384
slots: (0 slots) master
replicates 76f3069d3efc460f2fb0b84e3ac06e9ccb79f265
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

集群测试:
[root@localhost bin]# ./redis-cli -c -h 172.19.2.158 -p 6382 -a sound_box123
172.19.2.158:6382> set test1 "this is test"
OK
172.19.2.158:6382> exit
[root@localhost bin]# ./redis-cli -c -h 172.19.2.159 -p 6379 -a sound_box123
172.19.2.159:6379> get test1
-> Redirected to slot [4768] located at 172.19.2.158:6382
"this is test"
172.19.2.158:6382>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  redis 集群 redis安装