• Ansible-部署haproxy高可用


    Ansible-部署haproxy高可用

    配置主机

    [root@server ~]# cd /etc/ansible/
    [root@server ansible]# ls
    ansible.cfg  hosts  httpd.yml  mysqld.yml  php.yml  roles
    [root@server ansible]# vim hosts
    [dev]
    node1
    node2
    [harproxy]
    node3
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    创建角色

    [root@server ansible]# cd roles/
    [root@server roles]# ls
    apache  mysql  php
    [root@server roles]# ansible-galaxy init httpd
    - Role httpd was created successfully
    [root@server roles]# ansible-galaxy init haproxy
    - Role haproxy was created successfully
    [root@server roles]# ls
    apache  haproxy  httpd  mysql  php
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    通过httpd角色部署web站点

    [root@server roles]# vim httpd/tasks/main.yml
    [root@server roles]# cat httpd/tasks/main.yml
    ---
    # tasks file for httpd
    - name: set firewalld
      service:
        name: firewalld
        state: stopped
        enabled: no
    - name: stop selinux
      lineinfile:
        path: /etc/sysconfig/selinux
        regexp: '^SELINUX='
        line: 'SELINUX=disabled'
    - name: setenforce 0
      shell: setenforce 0
    - name: install httpd
      yum:
        name: httpd
        state: present
    - name: index.html.j2
      template:
        src: index.html.j2
        dest: /var/www/html/index.html
    - name: restart httpd
      service:
        name: httpd
        state: restarted
        enabled: yes
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    模板文件

    [root@server roles]# vim httpd/templates/index.html.j2
    [root@server roles]# cat httpd/templates/index.html.j2
    hello {{ ansible_fqdn }} is {{ ansible_ens33.ipv4.address }}
    
    
    • 1
    • 2
    • 3
    • 4

    调用httpd角色

    [root@server ansible]# vim httpd-2.yml
    [root@server ansible]# cat httpd-2.yml
    ---
    - name: use httpd roles
      hosts: node2
      roles:
        - httpd
    [root@server ansible]# ansible-playbook httpd-2.yml
    
    PLAY [use httpd roles] ********************************************************************************
    
    TASK [Gathering Facts] ********************************************************************************
    ok: [node2]
    
    TASK [httpd : set firewalld] **************************************************************************
    changed: [node2]
    
    TASK [httpd : stop selinux] ***************************************************************************
    changed: [node2]
    
    TASK [httpd : setenforce 0] ***************************************************************************
    changed: [node2]
    
    TASK [install httpd] **********************************************************************************
    changed: [node2]
    
    TASK [httpd : index.html.j2] **************************************************************************
    changed: [node2]
    
    TASK [restart httpd] **********************************************************************************
    changed: [node2]
    
    PLAY RECAP ********************************************************************************************
    node2                      : ok=7    changed=6    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
    
    [root@server ansible]# curl http://node2
    hello node2.example.com is 192.168.181.182
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    使用haproxy角色部署haproxy

    [root@server ansible]# vim roles/haproxy/tasks/main.yml
    [root@server ansible]# cat roles/haproxy/tasks/main.yml
    ---
    # tasks file for haproxy
    - name: - name: set firewalld
      service:
        name: firewalld
        state: stopped
        enabled: no
    - name: stop selinux
      lineinfile:
        path: /etc/sysconfig/selinux
        regexp: '^SELINUX='
        line: 'SELINUX=disabled'
    - name: setenforce 0
      shell: setenforce 0
    - name: install haproxy
      yum:
        name: haproxy
        state: present
    - name: haproxy.cfg.j2
      template:
        src: haproxy.cfg.j2
        dest: /etc/haproxy/haproxy.cfg
    - name: restart haproxy
      sercice:
        name: haproxy
        state: restarted
        enabled: yes
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    模板文件

    先在ansible节点yum安装haproxy复制他的haproxy.cfg,编写模板文件

    [root@server ~]# yum -y install haproxy
    [root@server ansible]# cp /etc/haproxy/haproxy.cfg roles/haproxy/templates/haproxy.cfg.j2
    [root@server ansible]# vim roles/haproxy/templates/haproxy.cfg.j2
    frontend main
        bind *:80
    backend app
        balance     roundrobin
    {% for host in groups.dev %}
    server {{ hostvars[host].ansible_fqdn }} {{ hostvars[host].ansible_ens33.ipv4.address }}:80 check
    {% endfor %}
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    调用haproxy

    [root@server ansible]# vim haproxy.yml
    ---
    - name: dev shishi
      hosts: dev
    
    - name: use haproxy roles
      hosts: node3
      roles:
        - haproxy
    [root@server ansible]# ansible-playbook haproxy.yml 
    
    PLAY [dev shishi] *******************************************************************************************
    
    TASK [Gathering Facts] **************************************************************************************
    ok: [node2]
    ok: [node1]
    
    PLAY [use haproxy roles] ************************************************************************************
    
    TASK [Gathering Facts] **************************************************************************************
    ok: [node3]
    
    TASK [haproxy : set firewalld] ******************************************************************************
    ok: [node3]
    
    TASK [haproxy : stop selinux] *******************************************************************************
    ok: [node3]
    
    TASK [haproxy : setenforce 0] *******************************************************************************
    changed: [node3]
    
    TASK [install haproxy] **************************************************************************************
    ok: [node3]
    
    TASK [haproxy.cfg.j2] ***************************************************************************************
    changed: [node3]
    
    TASK [restart haproxy] **************************************************************************************
    changed: [node3]
    
    PLAY RECAP **************************************************************************************************
    node1                      : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    node2                      : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    node3                      : ok=7    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0      
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    访问

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

  • 相关阅读:
    会议OA之待开会议&所有会议
    基于JavaFX的扫雷游戏实现(二)——游戏界面
    c++征途 --- 文件操作
    python+django车辆违章信息查询管理系统pycharm项目
    现在大火的低代码是怎么回事?进来聊聊低代码
    运维理想和现实,你是?
    Linux内核(十六)Linux 内核态进行读写文件的函数 使用和解析
    【Java】想进大厂?你应该知道的算法经典习题(队列)
    如何使用python获取ssl证书信息
    「UG/NX」Block UI 指定坐标SpecifyCSYS
  • 原文地址:https://blog.csdn.net/weixin_72900594/article/details/127753744