Use haproxy to complete load balancing under Ansible

1. Use haproxy to complete load balancing under Ansible:
1-1: Download haproxy:

dnf install haproxy -y (included in AppStream);

1-2: Write a playbook, install and start apache in the inventory host:
Insert picture description hereInsert picture description here

1-3 Configure haproxy files on the server side:

vim  /etc/haproxy/haproxy.cfg

Write the background status and authenticate users at 64, 65; change the port on line 71 to port 80. Note that the default port of apache is also 80. Therefore, when you open haproxy after the change, you must close apache:
Insert picture description here

Set the server address and port information for load rotation:
Insert picture description here

1-4: Test result after opening
Insert picture description here
haproxy : You can check the background status edited in haproxy.cfg in the website: (password required)

Insert picture description here
Insert picture description here

1-5: Complete ansible automation to add load balancing:

Note: The trigger will cause problems when the playbook executes an error: that is, the file has been changed when the first error is reported, and the trigger will not be triggered the second time, and the service will not restart;

cp /etc/haproxy/haproxy.cfg   ./haproxy.cfg.j2
编写playbook中的内容:使用jinjia2模板(template)
设定触发器(notify  ;  handlers):修改/etc/haproxy/haproxy.cfg的内容后重启 haproxy ;

 - hosts: ms
   tasks:
   - name: configure haproxy
     template:
       src: haproxy.cfg.j2
       dest: /etc/haproxy/haproxy.cfg

Use host variables in haproxy.cfg.j2 to complete timely changes to the members of the group:
Insert picture description here

Guess you like

Origin blog.csdn.net/lb1331/article/details/112271579