ansible 一键部署HAproxy+lnmp

架构图

架构图

hosts

[nginx]
192.168.65.134
192.168.65.135
[php]
192.168.65.134
192.168.65.135
[mysql]
192.168.65.134
192.168.65.135
[haproxy]
192.168.65.136

Tree

my_haproxy_lamp/
├── group_vars
│   ├── all
│   ├── haproxy
│   └── mysql
├── hosts
├── roles
│   ├── base
│   │   ├── tasks
│   │   │   └── main.yml
│   │   └── templates
│   │       ├── CentOS-Base.repo
│   │       └── epel-7.repo
│   ├── haproxy
│   │   ├── tasks
│   │   │   └── main.yml
│   │   └── templates
│   │       ├── haproxy.cfg.j2
│   │       └── haproxy.cfg.j2.bak
│   ├── mysql
│   │   ├── tasks
│   │   │   └── main.yml
│   │   └── templates
│   │       └── my.cnf.j2
│   ├── nginx
│   │   ├── handlers
│   │   │   └── main.yml
│   │   ├── tasks
│   │   │   └── main.yml
│   │   └── templates
│   │       ├── index.php
│   │       └── nginx.conf.j2
│   └── php
│       └── tasks
│           └── main.yml
└── site.yml
  • 以上是整个的目录结构

site.yml

---
- name: Modify the yum 
  hosts: all
  remote_user: root
  roles:
    - base

- name: install the nginx
  hosts: nginx
  remote_user: root
  roles:
    - nginx

- name: install the php
  hosts: nginx
  remote_user: root
  roles:
    - php

- name: install the mariadb
  hosts: mysql
  remote_user: root
  roles:
    - mysql

- name: install the haproxy
  hosts: haproxy
  remote_user: root
  roles:
    - haproxy

一共四个角色

  • base 安装配置yum源
  • nginx 安装配置nginx
  • mysql 安装配置mysql
  • haproxy 安装配置HAproxy

base角色

sudo wget -O CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
sudo wget http://mirrors.aliyun.com/repo/epel-7.repo 
  • 获取阿里源
  • 放到templates目录下

task/main.yml

---
- name: Copy the yum repo to the remote hosts
  template: src={{item}} dest=/etc/yum.repos.d/{{item}}
  with_items:
    - CentOS-Base.repo
    - epel-7.repo

nginx角色

task/main.yml

---
- name: Install nginx
  yum: name=nginx state=present

- name: Copy nginx configuration 
  template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
  notify: restart nginx  #通知handler执行任务

- name: Copy php file to nginx web root
  template: src=index.php dest=/usr/share/nginx/html/index.php 

  • 通知handler执行任务
  • 拷贝nginx配置文件和主页文件到远程主机

handlers/main.yml

---
- name: restart nginx
  service: name=nginx state=restarted 

templates/index.php

{{ ansible_default_ipv4.get('address') }}
  • 可以通过ansible all -m setup获得具体主机数据
  • 作用为当HAproxy后端代理不同的nginx服务器显示不同的ip地址

mysql角色

---
- name: install mysql
  yum: name={{item}} state=installed
  with_items:
    - mariadb-server
    - MySQL-python

- name: create mysql configuration file
  template: src=my.cnf.j2 dest=/etc/my.cnf

- name: start mariadb service 
  service: name=mariadb state=started 

templates/my.cnf.j2

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
port={{ mysql_port }}

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mariadb/mysqld.pid

  • 主要是mysql_port

haproxy 角色

- name: install haproxy
  yum: name=haproxy state=present
 
- name: configure the haproxy cnf file with hosts
  template: src=haproxy.cfg.j2 dest=/etc/haproxy/haproxy.cfg

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

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                    {{mode}}
    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 *:{{listenport}}
    stats uri   /haproxy
    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     {{balance}}
    server      static 127.0.0.1:8080 check

#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
    balance     {{balance}}
    {% for host in groups['nginx'] %}
    server {{hostvars[host]['inventory_hostname']}} {{host}}:{{nginx_port}}
    {% endfor %}
  • 那什么的鬼的static我也不懂
  • 之后是一个jinja2语法循环在hosts里的nginx主机,然后hostvars[host]可以得到这个主机的变量,然后使用inventory_hostname变量,结果是在这里可以获取到主机ip,后面的{{host}}也是会获得到主机ip

group_vars

这个目录下放着是每个主机组的变量,和角色名可以不同,但是一定要和主机组相同。

  • all
---
nginx_port: 8089

all表示所有的主机组

  • haproxy
---
mode: http #这个是HAproxy采用http模式,还是tcp模式,还是xx模式
listenport: 80 #监听前端访问端口
balance: roundrobin #使用轮询的方式负载均衡,也可以使用别的方式

haproxy表示只有这个主机组可以使用
haproxy官方文档地址了解一下?

  • mysql
---
mysqlservice: mysqld
mysql_port: 3306

mysql表示只有这个mysql主机组可以使用

结果

执行
ansible-playbook -i hosts site.yml
最后结果

[root@node04 my_haproxy_lamp]# curl localhost
192.168.65.134
[root@node04 my_haproxy_lamp]# curl localhost
192.168.65.135
[root@node04 my_haproxy_lamp]# curl 192.168.65.136
192.168.65.134
[root@node04 my_haproxy_lamp]# curl 192.168.65.136
192.168.65.135

总结

  • 还有mysql的数据库创建角色,和角色授权没弄,有空搞搞。
  • site.yml为入口,task目录为任务目录,handler目录为响应notify的目录,templates目录为template模块用的目录,一般放jinja2文件(j2结尾的),也可以用file目录。

欢迎关注、点赞、收藏、留言交流。

你可能感兴趣的:(ansible 一键部署HAproxy+lnmp)