Ansible部署小型企業服務框架+wordpress

1、前言
ansible做爲一款靈活、高效、功能豐富的自動化部署工具在企業運維管理中備受推崇。通過測試,我來使用ansible部署小型企業服務框架,實現高可用、負載均衡的目標。若有錯誤敬請賜教。
目標環境拓撲:
Ansible部署小型企業服務框架+wordpressphp

環境介紹:
前端代理層由兩臺nginx實現,並安裝keepalived實現地址滑動達成高可用。 web層由兩套Apache+PHP+WordPress 構建應用環境。數據層由一臺mariadb組成,篇幅限制這裏並無作數據庫主從複製、讀寫分離(實際環境數據庫必定要實現這兩項功能)。
IP一覽:
主機名 IP 功能
ansible-master 192.168.23.63 ansible管理機
ansible-vs1 192.168.23.71 vs1(nginx+keepalived)
ansible-vs2 192.168.23.72 vs2(nginx+keepalived)
ansible-rs1 192.168.23.61 rs1(httpd+php+wordpress)
ansible-rs2 192.168.23.62 rs2(httpd+php+wordpress)
ansible-mariadb 192.168.23.73 mariadbhtml

環境介紹:
前端代理層由兩臺nginx實現,並安裝keepalived實現地址滑動達成高可用。 web層由兩套Apache+PHP+WordPress 構建應用環境。數據層由一臺mariadb組成,篇幅限制這裏並無作數據庫主從複製、讀寫分離(實際環境數據庫必定要實現這兩項功能)。
IP一覽:
主機名 IP 功能
ansible-master 192.168.23.63 ansible管理機
ansible-vs1 192.168.23.71 vs1(nginx+keepalived)
ansible-vs2 192.168.23.72 vs2(nginx+keepalived)
ansible-rs1 192.168.23.61 rs1(httpd+php+wordpress)
ansible-rs2 192.168.23.62 rs2(httpd+php+wordpress)
ansible-mariadb 192.168.23.73 mariadb
  
2、環境準備:
2.1 管理端安裝ansible,配置ssh祕鑰使主機間實現基於密鑰的認證,測試是否能夠互相鏈接!
ssh-keygen -t rsa #三次回車,中途的問題是問祕鑰存放位置(默認/root/.ssh),是否加密祕鑰。實驗方便這裏不加密。
ssh-copy-id -i .ssh/id_rsa.pub root@192.168.23.71 #將公鑰發送給目標主機
ssh-copy-id -i .ssh/id_rsa.pub root@192.168.23.72
ssh-copy-id -i .ssh/id_rsa.pub root@192.168.23.73
ssh-copy-id -i .ssh/id_rsa.pub root@192.168.23.61
ssh-copy-id -i .ssh/id_rsa.pub root@192.168.23.62前端

2.2 編輯ansible的hosts文件,定義全部的主機
vim /etc/ansible/hosts
[web]
192.168.23.61
192.168.23.62
[nginx]
192.168.23.71 state=MASTER priority=100
192.168.23.72 state=BACKUP priotity=90
[mysql]
192.168.23.73mysql

2.3 爲全部主機同步時間
ansible all -a 'ntpdate cn.pool.ntp.org'linux

2.4 建立ansible相關角色的目錄
mkdir -pv /etc/ansible/roles/{mysql,web,nginx}/{files,tasks,templates,vars,handlers,meta}
3、配置web的playbook:
3.1 建立tasks文件
vim /etc/ansible/roles/web/tasks/main.ymlnginx

  • name: install web pakgs
    yum: name={{ item }}
    with_items:
    • httpd
    • php
    • php-mysql
  • name: config web
    copy: src=httpd.conf dest=/etc/httpd/conf/httpd.conf
    notify: restart the service # 注意這裏要與handlers裏定義的name相同 ## notify 通知
  • name: copy wordpress
    synchronize: src=wordpress dest=/var/www/html/wordpress/
    ##synchronize 使…..同步web

  • name: restart the service
    service: name=httpd state=started

3.2 建立handles
vim /etc/ansible/roles/web/handlers/main.ymlsql

  • name: restart the service #就這
    service: name=httpd state=restarted

3.3 添加要複製過去的配置文件
放在/etc/ansible/roles/web/files/下 ① WordPress目錄 ② httpd.conf #從別的地方考過來 #修改WordPress 配置文件shell

