首页 > 科技 > ansible-playbook实战之批量安装nginx高可用集群

ansible-playbook实战之批量安装nginx高可用集群

1、创建roles目录

mkdir -p ansible_install/{group_vars,roles}

mkdir -p ansible_install/roles/ha/{files,handlers,tasks,templates}

2、roles基本配置

#cat hosts

[lb]

192.168.52.18 ansible_ssh_user=root ansible_ssh_pass=1qaz@WSX lb_name=lb-master

192.168.52.19 ansible_ssh_user=root ansible_ssh_pass=1qaz@WSX lb_name=lb-backup

site.yml配置:

#cat site.yml

- name: 11.部署Nginx负载均衡并高可用

gather_facts: false

hosts: lb

roles:

- ha

tags: ha

#cat group_vars/all

# 高可用,如果部署单Master,该项忽略

vip: '192.168.52.88'

nic: 'ens33'

#cat roles/ha/files/check_nginx.sh

#!/bin/bash

count=$(ps -ef |grep nginx |egrep -cv "grep|$$")

if [ "$count" -eq 0 ]; then

exit 1

#systemctl stop keepalived

fi

#cat roles/ha/tasks/main.yml

---

- name: 拷贝nginx,keepalived安装包

unarchive: src=ha.tar.gz dest=/tmp

- name: 安装keepalived高可用软件

yum: name=/tmp/{{ item }} state=present

with_items:

- "net-snmp-libs-5.7.2-43.el7.x86_64.rpm"

- "net-snmp-agent-libs-5.7.2-43.el7.x86_64.rpm"

- "keepalived-1.3.5-16.el7.x86_64.rpm"

- name: 安装nginx负载均衡器

yum: name=/tmp/nginx-1.16.1-1.el7.ngx.x86_64.rpm state=present

- name: Mkdir /etc/nginx/conf.d

file: dest=/etc/nginx/conf.d state=directory

- name: Mkdir /etc/nginx/logs

file: dest=/etc/nginx/logs state=directory

- name: 拷贝nginx配置文件

template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf

- name: remove nginx configuration file

file: dest=/etc/nginx/conf.d/default.conf state=absent

- name: Copy nginx configuration file

template: src=site.conf.j2 dest=/etc/nginx/conf.d/site.conf

- name: 拷贝keepalived配置文件

template: src=keepalived.conf.j2 dest=/etc/keepalived/keepalived.conf

- name: 拷贝nginx健康检查脚本

copy: src=check_nginx.sh dest=/etc/keepalived/ mode=u+x

- name: 启动服务

systemd: name={{ item }} state=restarted enabled=yes daemon_reload=yes

with_items:

- nginx

- keepalived

- name: Create wwwroot directory

file: dest=/var/www/html state=directory

- name: Create test page index.html

shell: echo "welcome to my site..." > /var/www/html/index.html

#cat roles/ha/templates/keepalived.conf.j2

{% if lb_name == 'lb-master' %}

{% set role = 'MASTER' %}

{% set priority = 100 %}

{% elif lb_name == 'lb-backup' %}

{% set role = 'BACKUP' %}

{% set priority = 90 %}

{% endif %}

global_defs {

notification_email {

acassen@firewall.loc

failover@firewall.loc

sysadmin@firewall.loc

}

notification_email_from Alexandre.Cassen@firewall.loc

smtp_server 127.0.0.1

smtp_connect_timeout 30

router_id NGINX_{{ role }}

}

vrrp_script check_nginx {

script "/etc/keepalived/check_nginx.sh"

}

vrrp_instance VI_1 {

state {{ role }}

interface {{ nic }}

virtual_router_id 51 # VRRP 路由 ID实例,每个实例是唯一的

priority {{ priority }} # 优先级,备服务器设置 90

advert_int 1 # 指定VRRP 心跳包通告间隔时间,默认1秒

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

{{ vip }}/24

}

track_script {

check_nginx

}

}

#cat roles/ha/templates/nginx.conf.j2

user root;

worker_processes auto;

#errdor_log logs/error.log;

#error_log logs/error.log notice;

error_log logs/error.log info;

pid /var/run/nginx.pid;

events {

worker_connections 1024;

}

http {

include mime.types;

default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for"';

access_log logs/access.log main;

sendfile on;

tcp_nopush on;

keepalive_timeout 65;

#gzip on;

include /etc/nginx/conf.d/*.conf;

}

#cat roles/ha/templates/site.conf.j2

server {

listen {{ http_port }};

server_name {{ server_name }};

location / {

root /var/www/html;

index index.html;

}

}

3、安装过程

ansible-playbook site.yml -i hosts --tags "ha"

如上,安装成功。

4、浏览器验证

http://192.168.52.88

本文来自投稿,不代表本人立场,如若转载,请注明出处:http://www.souzhinan.com/kj/379820.html