ansible搭建apache+haproxy+keepalived

apache部分

1.创建角色
[devops@server1 ansible]$ mkdir roles
[devops@server1 ansible]$ cd roles
[devops@server1 roles]$ ls
[devops@server1 roles]$ ansible-galaxy init apache

  • apache was created successfully
    [devops@server1 roles]$ ls
    apache
    [devops@server1 roles]$ cd apache/
    [devops@server1 apache]$ ls
    defaults files handlers meta README.md tasks templates tests vars
    2.编写任务
    [devops@server1 apache]$ cd tasks/
    [devops@server1 tasks]$ ls
    main.yml
    [devops@server1 tasks]$ \vi main.yml
---
- name: install httpd
  yum:
    name: httpd
    state: present

- name: copy index.html
  copy:
    content: "{{ ansible_facts['hostname'] }}"
    dest: /var/www/html/index.html

- name: configure httpd
  copy:
    src: httpd.conf
    dest: /etc/httpd/conf/httpd.conf
    ower: root
    group: root
    mode: 644
  notify: restart httpd

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

- name: configure firewalld
  firewalld:
    service: http
    permanent: yes
    immediate: yes
    state: enabled

里面有激发器,所以我们需要编写激发器
3.


  • name: restart httpd
    service:
    name: httpd
    state: restarted
  1. 把httpd和haproxy的配置文件放在templates目录下,有需求可以修改修改
    [devops@server1 apache]$ cd templates/
    [devops@server1 templates]$ ls
    httpd.conf

haproxy部分
1.建立角色
[devops@server1 roles]$ ansible-galaxy init haproxy
[devops@server1 roles]$ cd haproxy/
[devops@server1 haproxy]$ ls
defaults files handlers meta README.md tasks templates tests vars

[devops@server1 tasks]$ \vi main.yml


  • name: install
    yum:
    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

3.复制haproxy配置文件到templates目录下

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

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

    default_backend             app

#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------

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

3.编辑playbook


  • hosts: webserver
    roles:
    • apache
  • hosts: lb
    roles:
    • haproxy
      4,
      [devops@server1 ansible]$ vim inventory
      [lb]
      localhost
      172.25.1.4
      [test]
      172.25.1.2
      [prod]
      172.25.1.3
      [webserver:children]
      prod
      test

keepalived部分

1.建立角色
[devops@server1 roles]$ ansible-galaxy init keepalived
[devops@server1 roles]$ cd keepalived/
[devops@server1 keepalived]$ ls
defaults files handlers meta tasks templates vars
2。创建任务


  • name: install
    yum:
    name: keepalived
    state: present

  • name: configure keepalived
    template:
    src: keepalived.conf.j2
    dest: /etc/keepalived/keepalived.conf
    notify: restart keepalived

  • name: start keepalived
    service:
    name: keepalived
    state: started

3。编辑激发器


  • name: restart keepalived
    service:
    name: keepalived
    state: restarted

4.编辑模板
[devops@server1 templates]$ cp /etc/keepalived/keepalived.conf .
[devops@server1 templates]$ mv keepalived.conf keepalived.conf.j2
[devops@server1 templates]$ vim keepalived.conf.j2

! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state {{ STATE }}
    interface eth0
    virtual_router_id 23
    priority {{ VRID }}
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.25.1.100


    }
}

[devops@server1 ansible]$ vim apache.yml

  • hosts: webserver
    roles:
    • apache
  • hosts: lb
    roles:
    • haproxy
  • hosts: lb
    roles:
    • keepalived

[devops@server1 ansible]$ vim inventory
[lb]
localhost STATE=MASTER VRID=100
172.25.1.4 STATE=BACKUP VRID=50
[test]
172.25.1.2
[prod]
172.25.1.3
[webserver:children]
prod
test

检测
[devops@server1 ansible]$ ansible-playbook -C apache.yml
[devops@server1 ansible]$ ansible-playbook apache.yml 运行

测试
查看master节点vip,关掉master的keepalived后,在看,vip已经漂移到backup的节点,
网页还能访问到

你可能感兴趣的:(ansible搭建apache+haproxy+keepalived)