3.4 修改WordPress鏈接數據庫的配置文件
cd wordpress
cp wp-config-sample.php wp-config.php
vim wp-config.php
/* WordPress數據庫的名稱 /
define('DB_NAME', 'wp');數據庫

/* MySQL數據庫用戶名 /
define('DB_USER', 'wpuser');

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

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

/* 建立數據表時默認的文字編碼 /
define('DB_CHARSET', 'utf8');
##define 定義,規定

3.5 添加web主劇本
vim /etc/ansible/web.yml

  • hosts: web
    remote_user: root
    roles:
    • web

3.6 測試,沒問題的話就下一步
(-C, –check 指定該參數後,執行playbook文件不會真正去執行,而是模擬執行一遍,而後輸出本次執行會對遠程主機形成的修改)
ansible-playbook -C /etc/ansible/web.yml

4、配置代理層:
4.1 添加task任務
vim /etc/ansible/roles/nginx/tasks/main.yml

  • name: install package
    yum: name={{ item }}
    with_items:
    • nginx
    • keepalived
  • name: config keepalived
    template: src=keepalived.conf.j2 dest=/etc/keepalived/keepalived.conf
    notify: restart keepalived
  • name: config nginx
    template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
    notify: restart nginx
  • name: start service
    service: name={{ item }} state=started enabled=yes
    with_items:
    • keepalived
    • nginx

4.2 添加handlers
vim /etc/ansible/roles/nginx/handlers/main.yml

  • name: restart keepalived
    service: name=keepalived state=restarted
  • name: restart nginx
    service: name=nginx state=restarted

4.3 準備template文件
①keepalived.conf.j2 ②nginx.conf.j2

4.4 修改keepalived模板文件keepalived.conf.j2
global_defs {
notification_email {br/>acassen@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id {{ansible_hostname}} #自帶變量,經過ansible 主機IP -m setup 查詢
vrrp_mcast_group4 224.0.0.43
}

vrrp_instance VI_1 {
state {{ state }} #已經過hosts文件定義變量
interface ens33 #網卡名
virtual_router_id 51
priority {{ priority }}
advert_int 1
authentication {
auth_type PASS
auth_pass lovelinux #設置密碼
}
virtual_ipaddress {
192.168.23.88 #虛擬IP
}
}

4.5 修改nginx模板文件nginx.conf.j2(定義在http段)
events {
worker_connections 1024; ## Default: 1024
}
http
{
upstream servers2.mydomain.com {
server 192.168.23.61;
server 192.168.23.62;
ip_hash;

}

server{
listen 80;
server_name www.mydomain.com;
root /usr/share/nginx/html;

location / {
    proxy_pass http://servers2.mydomain.com;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

}
}

4.6 添加nginx主劇本
vim /etc/ansible/nginx.yml

  • hosts: nginx
    remote_user: root
    roles:
    • nginx

4.7 測試,沒問題的話就下一步
ansible-playbook -C /etc/ansible/nginx.yml

5、配置mariadb:
5.1 配置mariadb的任務清單
vim /etc/ansible/roles/mysql/tasks/main.yml
roles/mysql/tasks/main.yml

  • name: install mariadb
    yum: name=mariadb-server
  • name: copy sql file
    copy: src=mysql.sql dest=/tmp/mysql.sql
  • name: start mysql service
    service: name=mariadb state=started
  • name: config mysql
    shell: "mysql < /tmp/mysql.sql"

5.2 設置files文件
vim /etc/ansible/roles/mysql/files/mysql.sql
CREATE DATABASE wp;
GRANT ALL ON wp.* TO 'wpuser'@'%' IDENTIFIED BY 'lovelinux';

5.3 添加mysql主劇本
vim /etc/ansible/mysql.yml

  • hosts: mysql
    remote_user: root
    roles:
    • mysql

5.4 測試,沒問題的話就下一步
ansible-playbook -C /etc/ansible/mysql.yml

6、開始表演(執行劇本):
6.1 目錄結構

6.2 分別執行
ansible-playbook web.yml

ansible-playbook nginx.yml

ansible-playbook mysql.yml

6.3 訪問頁面
http://192.168.23.88/wordpress

7、項目總結:7.1 在定義web的playbook時複製wordpress時開始用的是copy模塊執行老是不成功,報錯,google找到用synchronize模塊要比copy模塊高效安全的多,synchronize採用rsync複製文件,因此係統必須安裝rsync 包不然沒法使用這個模塊。使用該模塊的優勢有① 增量複製(只複製與目標主機有差別的文件) ② 複製時採用壓縮,對複製大文件支持優秀(用copy複製大文件會出錯)1

相關文章
相關標籤/搜索