{"id":6753,"date":"2017-06-03T18:01:24","date_gmt":"2017-06-03T10:01:24","guid":{"rendered":"http:\/\/rmohan.com\/?p=6753"},"modified":"2017-06-03T18:01:24","modified_gmt":"2017-06-03T10:01:24","slug":"keepalived-nginx-dual-primary-high-availability-load-balancing-cluster","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=6753","title":{"rendered":"Keepalived Nginx Dual Primary High Availability Load Balancing Cluster"},"content":{"rendered":"<p><strong>Keepalived Nginx Dual Primary High Availability Load Balancing Cluster<\/strong><\/p>\n<p>Purpose of the experiment: Use keepalived to implement Nginx&#8217;s dual main high availability load balancing cluster.<\/p>\n<p>Experimental environment: two Nginx proxy (two main Nginx, each need two NIC, eth0 connection network, eth1 connected to the external network), two web server (request load balancing), a client used to verify the results.<\/p>\n<p>Note: In order not to affect the experimental results, before the start of the experiment to close iptables and selinux<\/p>\n<p>Steps:<\/p>\n<p>First, configure IP<\/p>\n<p>1. Configure the IP of the A host<\/p>\n<p># Ip addr add dev eth0 192.168.1.2\/24<\/p>\n<p>2. Configure the IP of the B host<\/p>\n<p># Ip addr add dev eth0 192.168.1.23\/24<\/p>\n<p>3. Configure the IP of the C host<\/p>\n<p># Ip addr add dev eth0 192.168.1.3\/24<\/p>\n<p>4. Configure the IP of the D host<\/p>\n<p># Ip addr add dev eth0 192.168.1.33\/24<\/p>\n<p>Second, the configuration of web services (C and D hosts have done the same configuration, just modify the default home page IP address for the machine can be IP to distinguish)<\/p>\n<p>1. install apache<\/p>\n<p># Yum -y install apache<\/p>\n<p>2. Create a default home page<\/p>\n<p># Vim \/var\/www\/html\/index.html<\/p>\n<p><H1> 192.168.1.3 <\/ h1><\/p>\n<p>3. Start apache<\/p>\n<p># Service httpd start<\/p>\n<p>Third, the configuration sorry_server (This service is configured on the Nginx proxy host, two Nginx proxy do the same configuration, just modify the default home page IP address for the machine can be IP to distinguish)<\/p>\n<p>1. install apache<\/p>\n<p># Yum -y install apache<\/p>\n<p>2. Create a default home page<\/p>\n<p># Vim \/var\/www\/html\/index.html<\/p>\n<p><H1> sorry_server: 192.168.1.2 <\/ h1><\/p>\n<p>3. Modify the listening port to 8080 to avoid conflicts with the ports that nginx listens on<\/p>\n<p># Vim \/etc\/httpd\/conf\/httpd.conf<\/p>\n<p>Listen 8080<\/p>\n<p>4. Start apache service<\/p>\n<p>Fourth, the configuration agent (two Nginx proxy do the same configuration)<\/p>\n<p>1. Install nginx<\/p>\n<p># Yum -y install nginx<\/p>\n<p>2. Define the upstream cluster group, defined in the http {} section;<\/p>\n<p># Vim \/etc\/nginx\/nginx.conf<\/p>\n<p>        Http {<\/p>\n<p>            Upstream websrvs {<\/p>\n<p>                Server 192.168.1.3:80;<\/p>\n<p>                Server 192.168.1.33:80;<\/p>\n<p>                Server 127.0.0.1:8080 backup;<\/p>\n<p>            }<\/p>\n<p>        }<\/p>\n<p>3. Call the defined cluster group and call it in the location {} section of the server {} section;<\/p>\n<p># Vim \/etc\/nginx\/conf.d\/default.conf<\/p>\n<p>        Server {<\/p>\n<p>            Location \/ {<\/p>\n<p>                Proxy_pass  http: \/\/ wersrvs ;<\/p>\n<p>                Index index.html;<\/p>\n<p>            }<\/p>\n<p>        }<\/p>\n<p>4. Start the service<\/p>\n<p># Service nginx start<\/p>\n<p>Fifth, configure keepalived<\/p>\n<p>A on the host operation<\/p>\n<p>1. Install keepalived<\/p>\n<p># Yum -y install keepalived<\/p>\n<p>2. Edit the A host&#8217;s configuration file \/etc\/keepalived\/keepalived.conf, as follows:<\/p>\n<p>! Configuration File for keepalived<\/p>\n<p>    Global_defs {<\/p>\n<p>    Notification_email {<\/p>\n<p>        Root @ localhost<\/p>\n<p>    }<\/p>\n<p>    Notification_email_from keepalived @ localhost<\/p>\n<p>    Smtp_server 127.0.0.1<\/p>\n<p>    Smtp_connect_timeout 30<\/p>\n<p>    Router_id CentOS 6<\/p>\n<p>    Vrrp_mcast_group4 224.0.100.39<\/p>\n<p>    }<\/p>\n<p>    Vrrp_script chk_down {<\/p>\n<p>        Script &#8220;[[-f \/ etc \/ keepalived \/ down]] &#038;&#038; exit 1 || exit 0&#8221;<\/p>\n<p>        Interval 1<\/p>\n<p>        Weight -5<\/p>\n<p>    }<\/p>\n<p>    Vrrp_script chk_nginx {<\/p>\n<p>        Script &#8220;killall -0 nginx &#038;&#038; exit 0 || exit 1&#8221;<\/p>\n<p>        Interval 1<\/p>\n<p>        Weight -5<\/p>\n<p>        Fall 2<\/p>\n<p>        Rise<\/p>\n<p>    }<\/p>\n<p>    Vrrp_instance ngx {<\/p>\n<p>        State MASTER<\/p>\n<p>        Interface eth1<\/p>\n<p>        Virtual_router_id 14<\/p>\n<p>        Priority 100<\/p>\n<p>        Advert_int 1<\/p>\n<p>        Authentication {<\/p>\n<p>            Auth_type PASS<\/p>\n<p>            Auth_pass MDQ41fTp<\/p>\n<p>        }<\/p>\n<p>        Virtual_ipaddress {<\/p>\n<p>            192.168.20.100\/24 ??dev eth1<\/p>\n<p>        }<\/p>\n<p>        Track_script {<\/p>\n<p>            Chk_down<\/p>\n<p>            Chk_nginx<\/p>\n<p>        }<\/p>\n<p>    }<\/p>\n<p>    Vrrp_instance ngx2 {<\/p>\n<p>        State BACKUP<\/p>\n<p>        Interface eth1<\/p>\n<p>        Virtual_router_id 15<\/p>\n<p>        Priority 98<\/p>\n<p>        Advert_int 1<\/p>\n<p>        Authentication {<\/p>\n<p>            Auth_type PASS<\/p>\n<p>            Auth_pass XYZ41fTp<\/p>\n<p>        }<\/p>\n<p>        Virtual_ipaddress {<\/p>\n<p>            192.168.20.200\/24 ??dev eth1<\/p>\n<p>        }<\/p>\n<p>        Track_script {<\/p>\n<p>            Chk_down<\/p>\n<p>        Chk_nginx<\/p>\n<p>        }<\/p>\n<p>    }<\/p>\n<p>B host also for the same configuration, a little change can be, need to modify the following places:<\/p>\n<p>    Vrrp_instance ngx {<\/p>\n<p>        State BACKUP<\/p>\n<p>        Priority 98<\/p>\n<p>    }<\/p>\n<p>    Vrrp_instance ngx2 {<\/p>\n<p>        State MASTER<\/p>\n<p>        Priority 100<\/p>\n<p>    }<\/p>\n<p>6, simulation failure, verify the results<\/p>\n<p>1. Start the two Nginx proxy keepalived services<\/p>\n<p># Service keepalived start<\/p>\n<p>2. Visit 192.168.20.100, the result should be the back-end web server polling response request<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Keepalived Nginx Dual Primary High Availability Load Balancing Cluster<\/p>\n<p>Purpose of the experiment: Use keepalived to implement Nginx&#8217;s dual main high availability load balancing cluster.<\/p>\n<p>Experimental environment: two Nginx proxy (two main Nginx, each need two NIC, eth0 connection network, eth1 connected to the external network), two web server (request load balancing), a client used [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6753"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6753"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6753\/revisions"}],"predecessor-version":[{"id":6754,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6753\/revisions\/6754"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}