单机多实例+主从+Cluster
  DcpJeOZ6VzTX 2023年11月02日 18 0

1)做一遍Redis Cluster 2)一台6个实例 3)6380 81 82 83 84 85 (前三个主,后三个从)

安装部署单机多实例redis

# 安装redis7.2
# 1.下载
[root@db01 ~]# wget https://github.com/redis/redis/archive/7.2.0.tar.gz

# 2.创建安装目录
[root@db01 ~]# mkdir /app

# 3.解压
[root@db01 ~]# tar xf 7.2.0.tar.gz -C /app/

# 4.编译和安装
[root@db01 ~]# cd /app/redis-7.2.0/
[root@db01 redis-7.2.0]# make && make install

# 5.做个软连接
[root@db01 app]# ln -s /app/redis-7.2.0/ /app/redis
[root@db01 app]# ll
total 4
lrwxrwxrwx 1 root root   17 Aug 31 10:15 redis -> /app/redis-7.2.0/
drwxrwxr-x 8 root root 4096 Aug 15 17:38 redis-7.2.0

-----优化内存-----------------------------------
# 永久生效
[root@db01 app]# vim /etc/sysctl.conf 
vm.overcommit_memory = 1

# 查看它生效没
[root@db01 app]# sysctl -p
vm.overcommit_memory = 1
-------------------------------------------------
# 创建多实例目录
[root@db01 ~]# mkdir -p /etc/redis/6380
[root@db01 ~]# mkdir -p /etc/redis/6381
[root@db01 ~]# mkdir -p /etc/redis/6382
[root@db01 ~]# mkdir -p /etc/redis/6383
[root@db01 ~]# mkdir -p /etc/redis/6384
[root@db01 ~]# mkdir -p /etc/redis/6385

# 编辑多实例配置文件
-#主库redis 6380 配置文件
[root@db01 ~]# vim /etc/redis/6380/redis.conf
port 6380
daemonize yes
pidfile /etc/redis/6380/redis.pid
loglevel notice
logfile /etc/redis/6380/redis.log
dbfilename dump.rdb
dir /etc/redis/6380
bind 127.0.0.1
protected-mode no
requirepass 123
masterauth 123  
cluster-enabled yes
cluster-config-file nodes-6380.conf
cluster-node-timeout 15000
.............................................................
-#主edis 6381 配置文件
[root@db01 ~]# vim /etc/redis/6381/redis.conf
port 6381
daemonize yes
pidfile /etc/redis/6381/redis.pid
loglevel notice
logfile /etc/redis/6381/redis.log
dbfilename dump.rdb
dir /etc/redis/6381
bind 127.0.0.1
protected-mode no
requirepass 123
masterauth 123 
cluster-enabled yes
cluster-config-file nodes-6381.conf
cluster-node-timeout 15000
-------------------------------------------------------------

-#主库redis 6382 配置文件
[root@db01 ~]# vim /etc/redis/6382/redis.conf
port 6382
daemonize yes
pidfile /etc/redis/6382/redis.pid
loglevel notice
logfile /etc/redis/6382/redis.log
dbfilename dump.rdb
dir /etc/redis/6382
bind 127.0.0.1
protected-mode no
requirepass 123
masterauth 123 
cluster-enabled yes
cluster-config-file nodes-6382.conf
cluster-node-timeout 15000
.............................................................
-#从库redis 6383 配置文件
[root@db01 ~]# vim /etc/redis/6383/redis.conf
port 6383
daemonize yes
pidfile /etc/redis/6383/redis.pid
loglevel notice
logfile /etc/redis/6383/redis.log
dbfilename dump.rdb
dir /etc/redis/6383
bind 127.0.0.1
protected-mode no
requirepass 123
masterauth 123 
cluster-enabled yes
cluster-config-file nodes-6383.conf
cluster-node-timeout 15000
-------------------------------------------------------------

-#从dis 6384 配置文件
[root@db01 ~]# vim /etc/redis/6384/redis.conf
port 6384
daemonize yes
pidfile /etc/redis/6384/redis.pid
loglevel notice
logfile /etc/redis/6384/redis.log
dbfilename dump.rdb
dir /etc/redis/6384
bind 127.0.0.1
protected-mode no
requirepass 123
masterauth 123  
cluster-enabled yes
cluster-config-file nodes-6384.conf
cluster-node-timeout 15000
.............................................................
-#从库redis 6385 配置文件
[root@db01 ~]# vim /etc/redis/6385/redis.conf
port 6385
daemonize yes
pidfile /etc/redis/6385/redis.pid
loglevel notice
logfile /etc/redis/6385/redis.log
dbfilename dump.rdb
dir /etc/redis/6385
bind 127.0.0.1
protected-mode no
requirepass 123
masterauth 123 
cluster-enabled yes
cluster-config-file nodes-6385.conf
cluster-node-timeout 15000
-------------------------------------------------------------

------systemd管理redis--------------------------------
# 1.随便拷贝一个
[root@db01 app]# cp /usr/lib/systemd/system/{sshd,redis6380}.service
[root@db01 app]# cp /usr/lib/systemd/system/{sshd,redis6381}.service
[root@db01 app]# cp /usr/lib/systemd/system/{sshd,redis6382}.service
[root@db01 app]# cp /usr/lib/systemd/system/{sshd,redis6383}.service
[root@db01 app]# cp /usr/lib/systemd/system/{sshd,redis6384}.service
[root@db01 app]# cp /usr/lib/systemd/system/{sshd,redis6385}.service

# 2.修改启动脚本
[root@db01 app]# vim /usr/lib/systemd/system/redis6380.service 

[Unit]
Description=Redis
After=network.target

[Service]
Type=forking
ExecStart=/app/redis/src/redis-server /etc/redis/6380/redis.conf

