Centos7+Nginx+Keepalived实现Apache服务的高可用&负载均衡

前端之家收集整理的这篇文章主要介绍了Centos7+Nginx+Keepalived实现Apache服务的高可用&负载均衡前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Centos7+Nginx+Keepalived实现Apache服务的高可用&负载均衡

今天是2017年的第一天,昨天也就是2016年的最后一天,我尝试部署了Centos7+Nginx+Keepalived实现WEB服务的高可用负载均衡服务,终于在2017年的第一天前完成了,所以在此分享给有需要的朋友;说到负载均衡,其实在linux下有很多服务可以实现,比如Nginx、haproxy、lvs等服务,当前我们在前面的文章有介绍过了,但是对于高可用服务,我们在linux下最常见也是应用最多的是Keepalived,对于这些综合服务对于一个管理员来说是必须要会的,所以今天我们主要介绍的是Centos7+Nginx+Keepalived实现WEB(Apache)服务的高可用负载均衡。具体见下:

环境介绍:

Hostname:A-S

IP:

Role:Apache Web Service

Hostname:B-S

IP:

Role:Apache Web Service

Hostname:BB-S

IP:

Role:Nginx+Keepalived

Hostname:BB-S

IP:

Role:Nginx+Keepalived

Virtual IP:

我们需要首先安装两台Apache Web服务

因为我们准备使用yum安装服务,所以需要定义阿里云仓库

  1. cd/etc/
  2. vim
  3. 添加以下内容
  4. [epel]name=aliyunepelbaseurl=/epel/7Server/x86_64/
  5. gpgcheck=0

首先在上安装apache

  1. yumisntall-yhttpd

然后定义显示页面

  1. vim/var/www/html/
  2. </html>
  3. <!DOCTYPEhtml>
  4. <html>
  5. <head>
  6. <title>WelcometoApache</title>
  7. <style>
  8. body{
  9. 35em;
  10. margin:0auto;
  11. font-family:Tahoma,Verdana,Arial,sans-serif;
  12. }
  13. </style>
  14. <styletype="text/css">
  15. h1{color:red}
  16. h2{color:blue}
  17. h3{color:green}
  18. h4{color:yellow}
  19. }
  20. </style>
  21. </head><bodybgcolor='#46A3FF'>
  22. <h1>WelcometoA-SApache</h1>
  23. <h2>HostName:A-S</h2>
  24. <h3>IP:</h3>
  25. <h4>Service:Apache</h4>
  26. <inputtype=buttonvalue="Refresh"onclick="('')">
  27. </body>
  28. </html>
  1. Systemctlstarthttpd

然后添加默认的防火墙端口8o

  1. Firewall-cmd--zone=public--add-port='80/tcp'--permanent
  1. 或者vim/etc/firewalld/zone/
  2. 添加一下格式
  3. <portportocal='tcp'port='80'>

我们测试访问

我们按照同上的方法在第二台服务器上进行配置,安装跳过

第二台主机的配置: 主机名 B-S

安装好httpd后,我们将a-s上的index拷贝到b-s服务器上

  1. scproot@:/var/www/html/

然后修改文件

  1. </html>
  2. <!DOCTYPEhtml>
  3. <html>
  4. <head>
  5. <title>WelcometoApache</title>
  6. <style>
  7. body{
  8. 35em;
  9. margin:0auto;
  10. font-family:Tahoma,sans-serif;
  11. }
  12. </style>
  13. <styletype="text/css">
  14. h1{color:red}
  15. h2{color:blue}
  16. h3{color:green}
  17. h4{color:yellow}
  18. }
  19. </style>
  20. </head><bodybgcolor='#CA8EFF'>
  21. <h1>WelcometoB-SApache</h1>
  22. <h2>HostName:B-S</h2>
  23. <h3>IP:</h3>
  24. <h4>Service:Apache</h4>
  25. <inputtype=buttonvalue="Refresh"onclick="('')">
  26. </body>
  27. </html>

测试访问

接下来我们开始准备在两台前端服务器、上安装Nginx和keepalived;我们将keepalived和Nginx安装在同一台机器上

我们首先在主备服务器上安装Nginx

我们首先要定于安装源, 才可以使用yum安装,我们在主备服务器上都配置

  1. cd/etc/
  2. vim
  3. 添加以下内容
  4. [epel]name=aliyunepelbaseurl=/epel/7Server/x86_64/
  5. gpgcheck=0

