码迷,mamicode.com
首页 > 其他好文 > 详细

keepalived实现nginx调度器高可用(二)

时间:2018-06-15 23:32:25      阅读:53      评论:0      收藏:0      [点我收藏+]

标签:exit   ima   install   修改   memcached   服务器配置   成功   www   res   

    keepalived实现nginx调度器高可用(二) 

构建LNAMMP架构:
1) Nginx既是前端调度器,又是反向代理缓存服务器;
2) 将php的session缓存于memcached中;
3) 使用php-fpm上部署Discuz论坛程序;
4) 使用https连接,即使用户使用的是http协议也可以以https协议进行访问;

    声明:提供5台主机,其中两台nginx做前端调度器(一台做主调度器:172.16.1.11主机,一台做备用调度器:172.16.1.20主机),

              另外两台主机做web服务器向外提供php-fpm服务;(172.16.1.12主机和172.16.1.13主机)

              一台提供memcache服务(172.16.1.14主机)

           框架如图:

            技术分享图片

          

        1.在两台nginx上配置nginx反代服务

            # vim /etc/nginx/nginx.conf

             在http上下文中添加下文:

             upstream webser {
             server 172.16.1.12:80 weight=1;
             server 172.16.1.13:80 weight=1;
             }
             server {
                 listen       80 default_server;

                 location / {
                       proxy_pass http://webser;
                 }

             }

            配置完成后,启动nginx服务,使配置生效;

        

         2.配置keepalived+nginx_master(172.16.1.11主机):(前提,安装keepalived : # yum install keepalived)

           # vim /etc/keepalived/keepalived.conf

              ! Configuration File for keepalived

             global_defs {
                     notification_email {
                                [email protected]
                     }
                     notification_email_from [email protected]
                     smtp_server 127.0.0.1
                     smtp_connect_timeout 30
                     router_id drct1
                    vrrp_mcast_group4 224.0.100.18
             }
             vrrp_script check_httpd {
                    script "killall -0 nginx && exit 0 || exit 1"
                    interval 1
                    weight -20
              }
              vrrp_instance VI_1 {
                    state BACKUP
                    interface ens33
                    virtual_router_id 51
                    priority 100
                    advert_int 1
                    authentication {
                           auth_type PASS
                           auth_pass cxqLUKJh(# openssl rand -base64 8 生成的密码取前八位)
                    }
                   virtual_ipaddress {
                          172.16.1.254/16
                    }
                   track_script {
                           check_httpd
                    }
               }

            启动keepalived服务:

             # systemctl start keepalived.service


            3.配置keepalived+nginx_backup(172.16.1.13主机):

              

           # vim /etc/keepalived/keepalived.conf

              ! Configuration File for keepalived

             global_defs {
                     notification_email {
                                [email protected]
                     }
                     notification_email_from [email protected]
                     smtp_server 127.0.0.1
                     smtp_connect_timeout 30
                     router_id drct2
                    vrrp_mcast_group4 224.0.100.18
             }
             vrrp_script check_httpd {
                    script "killall -0 nginx && exit 0 || exit 1"
                    interval 1
                    weight -20
              }
              vrrp_instance VI_1 {
                    state BACKUP
                    interface ens33
                    virtual_router_id 51
                    priority 90
                    advert_int 1
                    authentication {
                           auth_type PASS
                           auth_pass cxqLUKJh
                    }
                   virtual_ipaddress {
                          172.16.1.254/16
                    }
                   track_script {
                           check_httpd
                    }
               }

            配置完成后,启动keepalived服务:

                   # systemctl start keepalived


           4.配置后端web服务器

              1)安装httpd,php-fpm,php-mysql,mariadb,php-pecl-memcache

                    # yum install http php-fpm php-mysql mariadb php-pecl-memcache

              2) 做php测试页面:

                    webserver1:

                         vim /var/www/html/index.php

                         Real Server 1
                        <?php
                        phpinfo();
                        ?>

                    webserver2:

                         vim /var/www/html/index.php                    

                         Real Server 2
                        <?php
                          phpinfo();
                        ?>

              3) 启动http服务,php-fpm服务和数据库服务:

                     # systemctl start httpd

                     # systemctl start php-fpm

                     # systemctl start mariadb

             先检测index.php页面是否可用:

              web测试:172.16.1.12/index.php    

               技术分享图片

              再测试172.16.1.13/index.php 

              技术分享图片

              2)将php的session缓存于memcached中

                     在两台提供php-fpm的主机上修改文件:

                     vim /etc/php-fpm.d/www.conf:

                     将最后两行修改为:

                          php_value[session.save_handler] = memcache
                          php_value[session.save_path] = "tcp://172.16.1.14:11211?persistent=1&weight=1&timeout=1&retry_interval=15"

                     目的是将php的客户端请求缓存于172.16.1.14主机的memcache中;

       

         

         5.memcache服务器配置:
                   安装memcache并启动服务:

                   # yum install memcached

                   # systemctl  start memcached

        6.上述操作完成后,测试调度器能否成功访问php-fpm服务器:

                 web端输入172.16.1.254/index.php            

                 技术分享图片

             继续访问:

                 技术分享图片




        7.确保调度器,php-fpm服务器,memcache服务器都能正常工作后:

               1)在两台php-fpm服务器上部署wordpress论坛,放到/var/www/html/目录下(这里不做赘述)

                  # cd /var/www/html/

                  # ls

                     index.html  index.php   wordpress

               2) 为论坛建立一个数据库并授权一个用户(两台服务器做相同操作)

                     # mysql -p

                        ...

                       MariaDB [(none)]> create database wordpress;

                       MariaDB [(none)]> use wordpress;
                       Reading table information for completion of table and column names
                       You can turn off this feature to get a quicker startup with -A

                      Database changed
                      MariaDB [wordpress]> grant all on *.* to 'ytc'@'localhost' identified by '123456';
                      Query OK, 0 rows affected (0.01 sec)

                      MariaDB [wordpress]> flush privileges;
                      Query OK, 0 rows affected (0.04 sec)

               3)切换到/var/www/html/wordpress目录下:

                      # cd /var/www/html/wordpress/

                      # cp wp-config-sample.php  wp-config.php

                      # vim wp-config.php

                          /** WordPress数据库的名称 */
                         define('DB_NAME', 'wordpress');

                         /** MySQL数据库用户名 */
                         define('DB_USER', 'myuser');

                        /** MySQL数据库密码 */
                        define('DB_PASSWORD', '123456');

                        /** MySQL主机 */
                        define('DB_HOST', '172.16.1.13');

               4)    可使用nfs或samba使两台php-fpm服务器数据库同步(我的博客中有,这里不再重复)

        


           8.在web端向调度器请求访问Wordpress论坛:

                       输入:172.16.1.254/wordpress/index.php

                       技术分享图片

                      登录后:

                        技术分享图片

                       这样就可以发布文章了;


                     


keepalived实现nginx调度器高可用(二)

标签:exit   ima   install   修改   memcached   服务器配置   成功   www   res   

原文地址:http://blog.51cto.com/yuantianchi/2129976

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!