[Install]
WantedBy=multi-user.target
------------------------------------------------------------
[root@db01 app]# vim /usr/lib/systemd/system/redis6381.service 

[Unit]
Description=Redis
After=network.target

[Service]
Type=forking
ExecStart=/app/redis/src/redis-server /etc/redis/6381/redis.conf

[Install]
WantedBy=multi-user.target
-------------------------------------------------------------
[root@db01 app]# vim /usr/lib/systemd/system/redis6382.service 

[Unit]
Description=Redis
After=network.target

[Service]
Type=forking
ExecStart=/app/redis/src/redis-server /etc/redis/6382/redis.conf

[Install]
WantedBy=multi-user.target
-------------------------------------------------------------
[root@db01 app]# vim /usr/lib/systemd/system/redis6383.service 

[Unit]
Description=Redis
After=network.target

[Service]
Type=forking
ExecStart=/app/redis/src/redis-server /etc/redis/6383/redis.conf

[Install]
WantedBy=multi-user.target
-------------------------------------------------------------
[root@db01 app]# vim /usr/lib/systemd/system/redis6384.service 

[Unit]
Description=Redis
After=network.target

[Service]
Type=forking
ExecStart=/app/redis/src/redis-server /etc/redis/6384/redis.conf

[Install]
WantedBy=multi-user.target
-------------------------------------------------------------
[root@db01 app]# vim /usr/lib/systemd/system/redis6385.service 

[Unit]
Description=Redis
After=network.target

[Service]
Type=forking
ExecStart=/app/redis/src/redis-server /etc/redis/6385/redis.conf

[Install]
WantedBy=multi-user.target
-------------------------------------------------------------

# 3.重新加载脚本
[root@db01 ~]# systemctl daemon-reload

# 4.启动
[root@db01 ~]# systemctl start redis6380
[root@db01 ~]# systemctl start redis6381
[root@db01 ~]# systemctl start redis6382
[root@db01 ~]# systemctl start redis6383
[root@db01 ~]# systemctl start redis6384
[root@db01 ~]# systemctl start redis6385

# 加入开机自启
[root@db01 ~]# systemctl enable redis6380
[root@db01 ~]# systemctl enable redis6381
[root@db01 ~]# systemctl enable redis6382
[root@db01 ~]# systemctl enable redis6383
[root@db01 ~]# systemctl enable redis6384
[root@db01 ~]# systemctl enable redis6385

# 检查端口
[root@db01 ~]# netstat -lntup

集群管理

创建集群
# 带密码
[root@db02 ~]# redis-cli -a 123 --cluster create 127.0.0.1:6380 127.0.0.1:6381  127.0.0.1:6382  127.0.0.1:6383  127.0.0.1:6384  127.0.0.1:6385 --cluster-replicas 1
# 不带密码
[root@db02 ~]# redis-cli --cluster create 127.0.0.1:6380 127.0.0.1:6381  127.0.0.1:6382  127.0.0.1:6383  127.0.0.1:6384  127.0.0.1:6385 --cluster-replicas 1  
---要输入yes---
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:6384 to 127.0.0.1:6380
Adding replica 127.0.0.1:6385 to 127.0.0.1:6381
Adding replica 127.0.0.1:6383 to 127.0.0.1:6382
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: f059498ce4d5417fe24289094c7c308360fae8e3 127.0.0.1:6380
   slots:[0-5460] (5461 slots) master
M: 4f138305e2b427256b23600672879230c5203915 127.0.0.1:6381
   slots:[5461-10922] (5462 slots) master
M: f5c6fdb82d98730f8b30a0d9bceb5a3d4c00f936 127.0.0.1:6382
   slots:[10923-16383] (5461 slots) master
S: a93ce4ef6f409e12161061100ae459d8784ec1bf 127.0.0.1:6383
   replicates f5c6fdb82d98730f8b30a0d9bceb5a3d4c00f936
S: 08699b8e69e0543ccfae6556529f4c9dcde654b1 127.0.0.1:6384
   replicates f059498ce4d5417fe24289094c7c308360fae8e3
S: 34df0fed33ae4f7a5828c54eeaf41dac95674274 127.0.0.1:6385
   replicates 4f138305e2b427256b23600672879230c5203915
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 127.0.0.1:6380)
M: f059498ce4d5417fe24289094c7c308360fae8e3 127.0.0.1:6380
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: a93ce4ef6f409e12161061100ae459d8784ec1bf 127.0.0.1:6383
   slots: (0 slots) slave
   replicates f5c6fdb82d98730f8b30a0d9bceb5a3d4c00f936
M: 4f138305e2b427256b23600672879230c5203915 127.0.0.1:6381
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 08699b8e69e0543ccfae6556529f4c9dcde654b1 127.0.0.1:6384
   slots: (0 slots) slave
   replicates f059498ce4d5417fe24289094c7c308360fae8e3
S: 34df0fed33ae4f7a5828c54eeaf41dac95674274 127.0.0.1:6385
   slots: (0 slots) slave
   replicates 4f138305e2b427256b23600672879230c5203915
M: f5c6fdb82d98730f8b30a0d9bceb5a3d4c00f936 127.0.0.1:6382
   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.

单机多实例+主从+Cluster_单机多实例redis

集群验证
连接集群
[root@db02 ~]# redis-cli -c -h 127.0.0.1 -p 6380
查看集群信息
# 登录redis-cli后执行如下命令
CLUSTER INFO
--------------
[root@db02 6380]# redis-cli -c -h 127.0.0.1 -p 6380
127.0.0.1:6380> CLUSTER INFO

单机多实例+主从+Cluster_单机多实例redis_02

查看节点信息
127.0.0.1:6380> CLUSTER NODES

单机多实例+主从+Cluster_单机多实例redis_03

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读