使用 haproxy 角色

  1. 在 roles.yml 新增任務,在 balancers 群組主機使用角色 balancer 安裝負載平衡代理伺服器 haproxy。
    [deyu1@kvm19 ansible]$ cat roles.yml
    ---
    - name: install httpd
      hosts: webservers
      roles:
      - apache
    
    - name: use balancer
      hosts: balancers
      roles:
      - balancer
    
  2. ansible-playbook 執行腳本 roles.yml。
    [deyu1@kvm19 ansible]$ ansible-playbook roles.yml 
    TASK [Gathering Facts] *********************************************************
    ok: [kvm13]
    ok: [kvm14]
    
    TASK [apache : install the latest version of Apache] ***************************
    ok: [kvm14]
    ok: [kvm13]
    
    TASK [apache : Start service httpd] ********************************************
    ok: [kvm13]
    ok: [kvm14]
    
    TASK [apache : Enable service firewalld] ***************************************
    ok: [kvm13]
    ok: [kvm14]
    
    TASK [apache : firewalld add http service] *************************************
    ok: [kvm14]
    ok: [kvm13]
    
    TASK [apache : Template a file] ************************************************
    ok: [kvm13]
    ok: [kvm14]
    
    PLAY [use balancer] ************************************************************
    
    TASK [Gathering Facts] *********************************************************
    ok: [kvm15]
    
    TASK [balancer : Download and install haproxy] *********************************
    changed: [kvm15]
    
    TASK [balancer : Configure the haproxy cnf file with hosts] ********************
    changed: [kvm15]
    
    TASK [balancer : Start the haproxy service] ************************************
    changed: [kvm15]
    
    RUNNING HANDLER [balancer : restart haproxy] ***********************************
    changed: [kvm15]
    
    PLAY RECAP *********************************************************************
    kvm13 : ok=6 changed=0  unreachable=0  failed=0  skipped=0  rescued=0  ignored=0
    kvm14 : ok=6 changed=0  unreachable=0  failed=0  skipped=0  rescued=0  ignored=0 
    kvm15 : ok=5 changed=4  unreachable=0  failed=0  skipped=0  rescued=0  ignored=0
    
  3. kvm15 的 selinux 沒處理,還不能連線。
    [deyu1@kvm19 ansible]$ curl http://kvm15/
    curl: (7) Failed to connect to kvm15 port 80: No route to host
    
  4. 登入 kvm15 測試連線負載平衡器主機 kvm15,第一次回應來自 kvm13,再連線一次,回應來自 kvm14,確定 haproxy 工作正常。
    [deyu1@kvm19 ansible]$ ssh kvm15
    [deyu1@kvm15 ~]$ curl http://kvm15/
    Welcome to kvm13.deyu.wang on 192.168.122.13
    [deyu1@kvm15 ~]$ curl http://kvm15/
    Welcome to kvm14.deyu.wang on 192.168.122.14
    [deyu1@kvm15 ~]$ exit
    logout
    Connection to kvm15 closed.