如何使用Visual Studio远程调试部署在Azure上的Web App

本文涉及的产品
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
日志服务 SLS,月写入数据量 50GB 1个月
简介:


介绍

Redis Sentinel 是一个分布式系统, 你可以在一个架构中运行多个 Sentinel 进程(progress), 这些进程使用流言协议(gossip protocols)来接收关于主服务器是否下线的信息, 并使用投票协议(agreement protocols)来决定是否执行自动故障迁移, 以及选择哪个从服务器作为新的主服务器。

虽然 Redis Sentinel 释出为一个单独的可执行文件 redis-sentinel , 但实际上它只是一个运行在特殊模式下的 Redis 服务器, 你可以在启动一个普通 Redis 服务器时通过给定 --sentinel 选项来启动 Redis Sentinel 。

Sentinel 系统用于管理多个 Redis 服务器(instance), 该系统执行以下三个任务:

  • 监控(Monitoring): Sentinel 会不断地检查你的主服务器和从服务器是否运作正常。
  • 提醒(Notification): 当被监控的某个 Redis 服务器出现问题时, Sentinel 可以通过 API 向管理员或者其他应用程序发送通知。
  • 自动故障迁移(Automatic failover): 当一个主服务器不能正常工作时, Sentinel 会开始一次自动故障迁移操作, 它会将失效主服务器的其中一个从服务器升级为新的主服务器, 并让失效主服务器的其他从服务器改为复制新的主服务器; 当客户端试图连接失效的主服务器时, 集群也会向客户端返回新主服务器的地址, 使得集群可以使用新主服务器代替失效服务器。

redis版本:3.0.7

主:6379 ,sentinel:26379

从:6380 ,sentinel:26380

配置

本章主要介绍怎样搭建自动故障转移的reids群集,当主宕机了从接替主成为新的主,宕机的主启动后自动变成了从,其实它和Mysql的双主模式是一样的互为主从;redis群集需要用到redis-sentinel程序和sentinel.conf配置文件。

主配置

vim redis.conf


 
 
  1. daemonize yes 
  2.  
  3. pidfile /usr/local/redis-6379/run/redis.pid 
  4.  
  5. port 6379 
  6.  
  7. tcp-backlog 128 
  8.  
  9. timeout 0 
  10.  
  11. tcp-keepalive 0 
  12.  
  13. loglevel notice 
  14.  
  15. logfile "" 
  16.  
  17. databases 16 
  18.  
  19. save 900 1 
  20.  
  21. save 300 10 
  22.  
  23. save 60 10000 
  24.  
  25. stop-writes-on-bgsave-error yes 
  26.  
  27. rdbcompression yes 
  28.  
  29. rdbchecksum yes 
  30.  
  31. dbfilename dump.rdb 
  32.  
  33. dir "/usr/local/redis-6379" 
  34.  
  35. masterauth "123456" 
  36.  
  37. requirepass "123456" 
  38.  
  39. slave-serve-stale-data yes 
  40.  
  41. slave-read-only yes 
  42.  
  43. repl-diskless-sync no 
  44.  
  45. repl-diskless-sync-delay 5 
  46.  
  47. repl-disable-tcp-nodelay no 
  48.  
  49. slave-priority 100 
  50.  
  51. appendonly no 
  52.  
  53. appendfilename "appendonly.aof" 
  54.  
  55. appendfsync everysec 
  56.  
  57. no-appendfsync-on-rewrite no 
  58.  
  59. auto-aof-rewrite-percentage 100 
  60.  
  61. auto-aof-rewrite-min-size 64mb 
  62.  
  63. aof-load-truncated yes 
  64.  
  65. lua-time-limit 5000 
  66.  
  67. slowlog-log-slower-than 10000 
  68.  
  69. slowlog-max-len 128 
  70.  
  71. latency-monitor-threshold 0 
  72.  
  73. notify-keyspace-events "" 
  74.  
  75. hash-max-ziplist-entries 512 
  76.  
  77. hash-max-ziplist-value 64 
  78.  
  79. list-max-ziplist-entries 512 
  80.  
  81. list-max-ziplist-value 64 
  82.  
  83. set-max-intset-entries 512 
  84.  
  85. zset-max-ziplist-entries 128 
  86.  
  87. zset-max-ziplist-value 64 
  88.  
  89. hll-sparse-max-bytes 3000 
  90.  
  91. activerehashing yes 
  92.  
  93. client-output-buffer-limit normal 0 0 0 
  94.  
  95. client-output-buffer-limit slave 256mb 64mb 60 
  96.  
  97. client-output-buffer-limit pubsub 32mb 8mb 60 
  98.  
  99. hz 10 
  100.  
  101. aof-rewrite-incremental-fsync yes 

