Redis实现单机多实例cluster集群
Posted 白-胖-子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis实现单机多实例cluster集群相关的知识,希望对你有一定的参考价值。
#!/bin/bash
rdsdir=/apps/redis
sysdir=/lib/systemd/system
for i in {1..6};do
cp -a $rdsdir/etc/redis.conf $rdsdir/etc/redis_638$i.conf
cp -a $sysdir/redis.service $sysdir/redis_638$i.service
sed -ri.bak -e "/dbfilename .*/c dbfilename dump6379.rdb" -e "/appendfilename .*/c appendfilename \\"appendonly_6379.aof\\"" $rdsdir/etc/redis_638$i.conf
sed -i.bak -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e '/masterauth/a masterauth 123456' -e '/# requirepass/a requirepass 123456' -e '/# cluster-enabled yes/a cluster-enabled yes' -e '/# cluster-config-file nodes-6379.conf/a cluster-config-file nodes-6379.conf' -e '/cluster-requirefull-coverage yes/c cluster-require-full-coverage no' $rdsdir/etc/redis_638$i.conf
sed -ri.bak "s/(.*)6379(.*)/\\1638$i\\2/g" $rdsdir/etc/redis_638$i.conf
sed -ri.bak "/^ExecStart.*/s/(.*)redis\\.conf(.*)/\\1redis_638$i\\.conf\\2/" $sysdir/redis_638$i.service
systemctl daemon-reload
systemctl enable --now redis_638$i
done
[root@c7-199 ~]# systemctl status redis_638{1..6}
● redis_6381.service - Redis persistent key-value database
Loaded: loaded (/usr/lib/systemd/system/redis_6381.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2021-07-02 15:23:12 CST; 22min ago
Main PID: 5782 (redis-server)
Status: "Ready to accept connections"
CGroup: /system.slice/redis_6381.service
└─5782 /apps/redis/bin/redis-server 0.0.0.0:6381 [cluster]
Jul 02 15:23:12 c7-199 systemd[1]: Starting Redis persistent key-value database...
Jul 02 15:23:12 c7-199 systemd[1]: Started Redis persistent key-value database.
● redis_6382.service - Redis persistent key-value database
Loaded: loaded (/usr/lib/systemd/system/redis_6382.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2021-07-02 15:23:12 CST; 22min ago
Main PID: 5832 (redis-server)
Status: "Ready to accept connections"
CGroup: /system.slice/redis_6382.service
└─5832 /apps/redis/bin/redis-server 0.0.0.0:6382 [cluster]
Jul 02 15:23:12 c7-199 systemd[1]: Starting Redis persistent key-value database...
Jul 02 15:23:12 c7-199 systemd[1]: Started Redis persistent key-value database.
● redis_6383.service - Redis persistent key-value database
Loaded: loaded (/usr/lib/systemd/system/redis_6383.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2021-07-02 15:23:12 CST; 22min ago
Main PID: 5882 (redis-server)
Status: "Ready to accept connections"
CGroup: /system.slice/redis_6383.service
└─5882 /apps/redis/bin/redis-server 0.0.0.0:6383 [cluster]
Jul 02 15:23:12 c7-199 systemd[1]: Starting Redis persistent key-value database...
Jul 02 15:23:12 c7-199 systemd[1]: Started Redis persistent key-value database.
● redis_6384.service - Redis persistent key-value database
Loaded: loaded (/usr/lib/systemd/system/redis_6384.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2021-07-02 15:23:13 CST; 22min ago
Main PID: 5932 (redis-server)
Status: "MASTER <-> REPLICA sync: Finished with success. Ready to accept connections in read-write mode."
CGroup: /system.slice/redis_6384.service
└─5932 /apps/redis/bin/redis-server 0.0.0.0:6384 [cluster]
Jul 02 15:23:13 c7-199 systemd[1]: Starting Redis persistent key-value database...
Jul 02 15:23:13 c7-199 systemd[1]: Started Redis persistent key-value database.
● redis_6385.service - Redis persistent key-value database
Loaded: loaded (/usr/lib/systemd/system/redis_6385.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2021-07-02 15:23:13 CST; 22min ago
Main PID: 5982 (redis-server)
Status: "MASTER <-> REPLICA sync: Finished with success. Ready to accept connections in read-write mode."
CGroup: /system.slice/redis_6385.service
└─5982 /apps/redis/bin/redis-server 0.0.0.0:6385 [cluster]
Jul 02 15:23:13 c7-199 systemd[1]: Starting Redis persistent key-value database...
Jul 02 15:23:13 c7-199 systemd[1]: Started Redis persistent key-value database.
● redis_6386.service - Redis persistent key-value database
Loaded: loaded (/usr/lib/systemd/system/redis_6386.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2021-07-02 15:23:13 CST; 22min ago
Main PID: 6032 (redis-server)
Status: "MASTER <-> REPLICA sync: Finished with success. Ready to accept connections in read-write mode."
CGroup: /system.slice/redis_6386.service
└─6032 /apps/redis/bin/redis-server 0.0.0.0:6386 [cluster]
Jul 02 15:23:13 c7-199 systemd[1]: Starting Redis persistent key-value database...
Jul 02 15:23:13 c7-199 systemd[1]: Started Redis persistent key-value database.
pidfile /apps/redis/run/redis_6379.pid
logfile "/apps/redis/log/redis_6379.log"
appendfilename "appendonly_6379.aof"
port 6379
pidfile /apps/redis/run/redis_6379.pid
logfile /apps/redis/log/redis-6379.log
dbfilename dump.rdb
appendfilename "appendonly.aof"
cat /lib/systemd/system/redis.service
[Unit]
Description=Redis persistent key-value database
After=network.target
[Service]
ExecStart=/apps/redis/bin/redis-server /apps/redis/etc/redis.conf --supervised systemd
ExecStop=/bin/kill -s QUIT $MAINPID
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
[Install]
WantedBy=multi-user.target
sed -r "s/(.*)6379(.*)/\\16380\\2/" /apps/redis/etc/redis.conf
sed -ri.bak "/dbfilename .*/c dbfilename $rdsdir/log/redis-6379.log" -e "/appendfilename .*/c appendfilename \\"appendonly_6379.aof\\"" -e "s/(.*)6379(.*)/\\1638$i\\2/g" redis.conf
sed -i.bak -e 's/bind 127.0.0.1/bind 0.0.0.0/' \\
-e '/masterauth/a masterauth 123456' -e '/# requirepass/a requirepass 123456' \\
-e '/# cluster-enabled yes/a cluster-enabled yes' \\
-e '/# cluster-config-filenodes-6379.conf/a cluster-config-file nodes-6379.conf' \\
-e '/cluster-requirefull-coverage yes/c cluster-require-full-coverage no' $rdsdir/etc/redis_638$i.conf
# cluster-config-file nodes-6386.conf
# cluster-config-filenodes-6379.conf/a cluster-config-file nodes-6379.conf
redis-cli -a 123456 --cluster create 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 127.0.0.1:6386 --cluster-replicas 1
[root@c7-199 ~]# ps aux | grep redis
redis 5726 0.1 0.5 195568 10808 ? Ssl 15:21 0:01 /apps/redis/bin/redis-server 0.0.0.0:6379
redis 5782 0.2 0.5 198128 10772 ? Ssl 15:23 0:01 /apps/redis/bin/redis-server 0.0.0.0:6381 [cluster]
redis 5832 0.1 0.5 204272 10696 ? Ssl 15:23 0:01 /apps/redis/bin/redis-server 0.0.0.0:6382 [cluster]
redis 5882 0.2 0.5 198128 10820 ? Ssl 15:23 0:01 /apps/redis/bin/redis-server 0.0.0.0:6383 [cluster]
redis 5932 0.1 0.5 198128 10864 ? Ssl 15:23 0:01 /apps/redis/bin/redis-server 0.0.0.0:6384 [cluster]
redis 5982 0.1 0.5 198128 10860 ? Ssl 15:23 0:01 /apps/redis/bin/redis-server 0.0.0.0:6385 [cluster]
redis 6032 0.1 0.5 198128 10820 ? Ssl 15:23 0:01 /apps/redis/bin/redis-server 0.0.0.0:6386 [cluster]
root 6069 0.0 0.0 112808 972 pts/0 S+ 15:36 0:00 grep --color=auto redis
[root@c7-199 ~]# redis-cli -a 123456 -p 6381 -c INFO replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6384,state=online,offset=252,lag=0
master_failover_state:no-failover
master_replid:88478f494a11d8b1c2de6bf9fe5d643ef72aea81
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:252
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:252
[root@c7-199 ~]# redis-cli -a 123456 -p 6382 -c INFO replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6385,state=online,offset=252,lag=0
master_failover_state:no-failover
master_replid:e96c89976ffc7e63f99355cd94bd308c878a5997
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:252
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:252
[root@c7-199 ~]# redis-cli -a 123456 -p 6383 -c INFO replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6386,state=online,offset=266,lag=1
master_failover_state:no-failover
master_replid:7725b1c7dfa36064b011a478c9673b4d91a2f7d9
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:266
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:266
[root@c7-199 ~]# redis-cli -a 123456 -p 6384 -c INFO replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:127.0.0.1
master_port:6381
master_link_status:up
master_last_io_seconds_ago:5
master_sync_in_progress:0
slave_repl_offset:266
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:88478f494a11d8b1c2de6bf9fe5d643ef72aea81
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:266
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:266
[root@c7-199 ~]# redis-cli -a 123456 -p 6385 -c INFO replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:127.0.0.1
master_port:6382
master_link_status:up
master_last_io_seconds_ago:9
master_sync_in_progress:0
slave_repl_offset:266
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:e96c89976ffc7e63f99355cd94bd308c878a5997
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:266
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:266
[root@c7-199 ~]# redis-cli -a 123456 -p 6386 -c INFO replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:127.0.0.1
master_port:6383
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:280
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:7725b1c7dfa36064b011a478c9673b4d91a2f7d9
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:280
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:280
[root@c7-199 ~]# ss -ntlp | grep 638
LISTEN 0 511 *:16381 *:* users:(("redis-server",pid=5782,fd=8))
LISTEN 0 511 *:16382 *:* users:(("redis-server",pid=5832,fd=8))
LISTEN 0 511 *:16383 *:* users:(("redis-server",pid=5882,fd=8))
LISTEN 0 511 *:16384 *:* users:(("redis-server",pid=5932,fd=8))
LISTEN 0 511 *:16385 *:* users:(("redis-server",pid=5982,fd=8))
LISTEN 0 511 *:16386 *:* users:(("redis-server",pid=6032,fd=8))
LISTEN 0 511 *:6381 *:* users:(("redis-server",pid=5782,fd=6))
LISTEN 0 511 *:6382 *:* users:(("redis-server",pid=5832,fd=6))
LISTEN 0 511 *:6383 *:* users:(("redis-server",pid=5882,fd=6))
LISTEN 0 511 *:6384 *:* users:(("redis-server",pid=5932,fd=6))
LISTEN 0 511 *:6385 *:* users:(("redis-server",pid=5982,fd=6))
LISTEN 0 511 *:6386 *:* users:(("redis-server",pid=6032,fd=6))
以上是关于Redis实现单机多实例cluster集群的主要内容,如果未能解决你的问题,请参考以下文章