he TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
这个警告大概是说somaxconn的值128设置过小,从/proc/sys/net/core/somaxconn这个路径也可大概知道这个值的设置是关于网络连接中某个最大值的限定设置,此值表示网络连接的队列大小,在配置文件redis.conf中的“tcp-backlog 511”就配置在高并发环境下的最大队列大小,此值受限于系统的somaxconn与tcp_max_syn_backlog这两个值,所以应该把这两个内核参数值调大,具体解决方法如下:
$ vim /etc/sysctl.conf
$ net.core.somaxconn = 20480 #最大队列长度,应付突发的大并发连接请求,默认为128
$ net.ipv4.tcp_max_syn_backlog = 20480 #半连接队列长度,此值受限于内存大小,默认为1024
$ sysctl -p #使参数生效
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.
警告:过量使用内存设置为0!在低内存环境下,后台保存可能失败。为了修正这个问题,
请在/etc/sysctl.conf 添加一项 ‘vm.overcommit_memory = 1‘ ,
然后重启(或者运行命令‘sysctl vm.overcommit_memory=1‘ )使其生效。
vm.overcommit_memory不同的值说明:
0 表示检查是否有足够的内存可用,如果是,允许分配;如果内存不够,拒绝该请求,并返回一个错误给应用程序。
1 允许分配超出物理内存加上交换内存的请求
2 内核总是返回true
redis的数据回写机制分为两种
同步回写即SAVE命令。redis主进程直接写数据到磁盘。当数据量大时,这个命令将阻塞,响应时间长
异步回写即BGSAVE命令。redis 主进程fork一个子进程,复制主进程的内存并通过子进程回写数据到磁盘。
由于RDB文件写的时候fork一个子进程。相当于复制了一个内存镜像。当时系统的内存是4G,而redis占用了
近3G的内存,因此肯定会报内存无法分配。如果 「vm.overcommit_memory」设置为0,在可用内存不足的情况
下,就无法分配新的内存。如果 「vm.overcommit_memory」设置为1。 那么redis将使用交换内存。
$ vim /etc/sysctl
$ vm.overcommit_memory = 1 #末尾追加
$ sysctl -p #参数生效
或者
[root@redis ~]# echo "vm.overcommit_memory=1" >> /etc/sysctl.conf
[root@redis ~]# sysctl -p
注:使用交换内存并不是一个完美的方案。最好的办法是扩大物理内存。
3291:S 28 Sep 14:22:44.479 # 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.
`Transparent Huge Pages (THP)`告警,这是一个关于透明内存巨页的话题。简单来说内存可管理的最小
单位是page,一个page通常是4kb,那1M内存就会有256个page,CPU通过内置的内存管理单元管理page表
记录。Huge Pages就是表示page的大小已超过4kb了,一般是2M到1G,它的出现主要是为了管理超大内存。
个人理解上TB的内存。而THP就是管理Huge Pages的一个抽象层次,根据一些资料显示THP会导致内存锁
影响性能,所以一般建议关闭此功能。
“/sys/kernel/mm/transparent_hugepage/enabled”有三个值,如下:
$ cat /sys/kernel/mm/transparent_hugepage/enabled
always [madvise] never
####
# always 尽量使用透明内存,扫描内存,有512个 4k页面可以整合,就整合成一个2M的页面
# never 关闭,不使用透明内存
# madvise 避免改变内存占用
$ vim /etc/rc.local
$ echo never > /sys/kernel/mm/transparent_hugepage/enabled #在开机脚本里追加此命令
错误:Error condition on socket for SYNC: Connection refused
bind拒绝,添加从服务相应ip即可。
如果报错:host lost rule 等则:
关闭防火墙或者在防火墙规则中放开6379端口的访问,建议生产环境为后者
错误:Error reply to PING from master: ‘-DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients.
protected mode 设为no,或者把主服务器的redis配置中的密码验证添加上,建议为后者即:
. 权限认证密码
主配置文件:
# vi /usr/local/redis/conf/redis.conf
port 6379
requirepass redis //打开密码 并设置为redis ---记得设置密码后连接时要加入密码才行
从配置文件:
# vi /usr/local/redis/conf/redis.conf
port 6379
slaveof 192.168.56.10 6379 设置需要拷贝的主服务器的ip 和 端口
masterauth redis 设置主服务器设置的密码
requirepass redis # 如果还有从服务器来拷贝这个从服务器(也就是这个从当一个主时)则需要设置这个,否则不需要。(这句话有待验证)--------------注意:从库需要配置本身的密码验证,还需要配置连接到主库的密码验证masterauth
一般性的错误也是上面几个没有设置好导致的。
然后说说集群中容易出现的错误:
1, //无论是主还是从或者是集群服务器请都确保能够使用ip连接,也就是说使用./redis-cli -h 58.25.94.24 是能够连接上的
//而不是只能使用./redis-cli -h 127.0.0.1 ,这个需要的配置是redis.conf中的bind段,添加上ip。
2, //当开启了集群后如果在从或者主服务器使用info查看信息时提示:DENIED Redis is running in protected mode,这个需要配置sentinel.conf文件,加入protected-mode no 这一句才行,如下例:
port 26379
# sentinel announce-ip <ip>
# sentinel announce-port <port>
dir /tmp
protected-mode no
################################# master001 #################################
sentinel monitor master001 192.168.110.101 6379 2
# sentinel auth-pass <master-name> <password>
sentinel down-after-milliseconds master001 30000
sentinel parallel-syncs master001 1
sentinel failover-timeout master001 180000
3,在集群中的服务器的protected-mode 建议都为no ,或者就麻烦一些都加入密码。而bind段都屏蔽或者就依次加入需要的ip地址
4,如果主从服务器redis版本不一致可能出现:
4695:S 29 Sep 19:13:45.897 # Can't handle RDB format version 8
4695:S 29 Sep 19:13:45.897 # Failed trying to load the MASTER synchronization DB from disk
4695:S 29 Sep 19:13:46.870 * Connecting to MASTER 192.168.1.168:6379
4695:S 29 Sep 19:13:46.870 * MASTER <-> SLAVE sync started
4695:S 29 Sep 19:13:46.872 * Non blocking connect for SYNC fired the event.
4695:S 29 Sep 19:13:46.874 * Master replied to PING, replication can continue...
4695:S 29 Sep 19:13:46.878 * Partial resynchronization not possible (no cached master)
4695:S 29 Sep 19:13:46.882 * Full resync from master: 5be7c520f9469b2ae9f473330adbf38ea123569f:500897
4695:S 29 Sep 19:13:46.918 * MASTER <-> SLAVE sync: receiving 296 bytes from master
4695:S 29 Sep 19:13:46.919 * MASTER <-> SLAVE sync: Flushing old data
4695:S 29 Sep 19:13:46.919 * MASTER <-> SLAVE sync: Loading DB in memory
4695:S 29 Sep 19:13:46.919 # Can't handle RDB format version 8
4695:S 29 Sep 19:13:46.919 # Failed trying to load the MASTER synchronization DB from disk
4695:S 29 Sep 19:13:47.879 * Connecting to MASTER 192.168.1.168:6379
这种向上兼容不容易处理,所以务必保证版本一致
一,主服务的配置
只是简单开启主从服务的话,主服务上需要配置的东西比较少
一般也就是加上权限认证或者关闭protected_mode或者修改bind等等,根据需求来即可。
二,从服务的配置
打开slaveof ,并添加上主服务器的ip 和 端口
其实redis 的主从复制就是这么简单,,如果配置成功了可以输入info 命令查看:
# Replication
role:slave
master_host:192.168.1.154
master_port:6379
master_link_status:up
当从节点中replication段中的master_link_status的值为up时说明主从通信成功,可以进行主从复制
主节点的replication显示为:
# Replication
role:master
connected_slaves:1 --从服务的数量
slave0:ip=192.168.1.205,port=6379,state=online,offset=6281,lag=0
master_repl_offset:6281
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:6280
配置sentinel集群并启动
port26379
# sentinel announce-ip <ip>
# sentinel announce-port <port>
dir/tmp
-
################################# master001 #################################
sentinel monitor master001192.168.110.10163792
# sentinel auth-pass <master-name> <password>
sentinel down-after-milliseconds master00130000
sentinel parallel-syncs master0011
sentinel failover-timeout master001180000
# sentinel notification-script <master-name> <script-path>
# sentinel client-reconfig-script <master-name> <script-path>
-
# 可以配置多个master节点
################################# master002 #################################
./redis-sentinel sentinel001.conf
./redis-sentinel sentinel002.conf
./redis-sentinel sentinel003.conf
[7743]01Oct06:20:38.162#
Sentinel runid is ba6c42e1accc31290e11d5876275e1562564295d[7743]01Oct06:20:38.162# +monitor master master001 192.168.110.101 6379 quorum 2
[7743]01Oct06:20:39.110*+slave slave 192.168.110.102:6379192.168.110.1026379@ master001 192.168.110.1016379
[7743]01Oct06:20:39.111*+slave slave 192.168.110.103:6379192.168.110.1036379@ master001 192.168.110.1016379
[7743]01Oct06:25:07.595*+sentinel sentinel 192.168.110.100:36379192.168.110.10036379@ master001 192.168.110.1016379
[7743]01Oct06:26:11.170*+sentinel sentinel 192.168.110.100:46379192.168.110.10046379@ master001 192.168.110.1016379
[7795]01Oct06:25:05.538#
Sentinel runid is 52c14768b15837fb601b26328acf150c6bd30682[7795]01Oct06:25:05.538# +monitor master master001 192.168.110.101 6379 quorum 2
[7795]01Oct06:25:06.505*+slave slave 192.168.110.102:6379192.168.110.1026379@ master001 192.168.110.1016379
[7795]01Oct06:25:06.515*+slave slave 192.168.110.103:6379192.168.110.1036379@ master001 192.168.110.1016379
[7795]01Oct06:25:07.557*+sentinel sentinel 192.168.110.100:26379192.168.110.10026379@ master001 192.168.110.1016379
[7795]01Oct06:26:11.168*+sentinel sentinel 192.168.110.100:46379192.168.110.10046379@ master001 192.168.110.1016379
[7828]01Oct06:26:09.076#
Sentinel runid is c8509594be4a36660b2122b3b81f4f74060c9b04[7828]01Oct06:26:09.076# +monitor master master001 192.168.110.101 6379 quorum 2
[7828]01Oct06:26:10.063*+slave slave 192.168.110.102:6379192.168.110.1026379@ master001 192.168.110.1016379
[7828]01Oct06:26:10.071*+slave slave 192.168.110.103:6379192.168.110.1036379@ master001 192.168.110.1016379
[7828]01Oct06:26:11.516*+sentinel sentinel 192.168.110.100:26379192.168.110.10026379@ master001 192.168.110.1016379
[7828]01Oct06:26:11.674*+sentinel sentinel 192.168.110.100:36379192.168.110.10036379@ master001 192.168.110.1016379
4.测试sentinel集群
[wch@localhost bin]$./redis-cli-h192.168.110.101
infoReplicationCould not connect toRedis at192.168.110.101:6379:Connection refused
-
[wch@localhost bin]$./redis-cli-h192.168.110.102 infoReplication
# Replication
role:slave
master_host:192.168.110.103
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:29128
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
-
[wch@localhost bin]$./redis-cli-h192.168.110.103 infoReplication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.110.102,port=6379,state=online,offset=30456,lag=1
master_repl_offset:30456
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:30455
[wch@localhost bin]$
### 启动脚本,仍然使用默认配置
[wch@localhost bin]$./redis-server
-
[wch@localhost bin]$./redis-cli-h192.168.110.101 infoReplication
# Replication
role:slave
master_host:192.168.110.103
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:57657
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
[wch@localhost bin]$./redis-cli-h192.168.110.102 infoReplication
# Replication
role:slave
master_host:192.168.110.103
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:60751
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
[wch@localhost bin]$./redis-cli-h192.168.110.103 infoReplication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.110.102,port=6379,state=online,offset=63247,lag=1
slave1:ip=192.168.110.101,port=6379,state=online,offset=63247,lag=1
master_repl_offset:63393
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:63392
[wch@localhost bin]$
[wch@localhost bin]$./redis-cli-h192.168.110.101
infoReplication# Replication
role:slave
master_host:192.168.110.103
master_port:6379
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:184231
master_link_down_since_seconds:43
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
-
[wch@localhost bin]$./redis-cli-h192.168.110.102 infoReplication
# Replication
role:slave
master_host:192.168.110.103
master_port:6379
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:184231
master_link_down_since_seconds:52
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
-
[wch@localhost bin]$./redis-cli-h192.168.110.103 infoReplication
Could not connect toRedis at192.168.110.103:6379:Connection refused