vim sentinel.conf

群集文件配置


 
 
  1. port 26379 
  2.  
  3. dir "/usr/local/redis-6379" 
  4.  
  5. # 守护进程模式 
  6.  
  7. daemonize yes 
  8.  
  9. # 指明日志文件名 
  10.  
  11. logfile "./sentinel.log" 
  12.  
  13. sentinel monitor mymaster 192.168.137.40 6379 1 
  14.  
  15. sentinel down-after-milliseconds mymaster 5000 
  16.  
  17. sentinel failover-timeout mymaster 18000 
  18.  
  19. sentinel auth-pass mymaster 123456 

从配置

vim redis.conf


 
 
  1. daemonize yes 
  2.  
  3. pidfile "/usr/local/redis-6380/run/redis.pid" 
  4.  
  5. port 6380 
  6.  
  7. tcp-backlog 128 
  8.  
  9. timeout 0 
  10.  
  11. tcp-keepalive 0 
  12.  
  13. loglevel notice 
  14.  
  15. logfile "" 
  16.  
  17. databases 16 
  18.  
  19. save 900 1 
  20.  
  21. save 300 10 
  22.  
  23. save 60 10000 
  24.  
  25. stop-writes-on-bgsave-error yes 
  26.  
  27. rdbcompression yes 
  28.  
  29. rdbchecksum yes 
  30.  
  31. dbfilename "dump.rdb" 
  32.  
  33. dir "/usr/local/redis-6380" 
  34.  
  35. masterauth "123456" 
  36.  
  37. requirepass "123456" 
  38.  
  39. slave-serve-stale-data yes 
  40.  
  41. slave-read-only yes 
  42.  
  43. repl-diskless-sync no 
  44.  
  45. repl-diskless-sync-delay 5 
  46.  
  47. repl-disable-tcp-nodelay no 
  48.  
  49. slave-priority 100 
  50.  
  51. appendonly no 
  52.  
  53. appendfilename "appendonly.aof" 
  54.  
  55. appendfsync everysec 
  56.  
  57. no-appendfsync-on-rewrite no 
  58.  
  59. auto-aof-rewrite-percentage 100 
  60.  
  61. auto-aof-rewrite-min-size 64mb 
  62.  
  63. aof-load-truncated yes 
  64.  
  65. lua-time-limit 5000 
  66.  
  67. slowlog-log-slower-than 10000 
  68.  
  69. slowlog-max-len 128 
  70.  
  71. latency-monitor-threshold 0 
  72.  
  73. notify-keyspace-events "" 
  74.  
  75. hash-max-ziplist-entries 512 
  76.  
  77. hash-max-ziplist-value 64 
  78.  
  79. list-max-ziplist-entries 512 
  80.  
  81. list-max-ziplist-value 64 
  82.  
  83. set-max-intset-entries 512 
  84.  
  85. zset-max-ziplist-entries 128 
  86.  
  87. zset-max-ziplist-value 64 
  88.  
  89. hll-sparse-max-bytes 3000 
  90.  
  91. activerehashing yes 
  92.  
  93. client-output-buffer-limit normal 0 0 0 
  94.  
  95. client-output-buffer-limit slave 256mb 64mb 60 
  96.  
  97. client-output-buffer-limit pubsub 32mb 8mb 60 
  98.  
  99. hz 10 
  100.  
  101. aof-rewrite-incremental-fsync yes 

