天天看点

linux (centos) 安装redis,集群+主从模式搭建

--环境

centos7.5

1.官网下载安装包 redis-5.0.7.tar.gz (最新的稳定版本)

2.上传至服务器安装目录/home,解压安装包

cp redis-5.0.7.tar.gz /home/

tar -xvf redis-5.0.7.tar.gz 

3.安装依赖包gcc

yum install gcc 

4.编译安装

cd  redis-5.0.7

make && make install 

5.执行初始化脚本 

cd redis-5.0.7/utils/

./install_server.sh   

6.编辑修改配置文件

vim /etc/redis/6379.conf 

找到第70行的bind  127.0.0.1

改为:bind 0.0.0.0

7.重启redis

/etc/init.d/redis_6379 stop

/etc/init.d/redis_6379 start

--配置集群测试(单机),集群模式至少需要六个redis节点,六个配置文件不能在同一目录

进入redis安装目录

[[email protected] init.d]# cd /home/redis-5.0.7

创建config目录

[[email protected] redis-5.0.7]# mkdir config

[[email protected] redis-5.0.7]# cd config/

在config目录下创建主从节点配置文件,并启动,启动命令需要在子文件夹下。因为nodes.conf文件是生成在当前目录的。

节点1:

mkdir 6380

cd 6380

vi redis-6380.conf

bind 0.0.0.0

port 6380

logfile "6380.log"

dbfilename "dump-6380.rdb"

daemonize yes

rdbcompression yes

cluster-enabled yes 

cluster-config-file nodes.conf

cluster-node-timeout 5000 

appendonly yes

protected-mode no 

pidfile  /var/run/redis_6380.pid

/home/redis-5.0.7/src/redis-server /home/redis-5.0.7/config/6380/redis-6380.conf

节点2:

mkdir 6381

cd 6381

vi redis-6381.conf

bind 0.0.0.0

port 6381

logfile "6381.log"

dbfilename "dump-6381.rdb"

daemonize yes

rdbcompression yes

cluster-enabled yes 

cluster-config-file nodes.conf

cluster-node-timeout 5000 

appendonly yes

protected-mode no 

pidfile  /var/run/redis_6381.pid

/home/redis-5.0.7/src/redis-server /home/redis-5.0.7/config/6381/redis-6381.conf

节点3:

mkdir 6382

cd 6382

vi redis-6382.conf

bind 0.0.0.0

port 6382

logfile "6382.log"

dbfilename "dump-6382.rdb"

daemonize yes

rdbcompression yes

cluster-enabled yes 

cluster-config-file nodes.conf

cluster-node-timeout 5000 

appendonly yes

protected-mode no 

pidfile  /var/run/redis_6382.pid

/home/redis-5.0.7/src/redis-server /home/redis-5.0.7/config/6382/redis-6382.conf

节点4:

mkdir 6383

cd 6383

vi redis-6383.conf

bind 0.0.0.0

port 6383

logfile "6383.log"

dbfilename "dump-6383.rdb"

daemonize yes

rdbcompression yes

cluster-enabled yes 

cluster-config-file nodes.conf

cluster-node-timeout 5000 

appendonly yes

protected-mode no 

pidfile  /var/run/redis_6383.pid

/home/redis-5.0.7/src/redis-server /home/redis-5.0.7/config/6383/redis-6383.conf

节点5:

mkdir 6384

cd 6384

vi redis-6384.conf

bind 0.0.0.0

port 6384

logfile "6384.log"

dbfilename "dump-6384.rdb"

daemonize yes

rdbcompression yes

cluster-enabled yes 

cluster-config-file nodes.conf

cluster-node-timeout 5000 

appendonly yes

protected-mode no 

pidfile  /var/run/redis_6384.pid

/home/redis-5.0.7/src/redis-server /home/redis-5.0.7/config/6384/redis-6384.conf

节点6:

mkdir 6385

cd 6385

vi redis-6385.conf

bind 0.0.0.0

port 6385

logfile "6385.log"

dbfilename "dump-6385.rdb"

daemonize yes

rdbcompression yes

cluster-enabled yes 

cluster-config-file nodes.conf

cluster-node-timeout 5000 

appendonly yes

protected-mode no

pidfile  /var/run/redis_6385.pid

/home/redis-5.0.7/src/redis-server /home/redis-5.0.7/config/6385/redis-6385.conf

