--- tags: 'cluster' --- Corosync + Pacemaker + Apache + Ubuntu 18.04 === <i class="fa fa-file-pdf-o" aria-hidden="true"></i> **Corosync + Pacemaker + Apache + Ubuntu 18.04** <i class="fa fa-user-circle-o" aria-hidden="true"></i> Johnny Pan <i class="fa fa-clock-o" aria-hidden="true"></i> 2020-11-25 <i class="fa fa-external-link" aria-hidden="true"></i> https://hackmd.io/@codeskill/ByW2dSpqD [TOC] ## 0. Basado en el siguiente tutorial https://www.tecmint.com/setup-high-availability-clustering-in-centos-ubuntu/ ## 1. Crear 2 tarjetas de red para cada nodo **cluster_node1** (Configurar 2 tarjetas de red) Adaptador puente (DHCP): 192.168.10.80/24 Red interna (Static): 10.10.10.1/24 **cluster_node1** (Configurar 2 tarjetas de red) Adaptador puente (DHCP): 192.168.10.90/24 Red interna (Static): 10.10.10.2/24 ## 2. Asignar IPs estaticas para cada nodo ```shell sudo nano /etc/netplan/01-netcfg.yaml ``` ```shell # === cluster_node1 === network: version: 2 renderer: networkd ethernets: enp6s0: dhcp4: no addresses: [10.10.10.1/24] dhcp6: no # === cluster_node2 === network: version: 2 renderer: networkd ethernets: enp6s0: dhcp4: no addresses: [10.10.10.2/24] dhcp6: no ``` Ejecutar lo siguiente para aplicar los cambios en la configuración de la interface de red ```shell sudo netplan apply ``` ## 3. Configurar el archivo `/etc/hosts` en cada nodo ```shell # === cluster_node1 === 127.0.0.1 localhost 127.0.1.1 cluster_node1 10.10.10.1 cluster_node1 10.10.10.2 cluster_node2 # The following lines are desirable for IPv6 capable hosts ::1 ip6-localhost ip6-loopback cluster_node1 fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters # === cluster_node2 === 127.0.0.1 localhost 127.0.1.1 cluster_node2 10.10.10.1 cluster_node1 10.10.10.2 cluster_node2 # The following lines are desirable for IPv6 capable hosts ::1 ip6-localhost ip6-loopback cluster_node2 fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters ``` ## 3. Instalar corosync, pacemaker y apache (Nodo1 + Nodo2) sudo apt install corosync pacemaker pcs apache2 ## 4. Habilitar el demonio pcsd (Nodo1 + Nodo2) ```shell sudo systemctl enable pcsd sudo systemctl start pcsd sudo systemctl status pcsd ``` ## 5. Crear cluster ### 5.1 Asignar clave al usuario hacluster (Nodo1 + Nodo2) ```shell sudo passwd hacluster ``` ### 5.2 Autenticación pcs (Nodo1 + Nodo2) ```shell sudo pcs cluster auth cluster_node1 cluster_node2 -u hacluster -p <clave_anterior> --force ``` ### 5.3 Configuración nombre del cluster (Nodo1 + Nodo2) ```shell sudo pcs cluster setup --name cluster-web cluster_node1 cluster_node2 ``` ### 5.4 Habilitar cluster al bootear (Nodo1 + Nodo2) ```shell sudo pcs cluster enable --all sudo pcs cluster start --all ``` ### 5.5 Revisar cluster (Nodo1 + Nodo2) ```shell sudo pcs status ``` ## 6. Configurar cluster (Nodo1 + Nodo2) ```shell sudo pcs property set stonith-enabled=false sudo pcs property set no-quorum-policy=ignore sudo pcs property list ``` ## 7. Agregar recursos al cluster (Nodo1 + Nodo2) En la IP ponemos una que este en el rango de la interface de NAT, pero que son sea ninguna de las que tiene asignados por DHCP, en mi caso utilice la **192.168.10.100** con máscara **24**. Creamos el recurso **floating_ip** ```shell sudo pcs resource create floating_ip ocf:heartbeat:IPaddr2 ip=192.168.10.100 cidr_netmask=24 op monitor interval=60s ``` Creamos el recurso **http_server** ```shell sudo pcs resource create http_server ocf:heartbeat:apache configfile="/etc/apache2/sites-available/000-default.conf" op monitor timeout="20s" interval="60s" ``` ## 8. Configuración de las páginas web (Nodo1 + Nodo2) Nos vamos a la ruta `/var/www/html/` y modificamos el archivo `index.html`. Realizar esto en el **cluster_node1** ```html <html> <title>Node 1</title> <body> <center> <h1>Test page - Node 1</h1> </center> </body> </html> ``` Realizar esto en el **cluster_node2** ```html <html> <title>Node 2</title> <body> <center> <h1>Test page - Node 2</h1> </center> </body> </html> ``` ## 9. Revisamos el estado del cluster (Nodo1 + Nodo2) Ejecutamos el siguiente comando `sudo pcs status` ```yaml Cluster name: cluster_apache Stack: corosync Current DC: cluster_node1 (version 1.1.18-2b07d5c5a9) - partition WITHOUT quorum Last updated: Fri Nov 27 05:15:04 2020 Last change: Fri Nov 27 04:36:38 2020 by root via cibadmin on cluster_node1 2 nodes configured 2 resources configured Online: [ cluster_node1 ] OFFLINE: [ cluster_node2 ] Full list of resources: floating_ip (ocf::heartbeat:IPaddr2): Started cluster_node1 http_server (ocf::heartbeat:apache): Stopped Daemon Status: corosync: active/enabled pacemaker: active/enabled pcsd: active/enabled ``` ## 9. Pruebas finales Abrimos una navegdor y ponemos la dirección IP **192.168.10.100** asignada en el **floating_ip** para cargar la página del nodo 1. ![](https://i.imgur.com/qEVxRJC.png) Ahora simulamos que se cae el servidor, apagando el cluster en el nodo 1, con el siguiente comando `sudo pcs cluster stop` ```shell Stopping Cluster (pacemaker)... Stopping Cluster (corosync)... ``` Refrescamos la página web en el navegador y vemos que el cluster funciona y carga la página del nodo 2. ![](https://i.imgur.com/budfsDR.png)