• 欢迎光临~

ubuntu redis sentinel安装部署

开发技术 开发技术 2022-12-18 次浏览

1.命令行安装

sudo apt update
sudo apt install redis-server
sudo apt install redis-sentinel

2.查看安装版本

# redis-cli --version
redis-cli 5.0.7

3.配置修改

这里我们使用本机3个节点的伪集群,其中6379为master节点,6380和6381为slave节点

配置文件位置: /etc/redis

cd /etc/redis
cp redis.conf redis-6380.conf
cp redis.conf redis-6381.conf

修改redis.conf配置信息:

bind 0.0.0.0 # 对所有节点放开
daemonize yes # 守护进程,开机启动
requirepass "ConnextNj@2022" # master节点密码
masterauth "ConnextNj@2022" # master节点密码,slave节点连接master使用
pidfile /var/run/redis_6379.pid
logfile /etc/redis/redis-6379.log #日志地址
protected-mode no

修改redis-6380.conf信息:

port 6380
bind 0.0.0.0 # 对所有节点放开
daemonize yes # 守护进程,开机启动
requirepass "ConnextNj@2022" # master节点密码
masterauth "ConnextNj@2022"  # master节点密码,slave节点连接master使用
replicaof 127.0.0.1 6379  # 指定master节点和端口号
pidfile /var/run/redis_6380.pid
logfile /etc/redis/redis-6381.log #日志地址
protected-mode no

另一个配置文件同理

port 6381
bind 0.0.0.0 # 对所有节点放开
daemonize yes # 守护进程,开机启动
requirepass "ConnextNj@2022" # master节点密码
masterauth "ConnextNj@2022"  # master节点密码,slave节点连接master使用
replicaof 127.0.0.1 6379
pidfile /var/run/redis_6381.pid
logfile /etc/redis/redis-6381.log
protected-mode no

4.启动服务

redis-server redis.conf && redis-server redis-6380.conf && redis-server redis-6381.conf

5.验证

命令 "redis-cli -p 6379 -a ConnextNj@2022",其中ConnextNj@2022是master节点密码 ,然后输入"info replication"

# redis-cli -p 6379 -a ConnextNj@2022
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6380,state=online,offset=28,lag=0
slave1:ip=127.0.0.1,port=6381,state=online,offset=28,lag=0
master_replid:20df2dfc46962ba0572a951e49677cb19ad82915
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:28
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:28
127.0.0.1:6379> 

可以看到connected_slaves:2 ,即2个slave节点已经连接

6.日志

默认日志路径: logfile /var/log/redis/redis-server.log

配置文件中已做修改,本次日志路径为 /etc/redis/redis-6379.log

2179605:C 15 Dec 2022 00:45:03.719 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2179605:C 15 Dec 2022 00:45:03.719 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=2179605, just started
2179605:C 15 Dec 2022 00:45:03.719 # Configuration loaded
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 5.0.7 (00000000/0) 64 bit
  .-`` .-```.  ```/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 2179606
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

2179606:M 15 Dec 2022 00:45:03.725 # Server initialized

问题: 查看日志,日志中出现"overcommit_memory is set to 0":

2179606:M 15 Dec 2022 00:45:03.725 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
2179606:M 15 Dec 2022 00:45:03.725 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.

根据提示

sudo vim /etc/sysctl.conf

修改为:

vm.overcommit_memory = 1

保存退出并reboot .

注意: 部分物理机Linux 中权限控制,即使是root用户也无法修改文件内容,待探究规避方法,云服务器则未发现此类问题.

7.配置哨兵模式 sentinel.conf

port 26379 # sentinel节点端口
daemonize yes # 守护线程
pidfile "/var/run/sentinel/redis-sentinel.pid"
logfile "/etc/redis/redis-sentinel-6379.log" # 日志路径
sentinel monitor mymaster 127.0.0.1 6379 2 # master节点别名 路径 端口号 过半数投票数
sentinel auth-pass mymaster ConnextNj@2022 # master节点密码
sentinel down-after-milliseconds mymaster 10000 # 下线时间,单位毫秒, 即超过10s则认为不可达
sentinel parallel-syncs mymaster 1 # 故障转移后,发起复制的节点限制个数
sentinel failover-timeout mymaster 60000 # 故障转移超时时间

复制配置文件

cp sentinel.conf sentinel-6380.conf
cp sentinel.conf sentinel-6381.conf

修改配置文件


port 26380
daemonize yes
pidfile "/var/run/sentinel/redis-sentinel-6380.pid"
logfile "/etc/redis/redis-sentinel-6380.log"
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel auth-pass mymaster ConnextNj@2022
sentinel down-after-milliseconds mymaster 10000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 60000


port 26381
daemonize yes
pidfile "/var/run/sentinel/redis-sentinel-6381.pid"
logfile "/etc/redis/redis-sentinel-6381.log"
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel auth-pass mymaster ConnextNj@2022
sentinel down-after-milliseconds mymaster 10000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 60000

运行sentinel

redis-sentinel sentinel.conf && redis-sentinel  sentinel-6380.conf && redis-sentinel  sentinel-6381.conf 

 查看线程:

# ps -ef | grep redis
root        1407       1  0 Dec15 ?        00:05:50 redis-server 0.0.0.0:6379
root        1412       1  0 Dec15 ?        00:05:31 redis-server 0.0.0.0:6380
root        1418       1  0 Dec15 ?        00:05:28 redis-server 0.0.0.0:6381
root       70553       1  0 00:49 ?        00:00:00 redis-sentinel 0.0.0.0:26379 [sentinel]
root       70616       1  0 00:50 ?        00:00:00 redis-sentinel 0.0.0.0:26380 [sentinel]
root       70636       1  0 00:51 ?        00:00:00 redis-sentinel 0.0.0.0:26381 [sentinel]
root       70642   70389  0 00:52 pts/0    00:00:00 grep --color=auto redis

验证主备切换: 断掉主节点,查看slave是否转化为master

kill -9 1407

查看

# redis-cli -p 6380 -a ConnextNj@2022
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6380> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6381,state=online,offset=537170,lag=0
master_replid:569a54755cbd226e31a948a147a654ce935d51e2
master_replid2:20df2dfc46962ba0572a951e49677cb19ad82915
master_repl_offset:537170
second_repl_offset:527103
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:537170

切换成功,重新拉起6379端口线程

# redis-server redis.conf
# ps -ef | grep redis
root        1412       1  0 Dec15 ?        00:05:32 redis-server 0.0.0.0:6380
root        1418       1  0 Dec15 ?        00:05:28 redis-server 0.0.0.0:6381
root       70553       1  0 00:49 ?        00:00:01 redis-sentinel 0.0.0.0:26379 [sentinel]
root       70616       1  0 00:50 ?        00:00:00 redis-sentinel 0.0.0.0:26380 [sentinel]
root       70636       1  0 00:51 ?        00:00:00 redis-sentinel 0.0.0.0:26381 [sentinel]
root       70695       1  0 00:57 ?        00:00:00 redis-server 0.0.0.0:6379
root       70700   70389  0 00:57 pts/0    00:00:00 grep --color=auto redis

主备切换成功;

 

程序员灯塔
转载请注明原文链接:ubuntu redis sentinel安装部署
喜欢 (0)