一、測試環境: centos 6.6;使用8臺虛擬機(上圖)
分別對它們設置主機名:
主機名 | ip地址 | 軟件包 |
node1 | 172.16.16.11 | keepalived+haproxy |
node2 | 172.16.16.12 | keepalived+haproxy |
php1 | 172.16.16.2 | httpd+php+php-mysql+nfs-utils |
php2 | 172.16.16.8 | httpd+php+php-mysql+nfs-utils |
web1 | 172.16.16.3 | httpd |
web2 | 172.16.16.4 | httpd |
mysql | 172.16.16.5 | mariadb-5.5.43-linux-x86_64.tar.gz |
nfs | 172.16.16.252 | nfs-utils |
上述服務器實現的作用介紹:
①keepalived+haproxy,實現高可用和haproxy的反向代理功能並實現動靜分離的效果;請求動態的內容交給後端的動態服務器組(php1或php2);靜態內容交給靜態服務器組(web1和web2)
②mysql服務器存儲discuz數據庫
③nfs文件共享服務器設置2個目錄/mydata和/discuz;前者給mysql服務器用做當datadir使用;後者給web和php服務器使用,共享discuz網頁文件
二、準備工作:這裏基於ansible的playbook部署
1、ansible使用環境爲centos 7;設置如下
# ssh-keygen -t rsa -P '' # ssh-copy-id -i ~/.ssh/id_rsa.pub node1 # ssh-copy-id -i ~/.ssh/id_rsa.pub node2 # ssh-copy-id -i ~/.ssh/id_rsa.pub web1 # ssh-copy-id -i ~/.ssh/id_rsa.pub web2 # ssh-copy-id -i ~/.ssh/id_rsa.pub php1 # ssh-copy-id -i ~/.ssh/id_rsa.pub php2 # ssh-copy-id -i ~/.ssh/id_rsa.pub mysql # ssh-copy-id -i ~/.ssh/id_rsa.pub nfs
2、ansible設置及管理節點探測:
# vim /etc/ansible/host [haproxy] 172.16.16.11 172.16.16.12 [dynamic_servers] 172.16.16.2 172.16.16.8 [static_servers] 172.16.16.3 172.16.16.4 [db] 172.16.16.5 [nfs] 172.16.16.252 # ansible all -m ping 172.16.16.12 | success >> { "changed": false, "ping": "pong" } 172.16.16.3 | success >> { "changed": false, "ping": "pong" } 172.16.16.11 | success >> { "changed": false, "ping": "pong" } 172.16.16.4 | success >> { "changed": false, "ping": "pong" } 172.16.16.5 | success >> { "changed": false, "ping": "pong" } 172.16.16.2 | success >> { "changed": false, "ping": "pong" } 172.16.16.8 | success >> { "changed": false, "ping": "pong" } 172.16.16.252 | success >> { "changed": false, "ping": "pong" }
3、ansible定義roles如下:
[root@localhost ansible_playbooks]# tree . |-- haproxy.yml //playbook劇本 `-- roles //定義的角色(7個) |-- haproxy //node1,node2節點用到的haproxy | |-- files | |-- handlers | | `-- main.yml | |-- tasks | | `-- main.yml | |-- templates | | |-- haproxy.cfg.j2 | | `-- rsyslog.conf | `-- vars | `-- main.yml |-- httpd //web1,web2節點用到的httpd | |-- files | | `-- mountnfs.sh | |-- handlers | |-- tasks | | `-- main.yml | `-- templates | `-- httpd.conf.j2 |-- initialization //所有節點用到的系統初始化 | |-- files | |-- handlers | |-- tasks | | `-- main.yml | `-- templates | |-- hosts | |-- resolv.conf | `-- selinux.conf |-- keepalived //keepalived設置 | |-- files | | `-- notify.sh | |-- handlers | | `-- main.yml | |-- tasks | | `-- main.yml | |-- templates | | |-- keepalived.conf.backup.j2 | | `-- keepalived.conf.master.j2 | `-- vars | `-- main.yml |-- mysql //mysql數據庫 | |-- files | | |-- mariadb-5.5.43-linux-x86_64.tar.gz | | |-- mariadb-5.5.43.sh | | `-- privilege.sh | |-- handlers | |-- tasks | | `-- main.yml | `-- templates |-- nfs //nfs文件共享 | |-- files | | |-- Discuz_X3.1_SC_UTF8.zip | | `-- discuz.sh | |-- handlers | |-- tasks | | `-- main.yml | `-- templates | `-- exports `-- php //php1,php2節點用到的相關配置 |-- files | |-- install-php.sh | `-- mountnfs.sh |-- handlers |-- tasks | `-- main.yml `-- templates `-- httpd.conf.j2
4、介紹上述roles代表的含義,從上至下進行介紹;
4.1、haproxy
handlers:main.yml | - name: restart haproxy //重啓haproxy服務 service: name=haproxy state=restarted - name: restart rsyslog //重啓rsyslog服務 service: name=rsyslog state=restarted
|
tasks:main.yml | - name: install haproxy package yum: name=haproxy state=present - name: configuration haproxy template: src=haproxy.cfg.j2 dest=/etc/haproxy/haproxy.cfg notify: - restart haproxy tags: cfg - name: configuration rsyslog for haproxy template: src=rsyslog.conf dest=/etc/rsyslog.conf notify: - restart rsyslog - name: start haproxy service service: name=haproxy state=started enabled=yes |
template | haproxy.cfg.j2: //只截取了重要的部分,動靜分離,後端服務器定義 frontend http bind *:80 mode http log global option httpclose option logasap option dontlognull acl url_static path_end -i .jpg .gif .png .css .js .html .ico use_backend static_servers if url_static default_backend dynamic_servers #--------------------------------------------------------------------- # static backend for serving up images, stylesheets and such #--------------------------------------------------------------------- backend static_servers balance roundrobin cookie WebID insert nocache server web1 ` web1_ip `:80 check cookie web1 server web2 ` web2_ip `:80 check cookie web2 #--------------------------------------------------------------------- # round robin balancing between the various backends #--------------------------------------------------------------------- backend dynamic_servers balance uri hash-type consistent server php1 ` php1_ip `:80 check cookie php1 server php2 ` php2_ip `:80 check cookie php2 rsyslog.conf |
vars:main.yml | web1_ip : 172.16.16.3 //靜態web1服務器ip web2_ip : 172.16.16.4 //靜態web2服務器ip php1_ip : 172.16.16.2 //動態php1服務器ip php2_ip : 172.16.16.8 //動態php2服務器ip |
4.2、httpd
files:mountnfs.sh | yum install -y nfs-utils &> /dev/null //掛載nfs共享目錄 echo " 172.16.16.252:/discuz /var/www/html/ nfs defaults,_netdev 0 0 " >> /etc/fstab mount -a |
tasks:main.yml | - name: install httpd package yum: name=httpd state=present - name: mount nfs share directory /discuz script: mountnfs.sh - name: set configuration template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf - name: start httpd service service: name=httpd state=started |
templates:http.conf.j2 | LogFormat %{X-Forwarded-For}i combined //將客戶端ip記入日誌 |
4.3、initialization
tasks:main.yml | - name: set selinux shell: sed -i 's@^SELINUX=.*@SELINUX=permissive@' /etc/selinux/config shell: setenforce 0 - name: install libselinux-python package yum: name=libselinux-python state=present - name: copy hosts to all node /etc/hosts template: src=hosts dest=/etc/hosts - name: copy resolv.conf to all node /etc/resolv.conf template: src=resolv.conf dest=/etc/resolv.conf - name: install ntpdate package yum: name=ntpdate state=present - name: synctime from ntp.sjtu.edu.cn shell: ntpdate ntp.sjtu.edu.cn - name: set a cron to synctime from ntp.sjtu.edu.cn cron: name="cron to synctime from ntp.sjtu.edu.cn" minute="*/5" job="/usr/sbin/ntpdate ntp.sjtu.edu.cn &> /dev/null" state=present - name: modify selinux configuration template: src=selinux.conf dest=/etc/selinux/conf - name: set selinux permissive shell: setenforce 0 |
templates:3個文件 | (1)hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 172.16.16.11 node1 172.16.16.12 node2 172.16.16.3 web1 172.16.16.4 web2 172.16.16.2 php1 172.16.16.8 php2 172.16.16.5 mysql 172.16.16.252 nfs (2)resolv.conf nameserver 172.16.0.1 (3)selinux.conf SELINUX=disabled SELINUXTYPE=targeted |
4.4、keepalived
files:notify.sh | 通知機制:當主從發生變化的時候給管理員發郵件 |
handlers:main.yml | - name: restart keepalived service: name=keepalived state=restarted |
tasks:main.yml | - name: install keepalived package yum: name=keepalived state=present - name: copy notify.sh to node1 and node2 copy: src=notify.sh dest=/etc/keepalived/notify.sh - name: configure keepalived on node1-master template: src=keepalived.conf.master.j2 dest=/etc/keepalived/keepalived.conf when: ansible_hostname == "node1" notify: - restart keepalived - name: configure keepalived on node2-backup template: src=keepalived.conf.backup.j2 dest=/etc/keepalived/keepalived.conf when: ansible_hostname == "node2" notify: - restart keepalived - name: start keepalived service service: name=keepalived state=started enabled=yes |
templates | keepalived.conf.master.j2 ! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from keepalived@localhost smtp_connect_timeout 3 smtp_server 127.0.0.1 router_id LVS_DEVEL } vrrp_script chk_haproxy { script "killall -0 haproxy" interval 1 weight 2 } vrrp_script chk_mantaince_down { script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0" interval 1 weight 2 } vrrp_instance VI_1 { interface eth0 state MASTER # BACKUP for slave routers priority 101 # 100 for BACKUP virtual_router_id 51 garp_master_delay 1 authentication { auth_type PASS auth_pass password12345 } track_interface { eth0 } virtual_ipaddress { ` vip `/16 dev eth0 label eth0:0 } track_script { chk_haproxy chk_mantaince_down } notify_master "/etc/keepalived/notify.sh master" notify_backup "/etc/keepalived/notify.sh backup" notify_fault "/etc/keepalived/notify.sh fault" } keepalived.conf.backup.j2 state BACKUP priority 100 |
vars:main.yml | vip : 172.16.16.50 |
4.5、mysql
files:3個文件 | (1)mariadb-5.5.43.sh: 編譯安裝配置mariadb的腳本,就不發了,太多 (2)privilege.sh #!/bin/bash #! privilege to dzuser echo "create database discuz; grant all on discuz.* to 'dzuser'@'172.16.%.%' identified by 'dzpasswd'; flush privileges; \q" | mysql ~ |
tasks:main.yml | - name: copy mariadb-5.5.43 package copy: src=mariadb-5.5.43-linux-x86_64.tar.gz dest=/root/mariadb-5.5.43-linux-x86_64.tar.gz - name: install mysql and cofiguration script: mariadb-5.5.43.sh - name: start mysqld service shell: service mysqld start - name: privileges to dzuser script: privilege.sh tags: haha |
4.6、nfs
files:discuz.sh | #!/bin/bash # discuz configuration unzip -d /discuz /root/Discuz_X3.1_SC_UTF8.zip chmod -R go+w /discuz/upload/config/ chmod -R go+w /discuz/upload/data/ chmod -R go+w /discuz/upload/uc_* |
tasks:main.yml | - name: mkdir share directory to mysql and web shell: mkdir -pv /{mydata,discuz} - name: install nfs package yum: name=nfs-utils state=present - name: useradd mysql user: name=mysql state=present system=yes - name: useradd apache user: name=apache state=present system=yes - name: copy exports to nfs node template: src=exports dest=/etc/exports - name: set mysql mode(rwx) to /mydata shell: setfacl -m u:mysql:rwx /mydata - name: set apache mode(rwx) to /discuz shell: setfacl -m u:apache:rwx /discuz - name: copy Discuz_X3.1_SC_UTF8.zip package copy: src=Discuz_X3.1_SC_UTF8.zip dest=/root/Discuz_X3.1_SC_UTF8.zip - name: install unzip package yum: name=unzip state=present - name: configuration discuz script: discuz.sh - name: start rpcbind service service: name=rpcbind state=started - name: start nfs service |
templates:exports | /mydata 172.16.0.0/16(rw,no_root_squash)//編譯安裝mysql需要這樣設置,安裝完可以改成rw權限即可 /discuz 172.16.0.0/16(rw) |
4.7、php
files:2個文件 | (1)install-php.sh #!/bin/bash # install nfs-utils httpd | php | php-mysql yum install -y nfs-utils httpd php php-mysql openssl-devel &> /dev/null (2)mountnfs.sh //同httpd使用的腳本 |
tasks:main.yml | - name: install nfs-utils | httpd | php | php-mysql package script: install-php.sh - name: mount nfs share directory /discuz script: mountnfs.sh - name: set configuration template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf - name: start httpd service service: name=httpd state=started |
templates:httpd.conf.j2 | 這個模板也同httpd使用的! |
三、執行過程如下
[root@localhost ansible_playbooks]# ansible-playbook haproxy.yml //執行ansible劇本 PLAY [nfs] ******************************************************************** GATHERING FACTS *************************************************************** ok: [172.16.16.252] TASK: [initialization | set selinux] ****************************************** changed: [172.16.16.252] //設置selinux TASK: [initialization | install libselinux-python package] ******************** changed: [172.16.16.252] //需要安裝libselinux-python TASK: [initialization | copy hosts to all node /etc/hosts] ******************** changed: [172.16.16.252] //複製主機名解析文件 TASK: [initialization | copy resolv.conf to all node /etc/resolv.conf] ******** changed: [172.16.16.252] //複製域名文件 TASK: [initialization | install ntpdate package] ****************************** changed: [172.16.16.252] //安裝ntpdate TASK: [initialization | synctime from ntp.sjtu.edu.cn] ************************ changed: [172.16.16.252] //同步時間 TASK: [initialization | set a cron to synctime from ntp.sjtu.edu.cn] ********** //設置計劃任務 changed: [172.16.16.252] TASK: [initialization | modify selinux configuration] ************************* changed: [172.16.16.252] //修改selinux TASK: [initialization | set selinux permissive] ******************************* changed: [172.16.16.252] TASK: [nfs | mkdir share directory to mysql and web] ************************** changed: [172.16.16.252] //創建nfs的共享目錄/mydata和/discuz TASK: [nfs | install nfs package] ********************************************* changed: [172.16.16.252] //安裝nfs-utils TASK: [nfs | useradd mysql] *************************************************** changed: [172.16.16.252] //添加mysql用戶 TASK: [nfs | useradd apache] ************************************************** changed: [172.16.16.252] //添加apache用戶 TASK: [nfs | copy exports to nfs node] **************************************** changed: [172.16.16.252] //設置nfs導出的共享目錄 TASK: [nfs | set mysql mode(rwx) to /mydata] ********************************** changed: [172.16.16.252] //給mysql對/mydata的rwx權限 TASK: [nfs | set apache mode(rwx) to /discuz] ********************************* changed: [172.16.16.252] //給apache對/discuz的rwx權限 TASK: [nfs | copy Discuz_X3.1_SC_UTF8.zip package] **************************** changed: [172.16.16.252] //複製discuz安裝包到nfs服務器 TASK: [nfs | install unzip package] ******************************************* changed: [172.16.16.252] //解壓discuz TASK: [nfs | configuration discuz] ******************************************** changed: [172.16.16.252] //配置discuz網頁文件(給其對應訪問權限) TASK: [nfs | start rpcbind service] ******************************************* changed: [172.16.16.252] //啓動rpcbind服務 TASK: [nfs | start nfs service] *********************************************** changed: [172.16.16.252] //啓動nfs服務 PLAY [db] ********************************************************************* //安裝及配置mysql GATHERING FACTS *************************************************************** ok: [172.16.16.5] TASK: [initialization | set selinux] ****************************************** changed: [172.16.16.5] TASK: [initialization | install libselinux-python package] ******************** changed: [172.16.16.5] TASK: [initialization | copy hosts to all node /etc/hosts] ******************** changed: [172.16.16.5] TASK: [initialization | copy resolv.conf to all node /etc/resolv.conf] ******** changed: [172.16.16.5] TASK: [initialization | install ntpdate package] ****************************** changed: [172.16.16.5] TASK: [initialization | synctime from ntp.sjtu.edu.cn] ************************ changed: [172.16.16.5] TASK: [initialization | set a cron to synctime from ntp.sjtu.edu.cn] ********** changed: [172.16.16.5] TASK: [initialization | modify selinux configuration] ************************* changed: [172.16.16.5] TASK: [initialization | set selinux permissive] ******************************* changed: [172.16.16.5] TASK: [mysql | copy mariadb-5.5.43 package] *********************************** changed: [172.16.16.5] //複製mariadb到mysql服務器 TASK: [mysql | install mysql and cofiguration] ******************************** changed: [172.16.16.5] //腳本執行mysql安裝,配置 TASK: [mysql | start mysqld service] ****************************************** changed: [172.16.16.5] //啓動mysqld服務 TASK: [mysql | privileges to dzuser] ****************************************** changed: [172.16.16.5] //使用腳本創建discuz數據庫,並給dzuser授權 PLAY [static_servers] ********************************************************* //靜態服務器設置 web1和web2節點 GATHERING FACTS *************************************************************** ok: [172.16.16.3] ok: [172.16.16.4] TASK: [initialization | set selinux] ****************************************** changed: [172.16.16.3] changed: [172.16.16.4] TASK: [initialization | install libselinux-python package] ******************** changed: [172.16.16.3] changed: [172.16.16.4] TASK: [initialization | copy hosts to all node /etc/hosts] ******************** changed: [172.16.16.4] changed: [172.16.16.3] TASK: [initialization | copy resolv.conf to all node /etc/resolv.conf] ******** changed: [172.16.16.3] changed: [172.16.16.4] TASK: [initialization | install ntpdate package] ****************************** changed: [172.16.16.3] changed: [172.16.16.4] TASK: [initialization | synctime from ntp.sjtu.edu.cn] ************************ changed: [172.16.16.4] changed: [172.16.16.3] TASK: [initialization | set a cron to synctime from ntp.sjtu.edu.cn] ********** changed: [172.16.16.3] changed: [172.16.16.4] TASK: [initialization | modify selinux configuration] ************************* changed: [172.16.16.3] changed: [172.16.16.4] TASK: [initialization | set selinux permissive] ******************************* changed: [172.16.16.4] changed: [172.16.16.3] TASK: [httpd | install httpd package] ***************************************** changed: [172.16.16.3] //安裝httpd軟件包 changed: [172.16.16.4] TASK: [httpd | mount nfs share directory /discuz] ***************************** changed: [172.16.16.3] //掛載nfs服務器共享的discuz目錄至/var/www/html changed: [172.16.16.4] TASK: [httpd | set configuration] ********************************************* changed: [172.16.16.4] //配置httpd.conf changed: [172.16.16.3] TASK: [httpd | start httpd service] ******************************************* changed: [172.16.16.3] //啓動httpd服務 changed: [172.16.16.4] PLAY [dynamic_servers] ******************************************************** //設置動態服務器php1和php2節點 GATHERING FACTS *************************************************************** ok: [172.16.16.2] ok: [172.16.16.8] TASK: [initialization | set selinux] ****************************************** changed: [172.16.16.2] changed: [172.16.16.8] TASK: [initialization | install libselinux-python package] ******************** changed: [172.16.16.2] changed: [172.16.16.8] TASK: [initialization | copy hosts to all node /etc/hosts] ******************** changed: [172.16.16.8] changed: [172.16.16.2] TASK: [initialization | copy resolv.conf to all node /etc/resolv.conf] ******** changed: [172.16.16.8] changed: [172.16.16.2] TASK: [initialization | install ntpdate package] ****************************** changed: [172.16.16.2] changed: [172.16.16.8] TASK: [initialization | synctime from ntp.sjtu.edu.cn] ************************ changed: [172.16.16.2] changed: [172.16.16.8] TASK: [initialization | set a cron to synctime from ntp.sjtu.edu.cn] ********** changed: [172.16.16.8] changed: [172.16.16.2] TASK: [initialization | modify selinux configuration] ************************* changed: [172.16.16.8] changed: [172.16.16.2] TASK: [initialization | set selinux permissive] ******************************* changed: [172.16.16.8] changed: [172.16.16.2] TASK: [php | install nfs-utils | httpd | php | php-mysql package] ************* changed: [172.16.16.8] //安裝php軟件包(使用的是centos 6系統自帶的) changed: [172.16.16.2] TASK: [php | mount nfs share directory /discuz] ******************************* changed: [172.16.16.8] //掛載discuz目錄至/var/www/html changed: [172.16.16.2] TASK: [php | set configuration] *********************************************** changed: [172.16.16.8] //配置httpd.conf,給日誌加入客戶端訪問的ip地址 changed: [172.16.16.2] TASK: [php | start httpd service] ********************************************* changed: [172.16.16.8] //啓用php功能 changed: [172.16.16.2] PLAY [haproxy] **************************************************************** //安裝設置keepalived+haproxy(node1和node2) GATHERING FACTS *************************************************************** ok: [172.16.16.12] ok: [172.16.16.11] TASK: [initialization | set selinux] ****************************************** changed: [172.16.16.12] changed: [172.16.16.11] TASK: [initialization | install libselinux-python package] ******************** changed: [172.16.16.12] changed: [172.16.16.11] TASK: [initialization | copy hosts to all node /etc/hosts] ******************** changed: [172.16.16.12] changed: [172.16.16.11] TASK: [initialization | copy resolv.conf to all node /etc/resolv.conf] ******** changed: [172.16.16.11] changed: [172.16.16.12] TASK: [initialization | install ntpdate package] ****************************** ok: [172.16.16.12] ok: [172.16.16.11] TASK: [initialization | synctime from ntp.sjtu.edu.cn] ************************ changed: [172.16.16.12] changed: [172.16.16.11] TASK: [initialization | set a cron to synctime from ntp.sjtu.edu.cn] ********** changed: [172.16.16.11] changed: [172.16.16.12] TASK: [initialization | modify selinux configuration] ************************* changed: [172.16.16.11] changed: [172.16.16.12] TASK: [initialization | set selinux permissive] ******************************* changed: [172.16.16.11] changed: [172.16.16.12] TASK: [keepalived | install keepalived package] ******************************* changed: [172.16.16.11] //安裝keepalived changed: [172.16.16.12] TASK: [keepalived | copy notify.sh to node1 and node2] ************************ changed: [172.16.16.12] //複製通知腳本給node1和node2 changed: [172.16.16.11] TASK: [keepalived | configure keepalived on node1-master] ********************* skipping: [172.16.16.12] //設置node1爲MASTER節點 changed: [172.16.16.11] TASK: [keepalived | configure keepalived on node2-backup] ********************* skipping: [172.16.16.11] //設置node2爲BACKUP節點 changed: [172.16.16.12] TASK: [keepalived | start keepalived service] ********************************* changed: [172.16.16.12] //啓動keepalived服務 changed: [172.16.16.11] TASK: [haproxy | install haproxy package] ************************************* changed: [172.16.16.12] //安裝haproxy軟件包 changed: [172.16.16.11] TASK: [haproxy | configuration haproxy] *************************************** changed: [172.16.16.12] //配置haproxy,實現動靜分離 changed: [172.16.16.11] TASK: [haproxy | configuration rsyslog for haproxy] *************************** changed: [172.16.16.11] //記錄haproxy的日誌 changed: [172.16.16.12] TASK: [haproxy | start haproxy service] *************************************** changed: [172.16.16.12] //啓動haproxy服務 changed: [172.16.16.11] NOTIFIED: [keepalived | restart keepalived] *********************************** changed: [172.16.16.12] //配置文件發生變化,重啓keepalived服務 changed: [172.16.16.11] NOTIFIED: [haproxy | restart haproxy] ***************************************** changed: [172.16.16.11] //配置文件發生變化,重啓haproxy服務 changed: [172.16.16.12] NOTIFIED: [haproxy | restart rsyslog] ***************************************** changed: [172.16.16.12] //配置文件發生變化,重啓rsyslog服務 changed: [172.16.16.11] PLAY RECAP ******************************************************************** 172.16.16.11 : ok=21 changed=19 unreachable=0 failed=0 172.16.16.12 : ok=21 changed=19 unreachable=0 failed=0 172.16.16.2 : ok=14 changed=13 unreachable=0 failed=0 172.16.16.252 : ok=22 changed=21 unreachable=0 failed=0 172.16.16.3 : ok=14 changed=13 unreachable=0 failed=0 172.16.16.4 : ok=14 changed=13 unreachable=0 failed=0 172.16.16.5 : ok=14 changed=13 unreachable=0 failed=0 172.16.16.8 : ok=14 changed=13 unreachable=0 failed=0
四、安裝discuz論壇
安裝完成,使用vip:172.16.16.50測試正常!!如下圖:
五、額外測試過程:
(1)測試keepalived是否工作正常,在master出現問題的時候,是否可以切換到backup上:
[root@node1 keepalived]# touch down//通過keepalived配置文件定義腳本測試vip是否轉移到node2 [root@node1 keepalived]# tail /var/log/messages //通過下面的日誌可看出node1成爲BACKUP Oct 24 20:08:19 node1 Keepalived_vrrp[2644]: VRRP_Script(chk_mantaince_down) failed Oct 24 20:08:21 node1 Keepalived_vrrp[2644]: VRRP_Instance(VI_1) Received higher prio advert Oct 24 20:08:21 node1 Keepalived_vrrp[2644]: VRRP_Instance(VI_1) Entering BACKUP STATE Oct 24 20:08:21 node1 Keepalived_vrrp[2644]: VRRP_Instance(VI_1) removing protocol VIPs. Oct 24 20:08:21 node1 Keepalived_healthcheckers[2643]: Netlink reflector reports IP 172.16.16.50 removed [root@node2 ~]# tail /var/log/messages //通過下面的日誌可看出node2成爲MASTER Oct 24 20:08:22 node2 Keepalived_vrrp[2442]: VRRP_Instance(VI_1) Transition to MASTER STATE Oct 24 20:08:23 node2 Keepalived_vrrp[2442]: VRRP_Instance(VI_1) Entering MASTER STATE Oct 24 20:08:23 node2 Keepalived_vrrp[2442]: VRRP_Instance(VI_1) setting protocol VIPs. Oct 24 20:08:23 node2 Keepalived_healthcheckers[2441]: Netlink reflector reports IP 172.16.16.50 added Oct 24 20:08:23 node2 Keepalived_vrrp[2442]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.16.16.50
[root@node1 keepalived]# tail /var/log/haproxy.log Oct 24 20:08:21 node1 Keepalived_healthcheckers[2643]: Netlink reflector reports IP 172.16.16.50 removed [root@node2 ~]# tail /var/log/haproxy.log Oct 24 20:08:23 node2 Keepalived_healthcheckers[2441]: Netlink reflector reports IP 172.16.16.50 added
這個時候訪問discuz論壇仍然沒有問題;說明keepalived發揮了作用!
(2)測試haproxy是否可以實現讀寫分離?方法:將所有static_servers的httpd服務停止,測試只提供php的功能,顯示效果如下圖
[root@web1 ~]# service httpd stop //停止web1和web2的httpd服務,提供靜態內容顯示 [root@web2 ~]# service httpd stop
# 可以看到上面的效果,圖片都已經不再顯示,將web2加回來,然後再查看效果,沒查看http的訪問日誌
[root@web2 ~]# service httpd start //啓動其中的一臺 [root@web2 ~]# tail /var/log/httpd/access_log 172.16.0.3 - - [24/Oct/2015:20:46:54 +0800] "GET /static/image/common/user_online.gif HTTP/1.1" 200 868 "http://172.16.16.50/data/cache/style_1_common.css?SFA" "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36" 172.16.0.3 - - [24/Oct/2015:20:46:54 +0800] "GET /static/image/common/arrwd.gif HTTP/1.1" 200 51 "http://172.16.16.50/data/cache/style_1_common.css?SFA" "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36" 172.16.0.3 - - [24/Oct/2015:20:46:54 +0800] "GET /static/image/common/qmenu.png HTTP/1.1" 200 225 "http://172.16.16.50/data/cache/style_1_common.css?SFA" "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36" 172.16.0.3 - - [24/Oct/2015:20:46:54 +0800] "GET /static/image/common/nv.png HTTP/1.1" 200 1939 "http://172.16.16.50/data/cache/style_1_common.css?SFA" "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36" 172.16.0.3 - - [24/Oct/2015:20:46:54 +0800] "GET /static/image/common/nv_a.png HTTP/1.1" 200 2076 "http://172.16.16.50/data/cache/style_1_common.css?SFA" "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36" 172.16.0.3 - - [24/Oct/2015:20:46:54 +0800] "GET /static/image/common/search.png HTTP/1.1" 200 1301 "http://172.16.16.50/data/cache/style_1_common.css?SFA" "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36" 172.16.0.3 - - [24/Oct/2015:20:46:54 +0800] "GET /static/image/common/chart.png HTTP/1.1" 200 990 "http://172.16.16.50/data/cache/style_1_forum_index.css?SFA" "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36" 172.16.0.3 - - [24/Oct/2015:20:46:54 +0800] "GET /static/image/common/pt_item.png HTTP/1.1" 200 3598 "http://172.16.16.50/data/cache/style_1_common.css?SFA" "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36" 172.16.0.3 - - [24/Oct/2015:20:46:54 +0800] "GET /static/image/common/titlebg.png HTTP/1.1" 200 315 "http://172.16.16.50/data/cache/style_1_common.css?SFA" "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36" 172.16.0.3 - - [24/Oct/2015:20:46:55 +0800] "GET /static/image/common/scrolltop.png HTTP/1.1" 200 1383 "http://172.16.16.50/data/cache/style_1_common.css?SFA" "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36"
至此,終於將所有劇本,腳本,圖片,日誌整理完成!