Python自動化運維之ansible的Haproxy+LAMP+Nagios經典案例說明(上)

一 目錄結構圖

[root@localhost lamp_haproxy]# tree
.
├── group_vars
│   ├── all
│   ├── dbservers
│   ├── lbservers
│   └── webservers
├── hosts
├── roles
│   ├── base-apache
│   │   └── tasks
│   │       └── main.yml
│   ├── common
│   │   ├── files
│   │   │   ├── epel.repo
│   │   │   └── RPM-GPG-KEY-EPEL-6
│   │   ├── handlers
│   │   │   └── main.yml
│   │   ├── tasks
│   │   │   └── main.yml
│   │   └── templates
│   │       ├── iptables.j2
│   │       └── ntp.conf.j2
│   ├── db
│   │   ├── handlers
│   │   │   └── main.yml
│   │   ├── tasks
│   │   │   └── main.yml
│   │   └── templates
│   │       └── my.cnf.j2
│   ├── haproxy
│   │   ├── handlers
│   │   │   └── main.yml
│   │   ├── tasks
│   │   │   └── main.yml
│   │   └── templates
│   │       └── haproxy.cfg.j2
│   ├── nagios
│   │   ├── files
│   │   │   ├── ansible-managed-services.cfg
│   │   │   ├── localhost.cfg
│   │   │   └── nagios.cfg
│   │   ├── handlers
│   │   │   └── main.yml
│   │   ├── tasks
│   │   │   └── main.yml
│   │   └── templates
│   │       ├── dbservers.cfg.j2
│   │       ├── lbservers.cfg.j2
│   │       └── webservers.cfg.j2
│   └── web
│       └── tasks
│           └── main.yml
└── site.yml

二 設備環境說明

1 說明

兩臺Web主機、1臺數據庫主機、1臺負載均衡器主機、1臺監控主機。

2 hosts文件

[root@localhost lamp_haproxy]# cat hosts
[webservers]
web1
web2
[dbservers]
db1
[lbservers]
lb1
[monitoring]
nagios

三 入口文件

1 說明

需要注意的是base-apache角色,由於webservers及monitoring都需要部署Apache環境,爲提高複用性,將部署Apache獨立成base-apache角色。

2 Site.yml

[webservers]
web1
web2

[dbservers]
db1

[lbservers]
lb1

[monitoring]
nagios
[root@localhost lamp_haproxy]# cat site.yml
---
# This playbook deploys the whole application stack in this site.  

# Apply common configuration to all hosts
- hosts: all
  roles:
  - common

# Configure and deploy database servers.
- hosts: dbservers
  user: root
  roles:
  - db

# Configure and deploy the web servers. Note that we include two roles here,
# the 'base-apache' role which simply sets up Apache, and 'web' which includes
# our example web application.
- hosts: webservers
  user: root

  roles:
  - base-apache
  - web

# Configure and deploy the load balancer(s).
- hosts: lbservers
  user: root
  roles:
  - haproxy

# Configure and deploy the Nagios monitoring node(s).
- hosts: monitoring
  user: root
  roles:
  - base-apache
  - nagios

四 定義組變量

1 全局變量

1.1 說明

變量作用域爲所有主機,all文件定義了匹配所有主機作用域的變量,一般爲系統公共類基 礎配置,如ntpserver地址、sysctl變量、iptables配置等。

1.2 代碼

---
# Variables here are applicable to all host groups

httpd_port: 80
ntpserver: 192.168.1.2

2 定義webservers組的變量

2.1 說明

變量作用域爲webservers組主機。webservers文件定義了webservers組作用域的變量。本示例涉及Apache相關配置,其中“iface:'{{ansible_default_ipv4.interface}}'”引用了 Facts獲取的本地網卡接口名信息,另外定義了一個GitHub的repository, 方便下載Web測試文件,如內部搭建git版本控制環境,此處也可以修改成本地的服務地址。

2.2 代碼

---
# Variables for the web server configuration

# Ethernet interface on which the web server should listen.
# Defaults to the first interface. Change this to:
#
#  iface: eth1
#
# ...to override.
#
iface: '{{ ansible_default_ipv4.interface }}'

# this is the repository that holds our sample webapp
repository: https://github.com/bennojoy/mywebapp.git

# this is the sha1sum of V5 of the test webapp.
webapp_version: 351e47276cc66b018f4890a04709d4cc3d3edb0d

3 dbservers組的變量

3.1 說明

變量作用域爲dbservers組主機。dbservers文件定義了dbservers組作用域變量,本示例涉及MySQL 數據庫的基本應用信息。

3.2 代碼

---
# The variables file used by the playbooks in the dbservers group.
# These don't have to be explicitly imported by vars_files: they are autopopulated.

mysqlservice: mysqld
mysql_port: 3306
dbuser: root
dbname: foodb
upassword: abc

4 lbservers組作用域變量

4.1 說明

定義lbservers組作用域變量文件,本示例主要涉及haproxy環境涉及的配置參數值。

4.2 代碼

---
# Variables for the HAproxy configuration

# HAProxy supports "http" and "tcp". For SSL, SMTP, etc, use "tcp".
mode: http

# Port on which HAProxy should listen
listenport: 8888

# A name for the proxy daemon, this wil be the suffix in the logs.
daemonname: myapplb

# Balancing Algorithm. Available options:
# roundrobin, source, leastconn, source, uri
# (if persistance is required use, "source")
balance: roundrobin

# Ethernet interface on which the load balancer should listen
# Defaults to the first interface. Change this to:
#
#  iface: eth1
#
# ...to override.
#
iface: '{{ ansible_default_ipv4.interface }}'

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章