自动化运维ansible一 一键部署haproxy和lnmp服务

一、环境

1.实验主机信息

ansible管理机:
主机名:liang
IP:10.0.0.128
nginx(php):
主机名:nginx
IP:10.0.0.131
IP:10.0.0.132
haproxy:
主机名:haproxy
IP:10.0.0.130
mysql(nginx):
主机名:mysql
IP:10.0.0.132

2.实验结构图

这里写图片描述

二、部署:

1.roles结构与配置

1.1编辑hosts清单

[root@liang ~]# cat /etc/ansible/hosts
# This is the default ansible 'hosts' file.
[haproxy]
10.0.0.130
[nginx]
10.0.0.131
10.0.0.132
[mysql]
10.0.0.132

1.2创建结构目录

[root@liang ~]# mkdir /roles/{nginx,haproxy,mysql}/tasks -p

1.3创建头文件

[root@liang ~]# cd /roles/
[root@liang roles]# cat site.yml
---
- name: install nginx
  hosts: nginx
  roles:
    - nginx
- name: install mysql
  hosts: mysql
  roles:
    - mysql
- name: install haproxy
  hosts: haproxy
  roles:
    - haproxy

2.nginx+php角色编辑与配置

2.1创建目录与php文件

[root@liang roles]# mkfir nginx/templates/
[root@liang roles]# cat nginx/templates/index.html.j2                       
{{ ansible_default_ipv4.address }

2.2创建nginx角色入口文件

[root@liang roles]# cat nginx/tasks/main.yml 
---
- name: install nginx
  yum: name=nginx state=present
- name: install php 
  yum: name=php state=present
- name: copy file
  template: src=index.html.j2 dest=/usr/share/nginx/html/index.html
- name: start nginx
  service: name=nginx state=started

3.mysql配置

3.1编辑mysql角色文件

[root@liang roles]# cat mysql/tasks/main.yml 
---
- name: install mysql
  yum: name={{item}} state=installed
  with_items:
    - mysql-server
    - MySQL-python
- name: start mysql
  service: name=mysqld state=started

4.haproxy配置

4.1创建目录与配置文件

[root@liang roles]# mkdir haproxy/templates
[root@liang roles]# cat haproxy/templates/haproxy.cfg.j2
global
    log         127.0.0.1 local2

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

    stats socket /var/lib/haproxy/stats

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

frontend  main *: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

backend static
    balance     roundrobin
    server      static 127.0.0.1:8080 check

backend app
    balance     roundrobin
    {% for host in groups['nginx'] %}
    server {{hostvars[host]['inventory_hostname']}} {{host}}:80}
    {% endfor %}

4.2创建haproxy入口文件

[root@liang roles]# cat haproxy/tasks/main.yml 
---
- name: install haproxy
  yum: name=haproxy state=installed
- name: template file
  template: src=haproxy.cfg.j2 dest=/etc/haproxy/haproxy.cfg owner=root group=root mode=644
- name: start haproxy
  service: name=haproxy state=started enabled=yes

三、执行site.yml文件

[root@liang roles]# ansible-playbook site.yml                  

PLAY [install nginx] ***********************************************************************

TASK [Gathering Facts] *********************************************************************
ok: [10.0.0.132]
ok: [10.0.0.131]

TASK [nginx : install nginx] ***************************************************************
ok: [10.0.0.132]
ok: [10.0.0.131]

TASK [nginx : install php] *****************************************************************
ok: [10.0.0.132]
ok: [10.0.0.131]

TASK [nginx : copy file] *******************************************************************
ok: [10.0.0.132]
ok: [10.0.0.131]

TASK [nginx : start nginx] *****************************************************************
ok: [10.0.0.132]
ok: [10.0.0.131]

PLAY [install mysql] ***********************************************************************

TASK [Gathering Facts] *********************************************************************
ok: [10.0.0.132]

TASK [mysql : install mysql] ***************************************************************
ok: [10.0.0.132] => (item=[u'mysql-server', u'MySQL-python'])

TASK [mysql : start mysql] *****************************************************************
ok: [10.0.0.132]

PLAY [install haproxy] *********************************************************************

TASK [Gathering Facts] *********************************************************************
ok: [10.0.0.130]

TASK [haproxy : install haproxy] ***********************************************************
ok: [10.0.0.130]

TASK [haproxy : template file] *************************************************************
changed: [10.0.0.130]

TASK [haproxy : start haproxy] *************************************************************
changed: [10.0.0.130]

PLAY RECAP *********************************************************************************
10.0.0.130                 : ok=4    changed=2    unreachable=0    failed=0   
10.0.0.131                 : ok=5    changed=0    unreachable=0    failed=0   
10.0.0.132                 : ok=8    changed=0    unreachable=0    failed=0   

四、测试:

[root@liang ~]# curl 10.0.0.130
10.0.0.132
[root@liang ~]# curl 10.0.0.130
10.0.0.131
[root@liang ~]# curl 10.0.0.130
10.0.0.132
[root@liang ~]# curl 10.0.0.130
10.0.0.131
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值