blog/linux/centos安装集群redis

centos安装集群redis

下载redis

wget http://tar.matosiki.site/tar/redis-4.0.9.tar.gz

tar -zxvf redis-4.0.9.tar.gz

mv redis-4.0.9 /opt/

cd /opt/redis-4.0.9/

mkdir -p /opt/redis-4.0.9/redis_cluster

cp redis.conf redis_cluster

# 编辑redis配置文件
vim redis_cluster/redis.conf
bind 0.0.0.0  //默认ip为127.0.0.1  也可以改为局域网 192.168.10.220
daemonize yes //redis后台运行
pidfile /var/run/redis_6379.pid //pidfile文件对应7000,7001,7002
cluster-enabled yes //开启集群 把注释#去掉
cluster-config-file nodes_6379.conf //集群的配置
cluster-node-timeout 15000 //请求超时 默认15秒,可自行设置
appendonly yes //aof日志开启 有需要就开启,它会每次写操作都记录一条日志

添加6个节点分别更改配置文件

cp redis_cluster/redis.conf  redis_cluster/redis-7000.conf

cp redis_cluster/redis.conf redis_cluster/redis-7001.conf

cp redis_cluster/redis.conf redis_cluster/redis-7002.conf

cp redis_cluster/redis.conf redis_cluster/redis-7003.conf

cp redis_cluster/redis.conf redis_cluster/redis-7004.conf

cp redis_cluster/redis.conf redis_cluster/redis-7005.conf

vim redis_cluster/redis-7000.conf
:%s/6379/7000/g

vim redis_cluster/redis-7001.conf
:%s/6379/7001/g

vim redis_cluster/redis-7002.conf
:%s/6379/7002/g

vim redis_cluster/redis-7003.conf
:%s/6379/7003/g

vim redis_cluster/redis-7004.conf
:%s/6379/7004/g

vim redis_cluster/redis-7005.conf
:%s/6379/7005/g

# 启动所有节点
src/redis-server redis_cluster/redis.conf
src/redis-server redis_cluster/redis-7000.conf
src/redis-server redis_cluster/redis-7001.conf
src/redis-server redis_cluster/redis-7002.conf
src/redis-server redis_cluster/redis-7003.conf
src/redis-server redis_cluster/redis-7004.conf
src/redis-server redis_cluster/redis-7005.conf

安装ruby并升级ruby-redis


yum -y install ruby ruby-devel rubygems rpm-build

gpg2 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3

curl -L get.rvm.io | bash -s stable

source /usr/local/rvm/scripts/rvm

rvm list known

# 升级ruby

#安装ruby
rvm install 2.4.0
#使用新版本
rvm use 2.4.0
#移除旧版本
rvm remove 2.0.0
#查看当前版本
ruby --version

gem install redis

新建分片
参数create表示创建一个新的集群,–replicas 1表示为每个master创建一个slave

# 单机请使用
./src/redis-trib.rb create --replicas 1 0.0.0.0:7000 0.0.0.0:7001 \
0.0.0.0:7002 0.0.0.0:7003 0.0.0.0:7004 0.0.0.0:7005
# master-slave请使用 先用telnet测试端口是否可以访问
./src/redis-trib.rb create --replicas 1 192.168.10.220:6379 192.168.10.221:6379 192.168.10.220:7000 192.168.10.221:7000 192.168.10.220:7001 192.168.10.221:7001 192.168.10.220:7002 192.168.10.221:7002 192.168.10.220:7003 192.168.10.221:7003 192.168.10.220:7004 192.168.10.221:7004 192.168.10.220:7005 192.168.10.221:7005

完成测试

$ redis-cli -c -p 7000

redis 127.0.0.1:7000> set foo bar
-> Redirected to slot [12182] located at 127.0.0.1:7002
OK
redis 127.0.0.1:7002> set hello world
-> Redirected to slot [866] located at 127.0.0.1:7000
OK
redis 127.0.0.1:7000> get foo
-> Redirected to slot [12182] located at 127.0.0.1:7002
"bar"
redis 127.0.0.1:7000> get hello
-> Redirected to slot [866] located at 127.0.0.1:7000
"world"

查看集群状态

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_ping_sent:48273
cluster_stats_messages_pong_sent:49884
cluster_stats_messages_sent:98157
cluster_stats_messages_ping_received:49879
cluster_stats_messages_pong_received:48273
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:98157

使用cluster nodes命令查看节点状态。

127.0.0.1:7000> cluster nodes

58d1fe2d2f06144def4a7de79f6851e0c68d316b 127.0.0.1:[email protected] slave 45fd67328af03fe008dd3605baaeb347373dd9c8 0 1539090532575 6 connected
45fd67328af03fe008dd3605baaeb347373dd9c8 127.0.0.1:[email protected] myself,master - 0 1539090533000 1 connected 0-5460
4387192aad51ddf6df1b5c9716088d2e96a55149 127.0.0.1:[email protected] master - 0 1539090534579 2 connected 5461-10922
b9afd1c136a5b379c695b8c49b3119f6daa25aa0 127.0.0.1:[email protected] slave 7990a7b654836850ff207146a790f9fec2f0199d 0 1539090535580 5 connected
c00d297dc429033ea2396a176d610d6acabe5b62 127.0.0.1:[email protected] slave 4387192aad51ddf6df1b5c9716088d2e96a55149 0 1539090534000 4 connected
7990a7b654836850ff207146a790f9fec2f0199d 127.0.0.1:[email protected] master - 0 1539090534000 3 connected 10923-16383

重新安装集群

./src/redis-cli -p 3679 shutdown
./src/redis-cli -p 7000 shutdown
./src/redis-cli -p 7001 shutdown
./src/redis-cli -p 7002 shutdown
./src/redis-cli -p 7003 shutdown
./src/redis-cli -p 7004 shutdown
./src/redis-cli -p 7005 shutdown

rm -f dump.rdb
rm -f appendonly.aof
rm -f nodes-*.conf

###

# 查看集群节点状态
./src/redis-cli -p 7000 cluster nodes

给redis添加密码

vi   redis_cluster/redis.conf
vi redis_cluster/redis-7000.conf
vi redis_cluster/redis-7001.conf
vi redis_cluster/redis-7002.conf
vi redis_cluster/redis-7003.conf
vi redis_cluster/redis-7004.conf
vi redis_cluster/redis-7005.conf
masterauth 123456
requirepass 123456
本文总阅读量