keepalived-haproxy

一、测试环境: 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;设置如下

1# ssh-keygen -t rsa -P ''
2# ssh-copy-id -i ~/.ssh/id_rsa.pub node1
3# ssh-copy-id -i ~/.ssh/id_rsa.pub node2
4# ssh-copy-id -i ~/.ssh/id_rsa.pub web1
5# ssh-copy-id -i ~/.ssh/id_rsa.pub web2
6# ssh-copy-id -i ~/.ssh/id_rsa.pub php1
7# ssh-copy-id -i ~/.ssh/id_rsa.pub php2
8# ssh-copy-id -i ~/.ssh/id_rsa.pub mysql
9# ssh-copy-id -i ~/.ssh/id_rsa.pub nfs

2、ansible设置及管理节点探测:

 1# vim /etc/ansible/host
 2[haproxy]
 3172.16.16.11
 4172.16.16.12
 5[dynamic_servers]
 6172.16.16.2
 7172.16.16.8
 8[static_servers]
 9172.16.16.3
10172.16.16.4
11[db]
12172.16.16.5
13[nfs]
14172.16.16.252
15# ansible all -m ping
16172.16.16.12 | success >> {
17    "changed": false,
18    "ping": "pong"
19}
20172.16.16.3 | success >> {
21    "changed": false,
22    "ping": "pong"
23}
24172.16.16.11 | success >> {
25    "changed": false,
26    "ping": "pong"
27}
28172.16.16.4 | success >> {
29    "changed": false,
30    "ping": "pong"
31}
32172.16.16.5 | success >> {
33    "changed": false,
34    "ping": "pong"
35}
36172.16.16.2 | success >> {
37    "changed": false,
38    "ping": "pong"
39}
40172.16.16.8 | success >> {
41    "changed": false,
42    "ping": "pong"
43}
44172.16.16.252 | success >> {
45    "changed": false,
46    "ping": "pong"
47}

3、ansible定义roles如下:

 1[root@localhost ansible_playbooks]# tree
 2.
 3|-- haproxy.yml                 //playbook剧本
 4`-- roles                       //定义的角色(7个)
 5    |-- haproxy                 //node1,node2节点用到的haproxy
 6    |   |-- files
 7    |   |-- handlers
 8    |   |   `-- main.yml
 9    |   |-- tasks
