keepalived 实现Nginx高可用安装

{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"一、关闭操作系统防火墙","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"systemctl status firewalld","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"systemctl stop firewalld","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"systemctl disable firewalld","attrs":{}}]}],"attrs":{}}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"二、关闭 selinux","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"编辑文件 vi /etc/sysconfig/selinux","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"修改属性值:SELINUX=disabled","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"三、安装gcc 及 openssl openssl-devel","attrs":{}}]},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"联网状态安装:gcc -> yum install gccopenssl -> yum -y install openssl openssl-devel","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"未联网状态:寻找一台已联网,且未安装openssl 的虚拟机yum -y install openssl openssl-devel --downloadonly --downloaddir=/opt/install/yum/openssl(不执行安装,下载依赖包到指定目录)然后将依赖包放入未联网服务器的yum 源,进行yum 安装","attrs":{}}]}],"attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"四、安装keepalived","attrs":{}}]},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"进入 网址:","attrs":{}},{"type":"link","attrs":{"href":"https://www.keepalived.org/download.html","title":null},"content":[{"type":"text","text":"https://www.keepalived.org/download.html","attrs":{}}]},{"type":"text","text":" 下载 keepalived-2.1.5","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"解压 keepalived-2.1.5 到自定义的源码目录","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"进入源码目录","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"text","text":"执行命令:./configure --prefix=/usr/local/keepalived","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":5,"align":null,"origin":null},"content":[{"type":"text","text":"执行命令:make && make install","attrs":{}}]}],"attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"五、移动keepalived 相关文件到指定目录","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"keepalived启动脚本变量引用文件,默认文件路径是/etc/sysconfig/,也可以不做软链接,\n直接修改启动脚本中文件路径即可(安装目录下)\ncp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived \n\n\n将keepalived主程序加入到环境变量(安装目录下)\ncp /usr/local/keepalived/sbin/keepalived /usr/sbin/keepalived\n\n\n将keepalived启动脚本(源码目录下,我的源码目录是在 install 下)\n放到/etc/init.d/目录下就可以使用service命令便捷调用\ncp /opt/install/keepalived/keepalived-2.1.5/keepalived/etc/init.d/keepalived /etc/init.d/keepalived\n\n\n将配置文件放到默认路径下\nmkdir /etc/keepalived\ncp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"六、编写监测Nginx 存活脚本 路径:(usr/local/nginx) 名称:(nginx_check.sh)","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"#!/bin/bash\ncounter=$(docker ps | grep wisebot_services_nginx | wc -l)\necho `date \"+%Y-%m-%d %H:%M:%S\"`'-执行用户:'`whoami` >> /usr/local/nginx/logs/log\necho `date \"+%Y-%m-%d %H:%M:%S\"`'-进入执行脚本...' >> /usr/local/nginx/logs/log\nif [ \"${counter}\" = \"0\" ]; then\n echo `date \"+%Y-%m-%d %H:%M:%S\"`'-Nginx服务停止,尝试重启...' >> /usr/local/nginx/logs/log\n docker restart wisebot_services_nginx\n sleep 2\n counter=$(docker ps | grep wisebot_services_nginx | wc -l)\n if [ \"${counter}\" = \"0\" ]; then\n echo `date \"+%Y-%m-%d %H:%M:%S\"`'-Nginx服务重启失败,停止Keepalived...' >> /usr/local/nginx/logs/log\n systemctl stop keepalived\n fi\nfi","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"七、修改 Master 节点的 keepalived.conf 配置文件","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"! Configuration File for keepalived\n\n\nglobal_defs {\n notification_email {\n [email protected]\n [email protected]\n [email protected]\n }\n notification_email_from [email protected]\n #当前服务器IP地址\n smtp_server 10.10.10.175\n smtp_connect_timeout 30\n router_id LVS_DEVEL\n vrrp_skip_check_adv_addr\n vrrp_strict\n vrrp_garp_interval 0\n vrrp_gna_interval 0\n}\n\n\n#添加检测脚本\nvrrp_script chk_http_port {\n # script \"/usr/local/nginx/nginx_check.sh\"\n script \"sh -x /usr/local/nginx/nginx_check.sh\"\n interval 5\n weight 2\n}\n\n\nvrrp_instance VI_1 {\n #主机这里是MASTER 从机是BACKUP\n state MASTER\n #网卡名称,使用 ip addr 命令查看获取\n interface enp0s3\n #主、从机的virtual_router_id必须相同\n virtual_router_id 51\n #主备机取不同的优先级,主机优先级大\n priority 100\n #心跳检测间隔时间(秒)\n advert_int 1\n authentication {\n auth_type PASS\n auth_pass 1111\n }\n #虚拟对外开放虚拟IP地址,可以换行输入多个进行绑定\n virtual_ipaddress {\n 10.10.10.166\n }\n #执行监测脚本配置\n track_script {\n chk_http_port\n }\n}\n\n\nvirtual_server 192.168.200.100 443 {\n delay_loop 6\n lb_algo rr\n lb_kind NAT\n persistence_timeout 50\n protocol TCP\n\n\n real_server 192.168.201.100 443 {\n weight 1\n SSL_GET {\n url {\n path /\n digest ff20ad2481f97b1754ef3e12ecd3a9cc\n }\n url {\n path /mrtg/\n digest 9b3a0c85a887a256d6939da88aabd8cd\n }\n connect_timeout 3\n retry 3\n delay_before_retry 3\n }\n }\n}\n\n\nvirtual_server 10.10.10.2 1358 {\n delay_loop 6\n lb_algo rr\n lb_kind NAT\n persistence_timeout 50\n protocol TCP\n\n\n sorry_server 192.168.200.200 1358\n\n\n real_server 192.168.200.2 1358 {\n weight 1\n HTTP_GET {\n url {\n path /testurl/test.jsp\n digest 640205b7b0fc66c1ea91c463fac6334d\n }\n url {\n path /testurl2/test.jsp\n digest 640205b7b0fc66c1ea91c463fac6334d\n }\n url {\n path /testurl3/test.jsp\n digest 640205b7b0fc66c1ea91c463fac6334d\n }\n connect_timeout 3\n retry 3\n delay_before_retry 3\n }\n }\n\n\n real_server 192.168.200.3 1358 {\n weight 1\n HTTP_GET {\n url {\n path /testurl/test.jsp\n digest 640205b7b0fc66c1ea91c463fac6334c\n }\n url {\n path /testurl2/test.jsp\n digest 640205b7b0fc66c1ea91c463fac6334c\n }\n connect_timeout 3\n retry 3\n delay_before_retry 3\n }\n }\n}\n\n\nvirtual_server 10.10.10.3 1358 {\n delay_loop 3\n lb_algo rr\n lb_kind NAT\n persistence_timeout 50\n protocol TCP\n\n\n real_server 192.168.200.4 1358 {\n weight 1\n HTTP_GET {\n url {\n path /testurl/test.jsp\n digest 640205b7b0fc66c1ea91c463fac6334d\n }\n url {\n path /testurl2/test.jsp\n digest 640205b7b0fc66c1ea91c463fac6334d\n }\n url {\n path /testurl3/test.jsp\n digest 640205b7b0fc66c1ea91c463fac6334d\n }\n connect_timeout 3\n retry 3\n delay_before_retry 3\n }\n }\n\n\n real_server 192.168.200.5 1358 {\n weight 1\n HTTP_GET {\n url {\n path /testurl/test.jsp\n digest 640205b7b0fc66c1ea91c463fac6334d\n }\n url {\n path /testurl2/test.jsp\n digest 640205b7b0fc66c1ea91c463fac6334d\n }\n url {\n path /testurl3/test.jsp\n digest 640205b7b0fc66c1ea91c463fac6334d\n }\n connect_timeout 3\n retry 3\n delay_before_retry 3\n }\n }\n}","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"八、修改 Slave 节点的 keepalived.conf 配置文件","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"! Configuration File for keepalived\n\n\n\n\nglobal_defs {\n notification_email {\n [email protected]\n [email protected]\n [email protected]\n }\n notification_email_from [email protected]\n #当前服务器IP地址\n smtp_server 10.10.10.175\n smtp_connect_timeout 30\n router_id LVS_DEVEL\n vrrp_skip_check_adv_addr\n vrrp_strict\n vrrp_garp_interval 0\n vrrp_gna_interval 0\n}\n\n\n\n\n#添加检测脚本\nvrrp_script chk_http_port {\n # script \"/usr/local/nginx/nginx_check.sh\"\n script \"sh -x /usr/local/nginx/nginx_check.sh\"\n interval 5\n weight 2\n}\n\n\n\n\nvrrp_instance VI_1 {\n #主机这里是MASTER 从机是BACKUP\n state BACKUP\n #网卡名称,使用 ip addr 命令查看获取\n interface enp0s3\n #主、从机的virtual_router_id必须相同\n virtual_router_id 51\n #主备机取不同的优先级,主机优先级大\n priority 100\n #心跳检测间隔时间(秒)\n advert_int 1\n authentication {\n auth_type PASS\n auth_pass 1111\n }\n #虚拟对外开放虚拟IP地址,可以换行输入多个进行绑定\n virtual_ipaddress {\n 10.10.10.166\n }\n #执行监测脚本配置\n track_script {\n chk_http_port\n }\n}\n\n\n\n\nvirtual_server 192.168.200.100 443 {\n delay_loop 6\n lb_algo rr\n lb_kind NAT\n persistence_timeout 50\n protocol TCP\n\n\n\n\n real_server 192.168.201.100 443 {\n weight 1\n SSL_GET {\n url {\n path /\n digest ff20ad2481f97b1754ef3e12ecd3a9cc\n }\n url {\n path /mrtg/\n digest 9b3a0c85a887a256d6939da88aabd8cd\n }\n connect_timeout 3\n retry 3\n delay_before_retry 3\n }\n }\n}\n\n\n\n\nvirtual_server 10.10.10.2 1358 {\n delay_loop 6\n lb_algo rr\n lb_kind NAT\n persistence_timeout 50\n protocol TCP\n\n\n\n\n sorry_server 192.168.200.200 1358\n\n\n\n\n real_server 192.168.200.2 1358 {\n weight 1\n HTTP_GET {\n url {\n path /testurl/test.jsp\n digest 640205b7b0fc66c1ea91c463fac6334d\n }\n url {\n path /testurl2/test.jsp\n digest 640205b7b0fc66c1ea91c463fac6334d\n }\n url {\n path /testurl3/test.jsp\n digest 640205b7b0fc66c1ea91c463fac6334d\n }\n connect_timeout 3\n retry 3\n delay_before_retry 3\n }\n }\n\n\n\n\n real_server 192.168.200.3 1358 {\n weight 1\n HTTP_GET {\n url {\n path /testurl/test.jsp\n digest 640205b7b0fc66c1ea91c463fac6334c\n }\n url {\n path /testurl2/test.jsp\n digest 640205b7b0fc66c1ea91c463fac6334c\n }\n connect_timeout 3\n retry 3\n delay_before_retry 3\n }\n }\n}\n\n\n\n\nvirtual_server 10.10.10.3 1358 {\n delay_loop 3\n lb_algo rr\n lb_kind NAT\n persistence_timeout 50\n protocol TCP\n\n\n\n\n real_server 192.168.200.4 1358 {\n weight 1\n HTTP_GET {\n url {\n path /testurl/test.jsp\n digest 640205b7b0fc66c1ea91c463fac6334d\n }\n url {\n path /testurl2/test.jsp\n digest 640205b7b0fc66c1ea91c463fac6334d\n }\n url {\n path /testurl3/test.jsp\n digest 640205b7b0fc66c1ea91c463fac6334d\n }\n connect_timeout 3\n retry 3\n delay_before_retry 3\n }\n }\n\n\n\n\n real_server 192.168.200.5 1358 {\n weight 1\n HTTP_GET {\n url {\n path /testurl/test.jsp\n digest 640205b7b0fc66c1ea91c463fac6334d\n }\n url {\n path /testurl2/test.jsp\n digest 640205b7b0fc66c1ea91c463fac6334d\n }\n url {\n path /testurl3/test.jsp\n digest 640205b7b0fc66c1ea91c463fac6334d\n }\n connect_timeout 3\n retry 3\n delay_before_retry 3\n }\n }\n}\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"九、启动keepalived master节点和backup 节点","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"# 启动\nsystemctl start keepalived\n\n# 停止\nsystemctl stop keepalived\n\n# 查看状态\nsystemctl status keepalived\n\n# 查看keepalived 日志\ntail -f /var/log/messages","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"十、启动之后,使用命令 ip addr 查看,虚拟IP已经飘到Master节点对应的网卡","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/69/696077b9f02839aab216a9492289931e.png","alt":"","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"​","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"注意事项-踩坑总结:","attrs":{}}]},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"需要注意,keepalived 中配置文件的执行时间间隔,不能与nginx检测脚本中的 sleep 时间间隔相等。这样容易出现nginx 挂了以后,不能使 keepalived 自动停止。","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"在离线服务器安装的时候,我这边出现了一个问题。安装 openssl-devel 的时候,服务器已经安装了一个相对较低版本的 krb5-libs。导致yum 源中升级的时候,一直升级不成功。所以,openssl-devel 就安装不成功,进而导致keepalived 不能安装成功。所以需要在yum 安装额时候,加上 -v 参数,查看更信息的报错信息。然后找到具体那个包安装不成功,直接使用yum remove 命令卸载掉,重新安装。就可以安装成功。这个是个大坑。","attrs":{}}]}],"attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章