1. 程式人生 > >利用keepalived實現nginx調度器高可用(二)

利用keepalived實現nginx調度器高可用(二)

font 相同 () ins db_name lob list default ast


構建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 {
root@localhost
}
notification_email_from keepalived@localhost
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 {
root@localhost
}
notification_email_from keepalived@localhost
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調度器高可用(二)