1. 程式人生 > 其它 >ansible自動化部署haproxy+http負載均衡

ansible自動化部署haproxy+http負載均衡

如果不利用ansible自動化部署工具,其部署繁瑣程度可通過點選這裡檢視

server1172.25.254.189輪詢控制端
server2172.25.254.188輪詢端
server3172.25.254.187輪詢端
客戶端17225.254.89測試

這裡我們通過ansible 進行部署,只需要熟悉playbook書寫相關規則:

[[email protected] ansible]# cat hosts
[test]
server2

[prod]
server3

[webserver:children]
test
prod

[lb]
server1
[[email protected]
ansible]# cat ansible.cfg [defaults] inventory = ./hosts [privilege_escalation] become=True become_method=sudo become_user=root become_ask_pass=False

部署apache:

[[email protected] ansible]# cat playbook1.yml
---
- hosts: webserver    ###在server2、server3主機上安裝部署apache
  tasks:
  - name: install
the latest version of Apache from the testing repo dnf: name: httpd state: present - name: add index.html copy: content: "{{ ansible_hostname }}" ##將登陸的主機名字寫入兩個輪詢主機發布目錄中。 dest: /var/www/html/index.html - name: start apache service: name: httpd state: started enabled: yes
- name: add http to firewalld firewalld: service: http permanent: yes state: enabled immediate: yes - import_playbook: haproxy.yml

部署haproxy:

[[email protected] ansible]# cat haproxy.yml 
--- 
- hosts: lb    ##在排程主機上安裝haproxy服務。
  tasks:
  - name: install haproxy
    dnf: 
      name: haproxy
      state: present

  - name: configure haproxy    ##更改該目錄下的haproxy.cfg檔案,將與/etc/haproxy/haproxy.cfg進行對比,若發生更改就將其同步,並觸發restart haproxy任務。
    template:
      src: haproxy.cfg
      dest: /etc/haproxy/haproxy.cfg
    notify: restart haproxy

  - name: start haproxy
    service:
      name: haproxy
      state: started

  - name: accept haproxy   
    firewalld:
      service: http
      permanent: yes
      immediate: yes
      state: enabled

  handlers:
  - name: restart haproxy
    service:
      name: haproxy
      state: restarted
[[email protected] ansible]# cat haproxy.cfg 

定義排程的埠:
在這裡插入圖片描述
將需要排程的主機寫進配置檔案:
在這裡插入圖片描述
在客戶機端測試:
在這裡插入圖片描述
我們發現在server2和server3之間進行輪詢切換。