首页 > 其他分享 >使用ansible实现haproxy轮询

使用ansible实现haproxy轮询

时间:2022-11-09 04:33:05浏览次数:41  
标签:haproxy httpd name 轮询 etc ansible yum

ansible实现haproxy轮询

项目环境:

主机名 IP地址 用户
ansible 192.168.100.141 greg
node1 192.168.100.142 root
node2 192.168.100.143 root
node3 192.168.100.140 root

实验步骤:

部署httpd

  1. 配置主机清单

    [root@ansible /etc/ansible]#vim hosts
    [webservice]
    node1
    node2
    [haproxy]
    node3
    
  2. 编写角色模板

    [root@ansible /etc/ansible/roles/httpd]#cat tasks/main.yml
    ---
    # tasks file for httpd
    - name: stop firewalld
      service:
    	name: firewalld
    	state: stopped
    	enabled: no
    - name: stop selinux
      lineinfile:
    	path: /etc/selinux/config
    	regexp: '^SELINUX='
    	line: SELINUX=disabled
    - name: stop selinux1
      shell:
    	cmd: setenforce 0
    - name: set yum
      script: yum.sh
    - name: install httpd
      yum:
    	name: httpd
    	state: present
    - name: index.html
      template:
    	src: index.html.j2
    	dest: /var/www/html/index.html
    - name: restart httpd
      service:
    	name: httpd
    	state: restarted
    	enabled: yes
    
  3. 编写仓库替换脚本

    [root@ansible /etc/ansible/roles/httpd]#cat files/yum.sh
    #!/bin/bash
    rm -rf /etc/yum.repos.d/*
    /usr/bin/curl -o /etc/yum.repos.d/CentOS-Base.repo
    https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
    yum reinstall -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
    /usr/bin/sed -i
    's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|'
    /etc/yum.repos.d/epel*
    /usr/bin/sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
    
  4. 编写模板

    [root@ansible /etc/ansible/roles/httpd]#cat templates/index.html.j2
    welcome to {{ ansible_fqdn }} of {{ ansible_default_ipv4.address }}
    
  5. 编写主要playbook

    [root@ansible /etc/ansible]#cat httpd.yml
    ---
    - name: use httpd role
      hosts: node2
    	roles:
    	  - httpd
    

部署harpoxy

  1. 编写角色模板

    [root@ansible /etc/ansible/roles/haproxy]#cat tasks/main.yml
    ---
    # tasks file for haproxy
    - name: stop firewalld
      service:
    	name: firewalld
    	state: stopped
    	enabled: no
    - name: stop selinux
      lineinfile:
    	path: /etc/selinux/config
    	regexp: '^SELINUX='
    	line: SELINUX=disabled
    - name: stop selinux1
      shell:
    	cmd: setenforce 0
    - name: set yum
      script: yum.sh
    - name: install haproxy
      yum:
    	name: haproxy
    	state: present
    - name: cp config
      template:
    	src: haproxy.cfg.j2
    	dest: /etc/haproxy/haproxy.cfg
    - name: restart haproxy
      service:
    	name: haproxy
    	state: restarted
    	enabled: yes
    
    
  2. yum仓库替换脚本

    [root@ansible /etc/ansible/roles/haproxy]#cat files/yum.sh
    #!/bin/bash
    rm -rf /etc/yum.repos.d/*
    /usr/bin/curl -o /etc/yum.repos.d/CentOS-Base.repo
    https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
    yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
    /usr/bin/sed -i
    's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|'
    /etc/yum.repos.d/epel*
    /usr/bin/sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
    
    
  3. 编写模板

    [root@ansible /etc/ansible/roles/haproxy]#cat templates/haproxy.cfg.j2
    {% for lx in groups.webservers %}
    server {{ hostvars[lx].ansible_fqdn }} {{
    hostvars[lx].ansible_default_ipv4.address }}:80 check
    {% endfor %}
    
  4. 编写playbook

    [root@ansible /etc/ansible]#cat haproxy.yml
    ---
    - name: get webservers facts
      hosts: webservers
      
    - name: user haproxy
      hosts: node3
      roles:
    	- haproxy
    
  5. 验证

    1667937043068

image-20221109041456245

标签:haproxy,httpd,name,轮询,etc,ansible,yum
From: https://www.cnblogs.com/Archer-x/p/16871897.html

相关文章

  • 使用ansible部署lamp
    使用Ansible部署Lamp项目环境:主机IP地址用户ansible192.168.100.110gregnode1192.168.100.120root实验步骤:部署Apache:首先控制端和受控端需要......
  • ansible部署httpd和haproxy
    角色部署httpd[root@ansibleansible]#lsansible.cfghostsphp.ymlapache.ymlmysql.ymlroles[root@ansibleansible]#vimhosts[webservers]node1no......
  • Flask框架:运用Ajax轮询动态绘图
    Ajax是异步JavaScript和XML可用于前后端交互,在之前《Flask框架:运用Ajax实现数据交互》简单实现了前后端交互,本章将通过Ajax轮询获取后端的数据,前台使用echart绘图库进行图......
  • Flask框架:运用Ajax轮询动态绘图
    Ajax是异步JavaScript和XML可用于前后端交互,在之前《Flask框架:运用Ajax实现数据交互》简单实现了前后端交互,本章将通过Ajax轮询获取后端的数据,前台使用echart绘图库进行图......
  • 基于ansible实现不同AWS账号之间EC2-Linux系统的自动化-环境准备
    不同的AWS之间的EC2之间如果需要通信,实现ansible自动化,是需要有网络条件的本文主要介绍不同AWS账号之间EC2-(Linux系统)之间实现通信的网络环境准备步骤1、在VPC中建立到另......
  • 关于haproxy不重启重新加载配置文件
    HAProxy:在不影响现有连接的情况下,重新加载配置第一种方法:原来的版本好像是比较麻烦,参见:http://haproxy.1wt.eu/download/1.3/doc/architecture.txt第二种方法:但......
  • CentOS7编译安装haproxy-2.6.6
    创建用户并安装依赖包#selinux会影响haproxy启动,会有'cannotbindUNIXsocket(Permissiondenied)'的报错,请关闭它除非你知道设置selinux规则sed-i'/SELINUX/s/enf......
  • 自动化利器 Ansible - 从了解到应用
    本文说明本系列使用ansible2.9.27版本来说明和汇总相关信息。#cat/etc/system-releaseRedHatEnterpriseLinuxServerrelease7.8(Maipo)##uname-aLinu......
  • Ansible - 7 - 性能优化
    性能优化在默认设置的情况下,Ansible的执行效率已经可以满足大多数场景。面对巨量目标主机时,可以通过一些配置优化去再提高ansible的执行效率。基本设置#通过time命......
  • Ansible - 9 - 技巧提示
    Ansible技巧提示1-免密登录#通过秘钥方式连接ssh-keygen-trsassh-copy-id-i/vipxf/.ssh/[email protected]/vipxf/.ssh/id_rsa......