vim sentinel.conf


 
 
  1. #sentinel端口 
  2.  
  3. port 26380 
  4.  
  5. #工作路径,注意路径不要和主重复 
  6.  
  7. dir "/usr/local/redis-6380" 
  8.  
  9. # 守护进程模式 
  10.  
  11. daemonize yes 
  12.  
  13. # 指明日志文件名 
  14.  
  15. logfile "./sentinel.log" 
  16.  
  17. #哨兵监控的master,主从配置一样, 
  18.  
  19. sentinel monitor mymaster 192.168.137.40 6379 1 
  20.  
  21. # master或slave多长时间(默认30秒)不能使用后标记为s_down状态。 
  22.  
  23. sentinel down-after-milliseconds mymaster 5000 
  24.  
  25. #若sentinel在该配置值内未能完成failover操作(即故障时master/slave自动切换),则认为本次failover失败。 
  26.  
  27. sentinel failover-timeout mymaster 18000 
  28.  
  29. #设置master和slaves验证密码 
  30.  
  31. sentinel auth-pass mymaster 123456 

启动redis

主从都要启动


 
 
  1. src/redis-server redis.conf 

启动群集监控

主从都要启动


 
 
  1. src/redis-sentinel sentinel.conf --sentinel 

启动报错处理

错误1:

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.


 
 
  1. 两个解决方法(overcommit_memory) 
  2.  
  3. 1. echo "vm.overcommit_memory=1" > /etc/sysctl.conf 或 vi /etcsysctl.conf , 然后reboot重启机器 
  4.  
  5. 2. echo 1 > /proc/sys/vm/overcommit_memory 不需要启机器就生效 

 
 
  1. overcommit_memory参数说明: 
  2.  
  3. 设置内存分配策略(可选,根据服务器的实际情况进行设置) 
  4.  
  5. /proc/sys/vm/overcommit_memory 
  6.  
  7. 可选值:0、1、2。 
  8.  
  9. 0, 表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。 
  10.  
  11. 1, 表示内核允许分配所有的物理内存,而不管当前的内存状态如何。 
  12.  
  13. 2, 表示内核允许分配超过所有物理内存和交换空间总和的内存 
  14.  
  15. 注意:redis在dump数据的时候,会fork出一个子进程,理论上child进程所占用的内存和parent是一样的,比如parent占用 的内存为8G,这个时候也要同样分配8G的内存给child,如果内存无法负担,往往会造成redis服务器的down机或者IO负载过高,效率下降。所 以这里比较优化的内存分配策略应该设置为 1(表示内核允许分配所有的物理内存,而不管当前的内存状态如何)。 
  16.  
  17. 这里又涉及到Overcommit和OOM。 
  18.  
  19. 什么是Overcommit和OOM 
  20.  
  21. 在Unix中,当一个用户进程使用malloc()函数申请内存时,假如返回值是NULL,则这个进程知道当前没有可用内存空间,就会做相应的处理工作。许多进程会打印错误信息并退出。 
  22.  
  23. Linux使用另外一种处理方式,它对大部分申请内存的请求都回复"yes",以便能跑更多更大的程序。因为申请内存后,并不会马上使用内存。这种技术叫做Overcommit。 
  24.  
  25. 当内存不足时,会发生OOM killer(OOM=out-of-memory)。它会选择杀死一些进程(用户态进程,不是内核线程),以便释放内存。 
  26.  
  27. Overcommit的策略 
  28.  
  29. Linux下overcommit有三种策略(Documentation/vm/overcommit-accounting): 
  30.  
  31. 0. 启发式策略。合理的overcommit会被接受,不合理的overcommit会被拒绝。 
  32.  
  33. 1. 任何overcommit都会被接受。 
  34.  
  35. 2. 当系统分配的内存超过swap+N%*物理RAM(N%由vm.overcommit_ratio决定)时,会拒绝commit。 
  36.  
  37. overcommit的策略通过vm.overcommit_memory设置。 
  38.  
  39. overcommit的百分比由vm.overcommit_ratio设置。 
  40.  
  41. # echo 2 > /proc/sys/vm/overcommit_memory 
  42.  
  43. # echo 80 > /proc/sys/vm/overcommit_ratio 
  44.  
  45. 当oom-killer发生时,linux会选择杀死哪些进程 
  46.  
  47. 选择进程的函数是oom_badness函数(在mm/oom_kill.c中),该函数会计算每个进程的点数(0~1000)。 
  48.  
  49. 点数越高,这个进程越有可能被杀死。 
  50.  
  51. 每个进程的点数跟oom_score_adj有关,而且oom_score_adj可以被设置(-1000最低,1000最高)。 

错误2:

WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.


 
 
  1. echo 511 > /proc/sys/net/core/somaxconn 

