Ansible deploy simple high-availability lamp
Ansible Host: 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
Configure Ansible
Yum-y Install Ansible
Vim/etc/ansible/hosts
[nginx]192.168.152.140192.168.152.141[ap]192.168.152.142192.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 Test connectivity
Create 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-my SQL 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:instal L MARIADB yum:name=mariadb-server state=present when:ansible_distribution_major_version = = "7"-Name:install conf fil E template:src=db.conf.j2 dest=/etc/my.cnf-name:start mysql service:name=mysqld state=started enabled=true when:an Sible_distribution_major_version = = "6"-Name:start mariadb service:name=mariadb state=started enabled=true when:ansi Ble_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
Vim Database/templates/db.conf.j2
[mysqld]innodb_file_per_table = Onskip_name_resolve = On
Call 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
Test
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
Run
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
Fine tuning required after completion192.168.152.141 #主主高可用, 140 is 190 master, 191 standby, 141 191 master, 190 standby
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主页内容, change the IP display
vim/var/www/html/index.php
192.168.152.144 #为root授权管理数据库权限, setting a password facilitates remote testing of connectivity
Mysql
Grant all privileges the ' root ' @ ' 192.168.%.% ' identified by ' 12345678 '
When you are finished, you can view the IP address list to observe the drift of virtual addresses by shutting down one of the host keepalived services.
Close one of the host HTTP services to see if the Web page can continue to be accessed.
This article is from the "Liberalism" blog, make sure to keep this source http://edonkey.blog.51cto.com/887243/1782327
Ansible deploy simple high-availability lamp