• 打赏 jQuery火箭图标返回顶部代码</h1> <hr> <span class="pre-scrollable"><div id="cnblogs_post_body" class="blogpost-body"> <h2><span style="font-family: 'Microsoft YaHei'; font-size: 14px;">Linux集群从功能分类</span></h2> <blockquote><ol> <li><span style="font-family: 'Microsoft YaHei'; font-size: 14px;">高可用集群,高可用集群通常为俩台服务器,一台工作,另一台冗余,当提供服务器的服务器宕机时候,冗余服务器将接替宕机的服务器继续提供服务。实现高可用的集群开源软件有Heatbeat和Keepalived。</span></li> <li><span style="font-family: 'Microsoft YaHei'; font-size: 14px;">负载均衡集群,需要有一台服务器作为分发器,它负载把用户的请求分发给后端的服务器处理,在负载均衡集群中,除分发器外,就是给用户提供服务的服务器了,这些服务器的数量至少是俩台。实现负载均衡的开源软件有很多,如:LVS,keepalived,Haproxy,Nginx,商业版有F5和Netscaler,商业版的优势在于并发量高,拥有很好的稳定性。</span></li> </ol></blockquote> <h2><span style="font-family: 'Microsoft YaHei'; font-size: 14px;">Keepalived的VRRP协议</span></h2> <blockquote> <p>  Keepalived是通过VRRP协议来实现高可用的,VRRP协议会将多台功能相同的路由器组成一个组,这个组里有一个master角色和N(N>1)个backup角色,master会通过组播的组播的形式向各个backup发送VRRP协议的数据包,当backup不会受到master发来的VRRP数据包时,就会认为master宕机了,就会根据backup的优先级来决定谁是master。</p> <p>  Keepalived的有三个模块,core,check和VRRP,core模块是keepalived的核心,负责主进程的启动,维护,以及全局配置文件的加载和解析,check模块负责健康检查,VRRP模块用来实现VRRP协议。</p> </blockquote> <h2><span style="font-family: 'Microsoft YaHei'; font-size: 14px;">集群准备,哈哈,来战</span></h2> <blockquote> <p><span style="font-family: 'Microsoft YaHei'; font-size: 14px;">  准备俩台虚拟机,ip为192.168.31.132的主机S-32作为master,ip为192.168.31.139的主机c-39作为backup,俩台虚拟主机同时安装keepalived和nginx。</span></p> </blockquote> <div class="cnblogs_Highlighter"> <pre class="brush:csharp;gutter:false;">yum install keepalived nginx -y systemctl stop firewalld systemctl enable firewalld </pre> </div> <h4><span style="font-family: 'Microsoft YaHei'; font-size: 14px;">设置Keepalived主服务器</span></h4> <blockquote> <p><span style="font-family: 'Microsoft YaHei'; font-size: 14px;">查看keepalived的配置文件vim /etc/keepalived/keepalived.conf</span></p> </blockquote> <div class="cnblogs_code"> <pre><span style="color: #008080;user-select:none;"> 1</span> ! Configuration File <span style="color: #0000ff;">for</span><span style="color: #000000;"> keepalived </span><span style="color: #008080;user-select:none;"> 2</span> <span style="color: #008080;user-select:none;"> 3</span> <span style="color: #000000;">global_defs { #全局定义参数 </span><span style="color: #008080;user-select:none;"> 4</span> <span style="color: #000000;"> notification_email { #出问题时给邮箱发邮件 </span><span style="color: #008080;user-select:none;"> 5</span> <span style="color: #000000;"> acassen@firewall.loc </span><span style="color: #008080;user-select:none;"> 6</span> <span style="color: #000000;"> failover@firewall.loc </span><span style="color: #008080;user-select:none;"> 7</span> <span style="color: #000000;"> sysadmin@firewall.loc #邮件报警,暂时可不设置,后期zabbix统一管理 </span><span style="color: #008080;user-select:none;"> 8</span> <span style="color: #000000;"> } </span><span style="color: #008080;user-select:none;"> 9</span> <span style="color: #000000;"> notification_email_from Alexandre.Cassen@firewall.loc </span><span style="color: #008080;user-select:none;"> 10</span> smtp_server <span style="color: #800080;">192.168</span>.<span style="color: #800080;">200.1</span> <span style="color: #008080;user-select:none;"> 11</span> smtp_connect_timeout <span style="color: #800080;">30</span> <span style="color: #008080;user-select:none;"> 12</span> <span style="color: #000000;"> router_id LVS_DEVEL #此处注意router_id为负载均衡标识,在局域网内应该是唯一的。 </span><span style="color: #008080;user-select:none;"> 13</span> <span style="color: #ff0000;"> vrrp_scripts chk_nginx{ </span><span style="color: #008080;user-select:none;"> 14</span> <span style="color: #ff0000;">scripts "/scripts/check_ng.sh" #检测服务是否正常,脚本的路径 </span><span style="color: #008080;user-select:none;"> 15</span> <span style="color: #ff0000;">interval 3 #检测间断3秒 </span><span style="color: #008080;user-select:none;"> 16</span> <span style="color: #ff0000;">} </span><span style="color: #008080;user-select:none;"> 17</span> <span style="color: #000000;"> vrrp_skip_check_adv_addr </span><span style="color: #008080;user-select:none;"> 18</span> <span style="color: #000000;"> vrrp_strict </span><span style="color: #008080;user-select:none;"> 19</span> vrrp_garp_interval <span style="color: #800080;">0</span> <span style="color: #008080;user-select:none;"> 20</span> vrrp_gna_interval <span style="color: #800080;">0</span> <span style="color: #008080;user-select:none;"> 21</span> <span style="color: #000000;">} </span><span style="color: #008080;user-select:none;"> 22</span> <span style="color: #008080;user-select:none;"> 23</span> <span style="color: #000000;">vrrp_instance VI_1 { </span><span style="color: #008080;user-select:none;"> 24</span> <span style="color: #000000;"> state MASTER #状态只有MASTER和BACKUP两种,并且要大写,MASTER为工作状态,BACKUP是备用状态。 </span><span style="color: #008080;user-select:none;"> 25</span> <span style="color: #0000ff;">interface</span><span style="color: #000000;"> ens33 #通过ens33发送广播,centos6是eth0。 </span><span style="color: #008080;user-select:none;"> 26</span> <span style="color: #000000;"> lvs_sync_daemon_inteface ens33 #这个默认没有,相当于心跳线接口,DR模式用的和上面的接口一样,也可以用机器上的其他网卡eth1,用来防止脑裂。 </span><span style="color: #008080;user-select:none;"> 27</span> virtual_router_id <span style="color: #800080;">51</span><span style="color: #000000;"> #虚拟路由标识,同一个vrrp_instance的MASTER和BACKUP的vitrual_router_id 是一致的。 </span><span style="color: #008080;user-select:none;"> 28</span> priority <span style="color: #800080;">100</span><span style="color: #000000;"> #优先级,同一个vrrp_instance的MASTER优先级必须比BACKUP高。 </span><span style="color: #008080;user-select:none;"> 29</span> advert_int <span style="color: #800080;">1</span> <span style="color: #008080;user-select:none;"> 30</span> <span style="color: #000000;"> authentication { </span><span style="color: #008080;user-select:none;"> 31</span> <span style="color: #000000;"> auth_type PASS #验证authentication。包含验证类型和验证密码。类型主要有PASS、AH 两种,通常使用的类型为PASS, </span><span style="color: #008080;user-select:none;"> 32</span> auth_pass <span style="color: #800080;">1111</span><span style="color: #000000;"> #据说AH 使用时有问题。验证密码为明文,同一vrrp 实例MASTER 与BACKUP 使用相同的密码才能正常通信。 </span><span style="color: #008080;user-select:none;"> 33</span> <span style="color: #000000;"> } </span><span style="color: #008080;user-select:none;"> 34</span> <span style="color: #000000;"> virtual_ipaddress { #虚拟ip地址,可以有多个地址,每个地址占一行,不需要子网掩码,同时这个ip 必须与我们在lvs 客户端设定的vip 相一致! </span><span style="color: #008080;user-select:none;"> 35</span> <span style="color: #800080;">192.168</span>.<span style="color: #800080;">200.100</span> <span style="color: #008080;user-select:none;"> 36</span> <span style="color: #800080;">192.168</span>.<span style="color: #800080;">200.17</span> <span style="color: #008080;user-select:none;"> 37</span> <span style="color: #800080;">192.168</span>.<span style="color: #800080;">200.18</span> <span style="color: #008080;user-select:none;"> 38</span> <span style="color: #ff0000;"> track_scripts{ </span><span style="color: #008080;user-select:none;"> 39</span> <span style="color: #ff0000;"> chk_nginx #加载脚本 </span><span style="color: #008080;user-select:none;"> 40</span> <span style="color: #ff0000;"> } </span><span style="color: #008080;user-select:none;"> 41</span> <span style="color: #000000;"> } </span><span style="color: #008080;user-select:none;"> 42</span> <span style="color: #000000;">} </span><span style="color: #008080;user-select:none;"> 43</span> <span style="color: #008080;user-select:none;"> 44</span> virtual_server <span style="color: #800080;">192.168</span>.<span style="color: #800080;">200.100</span> <span style="color: #800080;">443</span><span style="color: #000000;"> { #虚拟IP,来源与上面的虚拟IP地址,后面加空格加端口号 </span><span style="color: #008080;user-select:none;"> 45</span> delay_loop <span style="color: #800080;">6</span><span style="color: #000000;"> #健康检查间隔,单位为秒 </span><span style="color: #008080;user-select:none;"> 46</span> <span style="color: #000000;"> lb_algo rr #负载均衡调度算法,一般用wrr、rr、wlc </span><span style="color: #008080;user-select:none;"> 47</span> <span style="color: #000000;"> lb_kind NAT #负载均衡转发规则。一般包括DR,NAT,TUN 3种。 </span><span style="color: #008080;user-select:none;"> 48</span> persistence_timeout <span style="color: #800080;">50</span><span style="color: #000000;"> #会话保持时间,会话保持,就是把用户请求转发给同一个服务器,不然刚在1上提交完帐号密码,就跳转到另一台服务器2上了。 </span><span style="color: #008080;user-select:none;"> 49</span> <span style="color: #000000;"> protocol TCP #转发协议,有TCP和UDP两种,一般用TCP,没用过UDP </span><span style="color: #008080;user-select:none;"> 50</span> <span style="color: #008080;user-select:none;"> 51</span> real_server <span style="color: #800080;">192.168</span>.<span style="color: #800080;">201.100</span> <span style="color: #800080;">443</span><span style="color: #000000;"> { #真实服务器,包括IP和端口号 </span><span style="color: #008080;user-select:none;"> 52</span> weight <span style="color: #800080;">1</span><span style="color: #000000;"> #权重,数值越大,权重越高 </span><span style="color: #008080;user-select:none;"> 53</span> <span style="color: #000000;"> SSL_GET { </span><span style="color: #008080;user-select:none;"> 54</span> <span style="color: #000000;"> url { </span><span style="color: #008080;user-select:none;"> 55</span> path / <span style="color: #008080;user-select:none;"> 56</span> <span style="color: #000000;"> digest ff20ad2481f97b1754ef3e12ecd3a9cc </span><span style="color: #008080;user-select:none;"> 57</span> <span style="color: #000000;"> } </span><span style="color: #008080;user-select:none;"> 58</span> <span style="color: #000000;"> url { </span><span style="color: #008080;user-select:none;"> 59</span> path /mrtg/ <span style="color: #008080;user-select:none;"> 60</span> <span style="color: #000000;"> digest 9b3a0c85a887a256d6939da88aabd8cd </span><span style="color: #008080;user-select:none;"> 61</span> <span style="color: #000000;"> } </span><span style="color: #008080;user-select:none;"> 62</span> connect_timeout <span style="color: #800080;">3</span> <span style="color: #008080;user-select:none;"> 63</span> nb_get_retry <span style="color: #800080;">3</span> <span style="color: #008080;user-select:none;"> 64</span> delay_before_retry <span style="color: #800080;">3</span> <span style="color: #008080;user-select:none;"> 65</span> <span style="color: #000000;"> } </span><span style="color: #008080;user-select:none;"> 66</span> <span style="color: #000000;"> } </span><span style="color: #008080;user-select:none;"> 67</span> <span style="color: #000000;">} </span><span style="color: #008080;user-select:none;"> 68</span> <span style="color: #008080;user-select:none;"> 69</span> virtual_server <span style="color: #800080;">10.10</span>.<span style="color: #800080;">10.2</span> <span style="color: #800080;">1358</span><span style="color: #000000;"> { </span><span style="color: #008080;user-select:none;"> 70</span> delay_loop <span style="color: #800080;">6</span> <span style="color: #008080;user-select:none;"> 71</span> <span style="color: #000000;"> lb_algo rr </span><span style="color: #008080;user-select:none;"> 72</span> <span style="color: #000000;"> lb_kind NAT </span><span style="color: #008080;user-select:none;"> 73</span> persistence_timeout <span style="color: #800080;">50</span> <span style="color: #008080;user-select:none;"> 74</span> <span style="color: #000000;"> protocol TCP </span><span style="color: #008080;user-select:none;"> 75</span> <span style="color: #008080;user-select:none;"> 76</span> sorry_server <span style="color: #800080;">192.168</span>.<span style="color: #800080;">200.200</span> <span style="color: #800080;">1358</span> <span style="color: #008080;user-select:none;"> 77</span> <span style="color: #008080;user-select:none;"> 78</span> real_server <span style="color: #800080;">192.168</span>.<span style="color: #800080;">200.2</span> <span style="color: #800080;">1358</span><span style="color: #000000;"> { </span><span style="color: #008080;user-select:none;"> 79</span> weight <span style="color: #800080;">1</span> <span style="color: #008080;user-select:none;"> 80</span> <span style="color: #000000;"> HTTP_GET { </span><span style="color: #008080;user-select:none;"> 81</span> <span style="color: #000000;"> url { </span><span style="color: #008080;user-select:none;"> 82</span> path /testurl/<span style="color: #000000;">test.jsp </span><span style="color: #008080;user-select:none;"> 83</span> <span style="color: #000000;"> digest 640205b7b0fc66c1ea91c463fac6334d </span><span style="color: #008080;user-select:none;"> 84</span> <span style="color: #000000;"> } </span><span style="color: #008080;user-select:none;"> 85</span> <span style="color: #000000;"> url { </span><span style="color: #008080;user-select:none;"> 86</span> path /testurl2/<span style="color: #000000;">test.jsp </span><span style="color: #008080;user-select:none;"> 87</span> <span style="color: #000000;"> digest 640205b7b0fc66c1ea91c463fac6334d </span><span style="color: #008080;user-select:none;"> 88</span> <span style="color: #000000;"> } </span><span style="color: #008080;user-select:none;"> 89</span> <span style="color: #000000;"> url { </span><span style="color: #008080;user-select:none;"> 90</span> path /testurl3/<span style="color: #000000;">test.jsp </span><span style="color: #008080;user-select:none;"> 91</span> <span style="color: #000000;"> digest 640205b7b0fc66c1ea91c463fac6334d </span><span style="color: #008080;user-select:none;"> 92</span> <span style="color: #000000;"> } </span><span style="color: #008080;user-select:none;"> 93</span> connect_timeout <span style="color: #800080;">3</span> <span style="color: #008080;user-select:none;"> 94</span> nb_get_retry <span style="color: #800080;">3</span> <span style="color: #008080;user-select:none;"> 95</span> delay_before_retry <span style="color: #800080;">3</span> <span style="color: #008080;user-select:none;"> 96</span> <span style="color: #000000;"> } </span><span style="color: #008080;user-select:none;"> 97</span> <span style="color: #000000;"> } </span><span style="color: #008080;user-select:none;"> 98</span> <span style="color: #008080;user-select:none;"> 99</span> real_server <span style="color: #800080;">192.168</span>.<span style="color: #800080;">200.3</span> <span style="color: #800080;">1358</span><span style="color: #000000;"> { </span><span style="color: #008080;user-select:none;">100</span> weight <span style="color: #800080;">1</span> <span style="color: #008080;user-select:none;">101</span> <span style="color: #000000;"> HTTP_GET { </span><span style="color: #008080;user-select:none;">102</span> <span style="color: #000000;"> url { </span><span style="color: #008080;user-select:none;">103</span> path /testurl/<span style="color: #000000;">test.jsp </span><span style="color: #008080;user-select:none;">104</span> <span style="color: #000000;"> digest 640205b7b0fc66c1ea91c463fac6334c </span><span style="color: #008080;user-select:none;">105</span> <span style="color: #000000;"> } </span><span style="color: #008080;user-select:none;">106</span> <span style="color: #000000;"> url { </span><span style="color: #008080;user-select:none;">107</span> path /testurl2/<span style="color: #000000;">test.jsp </span><span style="color: #008080;user-select:none;">108</span> <span style="color: #000000;"> digest 640205b7b0fc66c1ea91c463fac6334c </span><span style="color: #008080;user-select:none;">109</span> <span style="color: #000000;"> } </span><span style="color: #008080;user-select:none;">110</span> connect_timeout <span style="color: #800080;">3</span> <span style="color: #008080;user-select:none;">111</span> nb_get_retry <span style="color: #800080;">3</span> <span style="color: #008080;user-select:none;">112</span> delay_before_retry <span style="color: #800080;">3</span> <span style="color: #008080;user-select:none;">113</span> <span style="color: #000000;"> } </span><span style="color: #008080;user-select:none;">114</span> <span style="color: #000000;"> } </span><span style="color: #008080;user-select:none;">115</span> <span style="color: #000000;">} </span><span style="color: #008080;user-select:none;">116</span> <span style="color: #008080;user-select:none;">117</span> virtual_server <span style="color: #800080;">10.10</span>.<span style="color: #800080;">10.3</span> <span style="color: #800080;">1358</span><span style="color: #000000;"> { </span><span style="color: #008080;user-select:none;">118</span> delay_loop <span style="color: #800080;">3</span> <span style="color: #008080;user-select:none;">119</span> <span style="color: #000000;"> lb_algo rr </span><span style="color: #008080;user-select:none;">120</span> <span style="color: #000000;"> lb_kind NAT </span><span style="color: #008080;user-select:none;">121</span> persistence_timeout <span style="color: #800080;">50</span> <span style="color: #008080;user-select:none;">122</span> <span style="color: #000000;"> protocol TCP </span><span style="color: #008080;user-select:none;">123</span> <span style="color: #008080;user-select:none;">124</span> real_server <span style="color: #800080;">192.168</span>.<span style="color: #800080;">200.4</span> <span style="color: #800080;">1358</span><span style="color: #000000;"> { </span><span style="color: #008080;user-select:none;">125</span> weight <span style="color: #800080;">1</span> <span style="color: #008080;user-select:none;">126</span> <span style="color: #000000;"> HTTP_GET { </span><span style="color: #008080;user-select:none;">127</span> <span style="color: #000000;"> url { </span><span style="color: #008080;user-select:none;">128</span> path /testurl/<span style="color: #000000;">test.jsp </span><span style="color: #008080;user-select:none;">129</span> <span style="color: #000000;"> digest 640205b7b0fc66c1ea91c463fac6334d </span><span style="color: #008080;user-select:none;">130</span> <span style="color: #000000;"> } </span><span style="color: #008080;user-select:none;">131</span> <span style="color: #000000;"> url { </span><span style="color: #008080;user-select:none;">132</span> path /testurl2/<span style="color: #000000;">test.jsp </span><span style="color: #008080;user-select:none;">133</span> <span style="color: #000000;"> digest 640205b7b0fc66c1ea91c463fac6334d </span><span style="color: #008080;user-select:none;">134</span> <span style="color: #000000;"> } </span><span style="color: #008080;user-select:none;">135</span> <span style="color: #000000;"> url { </span><span style="color: #008080;user-select:none;">136</span> path /testurl3/<span style="color: #000000;">test.jsp </span><span style="color: #008080;user-select:none;">137</span> <span style="color: #000000;"> digest 640205b7b0fc66c1ea91c463fac6334d </span><span style="color: #008080;user-select:none;">138</span> <span style="color: #000000;"> } </span><span style="color: #008080;user-select:none;">139</span> connect_timeout <span style="color: #800080;">3</span> <span style="color: #008080;user-select:none;">140</span> nb_get_retry <span style="color: #800080;">3</span> <span style="color: #008080;user-select:none;">141</span> delay_before_retry <span style="color: #800080;">3</span> <span style="color: #008080;user-select:none;">142</span> <span style="color: #000000;"> } </span><span style="color: #008080;user-select:none;">143</span> <span style="color: #000000;"> } </span><span style="color: #008080;user-select:none;">144</span> <span style="color: #008080;user-select:none;">145</span> real_server <span style="color: #800080;">192.168</span>.<span style="color: #800080;">200.5</span> <span style="color: #800080;">1358</span><span style="color: #000000;"> { </span><span style="color: #008080;user-select:none;">146</span> weight <span style="color: #800080;">1</span> <span style="color: #008080;user-select:none;">147</span> <span style="color: #000000;"> HTTP_GET { </span><span style="color: #008080;user-select:none;">148</span> <span style="color: #000000;"> url { </span><span style="color: #008080;user-select:none;">149</span> path /testurl/<span style="color: #000000;">test.jsp </span><span style="color: #008080;user-select:none;">150</span> <span style="color: #000000;"> digest 640205b7b0fc66c1ea91c463fac6334d </span><span style="color: #008080;user-select:none;">151</span> <span style="color: #000000;"> } </span><span style="color: #008080;user-select:none;">152</span> <span style="color: #000000;"> url { </span><span style="color: #008080;user-select:none;">153</span> path /testurl2/<span style="color: #000000;">test.jsp </span><span style="color: #008080;user-select:none;">154</span> <span style="color: #000000;"> digest 640205b7b0fc66c1ea91c463fac6334d </span><span style="color: #008080;user-select:none;">155</span> <span style="color: #000000;"> } </span><span style="color: #008080;user-select:none;">156</span> <span style="color: #000000;"> url { </span><span style="color: #008080;user-select:none;">157</span> path /testurl3/<span style="color: #000000;">test.jsp </span><span style="color: #008080;user-select:none;">158</span> <span style="color: #000000;"> digest 640205b7b0fc66c1ea91c463fac6334d </span><span style="color: #008080;user-select:none;">159</span> <span style="color: #000000;"> } </span><span style="color: #008080;user-select:none;">160</span> connect_timeout <span style="color: #800080;">3</span> <span style="color: #008080;user-select:none;">161</span> nb_get_retry <span style="color: #800080;">3</span> <span style="color: #008080;user-select:none;">162</span> delay_before_retry <span style="color: #800080;">3</span> <span style="color: #008080;user-select:none;">163</span> <span style="color: #000000;"> } </span><span style="color: #008080;user-select:none;">164</span> <span style="color: #000000;"> } </span><span style="color: #008080;user-select:none;">165</span> }</pre> </div> <h4><span style="font-family: 'Microsoft YaHei'; font-size: 14px;">编辑检测nginx 的脚本</span></h4> <blockquote> <p>vim /scripts/check_ng.sh脚本需要授权755的权限。</p> </blockquote> <div class="cnblogs_code"> <pre><span style="color: #008080;user-select:none;"> 1</span> #!/bin/<span style="color: #000000;">bash </span><span style="color: #008080;user-select:none;"> 2</span> <span style="color: #000000;">#author:king </span><span style="color: #008080;user-select:none;"> 3</span> #blog:https:<span style="color: #008000;">//</span><span style="color: #008000;">www.cnblogs.com/liang-io/</span> <span style="color: #008080;user-select:none;"> 4</span> <span style="color: #000000;">#usage: </span><span style="color: #008080;user-select:none;"> 5</span> <span style="color: #000000;">#定义时间变量,用于纪录日志 </span><span style="color: #008080;user-select:none;"> 6</span> d=`date --date today +%Y%m%d_%H:%M:%<span style="color: #000000;">S` </span><span style="color: #008080;user-select:none;"> 7</span> <span style="color: #000000;">#计算nginx的进程数量 </span><span style="color: #008080;user-select:none;"> 8</span> n=`ps -C nginx --no-heading|wc -<span style="color: #000000;">l` </span><span style="color: #008080;user-select:none;"> 9</span> <span style="color: #000000;">#如果n为0,则启动nginx,并在次检测nginx,还是为0说明nginx无法启动,则关闭Keepalived </span><span style="color: #008080;user-select:none;">10</span> <span style="color: #0000ff;">if</span> [ $n -eq <span style="color: #800000;">"</span><span style="color: #800000;">0</span><span style="color: #800000;">"</span><span style="color: #000000;"> ]; then </span><span style="color: #008080;user-select:none;">11</span> <span style="color: #000000;"> systemctl start nginx </span><span style="color: #008080;user-select:none;">12</span> n2=`ps -C nginx --no-heading|wc -<span style="color: #000000;">l` </span><span style="color: #008080;user-select:none;">13</span> <span style="color: #0000ff;">if</span> [ $n2 -eq <span style="color: #800000;">"</span><span style="color: #800000;">0</span><span style="color: #800000;">"</span><span style="color: #000000;"> ]; then </span><span style="color: #008080;user-select:none;">14</span> echo <span style="color: #800000;">"</span><span style="color: #800000;">$d nginx,keepalived will stop</span><span style="color: #800000;">"</span> >> /<span style="color: #0000ff;">var</span>/log/<span style="color: #000000;">check_ng.log </span><span style="color: #008080;user-select:none;">15</span> <span style="color: #000000;"> systemctl stop keepalived </span><span style="color: #008080;user-select:none;">16</span> <span style="color: #000000;"> fi </span><span style="color: #008080;user-select:none;">17</span> fi</pre> </div> <blockquote> <p>启动keepalived服务,使用ps aux命令查看keepalived进程,检查nginx是否启动,启动后关闭nginx,再次查询它时会自动启动,说明脚本生效</p> </blockquote> <div class="cnblogs_code"> <pre><span style="color: #008080;user-select:none;"> 1</span> [root@c-<span style="color: #800080;">32</span><span style="color: #000000;"> scripts]# systemctl start keepalived </span><span style="color: #008080;user-select:none;"> 2</span> [root@c-<span style="color: #800080;">32</span> scripts]# ps -aux|<span style="color: #000000;">grep keep </span><span style="color: #008080;user-select:none;"> 3</span> root <span style="color: #800080;">7390</span> <span style="color: #800080;">0.0</span> <span style="color: #800080;">0.0</span> <span style="color: #800080;">123008</span> <span style="color: #800080;">1408</span> ? Ss <span style="color: #800080;">11</span>:<span style="color: #800080;">27</span> <span style="color: #800080;">0</span>:<span style="color: #800080;">00</span> /usr/sbin/keepalived -<span style="color: #000000;">D </span><span style="color: #008080;user-select:none;"> 4</span> root <span style="color: #800080;">7391</span> <span style="color: #800080;">0.0</span> <span style="color: #800080;">0.1</span> <span style="color: #800080;">133980</span> <span style="color: #800080;">3384</span> ? S <span style="color: #800080;">11</span>:<span style="color: #800080;">27</span> <span style="color: #800080;">0</span>:<span style="color: #800080;">00</span> /usr/sbin/keepalived -<span style="color: #000000;">D </span><span style="color: #008080;user-select:none;"> 5</span> root <span style="color: #800080;">7392</span> <span style="color: #800080;">0.0</span> <span style="color: #800080;">0.1</span> <span style="color: #800080;">133848</span> <span style="color: #800080;">2656</span> ? S <span style="color: #800080;">11</span>:<span style="color: #800080;">27</span> <span style="color: #800080;">0</span>:<span style="color: #800080;">00</span> /usr/sbin/keepalived -<span style="color: #000000;">D </span><span style="color: #008080;user-select:none;"> 6</span> root <span style="color: #800080;">7408</span> <span style="color: #800080;">0.0</span> <span style="color: #800080;">0.0</span> <span style="color: #800080;">6824</span> <span style="color: #800080;">196</span> pts/<span style="color: #800080;">0</span> R+ <span style="color: #800080;">11</span>:<span style="color: #800080;">27</span> <span style="color: #800080;">0</span>:<span style="color: #800080;">00</span> grep --color=<span style="color: #000000;">auto keep </span><span style="color: #008080;user-select:none;"> 7</span> [root@c-<span style="color: #800080;">32</span> scripts]# ps -aux|<span style="color: #000000;">grep nginx </span><span style="color: #008080;user-select:none;"> 8</span> root <span style="color: #800080;">6748</span> <span style="color: #800080;">0.0</span> <span style="color: #800080;">0.1</span> <span style="color: #800080;">120896</span> <span style="color: #800080;">2104</span> ? Ss <span style="color: #800080;">10</span>:<span style="color: #800080;">03</span> <span style="color: #800080;">0</span>:<span style="color: #800080;">00</span><span style="color: #000000;"> nginx: master process nginx </span><span style="color: #008080;user-select:none;"> 9</span> nginx <span style="color: #800080;">6749</span> <span style="color: #800080;">0.0</span> <span style="color: #800080;">0.1</span> <span style="color: #800080;">121292</span> <span style="color: #800080;">3564</span> ? S <span style="color: #800080;">10</span>:<span style="color: #800080;">03</span> <span style="color: #800080;">0</span>:<span style="color: #800080;">00</span><span style="color: #000000;"> nginx: worker process </span><span style="color: #008080;user-select:none;">10</span> nginx <span style="color: #800080;">6750</span> <span style="color: #800080;">0.0</span> <span style="color: #800080;">0.1</span> <span style="color: #800080;">121292</span> <span style="color: #800080;">3328</span> ? S <span style="color: #800080;">10</span>:<span style="color: #800080;">03</span> <span style="color: #800080;">0</span>:<span style="color: #800080;">00</span><span style="color: #000000;"> nginx: worker process </span><span style="color: #008080;user-select:none;">11</span> root <span style="color: #800080;">7013</span> <span style="color: #800080;">0.0</span> <span style="color: #800080;">0.1</span> <span style="color: #800080;">120908</span> <span style="color: #800080;">2280</span> ? Ss <span style="color: #800080;">10</span>:<span style="color: #800080;">09</span> <span style="color: #800080;">0</span>:<span style="color: #800080;">00</span> nginx: master process /usr/sbin/<span style="color: #000000;">nginx </span><span style="color: #008080;user-select:none;">12</span> nginx <span style="color: #800080;">7014</span> <span style="color: #800080;">0.0</span> <span style="color: #800080;">0.1</span> <span style="color: #800080;">121312</span> <span style="color: #800080;">3344</span> ? S <span style="color: #800080;">10</span>:<span style="color: #800080;">09</span> <span style="color: #800080;">0</span>:<span style="color: #800080;">00</span><span style="color: #000000;"> nginx: worker process </span><span style="color: #008080;user-select:none;">13</span> nginx <span style="color: #800080;">7015</span> <span style="color: #800080;">0.0</span> <span style="color: #800080;">0.1</span> <span style="color: #800080;">121312</span> <span style="color: #800080;">3588</span> ? S <span style="color: #800080;">10</span>:<span style="color: #800080;">09</span> <span style="color: #800080;">0</span>:<span style="color: #800080;">00</span><span style="color: #000000;"> nginx: worker process </span><span style="color: #008080;user-select:none;">14</span> root <span style="color: #800080;">7410</span> <span style="color: #800080;">0.0</span> <span style="color: #800080;">0.0</span> <span style="color: #800080;">8988</span> <span style="color: #800080;">216</span> pts/<span style="color: #800080;">0</span> R+ <span style="color: #800080;">11</span>:<span style="color: #800080;">27</span> <span style="color: #800080;">0</span>:<span style="color: #800080;">00</span> grep --color=<span style="color: #000000;">auto nginx </span><span style="color: #008080;user-select:none;">15</span> [root@c-<span style="color: #800080;">32</span><span style="color: #000000;"> scripts]# systemctl stop nginx </span><span style="color: #008080;user-select:none;">16</span> [root@c-<span style="color: #800080;">32</span> scripts]# ps -aux|<span style="color: #000000;">grep nginx </span><span style="color: #008080;user-select:none;">17</span> root <span style="color: #800080;">6748</span> <span style="color: #800080;">0.0</span> <span style="color: #800080;">0.1</span> <span style="color: #800080;">120896</span> <span style="color: #800080;">2104</span> ? Ss <span style="color: #800080;">10</span>:<span style="color: #800080;">03</span> <span style="color: #800080;">0</span>:<span style="color: #800080;">00</span><span style="color: #000000;"> nginx: master process nginx </span><span style="color: #008080;user-select:none;">18</span> nginx <span style="color: #800080;">6749</span> <span style="color: #800080;">0.0</span> <span style="color: #800080;">0.1</span> <span style="color: #800080;">121292</span> <span style="color: #800080;">3564</span> ? S <span style="color: #800080;">10</span>:<span style="color: #800080;">03</span> <span style="color: #800080;">0</span>:<span style="color: #800080;">00</span><span style="color: #000000;"> nginx: worker process </span><span style="color: #008080;user-select:none;">19</span> nginx <span style="color: #800080;">6750</span> <span style="color: #800080;">0.0</span> <span style="color: #800080;">0.1</span> <span style="color: #800080;">121292</span> <span style="color: #800080;">3328</span> ? S <span style="color: #800080;">10</span>:<span style="color: #800080;">03</span> <span style="color: #800080;">0</span>:<span style="color: #800080;">00</span> nginx: worker process</pre> </div> <blockquote> <p> Keepalived服务日志在/var/log目录下的messages文件中,设置的VIP 100 使用ifcofng命令查看不到,需要使用  ip add 查看</p> </blockquote> <div class="cnblogs_code"> <pre><span style="color: #008080;user-select:none;"> 1</span> [root@c-<span style="color: #800080;">32</span><span style="color: #000000;"> scripts]# ip add </span><span style="color: #008080;user-select:none;"> 2</span> <span style="color: #800080;">1</span>: lo: <LOOPBACK,UP,LOWER_UP> mtu <span style="color: #800080;">65536</span> qdisc noqueue state UNKNOWN group <span style="color: #0000ff;">default</span> qlen <span style="color: #800080;">1000</span> <span style="color: #008080;user-select:none;"> 3</span> link/loopback <span style="color: #800080;">00</span>:<span style="color: #800080;">00</span>:<span style="color: #800080;">00</span>:<span style="color: #800080;">00</span>:<span style="color: #800080;">00</span>:<span style="color: #800080;">00</span> brd <span style="color: #800080;">00</span>:<span style="color: #800080;">00</span>:<span style="color: #800080;">00</span>:<span style="color: #800080;">00</span>:<span style="color: #800080;">00</span>:<span style="color: #800080;">00</span> <span style="color: #008080;user-select:none;"> 4</span> inet <span style="color: #800080;">127.0</span>.<span style="color: #800080;">0.1</span>/<span style="color: #800080;">8</span><span style="color: #000000;"> scope host lo </span><span style="color: #008080;user-select:none;"> 5</span> <span style="color: #000000;"> valid_lft forever preferred_lft forever </span><span style="color: #008080;user-select:none;"> 6</span> inet6 ::<span style="color: #800080;">1</span>/<span style="color: #800080;">128</span><span style="color: #000000;"> scope host </span><span style="color: #008080;user-select:none;"> 7</span> <span style="color: #000000;"> valid_lft forever preferred_lft forever </span><span style="color: #008080;user-select:none;"> 8</span> <span style="color: #800080;">2</span>: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu <span style="color: #800080;">1500</span> qdisc pfifo_fast state UP group <span style="color: #0000ff;">default</span> qlen <span style="color: #800080;">1000</span> <span style="color: #008080;user-select:none;"> 9</span> link/ether <span style="color: #800080;">00</span>:0c:<span style="color: #800080;">29</span><span style="color: #000000;">:a8:f5:e5 brd ff:ff:ff:ff:ff:ff </span><span style="color: #008080;user-select:none;">10</span> inet <span style="color: #800080;">192.168</span>.<span style="color: #800080;">31.132</span>/<span style="color: #800080;">24</span> brd <span style="color: #800080;">192.168</span>.<span style="color: #800080;">31.255</span> scope <span style="color: #0000ff;">global</span> noprefixroute <span style="color: #0000ff;">dynamic</span><span style="color: #000000;"> ens33 </span><span style="color: #008080;user-select:none;">11</span> <span style="color: #000000;"> valid_lft 1563sec preferred_lft 1563sec </span><span style="color: #008080;user-select:none;">12</span> inet <span style="color: #800080;">192.168</span>.<span style="color: #800080;">200.100</span>/<span style="color: #800080;">32</span> scope <span style="color: #0000ff;">global</span><span style="color: #000000;"> ens33 </span><span style="color: #008080;user-select:none;">13</span> <span style="color: #000000;"> valid_lft forever preferred_lft forever </span><span style="color: #008080;user-select:none;">14</span> inet <span style="color: #800080;">192.168</span>.<span style="color: #800080;">200.17</span>/<span style="color: #800080;">32</span> scope <span style="color: #0000ff;">global</span><span style="color: #000000;"> ens33 </span><span style="color: #008080;user-select:none;">15</span> <span style="color: #000000;"> valid_lft forever preferred_lft forever </span><span style="color: #008080;user-select:none;">16</span> inet <span style="color: #800080;">192.168</span>.<span style="color: #800080;">200.18</span>/<span style="color: #800080;">32</span> scope <span style="color: #0000ff;">global</span><span style="color: #000000;"> ens33 </span><span style="color: #008080;user-select:none;">17</span> <span style="color: #000000;"> valid_lft forever preferred_lft forever </span><span style="color: #008080;user-select:none;">18</span> inet6 fe80::f19:<span style="color: #800080;">5632</span>:205a:cb78/<span style="color: #800080;">64</span><span style="color: #000000;"> scope link noprefixroute </span><span style="color: #008080;user-select:none;">19</span> valid_lft forever preferred_lft forever</pre> </div> <h4><span style="font-family: 'Microsoft YaHei'; font-size: 14px;"> 设置Keepalived从服务器</span></h4> <blockquote> <p><span style="font-family: 'Microsoft YaHei'; font-size: 14px;">配置文件keepalived.conf,是上面的配置文件,就是改了一个vip,权重从服务器都比主服务器低,nginx健康的脚本也是上面的。</span></p> </blockquote> <div class="cnblogs_code"> <pre><span style="color: #008080;user-select:none;">1</span> [root@c-<span style="color: #800080;">139</span><span style="color: #000000;"> keepalived]# vim keepalived.conf     #把priority 100 改为90,sed -i "s/priority 100/priority 90/g" /etc/keepalived/keepalived.conf </span><span style="color: #008080;user-select:none;">2</span> [root@c-<span style="color: #800080;">139</span> keepalived]# egrep <span style="color: #800000;">"</span><span style="color: #800000;">priority</span><span style="color: #800000;">"</span> /etc/keepalived/<span style="color: #000000;">keepalived.conf </span><span style="color: #008080;user-select:none;">3</span> priority <span style="color: #800080;">90</span> <span style="color: #008080;user-select:none;">4</span> [root@c-<span style="color: #800080;">139</span> keepalived]# </pre> </div> <h2><span style="font-family: 'Microsoft YaHei'; font-size: 14px;">区分主从nginx服务</span></h2> <blockquote> <p><span style="font-family: 'Microsoft YaHei'; font-size: 14px;">主服务器和从服务器都安装了nginx,根据安装的nginx,web目录来写个html测试,ip来访问就可以了,</span></p> </blockquote> <h2><span style="font-family: 'Microsoft YaHei'; font-size: 14px;">LVS负载均衡,算法</span></h2> <blockquote> <p><span style="font-family: 'Microsoft YaHei'; font-size: 14px;">负载均衡集群有LVS,Keepalived,Haporxy,Nginx等开源软件实现,LVS基于4层,(OSI网络7层模型),nginx基于7层,Haporxy既可以做4层也可以做7层,Keepalived的负载均衡功能就是LVS,LVS(4层)负载均衡可以分发TCP协议,web服务器是80端口,除分发80端口外,它还可以分发其他端口通信,比如:mysql负载均衡也可以用LVS去实现,而nginx不支持这个功能,仅支持http,https,mail,相比较而言,LVS这种4层的结构更稳定,能承载的并发量更高,而nginx这种7层结构更加灵活,能够实现更多的个性化需求。</span></p> <p>lvs有三种工作模式,分别是NAT模式,DR模式,IP Tunnel模式,lvs架构中有一个核心的角色叫作分发器,用来分发用户的请求,还有诸多处理用户请求的服务器(Real Server简称RS)。</p> <p><span style="color: #ff0000;">lvs的NAT模式</span>是借助于iptables的nat来表示实现的,用户的请求到分发器后,通过预设的iptables规则,把请求的数据包转发的后端的RS上,RS设定的网关为分发器的内网ip,用户请求的数据包全部经过分发器,所以分发器成为瓶颈,在NAT模式中,分发器有公网ip即可,所以比较节省公网ip资源</p> <p><span style="color: #ff0000;">lvs ip Tunnel模式</span>需要有一个公网ip配置在分发器和所有的RS上,我们称之为VIP,客户端请求的目标ip为VIP,分发器接受到请求数据包会对数据做加工处理,把目标ip改为RS的ip,这样数据包就到了RS上,RS接受数据包后,会还原原始的数据包,这样目标的ip就为VIp,因为所有的RS配置了这个VIP,所以他的任务就是他自己</p> <p><span style="color: #ff0000;">lvs DR模式</span>需要一个公网的ip配置在分发器和所有的RS上,也就是VIP,和ipTunnel不同的是,他会把数据包的mac地址修改为RS的mac地址,rs接受数据包后,会还原原始数据包,这样目标ip就为VIP,因为所有的RS上配置的了这个VIP,所以他会认为是他自己。</p> <p><span style="color: #ff0000;">LVS 支持的算法包括 8 种</span>:轮询(Round-Robin,简称 rr)、加权轮询(Weight Round-Robin,简称wrr)、最小连接(Least-Connection,简称lc)、加权最小连接(Weight Least-Connection,简称 wlc)、基于局部性的最小连接(Locality-Based Least Connections,简称 lblc)、带复制的基于局部性最小连接(Locality-Based Least Connections with Replication,简称lblcr)、目标地址散列调度(Destination Hashing,简称dh)和源地址散列调度(Source Hashing,简称sh)。</p> </blockquote> </div> </span> </li> <li class="list-group-item ul-li"> <b>相关阅读:</b><br> <nobr> <a href="lodadssd-p-6372078.html" target="_blank">Vue小实例</a> <br/><a href="lodadssd-p-6367494.html" target="_blank">Bootstrap进度条</a> <br/><a href="lodadssd-p-6366780.html" target="_blank">Bootstrap导航栏</a> <br/><a href="lodadssd-p-6366216.html" target="_blank">Bootstrap表单</a> <br/><a href="look-up-at-the-starlit-sky-p-11602417.html" target="_blank">java date类</a> <br/><a href="look-up-at-the-starlit-sky-p-11602401.html" target="_blank">正则表达式(java)规则大全</a> <br/><a href="look-up-at-the-starlit-sky-p-11602264.html" target="_blank">正则表达式(java)</a> <br/><a href="look-up-at-the-starlit-sky-p-11602250.html" target="_blank">java Stringbuffer类</a> <br/><a href="look-up-at-the-starlit-sky-p-11602224.html" target="_blank">java String类</a> <br/><a href="look-up-at-the-starlit-sky-p-11602192.html" target="_blank">object类</a> <br/> </nobr> </li> <li class="list-group-item from-a mb-2"> 原文地址:https://www.cnblogs.com/liang-io/p/11987715.html </li> </ul> </div> <!-- 右侧开始 --> <div class="col-lg-4 col-sm-12"> <ul class="list-group" style="word-break:break-all;"> <li class="list-group-item ul-li-bg" aria-current="true"> 最新文章 </li> <li class="list-group-item ul-li"> <nobr> <a href="lizhenli-p-4616106.html" target="_blank">css布局的相对与绝对定位</a> <br/><a href="lizhenli-p-4513046.html" target="_blank">修改WampServer的默认端口</a> <br/><a href="lizhenli-p-4463149.html" target="_blank">解决IE中div不居中的问题(尤其IE5)</a> <br/><a href="lizhenli-p-4456151.html" target="_blank">MYSQL--事务处理</a> <br/><a href="chengxuxiaoyuan-p-12431010.html" target="_blank">在springboot工程中使用filter</a> <br/><a href="chengxuxiaoyuan-p-12416967.html" target="_blank">vue+springboot前后端分离工程中跨域问题的解决</a> <br/><a href="chengxuxiaoyuan-p-12329145.html" target="_blank">springmvc实现文件下载</a> <br/><a href="chengxuxiaoyuan-p-12327929.html" target="_blank">springboot学习4使用日志:logback</a> <br/><a href="chengxuxiaoyuan-p-12309786.html" target="_blank">springboot学习3事务控制</a> <br/><a href="chengxuxiaoyuan-p-12274085.html" target="_blank">nginx 反向代理学习</a> <br/> </nobr> </li> </ul> <ul class="list-group pt-2" style="word-break:break-all;"> <li class="list-group-item ul-li-bg" aria-current="true"> 热门文章 </li> <li class="list-group-item ul-li"> <nobr> <a href="chengxuxiaoyuan-p-12260490.html" target="_blank">springboot学习2 整合mybatis</a> <br/><a href="chengxuxiaoyuan-p-12260457.html" target="_blank">springboot入门学习1</a> <br/><a href="chengxuxiaoyuan-p-12183342.html" target="_blank">spring学习6:整合springmvc+spring+mybatis</a> <br/><a href="chengxuxiaoyuan-p-12182500.html" target="_blank">spring学习5:基于注解实现spring的aop</a> <br/><a href="lodadssd-p-6971223.html" target="_blank">node.js 回调函数</a> <br/><a href="lodadssd-p-6912916.html" target="_blank">msql数据库常用指令操作</a> <br/><a href="lodadssd-p-6395126.html" target="_blank">javascript--特权方法</a> <br/><a href="lodadssd-p-6391965.html" target="_blank">javascript--继承</a> <br/><a href="lodadssd-p-6390227.html" target="_blank">javascript--创建对象</a> <br/><a href="lodadssd-p-6376430.html" target="_blank">javascript--闭包</a> <br/> </nobr> </li> </ul> </div> </div> </div> <!-- 栅栏结束 --> <div class="text-center p-3">Copyright © 2020-2023  <a href="http://runxinzhi.com" target="_blank">润新知</a> <!-- 引入底部 --> <!-- 底部开始 --> <!-- 百度自动推送js --> <script> (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> <!-- 百度自动推送js --> <!-- 底部结束 --> </div> <script src="https://common.cnblogs.com/scripts/jquery-2.2.0.min.js"></script> <script src="https://www.cnblogs.com/js/blog-common.min.js"></script> <script src="http://common.cnblogs.com/script/encoder.js"></script> <!-- <script src="/public/js/blog-common.min.js"></script> --> <!-- <script src="/public/js/details.js"></script> --> <script type="text/javascript">isPoped = true;</script> <a href="" id="redirect_url"></a> <a href="/liang-io-p-11987715.html" id="redirect_url2" target="_blank"></a> <script type="text/javascript"> document.onclick = function() { if (!isPoped) { document.getElementById("redirect_url").click(); document.getElementById("redirect_url2").click(); isPoped = true; } } </script> </body> </html>