[[email protected] 6385]# ps -ef |grep redis

root      42739      1  0 13:12 ?        00:00:00 /home/redis-5.0.7/src/redis-server 0.0.0.0:6381 [cluster]

root      42753      1  0 13:13 ?        00:00:00 /home/redis-5.0.7/src/redis-server 0.0.0.0:6380 [cluster]

root      42761      1  0 13:17 ?        00:00:00 /home/redis-5.0.7/src/redis-server 0.0.0.0:6382 [cluster]

root      42768      1  0 13:18 ?        00:00:00 /home/redis-5.0.7/src/redis-server 0.0.0.0:6383 [cluster]

root      42774      1  0 13:18 ?        00:00:00 /home/redis-5.0.7/src/redis-server 0.0.0.0:6384 [cluster]

root      42780      1  1 13:18 ?        00:00:00 /home/redis-5.0.7/src/redis-server 0.0.0.0:6385 [cluster]

root      42785  33683  0 13:18 pts/0    00:00:00 grep --color=auto redis

--创建redis cluster

redis-cli --cluster create 192.168.3.5:6380 192.168.3.5:6381 192.168.3.5:6382 192.168.3.5:6383 192.168.3.5:6384 192.168.3.5:6385 --cluster-replicas 1

>>> Performing hash slots allocation on 6 nodes...

Master[0] -> Slots 0 - 5460

Master[1] -> Slots 5461 - 10922

Master[2] -> Slots 10923 - 16383

Adding replica 192.168.3.5:6384 to 192.168.3.5:6380

Adding replica 192.168.3.5:6385 to 192.168.3.5:6381

Adding replica 192.168.3.5:6383 to 192.168.3.5:6382

>>> Trying to optimize slaves allocation for anti-affinity

[WARNING] Some slaves are in the same host as their master

M: fc567758cab9c7ee6dae22872b7329d70e838733 192.168.3.5:6380

   slots:[0-5460] (5461 slots) master

M: 54bd34e85c120862f0ceac8f19e0ff5d5b61fd1a 192.168.3.5:6381

   slots:[5461-10922] (5462 slots) master

M: db3bc86e8337031f4ba5002430b8d3ec6bfd82cc 192.168.3.5:6382

   slots:[10923-16383] (5461 slots) master

S: d487d3b475cfb1e13224787b603cd9a433a340f1 192.168.3.5:6383

   replicates 54bd34e85c120862f0ceac8f19e0ff5d5b61fd1a

S: 241f2eda3be02561fb0361ea91cf4d40e50a47ac 192.168.3.5:6384

   replicates db3bc86e8337031f4ba5002430b8d3ec6bfd82cc

S: d7ec771a2fc3845c08bb3b9d219a26603532b67a 192.168.3.5:6385

   replicates fc567758cab9c7ee6dae22872b7329d70e838733

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.3.5:6380)

M: fc567758cab9c7ee6dae22872b7329d70e838733 192.168.3.5:6380

   slots:[0-5460] (5461 slots) master

   1 additional replica(s)

M: 54bd34e85c120862f0ceac8f19e0ff5d5b61fd1a 192.168.3.5:6381

   slots:[5461-10922] (5462 slots) master

   1 additional replica(s)

S: 241f2eda3be02561fb0361ea91cf4d40e50a47ac 192.168.3.5:6384

   slots: (0 slots) slave

   replicates db3bc86e8337031f4ba5002430b8d3ec6bfd82cc

S: d7ec771a2fc3845c08bb3b9d219a26603532b67a 192.168.3.5:6385

   slots: (0 slots) slave

   replicates fc567758cab9c7ee6dae22872b7329d70e838733

M: db3bc86e8337031f4ba5002430b8d3ec6bfd82cc 192.168.3.5:6382

   slots:[10923-16383] (5461 slots) master

   1 additional replica(s)

S: d487d3b475cfb1e13224787b603cd9a433a340f1 192.168.3.5:6383

   slots: (0 slots) slave

   replicates 54bd34e85c120862f0ceac8f19e0ff5d5b61fd1a

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

--6个节点默认是3主,3从

--集群关闭

redis-cli -c -p 6380 shutdown

redis-cli -c -p 6381 shutdown

redis-cli -c -p 6382 shutdown

redis-cli -c -p 6383 shutdown

redis-cli -c -p 6384 shutdown

redis-cli -c -p 6385 shutdown

继续阅读