1. 程式人生 > >ansible管理實現LNAMMP架構(一)

ansible管理實現LNAMMP架構(一)

var path 分享 define 調度器 package 創建 運行 創建數據庫

ansible管理實現LNAMMP架構(一)


keepalived主/備模式高可用{nginx(proxy)|lvs}

兩臺主機(主/備)高可用nginx(proxy)

兩臺主機:httpd + php-fpm + Discuz或 phpMyAdmin+php-mysql

一臺主機:memcached用來緩存php的session;
一臺主機:mysql-server或mariadb-server;


(一):172.16.75.2做ansible服務器(主控設備),其余皆為被管設備;

(二): 172.16.1.11主機做主nginx調度器並實現反代功能;

172.16.1.12主機做備nginx調度器

(三): 172.16.1.13和172.16.1.14主機做web服務器向外提供web服務;

(四): 172.16.1.15主機安裝memcached

(六) : 172.16.1.16主機安裝mariadb-server包,提供數據庫服務


大前提:實現主控設備172.16.75.2對被管設備的免密碼登錄:

]# ssh-keygen -t rsa -P ''

]# ssh-copy-id -i .ssh/id_rsa.pub [email protected]

]# ssh-copy-id -i .ssh/id_rsa.pub [email protected]

...

]# ssh-copy-id -i .ssh/id_rsa.pub [email protected]


以下操作均在主控設備172.16.75.2主機上完成:

安裝ansible,keepalived,nginx,httpd,php-fpm

# yum install ansible keepalived nginx httpd php-fpm

1. 創建主機清單,定義主機分組

# vim /etc/ansible/hosts

[hasrvs]
172.16.1.11
172.16.1.12

[websrvs]
172.16.1.13
172.16.1.14

[memsrvs]
172.16.1.15

[mysqlsrvs]

172.16.1.16


2. 給調度器提供安裝包和必要的配置文件並啟動服務

1) vim /etc/ansible/hasrvs.yaml

- hosts: hasrvs
remote_user: root
tasks:
- name: install nginx package
yum : name=nginx state=present
- name: install keepalived package
yum : name=keepalived state=present
- name: provide nginx configure file
copy: src=/etc/nginx/nginx.conf dest=/etc/nginx/nginx.conf
- name: provide keepalived configure file
copy: src=/etc/keepalived/keepalived.conf dest=/etc/keepalived/keepalived.conf
- name: start nginx service
service: name=nginx state=started enabled=true
- name: start keepalived service
service: name=keepalived state=started enabled=true


2) 給nginx提供配置文件,在http配置段中添加upstream和server上下文:

upstream websrvs {
server 172.16.1.13:80 weight=1;
server 172.16.1.14:80 weight=1;
}

server {
listen 80 default_server;
location / {
proxy_pass http://websrvs;
}
}

註意:在原有的nginx配置文件中,http配置段已存在server上下文,在這需要把原有的默認監聽的服務禁用

即 # listen 80 default_server;

3) 給主nginx調度器提供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 MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass axtyXIHt
}
virtual_ipaddress {
172.16.1.254/16
}
track_script {
check_httpd
}
}

4)運行hasrvs.yaml,先預運行,無錯誤在運行

]# ansible-playbook -C hasrvs.yaml

]# ansible-playbook hasrvs.yaml


5) 給備nginx調度器提供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 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 axtyXIHt
}
virtual_ipaddress {
172.16.1.254/16
}
track_script {
check_httpd
}
}


6)給備nginx提供啟動腳本:

]# vim /etc/ansible/backuphasrvs.yaml

- hosts: 172.16.1.12
remote_user: root
tasks:
- name: provide keepalived configure file
copy: src=/etc/keepalived/keepalived.conf dest=/etc/keepalived/keepalived.conf
- name: start keepalived service
service: name=keepalived state=started enabled=true

7) 運行backuphasrvs.yaml,先預運行,無錯誤在運行

]# ansible-playbook -C backuphasrvs.yaml

]# ansible-playbook backuphasrvs.yaml



3 給websrvs主機組提供配置文件,安裝包並啟動:

1) ]# vim /etc/ansible/websrvs.yaml

- hosts: websrvs
remote_user: root
tasks:
- name: install httpd package
yum : name=httpd state=present

- name: install php-fpm package
yum : name=php-fpm state=present
-name : install php-mysql
yum : name=php-mysql state=present
- name: provide httpd configure file
copy: src=/etc/httpd/conf/httpd.conf dest=/etc/httpd/conf/httpd.conf
- name: start httpd service
service: name=httpd state=started enabled=true

- name: start php-fpm service
service: name=php-fpm state=started enabled=true


2) 編輯httpd的主配置文件

]# vim /etc/httpd/conf/httpd.conf(找到相應位置編輯即可)

DocumentRoot "/var/www/html"
Proxyrequests off
Proxypassmatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/var/www/html/$1


