Ansible-Playbook部署Nginx负载均衡

1.实验环境

准备虚拟机:
Ansible: 192.168.153.183
Nginx_lb: 192.168.153.189
web1: 192.168.153.185
web2: 192.168.153.186
实验逻辑图
在这里插入图片描述

2.Ansible服务器配置

2.1 定义主机清单

[root@localhost ~]# vim /etc/ansible/hosts 
[webservers]
192.168.153.185
192.168.153.186
[nginx_lb]
192.168.153.189

2.2 给所有主机配置ssh免密登录

[root@localhost ~]# ssh-copy-id root@192.168.153.189
[root@localhost ~]# ssh-copy-id root@192.168.153.185
[root@localhost ~]# ssh-copy-id root@192.168.153.186

2.3 测试主机的连通性

测试web服务器的连通性
[root@localhost ~]# ansible webservers -m ping
192.168.153.185 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
192.168.153.186 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
测试nginx负载均衡器的连通性
[root@localhost ~]# ansible nginx_lb -m ping
192.168.153.189 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

2.4 创建工作目录并上传源码包

[root@localhost ~]# mkdir nginx_lb
[root@localhost ~]# cd nginx_lb/
[root@localhost nginx_lb]# ls
nginx-1.18.0.tar.gz

2.5 修改web服务器的主机名

[root@localhost ~]# hostname web-185
[root@localhost ~]# hostname web-186

2.6 编写web服务器模板文件和修改nginx配置文件

编写web服务器模板文件

[root@localhost nginx_lb]# vim index.html.j2
[root@localhost nginx_lb]# cat index.html.j2 
<h1>{{ ansible_all_ipv4_addresses }}</h1>
<h1>{{ ansible_hostname }}</h1>
[root@localhost nginx_lb]# 

修改nginx配置文件
定义服务器组

 34     upstream  webs {
 35         server 192.168.153.185:80;
 36         server 192.168.153.186:80;
 37      }

设置反向代理

 47         location / {
 48              proxy_pass   http://webs; 
 49         }

2.7 编写playbook剧本文件

[root@localhost ~]# cd nginx_lb/
[root@localhost nginx_lb]# vim nginx_lb.yml 
---
- hosts: webservers
  tasks:
    - name: "安装Apache服务"
      yum: name=httpd state=latest
    - name: "启动Apache服务"
      service: name=httpd state=started enabled=yes
    - name: "关闭防火墙"
      service: name=firewalld state=stopped
    - name: "创建测试页面"
      template: src=/root/nginx_lb/index.html.j2  dest=/var/www/html/index.html
- hosts: nginx_lb
  tasks:
    - name: "分发nginx源码包"
      unarchive: src=/root/nginx_lb/nginx-1.18.0.tar.gz dest=/root/
    - name: "安装依赖环境"
      yum: name=gcc,pcre-devel,openssl-devel state=latest
    - name: "编译安装nginx"
      shell: cd /root/nginx-1.18.0  && ./configure && make && make install
    - name: "推送配置文件"
      copy: src=/root/nginx_lb/nginx.conf  dest=/usr/local/nginx/conf/nginx.conf
    - name: "关闭防火墙"
      service: name=firewalld state=stopped
    - name: "开启nginx"
      shell: /usr/local/nginx/sbin/nginx

2.8 检测并执行剧本

2.8.1 检测

[root@localhost nginx_lb]# ansible-playbook --syntax-check nginx_lb.yml 

playbook: nginx_lb.yml

2.8.2 执行

[root@localhost nginx_lb]# ansible-playbook nginx_lb.yml 

PLAY [webservers] ******************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************
ok: [192.168.153.186]
ok: [192.168.153.185]

TASK [安装Apache服务] ******************************************************************************************************************
changed: [192.168.153.186]
changed: [192.168.153.185]

TASK [启动Apache服务] ******************************************************************************************************************
changed: [192.168.153.185]
changed: [192.168.153.186]

TASK [关闭防火墙] ***********************************************************************************************************************
changed: [192.168.153.186]
changed: [192.168.153.185]

TASK [创建测试页面] **********************************************************************************************************************
changed: [192.168.153.185]
changed: [192.168.153.186]

PLAY [nginx_lb] ********************************************************************************************************************

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

TASK [分发nginx源码包] ******************************************************************************************************************
changed: [192.168.153.189]

TASK [安装依赖环境] **********************************************************************************************************************
changed: [192.168.153.189]

TASK [编译安装nginx] *******************************************************************************************************************
changed: [192.168.153.189]

TASK [推送配置文件] **********************************************************************************************************************
changed: [192.168.153.189]

TASK [关闭防火墙] ***********************************************************************************************************************
changed: [192.168.153.189]

TASK [开启nginx] *********************************************************************************************************************
changed: [192.168.153.189]

PLAY RECAP *************************************************************************************************************************
192.168.153.185            : ok=5    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.153.186            : ok=5    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.153.189            : ok=7    changed=6    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@localhost nginx_lb]# 

2.9 浏览器测试效果

浏览器输入nginx负载均衡服务器的IP地址,可以看到负载均衡的效果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值