1. 环境准备
redis采用redis5.0.9版本
1.1 环境配置
No | 主机名 | 目录假定 | 作用 | IP | Port |
---|---|---|---|---|---|
1 | redis1 | /usr/local/redis | bin目录 | 192.168.8.110 | |
2 | /usr/local/redis_cluster/7000/redis.conf | 集群端口 | 7000 | ||
3 | /usr/local/redis_cluster/7001/redis.conf | 集群端口 | 7001 | ||
4 | /usr/local/redis_cluster/7002/redis.conf | 集群端口 | 7002 | ||
5 | redis2 | /usr/local/redis | bin目录 | 192.168.8.111 | |
6 | /usr/local/redis_cluster/7003/redis.conf | 集群端口 | 7003 | ||
7 | /usr/local/redis_cluster/7004/redis.conf | 集群端口 | 7004 | ||
8 | /usr/local/redis_cluster/7005/redis.conf | 集群端口 | 7005 |
2 环境安装
2.1 hostname
[root@base1 ~]# hostnamectl set-hostname redis1 --static
[root@base2 ~]# hostnamectl set-hostname redis2 --static
需要重新启动操作系统
2.2 网络设置
[root@my1 ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33
[root@my2 ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33
[root@my3 ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33
BOOTPROTO="static" #dhcp改为static
ONBOOT="yes" #开机启用本配置
IPADDR=192.168.8.110 #静态IP 192.168.8.111
GATEWAY=192.168.8.2 #默认网关
NETMASK=255.255.255.0 #子网掩码
DNS1=114.114.114.114 #DNS 配置
DNS2=8.8.8.8 #DNS 配置
$# service network restart
2.3 工具安装
yum install net-tools
yum install tree
3 Redis安装
3.1 redis依赖安装
yum -y install gcc automake autoconf libtool make
3.2 下载redis源文件,并解压
yum install wget
wget http://download.redis.io/releases/redis-5.0.9.tar.gz
tar xvzf redis-5.0.9.tar.gz
3.3 安装redis
cd redis-5.0.9
make PREFIX=/usr/local/redis install
将 redis-trib.rb 复制到 /usr/local/bin 目录下
cd src
cp redis-trib.rb /usr/local/bin/
3.4 创建 Redis 节点,
首先在 redis1 机器上 创建 redis_cluster 目录
mkdir /usr/local/redis_cluster
mkdir /usr/local/redis_cluster/7000 /usr/local/redis_cluster/7001 /usr/local/redis_cluster/7002
cp redis.conf /usr/local/redis_cluster/7000
cp redis.conf /usr/local/redis_cluster/7001
cp redis.conf /usr/local/redis_cluster/7002
vi /usr/local/redis_cluster/7000/redis.conf
port 7000 //端口7000,7001,7002
bind 本机ip //默认ip为127.0.0.1 需要改为其他节点机器可访问的ip 否则创建集群时无法访问对应的端口,无法创建集群
daemonize yes //redis后台运行
pidfile /var/run/redis_7000.pid //pidfile文件对应7000,7001,7002
cluster-enabled yes //开启集群 把注释#去掉
cluster-config-file nodes_7000.conf //集群的配置 配置文件首次启动自动生成 7000,7001,7002 把注释#去掉
cluster-node-timeout 15000 //请求超时 默认15秒,可自行设置 把注释#去掉
appendonly yes //aof日志开启 有需要就开启,它会每次写操作都记录一条日志
其次在 redis2 机器上 创建 redis_cluster 目录
mkdir /usr/local/redis_cluster
mkdir /usr/local/redis_cluster/7003 /usr/local/redis_cluster/7004 /usr/local/redis_cluster/7005
cp redis.conf /usr/local/redis_cluster/7003
cp redis.conf /usr/local/redis_cluster/7004
cp redis.conf /usr/local/redis_cluster/7005
vi /usr/local/redis_cluster/7000/redis.conf
port 7003 //端口7003,7004,7005
bind 本机ip //默认ip为127.0.0.1 需要改为其他节点机器可访问的ip 否则创建集群时无法访问对应的端口,无法创建集群
daemonize yes //redis后台运行
pidfile /var/run/redis_7003.pid //pidfile文件对应7003,7004,7005
cluster-enabled yes //开启集群 把注释#去掉
cluster-config-file nodes_7003.conf //集群的配置 配置文件首次启动自动生成 7003,7004,7005 把注释#去掉
cluster-node-timeout 15000 //请求超时 默认15秒,可自行设置 把注释#去掉
appendonly yes //aof日志开启 有需要就开启,它会每次写操作都记录一条日志
3.5 启动各个节点
redis1
cd /usr/local/redis
./bin/redis-server ../redis_cluster/7000/redis.conf
./bin/redis-server ../redis_cluster/7001/redis.conf
./bin/redis-server ../redis_cluster/7002/redis.conf
redis2
cd /usr/local/redis
./bin/redis-server ../redis_cluster/7003/redis.conf
./bin/redis-server ../redis_cluster/7004/redis.conf
./bin/redis-server ../redis_cluster/7005/redis.conf
3.6 检查 redis 启动情况
查看redis端口开启情况
redis1
ps -ef | grep redis
netstat -tnlp | grep redis
tcp 0 0 192.168.8.110:7000 0.0.0.0:* LISTEN 1627/./bin/redis-se
tcp 0 0 192.168.8.110:7001 0.0.0.0:* LISTEN 1632/./bin/redis-se
tcp 0 0 192.168.8.110:7002 0.0.0.0:* LISTEN 1637/./bin/redis-se
tcp 0 0 192.168.8.110:17000 0.0.0.0:* LISTEN 1627/./bin/redis-se
tcp 0 0 192.168.8.110:17001 0.0.0.0:* LISTEN 1632/./bin/redis-se
tcp 0 0 192.168.8.110:17002 0.0.0.0:* LISTEN 1637/./bin/redis-se
redis2
ps -ef | grep redis
netstat -tnlp | grep redis
tcp 0 0 192.168.8.111:17003 0.0.0.0:* LISTEN 1655/./bin/redis-se
tcp 0 0 192.168.8.111:17004 0.0.0.0:* LISTEN 1660/./bin/redis-se
tcp 0 0 192.168.8.111:17005 0.0.0.0:* LISTEN 1665/./bin/redis-se
tcp 0 0 192.168.8.111:7003 0.0.0.0:* LISTEN 1655/./bin/redis-se
tcp 0 0 192.168.8.111:7004 0.0.0.0:* LISTEN 1660/./bin/redis-se
tcp 0 0 192.168.8.111:7005 0.0.0.0:* LISTEN 1665/./bin/redis-se
3.7 开启两台机器的防火墙
redis1
firewall-cmd --zone=public --add-port=7000-7002/tcp --permanent
firewall-cmd --zone=public --add-port=17000-17002/tcp --permanent
firewall-cmd --reload
firewall-cmd --zone=public --list-ports
redis2
firewall-cmd --zone=public --add-port=7003-7005/tcp --permanent
firewall-cmd --zone=public --add-port=17003-17005/tcp --permanent
firewall-cmd --reload
firewall-cmd --zone=public --list-ports
4 集群
4.1 创建集群
redis1
./bin/redis-cli --cluster create --cluster-replicas 1 192.168.8.110:7000 192.168.8.110:7001 192.168.8.110:7002 192.168.8.111:7003 192.168.8.111:7004 192.168.8.111:7005
>>> 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.8.111:7005 to 192.168.8.110:7000
Adding replica 192.168.8.110:7002 to 192.168.8.111:7003
Adding replica 192.168.8.111:7004 to 192.168.8.110:7001
M: 15f5b96bb6d9c11071fd0afc3a38977cae52e3b7 192.168.8.110:7000
slots:[0-5460] (5461 slots) master
M: 209e4caac50f5942805064f21eb8164a21988da1 192.168.8.110:7001
slots:[10923-16383] (5461 slots) master
S: 7edcbdeae47f88920929d7905fdd8bdda052c02b 192.168.8.110:7002
replicates 27671bbba62a158b74c7d5961c1114f11a37bde1
M: 27671bbba62a158b74c7d5961c1114f11a37bde1 192.168.8.111:7003
slots:[5461-10922] (5462 slots) master
S: a6b162cf39073bf74bff696312feaa5f6cc4a30f 192.168.8.111:7004
replicates 209e4caac50f5942805064f21eb8164a21988da1
S: ca2f2d86fede562b685588617af7ab2923d700e2 192.168.8.111:7005
replicates 15f5b96bb6d9c11071fd0afc3a38977cae52e3b7
输入 yes 即可,然后出现如下内容,说明安装成功
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.8.110:7000)
M: 15f5b96bb6d9c11071fd0afc3a38977cae52e3b7 192.168.8.110:7000
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 27671bbba62a158b74c7d5961c1114f11a37bde1 192.168.8.111:7003
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: ca2f2d86fede562b685588617af7ab2923d700e2 192.168.8.111:7005
slots: (0 slots) slave
replicates 15f5b96bb6d9c11071fd0afc3a38977cae52e3b7
S: a6b162cf39073bf74bff696312feaa5f6cc4a30f 192.168.8.111:7004
slots: (0 slots) slave
replicates 209e4caac50f5942805064f21eb8164a21988da1
S: 7edcbdeae47f88920929d7905fdd8bdda052c02b 192.168.8.110:7002
slots: (0 slots) slave
replicates 27671bbba62a158b74c7d5961c1114f11a37bde1
M: 209e4caac50f5942805064f21eb8164a21988da1 192.168.8.110:7001
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
集群创建后,主从分配如下表:
主从 | IP | Port |
---|---|---|
Master | 192.168.8.110 | 7000 |
Master | 192.168.8.110 | 7001 |
Master | 192.168.8.111 | 7003 |
Slave | 192.168.8.110 | 7002 |
Slave | 192.168.8.111 | 7004 |
Slave | 192.168.8.111 | 7005 |
4.2 集群验证
在第一台机器上连接集群的7000端口的节点,在另外一台连接7003节点,连接方式为
redis1
./bin/redis-cli -h 192.168.8.110 -c -p 7000
192.168.8.110:7000> set hello world
OK
然后在另外一台7005端口,查看 key 为 hello 的内容, get hello ,执行结果如下:
redis2
./bin/redis-cli -h 192.168.8.111 -c -p 7005
192.168.8.111:7005> get hello
-> Redirected to slot [866] located at 192.168.8.110:7000
"world"
4.3 bin目录介绍
├── bin
│ ├── redis-benchmark #Redis的压力测试工具
│ ├── redis-check-aof #检查AOF日志文件
│ ├── redis-check-rdb #检查RDB日志文件
│ ├── redis-cli #Redis的客户端脚本
│ ├── redis-sentinel -> redis-server #Redis的哨兵
│ └── redis-server #Redis的服务器脚本
5 脚本化
5.1 redis1
start-all-redis.sh
#!/bin/bash
cd /usr/local/redis
./bin/redis-server ../redis_cluster/7000/redis.conf
./bin/redis-server ../redis_cluster/7001/redis.conf
./bin/redis-server ../redis_cluster/7002/redis.conf
5.2 redis2
start-all-redis.sh
#!/bin/bash
cd /usr/local/redis
./bin/redis-server ../redis_cluster/7003/redis.conf
./bin/redis-server ../redis_cluster/7004/redis.conf
./bin/redis-server ../redis_cluster/7005/redis.conf
5.3 创建集群
create-cluster-redis.sh
#!/bin/bash
cd /usr/local/redis
./bin/redis-cli --cluster create --cluster-replicas 1 192.168.8.110:7000 192.168.8.110:7001 192.168.8.110:7002 192.168.8.111:7003 192.168.8.111:7004 192.168.8.111:7005
参考
今天的文章redis5.09 集群安装分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/22762.html