Linux下安装配置Redis-cluster集群
一、准备工作:
redis-5.0.10.tar.gz安装包
三台服务器:
10.10.175.75
10.10.175.76
10.10.175.77
二、开始搭建:
1、把redis-5.0.10.tar.gz放到/opt/app目录下
2、使用tar -xvf redis-5.0.10.tar.gz解压安装包
3、在当前目录下使用mv redis-5.0.10 /usr/local命令移动解压包到/usr/local目录下(需要管理员权限)
4、使用cd /usr/local切换路径
5、使用mv redis-5.0.10/ redis命令修改文件名称
6、进入到Redis目录cd redis
7、使用make && make install编译并安装
三、搭建集群:
1、进入/usr/local/redis/目录
2、cp redis.conf /etc/redis-cluster/redis-9376.conf
3、cp redis.conf /etc/redis-cluster/redis-8376.conf
4、分别修改redis-9376.conf、redis-8376.conf
#9376配置文件
# 端口
port 9376
# 开启实例的集群模式
cluster-enabled yes
# 保存节点配置文件的路径。节点配置文件无须人为修改, 它由 Redis 集群在启动时创建, 并在有需要时自动进行更新
cluster-config-file nodes.conf
#指定超时时间,不算失败,而是切换
cluster-node-timeout 5000
appendfilename "appendonly_9376.aof"
cluster-config-file nodes_9376.conf
appendonly yes
#后台启动
daemonize yes
#指定日志路径
logfile "/etc/redis-cluster/9376.log"
#关闭保护,不然远程无法访问
protected-mode no
#8376配置文件
# 端口
port 9376
# 开启实例的集群模式
cluster-enabled yes
# 保存节点配置文件的路径。节点配置文件无须人为修改, 它由 Redis 集群在启动时创建, 并在有需要时自动进行更新
cluster-config-file nodes.conf
#指定超时时间,不算失败,而是切换
cluster-node-timeout 5000
appendfilename "appendonly_8376.aof"
cluster-config-file nodes_8376.conf
appendonly yes
#后台启动
daemonize yes
#指定日志路径
logfile "/etc/redis-cluster/8376.log"
5、在/usr/local/redis/src/目录下执行
./redis-server /etc/redis-cluster/redis-9376.conf
./redis-server /etc/redis-cluster/redis-8376.conf
6、验证是否启动
ps -ef | grep redis
7、分别在3台机器上执行步骤1~13,搭建3台服务器
8、启动集群节点
./redis-cli --cluster create 10.10.175.75:9376 10.10.175.75:8376 10.10.175.76:9376 10.10.175.76:8376 10.10.175.77:9376 10.10.175.77:8376 --cluster-replicas 1
9、查看配置文件vim nodes_9376.conf,表示分配了曹
四、测试集群是否成功
[umpay@bjtn-jrxx183-68 redis-5.0.10]$ ./src/redis-cli -c -h 10.10.183.68 -p 8376
10.10.183.68:8376> set testkey test
-> Redirected to slot [4757] located at 10.10.183.68:9376
OK
10.10.183.68:9376> get testkey
"test"
10.10.183.68:9376> exit
[umpay@bjtn-jrxx183-68 redis-5.0.10]$ ./src/redis-cli -c -h 10.10.183.68 -p 9376
10.10.183.68:9376> get testkey
"test"
10.10.183.68:9376> exit
[umpay@bjtn-jrxx183-68 redis-5.0.10]$ ./src/redis-cli -c -h 10.10.183.70 -p 9376
10.10.183.70:9376> get testkey
-> Redirected to slot [4757] located at 10.10.183.68:9376
"test"
10.10.183.68:9376> exit
[umpay@bjtn-jrxx183-68 redis-5.0.10]$ ./src/redis-cli -c -h 10.10.183.70 -p 8376
10.10.183.70:8376> get testkey
-> Redirected to slot [4757] located at 10.10.183.68:9376
"test"
10.10.183.68:9376> exit
[umpay@bjtn-jrxx183-68 redis-5.0.10]$ ./src/redis-cli -c -h 10.10.183.71 -p 8376
10.10.183.71:8376> get testkey
-> Redirected to slot [4757] located at 10.10.183.68:9376
"test"
10.10.183.68:9376> exit
[umpay@bjtn-jrxx183-68 redis-5.0.10]$ ./src/redis-cli -c -h 10.10.183.71 -p 9376
10.10.183.71:9376> get testkey
-> Redirected to slot [4757] located at 10.10.183.68:9376
"test"
10.10.183.68:9376> exit
至此表示Redis集群完全配置完成
五、Redis集群密码设置
1.密码设置
方式一:修改所有Redis集群中的redis.conf文件
masterauth 123456
requirepass 123456
注意:所有节点的密码都必须一致,masterauth也要加的。
说明:这种方式需要重新启动各节点
方式二:进入各个实例通过config set设置
[root@iZj6c7eeosj2t5vjw8rf4xZ redis_cluster]# redis-cli -c -p 7000
127.0.0.1:7000> config set masterauth 1234
OK
127.0.0.1:7000> config set requirepass 1234
OK
127.0.0.1:7000> auth 1234
OK
127.0.0.1:7000> config rewrite
OK
127.0.0.1:7000> exit
[root@iZj6c7eeosj2t5vjw8rf4xZ redis_cluster]# redis-cli -c -p 7000 -a 1234
验证密码是否设置成功
2.重启所有服务节点
3.登录验证
设置Redis认证密码后,客户端登录时需要使用-a参数输入认证密码,不添加该参数虽然也可以登录成功,但是没有任何操作权限。如下:
$ ./redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> keys *
(error) NOAUTH Authentication required.
使用密码认证登录,并验证操作权限:
$ ./redis-cli -h 127.0.0.1 -p 6379 -a myPassword
127.0.0.1:6379> config get requirepass
1) "requirepass"
2) "myPassword"