ansible--playbook实现负载均衡

创建缩进文件,指定缩进为2格

[devops@server4 ~]$ vim .vimrc
[devops@server4 ~]$ cat .vimrc
autocmd filetype yaml setlocal ai
[devops@server4 ~]$ pwd
/home/devops

四台主机
172.25.2.104 server4 服务端
172.25.2.105 server5 客户端 httpd
172.25.2.106 server6 客户端 httpd
172.25.2.107 server7 haproxy

Apache的安装

1.设置需要安装的主机

[devops@server4 ansible]$ vim inventory 
[devops@server4 ansible]$ cat inventory 
[test]
172.25.2.105
[web]
172.25.2.106

[webservers:children]
test
web

2.apache.yml的创建

[devops@server4 ansible]$ mkdir apache
[devops@server4 ansible]$ cd apache/
[devops@server4 apache]$ vim httpd.yml
---
- hosts: all  ##针对所有主机
  vars:
    http_port: 80  ##端口
    http_ip: '{{ ansible_facts["eth0"]["ipv4"]["address"]}}'  ##ip的抓取
  tasks:  ##任务1
    - name: install httpd  ##安装httpd
      yum:
        name: httpd
        state: present  ##当前版本

    - name: configure httpd
      template:  ##jinja模板
        src: templates/httpd.conf.j2  ##配置文件所在当前路径
        dest: /etc/httpd/conf/httpd.conf  ##配置文件安装后的路径
        owner: root
        group: root
        mode: 644
      notify: restart httpd  ##notify:触发器
    - name: test page
      copy:
        src: files/index.html
        dest: /var/www/html/index.html

    - name: start httpd
      service:
        name: "{{ item }}"
        state: started
        enabled: true
      loop:
        - httpd
        - firewalld

    - name: configure firewalld
      firewalld:
        service: "{{ item }}"
        permanent: yes
        immediate: yes
        state: enabled

      loop:
        - http
        - https

  handlers:  ##触发器触发条件
    - name:  restart httpd
      service:
        name: httpd
        state: restarted
[devops@server4 apache]$ mkdir files
[devops@server4 apache]$ cd files
[devops@server4 files]$ vim index.html
[devops@server4 files]$ cat index.html
www.westos.org
[devops@server4 files]$ cd ..
[devops@server4 apache]$ mkdir templates ## jinja模板路径,可以将参数放入jinja模板,相应客户端的内容会随着参数的改变而改变
[devops@server4 apache]$ cd templates
[devops@server4 templates]$ ls
httpd.conf.j2
 42 Listen {{ http_ip }}:{{ http_port }}
[devops@server4 ansible]$ ansible-playbook apache/httpd.yml

roles目录结构

role_name:
files:存放需要同步到异地服务器的源码文件及配置文件;
handlers:当资源发生变化时需要进行的操作,若没有此目录可以不建或为空;
meta:存放说明信息、说明角色依赖等信息,可留空;
tasks:定义各种task,要有main.yml,其他文件include包含调用;
templates:存储由template模块调用的模板文本;
vars:本次安装定义的变量
defaults:要有main.yml的文件,用于设定默认的变量

haproxy搭建

[devops@server4 ansible]$ vim inventory 
[devops@server4 ansible]$ cat inventory 
[test]
172.25.2.104
[devops@server4 ansible]$ ls
ansible.cfg  apache  haproxy  inventory 
[devops@server4 ansible]$ cd haproxy/
[devops@server4 haproxy]$ ls
defaults  files  handlers  meta  README.md  tasks  templates  tests  vars
[devops@server4 haproxy]$ cd templates/
[devops@server4 templates]$ pwd
/home/devops/ansible/haproxy/templates
[devops@server4 templates]$ ls
haproxy.cfg.j2
[devops@server4 ansible]$ vim haproxy/templates/haproxy.cfg.j2 
#---------------------------------------------------------------------
# Example configuration for a possible web application.  See the
# full configuration options online.
#
#   http://haproxy.1wt.eu/download/1.4/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

#---------------------------------------------------------------------
# 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

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
#frontend  main *:8080
#    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
##---------------------------------------------------------------------
listen http *:80
    balance     roundrobin
    server  app1 172.25.2.105:80 check
    server  app2 172.25.2.106:80 check
[devops@server4 ansible]$ vim haproxy/haproxy.yml
---
- hosts: test
  tasks:
    - name: install haproxy
      yum:
        name: haproxy
        state: present

    - name: configure haproxy
      template:
        src: haproxy.cfg.j2
        dest: /etc/haproxy/haproxy.cfg
        owner: root
        group: root
        mode: 664
      notify: restart haproxy

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

  handlers:
    - name: restart haproxy
      service:
        name: haproxy
        state: restarted

物理机测试:

[kiosk@foundation2 ~]$ curl 172.25.2.107:80
server6 www.westos.org
[kiosk@foundation2 ~]$ curl 172.25.2.107:80
server5 www.westos.org
[kiosk@foundation2 ~]$ curl 172.25.2.107:80
server6 www.westos.org
[kiosk@foundation2 ~]$ curl 172.25.2.107:80
server5 www.westos.org
[kiosk@foundation2 ~]$ curl 172.25.2.107:80
server6 www.westos.org
[kiosk@foundation2 ~]$ curl 172.25.2.107:80
server5 www.westos.org

负载均衡实现

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值