ansible部署简单高可用LAMP
ansible主机:192.168.152.145
HA1(HighAvailable):192.168.152.140
HA2:192.168.152.141
VIP1(virtualIP):192.168.152.190
VIP2:192.168.152.191
AP1(Apache+PHP):192.168.152.142
AP2(Apache+PHP):192.168.152.143
DB:192.168.152.144
配置ansible
yum -y install ansible
vim /etc/ansible/hosts
[nginx] 192.168.152.140 192.168.152.141 [ap] 192.168.152.142 192.168.152.143 [db] 192.168.152.144
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]
ssh-copy-id -i .ssh/id_rsa.pub [email protected]
ssh-copy-id -i .ssh/id_rsa.pub [email protected]
ansible all -m ping测试连通性
创建roles
mkdir /etc/ansible/roles/keepalived/{files,tasks,templates,handlers,vars,default} -pv
mkdir /etc/ansible/roles/nginx/{files,tasks,templates,handlers,vars,default} -pv
mkdir /etc/ansible/roles/apachephp/{files,tasks,templates,handlers,vars,default} -pv
mkdir /etc/ansible/roles/database/{files,tasks,templates,handlers,vars,default} -pv
vim /etc/ansible/roles/keepalived/tasks/main.yml
- name: install keepalived yum: name=keepalived state=present - name: install conf file template: src=keepalived.conf.j2 dest=/etc/keepalived/keepalived.conf - name: start keepalived service: name=keepalived state=started enabled=true
vim /etc/ansible/roles/nginx/tasks/main.yml
- name: install nginx yum: name=nginx state=present - name: install conf file template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf - name: start nginx service: name=nginx state=started enabled=true
vim /etc/ansible/roles/apachephp/tasks/main.yml
- name: install httpd yum: name=httpd state=present - name: install php yum: name=php state=present - name: install php-mysql yum: name=php-mysql state=present - name: install httpconf file template: src=http.conf.j2 dest=/etc/httpd/conf/httpd.conf - name: install html file template: src=html.index.j2 dest=/var/www/html/index.php - name: start httpd service: name=httpd state=started enabled=true
vim /etc/ansible/roles/database/tasks/main.yml
- name: install mysql yum: name=mysql-server state=present when: ansible_distribution_major_version == "6" - name: install mariadb yum: name=mariadb-server state=present when: ansible_distribution_major_version == "7" - name: install conf file template: src=db.conf.j2 dest=/etc/my.cnf - name: start mysql service: name=mysqld state=started enabled=true when: ansible_distribution_major_version == "6" - name: start mariadb service: name=mariadb state=started enabled=true when: ansible_distribution_major_version == "7"
cd /etc/ansible/roles/
vim keepalived/templates/keepalived.conf.j2
vrrp_instance VI_1 { state MASTER interface eno16777736 virtual_router_id 51 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.152.190 } } vrrp_instance VI_2 { state BACKUP interface eno16777736 virtual_router_id 151 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.152.191 } }
vim nginx/templates/nginx.conf.j2
http { upstream webservers { server 192.168.152.142:80; server 192.168.152.143:80; } location / { proxy_pass http://webservers; } }
vim apachephp/templates/http.conf.j2
DirectoryIndex index.html index.php
vim apachephp/templates/html.index.j2
<h1>192.168.152.142</h1> <?php phpinfo() ?> <?php $link = mysql_connect(‘192.168.152.144‘,‘root‘,‘12345678‘); if($link) echo "Success..."; else echo "Failed...."; mysql_close; ?>
vim database/templates/db.conf.j2
[mysqld] innodb_file_per_table = ON skip_name_resolve = ON
调用roles
mkdir /etc/ansible/yml
vim /etc/ansible/yml/keepalived.yml
- hosts: nginx remote_user: root roles: - keepalived
vim /etc/ansible/yml/nginx.yml
- hosts: nginx remote_user: root roles: - nginx
vim /etc/ansible/yml/apachephp.yml
- hosts: ap remote_user: root roles: - apachephp
vim /etc/ansible/yml/database.yml
- hosts: db remote_user: root roles: - database
测试
ansible-playbook --check /etc/ansible/yml/keepalived.yml
ansible-playbook --check /etc/ansible/yml/nginx.yml
ansible-playbook --check /etc/ansible/yml/apachephp.yml
ansible-playbook --check /etc/ansible/yml/database.yml
运行
ansible-playbook --check /etc/ansible/yml/keepalived.yml
ansible-playbook --check /etc/ansible/yml/nginx.yml
ansible-playbook --check /etc/ansible/yml/apachephp.yml
ansible-playbook --check /etc/ansible/yml/database.yml
完成后需要做微调
192.168.152.141 #主主高可用,140为190主,191备;141为191主,190备
vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 { state BACKUP interface eno16777736 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.152.190 } } vrrp_instance VI_2 { state MASTER interface eno16777736 virtual_router_id 151 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.152.191 } }
192.168.152.143 #为区别与142主页内容,更改IP显示
vim /var/www/html/index.php
<h1>192.168.152.143</h1> <?php phpinfo() ?> <?php $link = mysql_connect(‘192.168.152.144‘,‘root‘,‘12345678‘); if($link) echo "Success..."; else echo "Failed...."; mysql_close; ?>
192.168.152.144 #为root授权管理数据库权限,设置密码便于远程测试连通性
mysql
grant all privileges on . to ‘root‘@‘192.168.%.%‘ identified by ‘12345678‘
完成后可通过关闭其中一台主机keepalived服务,查看ip address list观察虚拟地址漂移
关闭其中一台主机HTTP服务,查看网页是否可继续访问即可。