错误3:

16433:X 12 Jun 14:52:37.734 * Increased maximum number of open files to 10032 (it was originally set to 1024).


 
 
  1. 新装的linux默认只有1024,当负载较大时,会经常出现error: too many open files 
  2.  
  3. ulimit -a:使用可以查看当前系统的所有限制值 
  4.  
  5. vim /etc/security/limits.conf 
  6.  
  7. 在文件的末尾加上 
  8.  
  9. * soft nofile 65535 
  10.  
  11. * hard nofile 65535 
  12.  
  13. 执行su或者重新关闭连接用户再执行ulimit -a就可以查看修改后的结果。 

故障切换机制

1. 启动群集后,群集程序默认会在从库的redis文件中加入连接主的配置


 
 
  1. # Generated by CONFIG REWRITE 
  2.  
  3. slaveof 192.168.137.40 6379 

2.启动群集之后,群集程序默认会在主从的sentinel.conf文件中加入群集信息

主:


 
 
  1. port 26379 
  2.  
  3. dir "/usr/local/redis-6379" 
  4.  
  5. # 守护进程模式 
  6.  
  7. daemonize yes 
  8.  
  9. # 指明日志文件名 
  10.  
  11. logfile "./sentinel.log" 
  12.  
  13. sentinel monitor mymaster 192.168.137.40 6379 1 
  14.  
  15. sentinel down-after-milliseconds mymaster 5000 
  16.  
  17. sentinel failover-timeout mymaster 18000 
  18.  
  19. sentinel auth-pass mymaster 123456 
  20.  
  21. # Generated by CONFIG REWRITE 
  22.  
  23. sentinel config-epoch mymaster 0 
  24.  
  25. sentinel leader-epoch mymaster 1 
  26.  
  27. sentinel known-slave mymaster 192.168.137.40 6380 
  28.  
  29. sentinel known-sentinel mymaster 192.168.137.40 26380 c77c5f64aaad0137a228875e531c7127ceeb5c3f 
  30.  
  31. sentinel current-epoch 1 

从:


 
 
  1. #sentinel端口 
  2.  
  3. port 26380 
  4.  
  5. #工作路径 
  6.  
  7. dir "/usr/local/redis-6380" 
  8.  
  9. # 守护进程模式 
  10.  
  11. daemonize yes 
  12.  
  13. # 指明日志文件名 
  14.  
  15. logfile "./sentinel.log" 
  16.  
  17. #哨兵监控的master,主从配置一样,在进行主从切换时6379会变成当前的master端口, 
  18.  
  19. sentinel monitor mymaster 192.168.137.40 6379 1 
  20.  
  21. # master或slave多长时间(默认30秒)不能使用后标记为s_down状态。 
  22.  
  23. sentinel down-after-milliseconds mymaster 5000 
  24.  
  25. #若sentinel在该配置值内未能完成failover操作(即故障时master/slave自动切换),则认为本次failover失败。 
  26.  
  27. sentinel failover-timeout mymaster 18000 
  28.  
  29. #设置master和slaves验证密码 
  30.  
  31. sentinel auth-pass mymaster 123456 
  32.  
  33. #哨兵程序自动添加的部分 
  34.  
  35. # Generated by CONFIG REWRITE 
  36.  
  37. sentinel config-epoch mymaster 0 
  38.  
  39. sentinel leader-epoch mymaster 1 
  40.  
  41. ###指明了当前群集的从库的ip和端口,在主从切换时该值会改变 
  42.  
  43. sentinel known-slave mymaster 192.168.137.40 6380 
  44.  
  45. ###除了当前的哨兵还有哪些监控的哨兵 
  46.  
  47. sentinel known-sentinel mymaster 192.168.137.40 26379 7a88891a6147e202a53601ca16a3d438e9d55c9d 
  48.  
  49. sentinel current-epoch 1 

