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安装目录
[root@k8s init.d]# cd /home/redis-5.0.7

创建config目录
[root@k8s redis-5.0.7]# mkdir config
[root@k8s 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


[root@k8s 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值