<IfModule dir_module>
DirectoryIndex index.html index.php()
</IfModule>

3) 運行websrvs.yaml,先預運行,無錯誤在運行

]# ansible-playbook -C websrvs.yaml

]# ansible-playbook websrvs.yaml


4.提供一個測試頁面,無誤後,提供論壇系統:

1) 提供一個測試頁面,為了加以區分,兩臺web服務器的測試頁面略有區別:

172.16.1.13主機做web server 1, 172.16.1.14主機做web server 2

]# vim /var/www/html/index.php

Web Server 1

<?php

phpinfo();

?>

將該測試頁面傳送給172.16.1.13主機,這裏就不再寫劇本了:

]# ansible 172.16.1.13 -m copy -a "src=/var/www/html/index.php dest=/var/www/html/index.php"


同理給172.16.1.14主機也提供這樣一個頁面:

]# vim /var/www/html/index.php

Web Server 1

<?php

phpinfo();

?>

]# ansible 172.16.1.14 -m copy -a "src=/var/www/html/index.php dest=/var/www/html/index.php"


2) 在web端進行頁面測試,先直接訪問服務器172.16.1.13和172.16.1.14沒有問題後,在訪問172.16.1.254

在這我已測試兩臺web服務器沒有問題,故直接測試nginx調度器組的虛擬接口172.16.1.254

技術分享圖片


技術分享圖片

註意:這裏需要關閉防火墻,否則可能無法訪問測試頁面:

]# iptables -F


3) 開始部署wordpress論壇系統:

聲明:我在主控設備172.16.75.2上的/var/www/html目錄下已經放置了安裝包並已解壓

可以使用ansible命令,也可以選擇劇本:

法一: ansible命令:

]# ansible websrvs -m copy -a "src=/var/www/html/wordpress dest=/var/www/html/wordpress"

法二: 劇本:

]# vim /etc/ansible/websrvs2.yaml

- hosts: websrvs
remote_user: root
tasks:

- name: provide wordpress
copy: src=/var/www/html/wordpress dest=/var/www/html/wordpress

運行劇本:

]# ansible-playbook websrvs2.yaml


5 給數據庫服務器172.16.1.16安裝mariadb-server,並給論壇創建一個名為wordpress的數據庫,並授權用戶

提供登錄論壇的密碼:


1)安裝mariadb-server包,可使用劇本也可直接使用命令,自行選擇

因為此處只有一個數據庫服務器,所以選擇命令直接安裝,但如果服務器較多,建議使用劇本;

]# ansible 172.16.1.14 -m yum -a "name=mariadb state=present"(建議加'-C'選項預運行)

啟動數據庫服務:

]# ansible 172.16.1.14 -m service -a "name=mariadb state=started"

創建數據庫並授權用戶:

此處我在172.16.1.16主機上直接操作:

]# mysql -p

Enter password:

......

MariaDB [(none)]> create database wordpress;

MariaDB [(none)]> grant all on *.* to 'ytc'@'172.16.%.%' identified by '123456';

MariaDB [(none)]> flush privileges;


2)給論壇提供相關配置文件:

]# 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', 'ytc');

/** MySQL數據庫密碼 */
define('DB_PASSWORD', '123456');

/** MySQL主機 */
define('DB_HOST', '172.16.1.16');


3) 將配置文件部署在websrvs上:

]# vim /etc/ansible/websrvs3.yaml

- hosts: websrvs
remote_user: root
tasks:
- name: provide wordpress configure file
copy: src=/var/www/html/wordpress/wp-config.php

執行腳本:
]# ansible-playbook websrvs3.yaml

4) 在web端登錄wordpress論壇:

技術分享圖片


技術分享圖片

接下來就可以發布文章了。


註意:1)如果訪問過程中出現“建立數據庫連接時出錯”字樣時,可能時SELINUX的幹擾將其關閉即可:

即:]# setenforce 0

2)登錄論壇後,若出現.php系列的文件列表,而不是上圖的登錄界面,查看httpd的主配置文件

<IfModule dir_module>
DirectoryIndex index.html index.php
()
</IfModule>

加上index.php即可。


6 . 配置memcached服務器啟動服務:

] # ansible memsrvs -m yum -a "name=memcached state=present"

] # ansible memsrvs -m service -a "name=memcached state=started"

需在websrvs上安裝php-pecl-memcached包,提供web服務器和memcached服務器連接的接口

] # ansible websrvs -m yum -a "name=php-peclmemcached state=present"

編輯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"

將更改後的文件發送給websrvs並重啟php-fpm服務:

]# ansible websrvs -m copy -a "src= /etc/php-fpm.d/www.conf dest= /etc/php-fpm.d/www.conf "

]# ansible websrvs -m service -a "name=php-fpm state=restarted"


這樣利用ansible工具部署LNAMMP架構就完成了。











































ansible管理實現LNAMMP架構(一)