定义好后,我们将定义的安装源给另外一台备服务器也拷贝一份

  1. scp/etc//root@/etc//

指定源后,我们就可以开始安装Nginx了,首先在主服务器上

  1. yuminstall-yNginx

安装后,我们首先查看默认的Nginx配置

  1. vim/etc/Nginx/
  2. #Formoreinformationonconfiguration,see:
  3. #*OfficialEnglishDocumentation:/en/docs/
  4. #*OfficialRussianDocumentation:/ru/docs/
  5. userNginx;
  6. worker_processesauto;
  7. error_log/var/log/Nginx/;
  8. pid/run/;
  9. #Loaddynamicmodules.See/usr/share/Nginx/.
  10. include/usr/share/Nginx/modules/*.conf;
  11. events{
  12. worker_connections1024;
  13. }
  14. http{
  15. log_formatmain'$remote_addr-$remote_user[$time_local]"$request"'
  16. '$status$body_bytes_sent"$http_referer"'
  17. '"$http_user_agent""$http_x_forwarded_for"';
  18. access_log/var/log/Nginx/main;
  19. sendfileon;
  20. tcp_nopushon;
  21. tcp_nodelayon;
  22. keepalive_timeout65;
  23. types_hash_max_size2048;
  24. include/etc/Nginx/;
  25. default_typeapplication/octet-stream;
  26. #Loadmodularconfigurationfilesfromthe/etc/Nginx/directory.
  27. #See/en/docs/ngx_core_#include
  28. #formoreinformation.
  29. include/etc/Nginx//*.conf;
  30. server{
  31. listen80default_server;
  32. listen[::]:80default_server;
  33. server_name_;
  34. root/usr/share/Nginx/html;
  35. #Loadconfigurationfilesforthedefaultserverblock.
  36. include/etc/Nginx//*.conf;
  37. location/{
  38. }
  39. error_page404/;
  40. location=/{
  41. }
  42. error_page500502503504/;
  43. location=/{
  44. }
  45. }
  46. #SettingsforaTLSenabledserver.
  47. #
  48. #server{
  49. #listen443sslhttp2default_server;
  50. #listen[::]:443sslhttp2default_server;
  51. #server_name_;
  52. #root/usr/share/Nginx/html;
  53. #
  54. #ssl_certificate"/etc/pki/Nginx/";
  55. #ssl_certificate_key"/etc/pki/Nginx/private/";
  56. #ssl_session_cacheshared:SSL:1m;
  57. #ssl_session_timeout10m;
  58. #ssl_ciphersHIGH:!aNULL:!MD5;
  59. #ssl_prefer_server_cipherson;
  60. #
  61. ##Loadconfigurationfilesforthedefaultserverblock.
  62. #include/etc/Nginx//*.conf;
  63. #
  64. #location/{
  65. #}
  66. #
  67. #error_page404/;
  68. #location=/{
  69. #}
  70. #
  71. #error_page500502503504/;
  72. #location=/{
  73. #}
  74. #}
  75. }

默认配置我们备份一份

  1. cp/etc/Nginx//etc/Nginx/

接下来我们要配置Nginx实现后端服务器的web的负载均衡;

在此我们使用的是Nginx负载均衡的默认方式-轮询

我们需要在http区域里面添加负载配置

  1. upstreamreal_server_pool{
  2. #ip_hash;
  3. server:80weight=1max_fails=2fail_timeout=30s;
  4. server:80weight=1max_fails=2fail_timeout=30s;
  5. }
  6. #添加一组真实的服务器地址池
  7. #供proxy_pass和fastcgi_pass指令中使用的代理服务器
  8. #后台如果有动态应用的时候,ip_hash指令可以通过hash算法
  9. #将客户端请求定位到同一台后端服务器上,解决session共享,#但建议用动态应用做session共享
  10. #server用于指定一个后端服务器的名称和参数
  11. #weight代表权,重默认为1,权重越高被分配的客户端越多
  12. #max_fails指定时间内对后端请求失败的次数
  13. #fail_timeout达到max_fails指定的失败次数后暂停的时间
  14. #down参数用来标记为离线,不参与负载均衡.在ip_hash下使用
  15. #backup仅仅在非backup服务器宕机或繁忙的时候使用
  16. server{
  17. listen:80;#监听ip改为本地ip
  18. server_namelocalhost;
  19. #charsetkoi8-r;
  20. #access_loglogs/main;
  21. location/{
  22. #roothtml;
  23. #index;
  24. proxy_next_upstreamhttp_502http_504errortimeoutinvalid_header;
  25. proxy_passreal_server_pool;
  26. proxy_set_headerHost$host;
  27. proxy_set_headerX-Forwarded-For$remote_addr;
  28. }

添加后的

  1. #Formoreinformationonconfiguration,see:
  2. #*OfficialEnglishDocumentation:/en/docs/
  3. #*OfficialRussianDocumentation:/ru/docs/
  4. userNginx;
  5. worker_processesauto;
  6. error_log/var/log/Nginx/;
  7. pid/run/;
  8. #Loaddynamicmodules.See/usr/share/Nginx/.
  9. include/usr/share/Nginx/modules/*.conf;
  10. events{
  11. worker_connections1024;
  12. }
  13. http{
  14. log_formatmain'$remote_addr-$remote_user[$time_local]"$request"'
  15. '$status$body_bytes_sent"$http_referer"'
  16. '"$http_user_agent""$http_x_forwarded_for"';
  17. access_log/var/log/Nginx/main;
  18. sendfileon;
  19. tcp_nopushon;
  20. tcp_nodelayon;
  21. keepalive_timeout65;
  22. types_hash_max_size2048;
  23. include/etc/Nginx/;
  24. default_typeapplication/octet-stream;
  25. upstreamreal_server_pool{
  26. #ip_hash;
  27. server:80weight=1max_fails=2fail_timeout=30s;
  28. server:80weight=1max_fails=2fail_timeout=30s;
  29. }
  30. #添加一组真实的服务器地址池
  31. #供proxy_pass和fastcgi_pass指令中使用的代理服务器
  32. #后台如果有动态应用的时候,不参与负载均衡.在ip_hash下使用
  33. #backup仅仅在非backup服务器宕机或繁忙的时候使用
  34. #Loadmodularconfigurationfilesfromthe/etc/Nginx/directory.
  35. #See/en/docs/ngx_core_#include
  36. #formoreinformation.
  37. include/etc/Nginx//*.conf;
  38. server{
  39. #listen80default_server;
  40. #listen[::]:80default_server;
  41. listen:80;#监听ip改为本地ip
  42. server_namelocalhost;
  43. #server_name_;
  44. root/usr/share/Nginx/html;
  45. #Loadconfigurationfilesforthedefaultserverblock.
  46. include/etc/Nginx//*.conf;
  47. #增加
  48. location/{
  49. #roothtml;
  50. #index;
  51. proxy_next_upstreamhttp_502http_504errortimeoutinvalid_header;
  52. proxy_passreal_server_pool;
  53. proxy_set_headerHost$host;
  54. proxy_set_headerX-Forwarded-For$remote_addr;
  55. }
  56. error_page404/;
  57. location=/{
  58. }
  59. error_page500502503504/;
  60. location=/{
  61. }
  62. }
  63. #SettingsforaTLSenabledserver.
  64. #
  65. #server{
  66. #listen443sslhttp2default_server;
  67. #listen[::]:443sslhttp2default_server;
  68. #server_name_;
  69. #root/usr/share/Nginx/html;
  70. #
  71. #ssl_certificate"/etc/pki/Nginx/";
  72. #ssl_certificate_key"/etc/pki/Nginx/private/";
  73. #ssl_session_cacheshared:SSL:1m;
  74. #ssl_session_timeout10m;
  75. #ssl_ciphersHIGH:!aNULL:!MD5;
  76. #ssl_prefer_server_cipherson;
  77. #
  78. ##Loadconfigurationfilesforthedefaultserverblock.
  79. #include/etc/Nginx//*.conf;
  80. #
  81. #location/{
  82. #}
  83. #
  84. #error_page404/;
  85. #location=/{
  86. #}
  87. #
  88. #error_page500502503504/;
  89. #location=/{
  90. #}
  91. #}
  92. }

接着我们重启Nginx

  1. systemctlenableNginx
  2. systemctlstartNginx

接下来我们尝试访问一下,因为使用的是轮询,所以访问Nginx跳转到不同的两个页面

我们在安装keepalived前,先配置路由转发

  1. vim/etc/
  2. _forward=1#此参数改为1
  1. sysctl-p#使修改生效

接着我们在主服务器上安装keepalived

  1. yuminstall-ykeepalived

安装完成后,我们备份一下keepalived的配置文件

  1. cp/etc/keepalived//etc/keepalived/

接下来我们查看一下默认的keepalived默认配置

  1. cat/etc/keepalived/
  2. !ConfigurationFileforkeepalived
  3. global_defs{
  4. notification_email{
  5. acassen@
  6. failover@
  7. sysadmin@
  8. }
  9. notification_email_from@
  10. smtp_server
  11. smtp_connect_timeout30
  12. router_idLVS_DEVEL
  13. }
  14. vrrp_instanceVI_1{
  15. stateMASTER
  16. interfaceeth0
  17. virtual_router_id51
  18. priority100
  19. advert_int1
  20. authentication{
  21. auth_typePASS
  22. auth_pass1111
  23. }
  24. virtual_ipaddress{
  25.  
  26.  
  27.  
  28. }
  29. }
  30. virtual_server443{
  31. delay_loop6
  32. lb_algorr
  33. lb_kindNAT
  34. nat_mask
  35. persistence_timeout50
  36. protocolTCP
  37. real_server443{
  38. weight1
  39. SSL_GET{
  40. url{
  41. path/
  42. digestff20ad2481f97b1754ef3e12ecd3a9cc
  43. }
  44. url{
  45. path/mrtg/
  46. digest9b3a0c85a887a256d6939da88aabd8cd
  47. }
  48. connect_timeout3
  49. nb_get_retry3
  50. delay_before_retry3
  51. }
  52. }
  53. }
  54. virtual_server1358{
  55. delay_loop6
  56. lb_algorr
  57. lb_kindNAT
  58. persistence_timeout50
  59. protocolTCP
  60. sorry_server1358
  61. real_server1358{
  62. weight1
  63. HTTP_GET{
  64. url{
  65. path/testurl/
  66. digest640205b7b0fc66c1ea91c463fac6334d
  67. }
  68. url{
  69. path/testurl2/
  70. digest640205b7b0fc66c1ea91c463fac6334d
  71. }
  72. url{
  73. path/testurl3/
  74. digest640205b7b0fc66c1ea91c463fac6334d
  75. }
  76. connect_timeout3
  77. nb_get_retry3
  78. delay_before_retry3
  79. }
  80. }
  81. real_server1358{
  82. weight1
  83. HTTP_GET{
  84. url{
  85. path/testurl/
  86. digest640205b7b0fc66c1ea91c463fac6334c
  87. }
  88. url{
  89. path/testurl2/
  90. digest640205b7b0fc66c1ea91c463fac6334c
  91. }
  92. connect_timeout3
  93. nb_get_retry3
  94. delay_before_retry3
  95. }
  96. }
  97. }
  98. virtual_server1358{
  99. delay_loop3
  100. lb_algorr
  101. lb_kindNAT
  102. nat_mask
  103. persistence_timeout50
  104. protocolTCP
  105. real_server1358{
  106. weight1
  107. HTTP_GET{
  108. url{
  109. path/testurl/
  110. digest640205b7b0fc66c1ea91c463fac6334d
  111. }
  112. url{
  113. path/testurl2/
  114. digest640205b7b0fc66c1ea91c463fac6334d
  115. }
  116. url{
  117. path/testurl3/
  118. digest640205b7b0fc66c1ea91c463fac6334d
  119. }
  120. connect_timeout3
  121. nb_get_retry3
  122. delay_before_retry3
  123. }
  124. }
  125. real_server1358{
  126. weight1
  127. HTTP_GET{
  128. url{
  129. path/testurl/
  130. digest640205b7b0fc66c1ea91c463fac6334d
  131. }
  132. url{
  133. path/testurl2/
  134. digest640205b7b0fc66c1ea91c463fac6334d
  135. }
  136. url{
  137. path/testurl3/
  138. digest640205b7b0fc66c1ea91c463fac6334d
  139. }
  140. connect_timeout3
  141. nb_get_retry3
  142. delay_before_retry3
  143. }
  144. }
  145. }

接下来我们修改默认的keepalived配置

  1. echo>/etc/keepalived/

我们清空默认配置后,添加以下内容

  1. vim/etc/keepalived/
  2. global_defs{
  3. notification_email{
  4. gavin@#定义通知邮箱,有多个可以换行添加
  5. }
  6. notification_email_fromadmin@#定义发送邮件的邮箱
  7. smtp_server#定义发件服务器
  8. smtp_connect_timeout30#定义连接smtp服务器超时时间
  9. router_idLVS_DEVEL
  10. }
  11. vrrp_instanceVI_1{
  12. stateMASTER#标示主备,备机上改为BACKUP
  13. interfaceens160#HA监测的端口
  14. virtual_router_id51#主备的virtual_router_id的值必须相同
  15. priority100#优先级,通常主要比备稍大
  16. advert_int1#VRRPMulticast广播周期秒数
  17. authentication{#定义认证
  18. auth_typePASS#认证方式
  19. auth_pass1111#认证口令字
  20. }
  21. virtual_ipaddress{#定义vip
  22. #多个可换行添加,一行一个
  23. }
  24. }
  25. virtual_server80{
  26. delay_loop6#每隔6秒查询realserver状态
  27. lb_algorr
  28. lb_kindNAT
  29. nat_mask
  30. persistence_timeout50#同一IP的连接50秒内被分配到同一台realserver
  31. protocolTCP#用TCP监测realserver的状态
  32. real_server80{#需要指向Nginx负载的地址
  33. weight3#权重
  34. TCP_CHECK{
  35. connect_timeout10#10秒无响应超时
  36. nb_get_retry3
  37. delay_before_retry3
  38. connect_port80
  39. }
  40. }
  41. real_server80{#需要指向Nginx负载的地址
  42. weight3
  43. TCP_CHECK{
  44. connect_timeout3
  45. delay_before_retry3
  46. connect_port80
  47. }
  48. }
  49. }

我们介绍一下主要参数

  1. global_defs
  2. notification_emailkeepalived在发生诸如切换操作时需要发送email通知地址,后面的smtp_server相比也都知道是邮件服务器地址。也可以通过其它方式报警,毕竟邮件不是实时通知的。
  3. router_id:机器标识,通常可设为hostname。故障发生时,邮件通知会用到
  4. vrrp_instance
  5. state:指定instance(Initial)的初始状态,就是说在配置好后,这台服务器的初始状态就是这里指定的,但这里指定的不算,还是得要通过竞选通过优先级来确定。如果这里设置为MASTER,但如若他的优先级不及另外一台,那么这台在发送通告时,会发送自己的优先级,另外一台发现优先级不如自己的高,那么他会就回抢占为MASTER
  6. interface:实例绑定的网卡,因为在配置虚拟IP的时候必须是在已有的网卡上添加
  7. mcast_src_ip:发送多播数据包时的源IP地址,这里注意了,这里实际上就是在那个地址上发送VRRP通告,这个非常重要,一定要选择稳定的网卡端口来发送,这里相当于heartbeat的心跳端口,如果没有设置那么就用默认的绑定的网卡的IP,也就是interface指定的IP地址
  8. virtual_router_id:这里设置VRID,这里非常重要,相同的VRID为一个组,他将决定多播的MAC地址
  9. priority:设置本节点的优先级,优先级高的为master
  10. advert_int:检查间隔,默认为1秒。这就是VRRP的定时器,MASTER每隔这样一个时间间隔,就会发送一个advertisement报文以通知组内其他路由器自己工作正常
  11. authentication:定义认证方式和密码,主从必须一样
  12. virtual_ipaddress:这里设置的就是VIP,也就是虚拟IP地址,他随着state的变化而增加删除,当statemaster的时候就添加,当statebackup的时候删除,这里主要是有优先级来决定的,和state设置的值没有多大关系,这里可以设置多个IP地址
  13. track_script:引用VRRP脚本,即在vrrp_script部分指定的名字。定期运行它们来改变优先级,并最终引发主备切换。
  14. vrrp_script
  15. 告诉keepalived在什么情况下切换,所以尤为重要。可以有多个vrrp_script
  16. script:自己写的检测脚本。也可以是一行命令如killall-0Nginx
  17. interval2:每2s检测一次
  18. weight-5:检测失败(脚本返回非0)则优先级-5
  19. fall2:检测连续2次失败才算确定是真失败。会用weight减少优先级(1-255之间)
  20. rise1:检测1次成功就算成功。但不修改优先级

保存退出后,我们启动keepalived

  1. systemctlstartkeepalived
  2. systemctlenablekeepalived

然后我们测试ping一下vistual ip

注:如果ping不通vistual ip的话,有可能是文件的中的网卡配置,centos6的网卡是eth0,而centos7的是 ens160

ping

接下来我们配置第二台备服务器,然后首先安装及配置Nginx

  1. yuminstall-yNginx

然后备份备服务器的文件

  1. cp/etc/Nginx//etc/Nginx/

然后我们从主服务器下的配置拷贝到备服务器Nginx目录下,然后替换默认的文件

  1. scp/etc/Nginx/root@:/etc/Nginx/

然后我们修改备服务器的监听地址,需要指向备服务器的本地地址---> listen :80; # 监听ip改为本地ip

  1. #Formoreinformationonconfiguration,see:
  2. #*OfficialEnglishDocumentation:/en/docs/
  3. #Formoreinformationonconfiguration,不参与负载均衡.在ip_hash下使用
  4. #backup仅仅在非backup服务器宕机或繁忙的时候使用
  5. #Loadmodularconfigurationfilesfromthe/etc/Nginx/directory.
  6. #See/en/docs/ngx_core_#include
  7. #formoreinformation.
  8. include/etc/Nginx//*.conf;
  9. server{
  10. #listen80default_server;
  11. #listen[::]:80default_server;
  12. listen:80;#监听ip改为本地ip
  13. server_namelocalhost;
  14. #server_name_;
  15. root/usr/share/Nginx/html;
  16. #Loadconfigurationfilesforthedefaultserverblock.
  17. include/etc/Nginx//*.conf;
  18. location/{
  19. #roothtml;
  20. #index;
  21. proxy_next_upstreamhttp_502http_504errortimeoutinvalid_header;
  22. proxy_passreal_server_pool;
  23. proxy_set_headerHost$host;
  24. proxy_set_headerX-Forwarded-For$remote_addr;
  25. }
  26. error_page404/;
  27. location=/{
  28. }
  29. error_page500502503504/;
  30. location=/{
  31. }
  32. }
  33. #SettingsforaTLSenabledserver.
  34. #
  35. #server{
  36. #listen443sslhttp2default_server;
  37. #listen[::]:443sslhttp2default_server;
  38. #server_name_;
  39. #root/usr/share/Nginx/html;
  40. #
  41. #ssl_certificate"/etc/pki/Nginx/";
  42. #ssl_certificate_key"/etc/pki/Nginx/private/";
  43. #ssl_session_cacheshared:SSL:1m;
  44. #ssl_session_timeout10m;
  45. #ssl_ciphersHIGH:!aNULL:!MD5;
  46. #ssl_prefer_server_cipherson;
  47. #
  48. ##Loadconfigurationfilesforthedefaultserverblock.
  49. #include/etc/Nginx//*.conf;
  50. #
  51. #location/{
  52. #}
  53. #
  54. #error_page404/;
  55. #location=/{
  56. #}
  57. #
  58. #error_page500502503504/;
  59. #location=/{
  60. #}
  61. #}
  62. }

其实我们也可以只修改添加内容

  1. upstreamreal_server_pool{
  2. #ip_hash;
  3. server:80weight=1max_fails=2fail_timeout=30s;
  4. server:80weight=1max_fails=2fail_timeout=30s;
  5. }
  6. #添加一组真实的服务器地址池
  7. #供proxy_pass和fastcgi_pass指令中使用的代理服务器
  8. #后台如果有动态应用的时候,不参与负载均衡.在ip_hash下使用
  9. #backup仅仅在非backup服务器宕机或繁忙的时候使用
  10. server{
  11. listen:80;#监听ip改为本地ip
  12. server_namelocalhost;
  13. #charsetkoi8-r;
  14. #access_loglogs/main;
  15. location/{
  16. #roothtml;
  17. #index;
  18. proxy_next_upstreamhttp_502http_504errortimeoutinvalid_header;
  19. proxy_passreal_server_pool;
  20. proxy_set_headerHost$host;
  21. proxy_set_headerX-Forwarded-For$remote_addr;
  22. }

然后我们启动备服务器的Nginx服务

  1. systemctlstartNginx
  2. systemctlenableNginx

我们在安装keepalived前,先配置路由转发

  1. vim/etc/
  2. _forward=1#此参数改为1
  1. sysctl-p#使修改生效

然后安装keepalived

然后我们备份默认keepalived配置

  1. cp/etc/keepalived//etc/keepalived/

然后修改拷贝的数据文件

  1. scp/etc/keepalived/root@:/etc/keepalived/

配置备用调度器的keepalived,只需要将state MASTER 改为state BACKUP,降低priority 100 的值:

  1. vim/etc/keepalived/
  2. global_defs{
  3. notification_email{
  4. gavin@#定义通知邮箱,有多个可以换行添加
  5. }
  6. notification_email_fromadmin@#定义发送邮件的邮箱
  7. smtp_server#定义发件服务器
  8. smtp_connect_timeout30#定义连接smtp服务器超时时间
  9. router_idLVS_DEVEL
  10. }
  11. vrrp_instanceVI_1{
  12. stateBACKUP#标示主备,备机上改为BACKUP
  13. interfaceens160#HA监测的端口
  14. virtual_router_id51#主备的virtual_router_id的值必须相同
  15. priority50#优先级,一行一个
  16. }
  17. }
  18. virtual_server80{
  19. delay_loop6#每隔6秒查询realserver状态
  20. lb_algorr
  21. lb_kindNAT
  22. nat_mask
  23. persistence_timeout50#同一IP的连接50秒内被分配到同一台realserver
  24. protocolTCP#用TCP监测realserver的状态
  25. real_server80{#需要指向Nginx负载的地址
  26. weight3#权重
  27. TCP_CHECK{
  28. connect_timeout10#10秒无响应超时
  29. nb_get_retry3
  30. delay_before_retry3
  31. connect_port80
  32. }
  33. }
  34. real_server80{#需要指向Nginx负载的地址
  35. weight3
  36. TCP_CHECK{
  37. connect_timeout3
  38. delay_before_retry3
  39. connect_port80
  40. }
  41. }
  42. }

我们介绍一下主要参数

  1. global_defs
  2. notification_emailkeepalived在发生诸如切换操作时需要发送email通知地址,后面的smtp_server相比也都知道是邮件服务器地址。也可以通过其它方式报警,毕竟邮件不是实时通知的。
  3. router_id:机器标识,通常可设为hostname。故障发生时,邮件通知会用到
  4. vrrp_instance
  5. state:指定instance(Initial)的初始状态,就是说在配置好后,这台服务器的初始状态就是这里指定的,但这里指定的不算,还是得要通过竞选通过优先级来确定。如果这里设置为MASTER,但如若他的优先级不及另外一台,那么这台在发送通告时,会发送自己的优先级,另外一台发现优先级不如自己的高,那么他会就回抢占为MASTER
  6. interface:实例绑定的网卡,因为在配置虚拟IP的时候必须是在已有的网卡上添加
  7. mcast_src_ip:发送多播数据包时的源IP地址,这里注意了,这里实际上就是在那个地址上发送VRRP通告,这个非常重要,一定要选择稳定的网卡端口来发送,这里相当于heartbeat的心跳端口,如果没有设置那么就用默认的绑定的网卡的IP,也就是interface指定的IP地址
  8. virtual_router_id:这里设置VRID,这里非常重要,相同的VRID为一个组,他将决定多播的MAC地址
  9. priority:设置本节点的优先级,优先级高的为master
  10. advert_int:检查间隔,默认为1秒。这就是VRRP的定时器,MASTER每隔这样一个时间间隔,就会发送一个advertisement报文以通知组内其他路由器自己工作正常
  11. authentication:定义认证方式和密码,主从必须一样
  12. virtual_ipaddress:这里设置的就是VIP,也就是虚拟IP地址,他随着state的变化而增加删除,当statemaster的时候就添加,当statebackup的时候删除,这里主要是有优先级来决定的,和state设置的值没有多大关系,这里可以设置多个IP地址
  13. track_script:引用VRRP脚本,即在vrrp_script部分指定的名字。定期运行它们来改变优先级,并最终引发主备切换。
  14. vrrp_script
  15. 告诉keepalived在什么情况下切换,所以尤为重要。可以有多个vrrp_script
  16. script:自己写的检测脚本。也可以是一行命令如killall-0Nginx
  17. interval2:每2s检测一次
  18. weight-5:检测失败(脚本返回非0)则优先级-5
  19. fall2:检测连续2次失败才算确定是真失败。会用weight减少优先级(1-255之间)
  20. rise1:检测1次成功就算成功。但不修改优先级

记得修改防火墙配置哦

  1. firewall-cmd--add-port='tcp/80'--permanent

然后我们启动 keepalived服务

  1. systemctlstartkeepalived
  2. systemcltlenablekeepalived

我们也测试ping一下vistual ip:

ping

接下来我们要实现realserver服务器配置

我们需要在两台web(http),,需要在这两台服务器上配置虚拟VIP,所以在服务器上执行以下脚本

首先在服务器上新建脚本文件

  1. vimreal_server
  2. 然后添加一下脚本内容
  3. #!/bin/bash
  4. #chkconfig:23458535
  5. #Description:Startrealserverwithhostboot
  6. VIP=
  7. functionstart(){
  8. ifconfiglo:0$VIPnetmaskbroadcast$VIP
  9. echo1>/proc/sys/net/ipv4/conf/lo/arp_ignore
  10. echo2>/proc/sys/net/ipv4/conf/lo/arp_announce
  11. echo1>/proc/sys/net/ipv4/conf/all/arp_ignore
  12. echo2>/proc/sys/net/ipv4/conf/all/arp_announce
  13. echoRealServer$(uname-n)started
  14. }
  15. functionstop(){
  16. ifconfiglo:0down
  17. ifconfiglo:0$VIPnetmaskbroadcast$VIP
  18. echo0>/proc/sys/net/ipv4/conf/lo/arp_ignore
  19. echo0>/proc/sys/net/ipv4/conf/lo/arp_announce
  20. echo0>/proc/sys/net/ipv4/conf/all/arp_ignore
  21. echo0>/proc/sys/net/ipv4/conf/all/arp_announce
  22. echoRealServer$(uname-n)stopped
  23. }
  24. case$1in
  25. start)
  26. start
  27. ;;
  28. stop)
  29. stop
  30. ;;
  31. *)
  32. echoUsage:$0{start|stop}”
  33. exit1
  34. esac

保存退出

然后赋予执行权限

  1. chmoea+xrealserver

然后将脚本拷贝到第二台web服务器上:

  1. scp/DATA/real_serverroot@/DATA/

我们在第二台服务器上查看

最后我们说一下如何将realserver的脚本添加到随系统启动

我们将使用chkconfig --add 来管理服务器的添加、顺序

  1. cprealserver/etc//将脚本拷贝到指定目录
  2. chkconfig--addrealserver添加realserver脚本到自动启动
  3. chkconfig--list查看自动启动服务
  1. chkconfig--list查看自动启动服务

通过以上配置后,我们就可以通过服务进行操作了

  1. /etc//realserverstop
  2. /etc//realserverstart
  1. chkconfigrealserveron设置为自动启动

然后我们在第二台web服务器上做同样的操作

  1. cprealserver/etc//将脚本拷贝到指定目录
  2. chkconfig--addrealserver添加realserver脚本到自动启动
  3. chkconfig--list查看自动启动服务
  1. chkconfig--list查看自动启动服务

通过以上配置后,我们就可以通过服务进行操作了

  1. /etc//realserverstop
  2. /etc//realserverstart
  1. chkconfigrealserveron设置为自动启动

接下来我们就是测试了,我们在两台realserver上执行查看ip状态

  1. a-s
  2. ipash
  1. b-s
  2. ipash

最后我们在keelalived服务器上查看ip状态,从状态上看,我们的keepalived是监听在备服务器上的,如果将备服务器keepalibed停止后,会切换到主服务器上的

  1. aa-s主服务器
  2. ipash

备服务器:

  1. bb-s备服务器
  2. ipash

最后我们就是测试了

我们访问vistual ip进行测试

我们查看log

  1. tail-f/var/log/message

我们停止主服务器的keepalived服务

查看备服务器的log

最后我们说一下keepalived的log;Keepalived默认所有的日志都是写入到/var/log/message下的,由于message的日志太多了,而Keepalived的日志又很难分离出来,所以本文提供了一个调整Keepalived日志输出路径的方法
具体操作步骤如下:
一、修改 /etc/sysconfig/keepalived

默认配置

  1. vim/etc/sysconfig/keepalived
  1. vim/etc/sysconfig/keepalived
  2. KEEPALIVED_OPTIONS="-D"修改KEEPALIVED_OPTIONS="-D-d-S0"
  3. #其中-S指定syslog的facility


  1. 接下来设置syslog
  2. vim/etc/
  3. 注:Cnetos6的路劲是/etc/
  4. Centos7的路劲是/etc/
  5. 添加以下配置
  6. #keepalived-S0
  7. local0.*/var/log/

保存退出后,重启服务

  1. systemctlrestartrsyslog
  2. systemctlrestartkeepalived

接下来我们查看/var/log路劲是否生成文件

  1. tail-f/var/log/

猜你在找的CentOS相关文章