模拟主故障


 
 
  1. [root@monitor redis-6380]# ps -ef|grep redis 
  2.  
  3. root 4171 1 0 14:20 ? 00:00:15 /usr/local/redis-6379/src/redis-server *:6379 
  4.  
  5. root 4175 1 0 14:20 ? 00:00:15 /usr/local/redis-6380/src/redis-server *:6380 
  6.  
  7. root 4305 1 0 15:28 ? 00:00:05 /usr/local/redis-6379/src/redis-sentinel *:26379 [sentinel] 
  8.  
  9. root 4306 1 0 15:28 ? 00:00:05 /usr/local/redis-6380/src/redis-sentinel *:26380 [sentinel] 
  10.  
  11. root 4337 4144 0 15:56 pts/1 00:00:00 grep redis 
  12.  
  13. [root@monitor redis-6380]# kill -9 4171 
  14.  
  15. [root@monitor redis-6380]# ps -ef|grep redis 
  16.  
  17. root 4175 1 0 14:20 ? 00:00:15 /usr/local/redis-6380/src/redis-server *:6380 
  18.  
  19. root 4305 1 0 15:28 ? 00:00:05 /usr/local/redis-6379/src/redis-sentinel *:26379 [sentinel] 
  20.  
  21. root 4306 1 0 15:28 ? 00:00:05 /usr/local/redis-6380/src/redis-sentinel *:26380 [sentinel] 
  22.  
  23. root 4339 4144 0 15:56 pts/1 00:00:00 grep redis 
  24.  
  25. [root@monitor redis-6380]# 

从哨兵配置文件中可以看到当前的主库的已经发生了改变

从日志文件也可以看到当前的主已经从6379转换成了6380

总结

redis的哨兵端口26379、26380使用客户端软件无法连接,使用程序可以连接,客户端软件只能直接连接6379和6380端口。使用哨兵监控当主故障后会自动切换从为主,当主启动后就变成了从。


作者:pursuer.chen

来源:51CTO

相关文章
|
安全 前端开发 API
【Azure 应用服务】Azure Web App 服务默认支持一些 Weak TLS Ciphers Suite,是否有办法自定义修改呢?
【Azure 应用服务】Azure Web App 服务默认支持一些 Weak TLS Ciphers Suite,是否有办法自定义修改呢?
208 4
|
安全 API 数据安全/隐私保护
【Azure App Service】通过Visual Studio部署Azure App Service 遇见 401 'Unauthorized'错误
【Azure App Service】通过Visual Studio部署Azure App Service 遇见 401 'Unauthorized'错误
139 1
【Azure 应用服务】Web App Service 中的 应用程序配置(Application Setting) 怎么获取key vault中的值
【Azure 应用服务】Web App Service 中的 应用程序配置(Application Setting) 怎么获取key vault中的值
159 0
【Azure App Service】PowerShell脚本批量添加IP地址到Web App允许访问IP列表中
Web App取消公网访问后,只允许特定IP能访问Web App。需要写一下段PowerShell脚本,批量添加IP到Web App的允许访问IP列表里!
198 2
【Azure Web Job】Azure Web Job执行Powershell脚本报错 The term 'Select-AzContext' is not recognized as the name
【Azure Web Job】Azure Web Job执行Powershell脚本报错 The term 'Select-AzContext' is not recognized as the name
102 3
|
监控 安全 Apache
构建安全的URL重定向策略:确保从Web到App平滑过渡的最佳实践
【10月更文挑战第2天】URL重定向是Web开发中常见的操作,它允许服务器根据请求的URL将用户重定向到另一个URL。然而,如果重定向过程没有得到妥善处理,可能会导致安全漏洞,如开放重定向攻击。因此,确保重定向过程的安全性至关重要。
558 0
|
关系型数据库 MySQL Linux
【Azure 应用服务】在创建Web App Service的时候,选Linux系统后无法使用Mysql in App
【Azure 应用服务】在创建Web App Service的时候,选Linux系统后无法使用Mysql in App
110 0
【Azure 应用服务】在创建Web App Service的时候,选Linux系统后无法使用Mysql in App
|
Python
【Azure 应用服务】如何为Web Jobs 安装Python包呢?
【Azure 应用服务】如何为Web Jobs 安装Python包呢?
【Azure 应用服务】如何为Web Jobs 安装Python包呢?
|
开发框架 .NET 开发工具
visualstudio如何加入工作插件---Web developer工具
visualstudio如何加入工作插件---Web developer工具
200 0
|
Shell PHP Windows
【Azure App Service】Web Job 报错 UNC paths are not supported. Defaulting to Windows directory.
【Azure App Service】Web Job 报错 UNC paths are not supported. Defaulting to Windows directory.
150 0