Ansible中的负载均衡


一、负载均衡是什么?

用户访问web资源时,对于管理者而言要合理的分配服务器资源,保证资源的合理分配,这是必要的,因此就需要一种能给实现负载均衡的手段。

一、haproxy

HAProxy是一个使用C语言编写的自由及开放源代码软件,其提供高可用性、负载均衡,以及基于TCP和HTTP的应用程序代理。
HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。

二、使用步骤

1.安装haproxy负载均衡器

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

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')
import  ssl
ssl._create_default_https_context = ssl._create_unverified_context

2.设定配置文件

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

3.编写用于测试的Playbook

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

4.网页访问,查看测试效果

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

5.实现自动添加节点

编写j2模板:
在这里插入图片描述
在这里插入图片描述
编辑haproxy文件:
在这里插入图片描述
在另一个剧本中导入haproxy.yml剧本:
在这里插入图片描述
haproxy.yml文件:

---
- hosts: lb      %lb组中即为haproxy调度主机
  tasks:
  - name: install haproxy
    dnf:
      name: haproxy
      state: present

  - name: configure haproxy
    template:
      src: haproxy.cfg.j2
      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: reloaded

playbook.yml文件:

---
- hosts: webserver
  tasks:
  - name: install httpd
    dnf:
      name: httpd
      state: present
  
  - name: create index.html
    copy:
      content: "{{ ansible_hostname }}\n"
      dest: /var/www/html/index.html
    
  - name: config httpd
    template:
      src: httpd.conf.j2
      dest: /etc/httpd/conf/httpd.conf
    notify: restart httpd
  
  - name: start httpd
    service:
      name: httpd
      state: started
      enabled: yes
  
  - name: accept httpd
    firewalld:
      service: http
      permanent: yes
      immediate: yes
      state: enabled
    
  - name: accept 8080
    firewalld:
      port: 8080/tcp
      permanent: yes
      immediate: yes
      state: enabled
 - import_playbook: haproxy.yml  

haproxy.cfg.j2文件:

#---------------------------------------------------------------------
# Example configuration for a possible web application.  See the
# full configuration options online.
#
#   https://www.haproxy.org/download/1.8/doc/configuration.txt
#
#---------------------------------------------------------------------

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    #
    # 1) configure syslog to accept network log events.  This is done
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    #
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #   file. A line like the following can be added to
    #   /etc/sysconfig/syslog
    #
    #    local2.*                       /var/log/haproxy.log
    #
    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats

    # utilize system-wide crypto-policies
    ssl-default-bind-ciphers PROFILE=SYSTEM
    ssl-default-server-ciphers PROFILE=SYSTEM

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000

    stats uri /status
    stats auth westos:westos
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main
    bind *:80
#    acl url_static       path_beg       -i /static /images /javascript /stylesheets
#    acl url_static       path_end       -i .jpg .gif .png .css .js
#
#    use_backend static          if url_static
     default_backend             app

#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
#backend static
#    balance     roundrobin
#    server      static 127.0.0.1:4331 check
#
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
    balance     roundrobin
{% for host in groups['webserver'] %}
   server {{ hostvars[host]['ansible_facts']['hostname'] }}  {{ hostvars[host]['ansible_facts']['eth0']['ipv4']['address'] }}:80 check
{% endfor %}

上述设定完成后,可实现当在hosts文件中添加相关节点(主机ip或主机名)时,执行playbook.yml后自动添加haproxy调度节点的功能。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值