10    |   |   `-- main.yml
11    |   |-- templates
12    |   |   |-- haproxy.cfg.j2
13    |   |   `-- rsyslog.conf
14    |   `-- vars
15    |       `-- main.yml
16    |-- httpd                 //web1,web2节点用到的httpd
17    |   |-- files
18    |   |   `-- mountnfs.sh
19    |   |-- handlers
20    |   |-- tasks
21    |   |   `-- main.yml
22    |   `-- templates
23    |       `-- httpd.conf.j2
24    |-- initialization               //所有节点用到的系统初始化
25    |   |-- files
26    |   |-- handlers
27    |   |-- tasks
28    |   |   `-- main.yml
29    |   `-- templates
30    |       |-- hosts
31    |       |-- resolv.conf
32    |       `-- selinux.conf
33    |-- keepalived                 //keepalived设置
34    |   |-- files
35    |   |   `-- notify.sh
36    |   |-- handlers
37    |   |   `-- main.yml
38    |   |-- tasks
39    |   |   `-- main.yml
40    |   |-- templates
41    |   |   |-- keepalived.conf.backup.j2
42    |   |   `-- keepalived.conf.master.j2
43    |   `-- vars
44    |       `-- main.yml
45    |-- mysql                      //mysql数据库
46    |   |-- files
47    |   |   |-- mariadb-5.5.43-linux-x86_64.tar.gz
48    |   |   |-- mariadb-5.5.43.sh
49    |   |   `-- privilege.sh
50    |   |-- handlers
51    |   |-- tasks
52    |   |   `-- main.yml
53    |   `-- templates
54    |-- nfs                            //nfs文件共享
55    |   |-- files
56    |   |   |-- Discuz_X3.1_SC_UTF8.zip
57    |   |   `-- discuz.sh
58    |   |-- handlers
59    |   |-- tasks
60    |   |   `-- main.yml
61    |   `-- templates
62    |       `-- exports
63    `-- php                    //php1,php2节点用到的相关配置
64        |-- files
65        |   |-- install-php.sh
66        |   `-- mountnfs.sh
67        |-- handlers
68        |-- tasks
69        |   `-- main.yml
70        `-- templates
71            `-- 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使用的!

三、执行过程如下

  1[root@localhost ansible_playbooks]# ansible-playbook haproxy.yml //执行ansible剧本
  2PLAY [nfs] ********************************************************************
  3GATHERING FACTS ***************************************************************
  4ok: [172.16.16.252]
  5TASK: [initialization | set selinux] ******************************************
  6changed: [172.16.16.252]                       //设置selinux
  7TASK: [initialization | install libselinux-python package] ********************
  8changed: [172.16.16.252]                       //需要安装libselinux-python
  9TASK: [initialization | copy hosts to all node /etc/hosts] ********************
 10changed: [172.16.16.252]                      //复制主机名解析文件
 11TASK: [initialization | copy resolv.conf to all node /etc/resolv.conf] ********
 12changed: [172.16.16.252]                    //复制域名文件
 13TASK: [initialization | install ntpdate package] ******************************
 14changed: [172.16.16.252]                     //安装ntpdate
 15TASK: [initialization | synctime from ntp.sjtu.edu.cn] ************************
 16changed: [172.16.16.252]
 17                                                 //同步时间
 18TASK: [initialization | set a cron to synctime from ntp.sjtu.edu.cn] **********                                              //设置计划任务
 19changed: [172.16.16.252]
 20TASK: [initialization | modify selinux configuration] *************************
 21changed: [172.16.16.252]                        //修改selinux
 22TASK: [initialization | set selinux permissive] *******************************
 23changed: [172.16.16.252]
 24TASK: [nfs | mkdir share directory to mysql and web] **************************
 25changed: [172.16.16.252]              //创建nfs的共享目录/mydata和/discuz
 26TASK: [nfs | install nfs package] *********************************************
 27changed: [172.16.16.252]                //安装nfs-utils
 28TASK: [nfs | useradd mysql] ***************************************************
 29changed: [172.16.16.252]                 //添加mysql用户
 30TASK: [nfs | useradd apache] **************************************************
 31changed: [172.16.16.252]                //添加apache用户
 32TASK: [nfs | copy exports to nfs node] ****************************************
 33changed: [172.16.16.252]                //设置nfs导出的共享目录
 34TASK: [nfs | set mysql mode(rwx) to /mydata] **********************************
 35changed: [172.16.16.252]                //给mysql对/mydata的rwx权限
 36TASK: [nfs | set apache mode(rwx) to /discuz] *********************************
 37changed: [172.16.16.252]                //给apache对/discuz的rwx权限
 38TASK: [nfs | copy Discuz_X3.1_SC_UTF8.zip package] ****************************
 39changed: [172.16.16.252]               //复制discuz安装包到nfs服务器
 40TASK: [nfs | install unzip package] *******************************************
 41changed: [172.16.16.252]                //解压discuz
 42TASK: [nfs | configuration discuz] ********************************************
 43changed: [172.16.16.252]                 //配置discuz网页文件(给其对应访问权限)
 44TASK: [nfs | start rpcbind service] *******************************************
 45changed: [172.16.16.252]                 //启动rpcbind服务
 46TASK: [nfs | start nfs service] ***********************************************
 47changed: [172.16.16.252]               //启动nfs服务
 48PLAY [db] *********************************************************************
 49                                         //安装及配置mysql
 50GATHERING FACTS ***************************************************************
 51ok: [172.16.16.5]
 52TASK: [initialization | set selinux] ******************************************
 53changed: [172.16.16.5]
 54TASK: [initialization | install libselinux-python package] ********************
 55changed: [172.16.16.5]
 56TASK: [initialization | copy hosts to all node /etc/hosts] ********************
 57changed: [172.16.16.5]
 58TASK: [initialization | copy resolv.conf to all node /etc/resolv.conf] ********
 59changed: [172.16.16.5]
 60TASK: [initialization | install ntpdate package] ******************************
 61changed: [172.16.16.5]
 62TASK: [initialization | synctime from ntp.sjtu.edu.cn] ************************
 63changed: [172.16.16.5]
 64TASK: [initialization | set a cron to synctime from ntp.sjtu.edu.cn] **********
 65changed: [172.16.16.5]
 66TASK: [initialization | modify selinux configuration] *************************
 67changed: [172.16.16.5]
 68TASK: [initialization | set selinux permissive] *******************************
 69changed: [172.16.16.5]
 70TASK: [mysql | copy mariadb-5.5.43 package] ***********************************
 71changed: [172.16.16.5]                //复制mariadb到mysql服务器
 72TASK: [mysql | install mysql and cofiguration] ********************************
 73changed: [172.16.16.5]                 //脚本执行mysql安装,配置
 74TASK: [mysql | start mysqld service] ******************************************
 75changed: [172.16.16.5]              //启动mysqld服务
 76TASK: [mysql | privileges to dzuser] ******************************************
 77changed: [172.16.16.5]              //使用脚本创建discuz数据库,并给dzuser授权
 78PLAY [static_servers] *********************************************************
 79                                   //静态服务器设置 web1和web2节点
 80GATHERING FACTS ***************************************************************
 81ok: [172.16.16.3]
 82ok: [172.16.16.4]
 83TASK: [initialization | set selinux] ******************************************
 84changed: [172.16.16.3]
 85changed: [172.16.16.4]
 86TASK: [initialization | install libselinux-python package] ********************
 87changed: [172.16.16.3]
 88changed: [172.16.16.4]
 89TASK: [initialization | copy hosts to all node /etc/hosts] ********************
 90changed: [172.16.16.4]
 91changed: [172.16.16.3]
 92TASK: [initialization | copy resolv.conf to all node /etc/resolv.conf] ********
 93changed: [172.16.16.3]
 94changed: [172.16.16.4]
 95TASK: [initialization | install ntpdate package] ******************************
 96changed: [172.16.16.3]
 97changed: [172.16.16.4]
 98TASK: [initialization | synctime from ntp.sjtu.edu.cn] ************************
 99changed: [172.16.16.4]
100changed: [172.16.16.3]
101TASK: [initialization | set a cron to synctime from ntp.sjtu.edu.cn] **********
102changed: [172.16.16.3]
103changed: [172.16.16.4]
104TASK: [initialization | modify selinux configuration] *************************
105changed: [172.16.16.3]
106changed: [172.16.16.4]
107TASK: [initialization | set selinux permissive] *******************************
108changed: [172.16.16.4]
109changed: [172.16.16.3]
110TASK: [httpd | install httpd package] *****************************************
111changed: [172.16.16.3]                //安装httpd软件包
112changed: [172.16.16.4]
113TASK: [httpd | mount nfs share directory /discuz] *****************************
114changed: [172.16.16.3]                 //挂载nfs服务器共享的discuz目录至/var/www/html
115changed: [172.16.16.4]
116TASK: [httpd | set configuration] *********************************************
117changed: [172.16.16.4]              //配置httpd.conf
118changed: [172.16.16.3]
119TASK: [httpd | start httpd service] *******************************************
120changed: [172.16.16.3]                 //启动httpd服务
121changed: [172.16.16.4]
122PLAY [dynamic_servers] ********************************************************
123                                    //设置动态服务器php1和php2节点
124GATHERING FACTS ***************************************************************
125ok: [172.16.16.2]
126ok: [172.16.16.8]
127TASK: [initialization | set selinux] ******************************************
128changed: [172.16.16.2]
129changed: [172.16.16.8]
130TASK: [initialization | install libselinux-python package] ********************
131changed: [172.16.16.2]
132changed: [172.16.16.8]
133TASK: [initialization | copy hosts to all node /etc/hosts] ********************
134changed: [172.16.16.8]
135changed: [172.16.16.2]
136TASK: [initialization | copy resolv.conf to all node /etc/resolv.conf] ********
137changed: [172.16.16.8]
138changed: [172.16.16.2]
139TASK: [initialization | install ntpdate package] ******************************
140changed: [172.16.16.2]
141changed: [172.16.16.8]
142TASK: [initialization | synctime from ntp.sjtu.edu.cn] ************************
143changed: [172.16.16.2]
144changed: [172.16.16.8]
145TASK: [initialization | set a cron to synctime from ntp.sjtu.edu.cn] **********
146changed: [172.16.16.8]
147changed: [172.16.16.2]
148TASK: [initialization | modify selinux configuration] *************************
149changed: [172.16.16.8]
150changed: [172.16.16.2]
151TASK: [initialization | set selinux permissive] *******************************
152changed: [172.16.16.8]
153changed: [172.16.16.2]
154TASK: [php | install nfs-utils | httpd | php | php-mysql package] *************
155changed: [172.16.16.8]              //安装php软件包(使用的是centos 6系统自带的)
156changed: [172.16.16.2]
157TASK: [php | mount nfs share directory /discuz] *******************************
158changed: [172.16.16.8]             //挂载discuz目录至/var/www/html
159changed: [172.16.16.2]
160TASK: [php | set configuration] ***********************************************
161changed: [172.16.16.8]              //配置httpd.conf,给日志加入客户端访问的ip地址
162changed: [172.16.16.2]
163TASK: [php | start httpd service] *********************************************
164changed: [172.16.16.8]             //启用php功能
165changed: [172.16.16.2]
166PLAY [haproxy] ****************************************************************
167                                   //安装设置keepalived+haproxy(node1和node2)
168GATHERING FACTS ***************************************************************
169ok: [172.16.16.12]
170ok: [172.16.16.11]
171TASK: [initialization | set selinux] ******************************************
172changed: [172.16.16.12]
173changed: [172.16.16.11]
174TASK: [initialization | install libselinux-python package] ********************
175changed: [172.16.16.12]
176changed: [172.16.16.11]
177TASK: [initialization | copy hosts to all node /etc/hosts] ********************
178changed: [172.16.16.12]
179changed: [172.16.16.11]
180TASK: [initialization | copy resolv.conf to all node /etc/resolv.conf] ********
181changed: [172.16.16.11]
182changed: [172.16.16.12]
183TASK: [initialization | install ntpdate package] ******************************
184ok: [172.16.16.12]
185ok: [172.16.16.11]
186TASK: [initialization | synctime from ntp.sjtu.edu.cn] ************************
187changed: [172.16.16.12]
188changed: [172.16.16.11]
189TASK: [initialization | set a cron to synctime from ntp.sjtu.edu.cn] **********
190changed: [172.16.16.11]
191changed: [172.16.16.12]
192TASK: [initialization | modify selinux configuration] *************************
193changed: [172.16.16.11]
194changed: [172.16.16.12]
195TASK: [initialization | set selinux permissive] *******************************
196changed: [172.16.16.11]
197changed: [172.16.16.12]
198TASK: [keepalived | install keepalived package] *******************************
199changed: [172.16.16.11]            //安装keepalived
200changed: [172.16.16.12]
201TASK: [keepalived | copy notify.sh to node1 and node2] ************************
202changed: [172.16.16.12]               //复制通知脚本给node1和node2
203changed: [172.16.16.11]
204TASK: [keepalived | configure keepalived on node1-master] *********************
205skipping: [172.16.16.12]             //设置node1为MASTER节点
206changed: [172.16.16.11]
207TASK: [keepalived | configure keepalived on node2-backup] *********************
208skipping: [172.16.16.11]           //设置node2为BACKUP节点
209changed: [172.16.16.12]
210TASK: [keepalived | start keepalived service] *********************************
211changed: [172.16.16.12]            //启动keepalived服务
212changed: [172.16.16.11]
213TASK: [haproxy | install haproxy package] *************************************
214changed: [172.16.16.12]               //安装haproxy软件包
215changed: [172.16.16.11]
216TASK: [haproxy | configuration haproxy] ***************************************
217changed: [172.16.16.12]             //配置haproxy,实现动静分离
218changed: [172.16.16.11]
219TASK: [haproxy | configuration rsyslog for haproxy] ***************************
220changed: [172.16.16.11]          //记录haproxy的日志
221changed: [172.16.16.12]
222TASK: [haproxy | start haproxy service] ***************************************
223changed: [172.16.16.12]          //启动haproxy服务
224changed: [172.16.16.11]
225NOTIFIED: [keepalived | restart keepalived] ***********************************
226changed: [172.16.16.12]          //配置文件发生变化,重启keepalived服务
227changed: [172.16.16.11]
228NOTIFIED: [haproxy | restart haproxy] *****************************************
229changed: [172.16.16.11]           //配置文件发生变化,重启haproxy服务
230changed: [172.16.16.12]
231NOTIFIED: [haproxy | restart rsyslog] *****************************************
232changed: [172.16.16.12]       //配置文件发生变化,重启rsyslog服务
233changed: [172.16.16.11]
234PLAY RECAP ********************************************************************
235172.16.16.11               : ok=21   changed=19   unreachable=0    failed=0
236172.16.16.12               : ok=21   changed=19   unreachable=0    failed=0
237172.16.16.2                : ok=14   changed=13   unreachable=0    failed=0
238172.16.16.252              : ok=22   changed=21   unreachable=0    failed=0
239172.16.16.3                : ok=14   changed=13   unreachable=0    failed=0
240172.16.16.4                : ok=14   changed=13   unreachable=0    failed=0
241172.16.16.5                : ok=14   changed=13   unreachable=0    failed=0
242172.16.16.8                : ok=14   changed=13   unreachable=0    failed=0

四、安装discuz论坛

ansible-discuz

具体步骤略,无非是下一步下一步。安装完成,使用vip:172.16.16.50测试正常!!如下图:

discuz

五、额外测试过程:

(1)测试keepalived是否工作正常,在master出现问题的时候,是否可以切换到backup上:

keepalived

 1[root@node1 keepalived]# tail /var/log/messages    //通过下面的日志可看出node1成为BACKUP
 2Oct 24 20:08:19 node1 Keepalived_vrrp[2644]: VRRP_Script(chk_mantaince_down) failed
 3Oct 24 20:08:21 node1 Keepalived_vrrp[2644]: VRRP_Instance(VI_1) Received higher prio advert
 4Oct 24 20:08:21 node1 Keepalived_vrrp[2644]: VRRP_Instance(VI_1) Entering BACKUP STATE
 5Oct 24 20:08:21 node1 Keepalived_vrrp[2644]: VRRP_Instance(VI_1) removing protocol VIPs.
 6Oct 24 20:08:21 node1 Keepalived_healthcheckers[2643]: Netlink reflector reports IP 172.16.16.50 removed
 7[root@node2 ~]# tail /var/log/messages         //通过下面的日志可看出node2成为MASTER
 8Oct 24 20:08:22 node2 Keepalived_vrrp[2442]: VRRP_Instance(VI_1) Transition to MASTER STATE
 9Oct 24 20:08:23 node2 Keepalived_vrrp[2442]: VRRP_Instance(VI_1) Entering MASTER STATE
10Oct 24 20:08:23 node2 Keepalived_vrrp[2442]: VRRP_Instance(VI_1) setting protocol VIPs.
11Oct 24 20:08:23 node2 Keepalived_healthcheckers[2441]: Netlink reflector reports IP 172.16.16.50 added
12Oct 24 20:08:23 node2 Keepalived_vrrp[2442]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.16.16.50

backupnode

这个时候访问discuz论坛仍然没有问题;说明keepalived发挥了作用!

(2)测试haproxy是否可以实现读写分离

方法:将所有static_servers的httpd服务停止,测试只提供php的功能,显示效果如下图:

1[root@web1 ~]# service httpd stop     //停止web1和web2的httpd服务,提供静态内容显示
2[root@web2 ~]# service httpd stop

haproxy-forum

可以看到上面的效果,图片都已经不再显示,将web2加回来,然后再查看效果,并查看http的访问日志

1[root@web2 ~]# service httpd start       //启动其中的一台
2[root@web2 ~]# tail /var/log/httpd/access_log
3172.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"
4172.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"

haproxy-keepalived

本篇自自于51cto bengbengtu的文章,实现haproxy+keepalived的方案属于一个比较成熟的方案并无太多新意,之所以将该篇拿过来看备忘下无法看到是通过ansible+haproxy+keepalived做了一个简单的整合,还是有点意思的。而且实的步骤也相对详尽,还是值得一看的。