diff --git a/lamp_haproxy/README.md b/lamp_haproxy/README.md index ff43f13..5626b0c 100644 --- a/lamp_haproxy/README.md +++ b/lamp_haproxy/README.md @@ -1,12 +1,10 @@ -Lamp Stack + load balancer(haproxy) + add/remove nodes from cluster + Serial Rolling update of webserserver ----------------------------------------------------------------------------------------------------------- +LAMP Stack + HAProxy: Example Playbooks +----------------------------------------------------------------------------- -This example is an extension of the simple lamp deployment, In this example we deploy a lampstack with a LoadBalancer in front. -This also has the capability to add/remove nodes from the deployment. It also includes examples to do a rolling update of a stack -without affecting the service. +This example is an extension of the simple LAMP deployment. Here we'll deploy a web server with an HAProxy load balancer in front. This set of playbooks also have the capability to dynamically add and remove web server nodes from the deployment. It also includes examples to do a rolling update of a stack without affecting the service. ###Setup Entire Site. -Firstly we setup the entire stack, configure the 'hosts' inventory file to include the names of your hosts on which the stack would be deployed. +First we configure the entire stack by listing our hosts in the 'hosts' inventory file, grouped by their purpose: [webservers] web3 @@ -16,26 +14,32 @@ Firstly we setup the entire stack, configure the 'hosts' inventory file to inclu [lbservers] lbserver -After which we execute the following command to deploy the site. +After which we execute the following command to deploy the site: ansible-playbook -i hosts site.yml -The deployment can be verified by accessing the webpage." lynx http://:8888. multiple access should land you up in different webservers. +The deployment can be verified by accessing the IP address of your load balnacer host in a web browser: http://:8888. Reloading the page should have you hit different webservers. -###Remove a node from the cluster. -Removal of a node from the cluster would be as simple as executing the following command: +###Remove a Node - ansible-playbook -i hosts playbooks/remove_webservers.yml --limit=web2 +Removal of a node from the cluster is as simple as executing the following command: + + ansible-playbook -i hosts playbooks/remove_webservers.yml --limit=web2 + +###Add a Node -###Adding a node to the cluster. Adding a node to the cluster can be done by executing the following command: - ansible-playbook -i hosts playbooks/add_webservers.yml --limit=web2 + ansible-playbook -i hosts playbooks/add_webservers.yml --limit=web2 + +###Rolling Update + +Rolling updates are the preferred way to update the web server software or deployed application, since the load balancer can be dynamically configured to take the hosts to be updated out of the pool. This will keep the service running on other servers so that the users are not interrupted. + +In this example the hosts are updated in serial fashion, which means +that only one server will be updated at one time. If you have a lot of web server hosts, this behaviour can be changed by setting the 'serial' keyword in webservers.yml file. -###Rolling update of the entire site or a single hosts -Rolling updates are the preffered way to do an update as this wont affect the end users, In this example the hosts are updated in serial fashion, which means -that only one server would be updated at one time, this behaviour can be changed by setting the 'serial' keyword in webservers.yml file. -Once the code has been updated in the repository which can be defined in the group_vars/all file, execute the following command: +Once the code has been updated in the source repository for your application which can be defined in the group_vars/all file, execute the following command: ansible-playbook -i hosts playbooks/rolling_update.yml diff --git a/lamp_haproxy/group_vars/all b/lamp_haproxy/group_vars/all index 1bfa7db..9e5363d 100644 --- a/lamp_haproxy/group_vars/all +++ b/lamp_haproxy/group_vars/all @@ -1,5 +1,5 @@ --- -# varialbles here would be applicable to all groups +# Variables here are applicable to all host groups httpd_port: 80 ntpserver: 192.168.1.2 diff --git a/lamp_haproxy/group_vars/dbservers b/lamp_haproxy/group_vars/dbservers index 3c48323..bb60d44 100644 --- a/lamp_haproxy/group_vars/dbservers +++ b/lamp_haproxy/group_vars/dbservers @@ -1,5 +1,6 @@ --- -# The variables file used by the playbooks in the dbservers group, these dont have to be imported by vars_files: these are autopopulated. +# The variables file used by the playbooks in the dbservers group. +# These don't have to be explicitly imported by vars_files: they are autopopulated. mysqlservice: mysqld mysql_port: 3306 diff --git a/lamp_haproxy/group_vars/lbservers b/lamp_haproxy/group_vars/lbservers index 042cc35..adb658c 100644 --- a/lamp_haproxy/group_vars/lbservers +++ b/lamp_haproxy/group_vars/lbservers @@ -1,18 +1,19 @@ --- -# File for the HAproxy configuration +# Variables for the HAproxy configuration -# Supports http and tcp, for ssl smtp etc.. use tcp +# HAProxy supports "http" and "tcp". For SSL, SMTP, etc, use "tcp". mode: http -# port on which the lb should listen +# Port on which HAProxy should listen listenport: 8888 -# A name for the proxy daemon, this would be the suffix in the logs. +# A name for the proxy daemon, this wil be the suffix in the logs. daemonname: myapplb -# Balancing Algorithm Avalilable options: roundrobin,source,leastconn,source,uri -# (if persistance is required use source) +# Balancing Algorithm. Available options: +# roundrobin, source, leastconn, source, uri +# (if persistance is required use, "source") balance: roundrobin -# Which Ethernet interface on which the load balancer should listen +# Ethernet interface on which the load balancer should listen iface: eth0 diff --git a/lamp_haproxy/group_vars/webservers b/lamp_haproxy/group_vars/webservers index fb06515..6668d77 100644 --- a/lamp_haproxy/group_vars/webservers +++ b/lamp_haproxy/group_vars/webservers @@ -1,5 +1,5 @@ --- -# File for the web server configuration +# Variables for the web server configuration -# Which Ethernet interface on which the web server should listen +# Ethernet interface on which the web server should listen iface: eth0 diff --git a/lamp_haproxy/playbooks/add_webservers.yml b/lamp_haproxy/playbooks/add_webservers.yml index b1e8511..fa2038f 100644 --- a/lamp_haproxy/playbooks/add_webservers.yml +++ b/lamp_haproxy/playbooks/add_webservers.yml @@ -1,5 +1,5 @@ --- -# This Playbook adds a webserver into the the web cluster +# This Playbook adds a webserver into the the web cluster - hosts: webservers user: root diff --git a/lamp_haproxy/playbooks/db.yml b/lamp_haproxy/playbooks/db.yml index 2aaf4d0..c97f241 100644 --- a/lamp_haproxy/playbooks/db.yml +++ b/lamp_haproxy/playbooks/db.yml @@ -1,5 +1,5 @@ --- -# This playbook deploys mysql and configures database on the db node/nodes +# This playbook deploys MySQL and configures the database on the db node(s) - hosts: dbservers user: root diff --git a/lamp_haproxy/playbooks/haproxy.yml b/lamp_haproxy/playbooks/haproxy.yml index 76d9edb..5c7b4c2 100644 --- a/lamp_haproxy/playbooks/haproxy.yml +++ b/lamp_haproxy/playbooks/haproxy.yml @@ -1,5 +1,5 @@ --- -#PlayBook for haproxy operations +# Playbook for HAProxy operations - hosts: lbservers user: root diff --git a/lamp_haproxy/playbooks/remove_webservers.yml b/lamp_haproxy/playbooks/remove_webservers.yml index c5526a0..564b3be 100644 --- a/lamp_haproxy/playbooks/remove_webservers.yml +++ b/lamp_haproxy/playbooks/remove_webservers.yml @@ -1,6 +1,7 @@ --- -# This Playbook removes a webserver from the pool serialy. - +# This playbook removes a webserver from the pool serially. +# Change the value of serial: to adjust the number of servers +# to be removed at a time. - hosts: webservers user: root diff --git a/lamp_haproxy/playbooks/rolling_update.yml b/lamp_haproxy/playbooks/rolling_update.yml index 24c3848..aa414ee 100644 --- a/lamp_haproxy/playbooks/rolling_update.yml +++ b/lamp_haproxy/playbooks/rolling_update.yml @@ -1,5 +1,6 @@ --- -# This Playbook does a rolling update of the code for all webservers serially (one at a time). Change the value of serial: to adjust the number of server to be updated. +# This Playbook does a rolling update of the code for all webservers serially (one at a time). +# Change the value of serial: to adjust the number of server to be updated. - hosts: webservers user: root diff --git a/lamp_haproxy/playbooks/web.yml b/lamp_haproxy/playbooks/web.yml index 3c6e9d0..669ae38 100644 --- a/lamp_haproxy/playbooks/web.yml +++ b/lamp_haproxy/playbooks/web.yml @@ -1,5 +1,5 @@ --- -# This Playbook deploys the WebServers with httpd and the code. +# This playbook deploys the webservers with httpd and the code. - hosts: webservers user: root diff --git a/lamp_haproxy/roles/common/handlers/main.yml b/lamp_haproxy/roles/common/handlers/main.yml index e6a655e..bca0737 100644 --- a/lamp_haproxy/roles/common/handlers/main.yml +++ b/lamp_haproxy/roles/common/handlers/main.yml @@ -1,5 +1,5 @@ --- -# Handler to handle common notifications +# Handlers for common notifications - name: restart ntp service: name=ntpd state=restarted diff --git a/lamp_haproxy/roles/common/tasks/main.yml b/lamp_haproxy/roles/common/tasks/main.yml index 10fcf6c..1d7e9f7 100644 --- a/lamp_haproxy/roles/common/tasks/main.yml +++ b/lamp_haproxy/roles/common/tasks/main.yml @@ -1,5 +1,5 @@ --- -# This playbook contains common plays that would be run on all Nodes. +# This playbook contains common plays that will run on all nodes. - name: Install ntp yum: name=ntp state=present @@ -18,8 +18,4 @@ get_url: url=http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm dest=/tmp/ force=yes - name: Install EPEL RPM -# command: rpm -Uvh --force /tmp/epel-release-6-8.noarch.rpm yum: name=/tmp/epel-release-6-8.noarch.rpm state=installed - -- name: Clean up - command: rm -f /tmp/epel-release-6-8.noarch.rpm diff --git a/lamp_haproxy/roles/db/tasks/main.yml b/lamp_haproxy/roles/db/tasks/main.yml index f6068a0..549a634 100644 --- a/lamp_haproxy/roles/db/tasks/main.yml +++ b/lamp_haproxy/roles/db/tasks/main.yml @@ -1,5 +1,5 @@ --- -# This playbook will install mysql and create db user and give permissions. +# This playbook will install MySQL and create db user and give permissions. - name: Install Mysql package action: yum pkg=$item state=installed @@ -24,7 +24,6 @@ lineinfile: dest=/etc/sysconfig/iptables state=present regexp="$mysql_port" insertafter="^:OUTPUT " line="-A INPUT -p tcp --dport $mysql_port -j ACCEPT" notify: restart iptables - - name: Create Application Database mysql_db: name=$dbname state=present diff --git a/lamp_haproxy/roles/haproxy/handlers/main.yml b/lamp_haproxy/roles/haproxy/handlers/main.yml index bdeda82..1eade08 100644 --- a/lamp_haproxy/roles/haproxy/handlers/main.yml +++ b/lamp_haproxy/roles/haproxy/handlers/main.yml @@ -1,5 +1,5 @@ --- -# Handlers for the HAproxy +# Handlers for HAproxy - name: restart haproxy service: name=haproxy state=restarted @@ -7,5 +7,3 @@ - name: reload haproxy service: name=haproxy state=reloaded - - diff --git a/lamp_haproxy/roles/haproxy/tasks/main.yml b/lamp_haproxy/roles/haproxy/tasks/main.yml index d5daaa9..9a68e3b 100644 --- a/lamp_haproxy/roles/haproxy/tasks/main.yml +++ b/lamp_haproxy/roles/haproxy/tasks/main.yml @@ -1,5 +1,5 @@ --- -# This PlayBook Installs the HAProxy and configures it. +# This playbook installs HAProxy and configures it. - name: Download and install haproxy and socat yum: name=$item state=installed diff --git a/lamp_haproxy/roles/haproxy/templates/haproxy.cfg.j2.bck b/lamp_haproxy/roles/haproxy/templates/haproxy.cfg.j2.bck deleted file mode 100644 index 0868327..0000000 --- a/lamp_haproxy/roles/haproxy/templates/haproxy.cfg.j2.bck +++ /dev/null @@ -1,36 +0,0 @@ -global - log 127.0.0.1 local2 - - chroot /var/lib/haproxy - pidfile /var/run/haproxy.pid - maxconn 4000 - user root - group root - daemon - - # turn on stats unix socket - stats socket /var/lib/haproxy/stats level admin - -defaults - mode {{ mode }} - log global - option httplog - option dontlognull - option http-server-close - option forwardfor except 127.0.0.0/8 - option redispatch - retries 3 - timeout http-request 10s - timeout queue 1m - timeout connect 10s - timeout client 1m - timeout server 1m - timeout http-keep-alive 10s - timeout check 10s - maxconn 3000 - -backend app - {% for host in groups['lbservers'] %} - listen {{ daemonname }} {{ hostvars[host].ansible_eth0.ipv4.address }}:{{ listenport }} - {% endfor %} - balance {{ balance }} diff --git a/lamp_haproxy/roles/web/handlers/main.yml b/lamp_haproxy/roles/web/handlers/main.yml index f613a8f..41cd34f 100644 --- a/lamp_haproxy/roles/web/handlers/main.yml +++ b/lamp_haproxy/roles/web/handlers/main.yml @@ -1,5 +1,5 @@ --- -# Handler for the webtier +# Handler for the web tier - name: restart iptables service: name=iptables state=restarted diff --git a/lamp_haproxy/roles/web/tasks/add_to_lb.yml b/lamp_haproxy/roles/web/tasks/add_to_lb.yml index 8d22efb..15d00b3 100644 --- a/lamp_haproxy/roles/web/tasks/add_to_lb.yml +++ b/lamp_haproxy/roles/web/tasks/add_to_lb.yml @@ -1,5 +1,5 @@ --- -# This Playbook does utility stuff's like adding a webserver into the pool, etc.. +# This Playbook handles the addition of a web server to the pool. - name: Add server to LB lineinfile: dest=/etc/haproxy/haproxy.cfg state=present regexp="${ansible_hostname}" line="server ${ansible_hostname} ${hostvars.{$inventory_hostname}.ansible_$iface.ipv4.address}:${httpd_port}" diff --git a/lamp_haproxy/roles/web/tasks/copy_code.yml b/lamp_haproxy/roles/web/tasks/copy_code.yml index 2c6ee09..95c705b 100644 --- a/lamp_haproxy/roles/web/tasks/copy_code.yml +++ b/lamp_haproxy/roles/web/tasks/copy_code.yml @@ -4,7 +4,5 @@ - name: Copy the code from repository git: repo=${repository} dest=/var/www/html/ - -- name: Create's the index.php file +- name: Create the index.php file template: src=../roles/web/templates/index.php.j2 dest=/var/www/html/index.php - diff --git a/lamp_haproxy/roles/web/tasks/install_httpd.yml b/lamp_haproxy/roles/web/tasks/install_httpd.yml index 19ea206..3fa703f 100644 --- a/lamp_haproxy/roles/web/tasks/install_httpd.yml +++ b/lamp_haproxy/roles/web/tasks/install_httpd.yml @@ -9,7 +9,6 @@ - php-mysql - libsemanage-python - libselinux-python - - name: insert iptables rule for httpd lineinfile: dest=/etc/sysconfig/iptables state=present regexp="$httpd_port" insertafter="^:OUTPUT " line="-A INPUT -p tcp --dport $httpd_port -j ACCEPT" diff --git a/lamp_haproxy/roles/web/tasks/remove_from_lb.yml b/lamp_haproxy/roles/web/tasks/remove_from_lb.yml index 985d0d4..a3ed52f 100644 --- a/lamp_haproxy/roles/web/tasks/remove_from_lb.yml +++ b/lamp_haproxy/roles/web/tasks/remove_from_lb.yml @@ -1,9 +1,9 @@ --- -# This Playbook does utility stuff's like adding a webserver into the pool, etc.. +# This playbook handles the removal of a webserver from the pool. - name: Remove the code from server command: rm -rf /var/www/html/* - + - name: Remove server from LB lineinfile: dest=/etc/haproxy/haproxy.cfg state=absent regexp="${ansible_hostname}" delegate_to: $item diff --git a/lamp_haproxy/roles/web/tasks/utils.yml b/lamp_haproxy/roles/web/tasks/utils.yml deleted file mode 100644 index ac3786f..0000000 --- a/lamp_haproxy/roles/web/tasks/utils.yml +++ /dev/null @@ -1,39 +0,0 @@ ---- -# This Playbook does utility stuff's like adding a webserver into the pool, etc.. - -- name: Add server to LB - lineinfile: dest=/etc/haproxy/haproxy.cfg state=present regexp="${ansible_hostname}" line="server ${ansible_hostname} ${hostvars.{$inventory_hostname}.ansible_$iface.ipv4.address}:${httpd_port}" - delegate_to: ${lbserver} - register: last_run - tags: add - -- name: Reload the haproxy - service: name=haproxy state=reloaded - delegate_to: ${lbserver} - only_if: ${last_run.changed} - tags: add - -- name: Remove the code from server - command: rm -rf /var/www/html/* - tags: remove - -- name: Remove server from LB - lineinfile: dest=/etc/haproxy/haproxy.cfg state=absent regexp="${ansible_hostname}" - delegate_to: $item - with_items: ${groups.lbservers}} - register: last_run - tags: remove - -- name: disable the server in haproxy - shell: echo "disable server myapplb/${ansible_hostname}" | socat stdio /var/lib/haproxy/stats - delegate_to: $item - with_items: ${groups.lbservers}} - tags: remove - -- name: Remove the httpd package - yum: name=httpd state=absent - tags: remove - - - - diff --git a/lamp_haproxy/roles/web/templates/index.php.j2 b/lamp_haproxy/roles/web/templates/index.php.j2 index 35f702c..5c45513 100644 --- a/lamp_haproxy/roles/web/templates/index.php.j2 +++ b/lamp_haproxy/roles/web/templates/index.php.j2 @@ -7,7 +7,7 @@ Homepage
"; ?>