Ansible自动部署nginx+keepalived高可用负载均衡

Ansible自动化部署nginx的负载均衡高可用,前端代理使用nginx+keepalived,后端web server使用2台httpd用于负载效果的体现

Ansible控制机:172.25.81.1
        Ansible nginx:172.25.81.3   172.25.81.4
        Ansible Keepalived: 172.25.81.3   172.25.81.4
        Ansible httpd: 172.25.81.1   172.25.81.2

 

[root@server1 ~]# mkdir /etc/ansible/roles/httpd/{files,templates,tasks,handlers,vars,defaults,meta} -p
[root@server1 ~]# mkdir /etc/ansible/roles/keepalived/{files,templates,tasks,handlers,vars,defaults,meta} -p
[root@server1 ~]# mkdir /etc/ansible/roles/nginx/{files,templates,tasks,handlers,vars,defaults,meta} -p
[root@server1 ~]# mkdir /etc/ansible/roles/memcached/{files,templates,tasks,handlers,vars,defaults,meta} -p

Ansible自动部署nginx+keepalived高可用负载均衡_第1张图片

定义主机列表:

[root@server1 ~]# vim /etc/ansible/hosts

[nginx]
server3 mb=MASTER priority=100
server4 mb=BACKUP priority=98

[httpd]
server1
server2

Ansible自动部署nginx+keepalived高可用负载均衡_第2张图片

创建Playbook文件

[root@server1 ansible]# vim /etc/ansible/service.yml

- hosts: all
  remote_user: root
  roles:
    - nginx
    - httpd
    - keepalived

Ansible自动部署nginx+keepalived高可用负载均衡_第3张图片

编写nginx模块:

tasks:

<1>install.yml

[root@server1 ~]# vim /etc/ansible/roles/nginx/tasks/install.yml

- name: copy ali.repo
  copy: src=ali.repo dest=/etc/yum.repos.d/ali.repo

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

- name: copy nginx index.html
  copy: src=index.html dest=/usr/share/nginx/html/index.html
  notify: restart nginx

- name: install config
  template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
  notify: restart nginx

- name: start nginx
  service: name=nginx state=started enabled=true

Ansible自动部署nginx+keepalived高可用负载均衡_第4张图片

<2>main.yml

[root@server1 ~]# vim /etc/ansible/roles/nginx/tasks/main.yml

- include: tasks/install.yml      
  when:  ansible_eth0.ipv4.address == '172.25.81.3' or ansible_eth0.ipv4.address == '172.25.81.4'

handlers:

[root@server1 ~]# vim /etc/ansible/roles/nginx/handlers/main.yml

files:

<1>index.html

[root@server1 ~]# vim /etc/ansible/roles/nginx/files/index.html

www.westos.org

<2>ali.repo

[root@server1 ~]# vim /etc/ansible/roles/nginx/files/ali.repo

[centos7]
name=centeros7 base
baseurl=http://mirrors.aliyun.com/centos/7/os/x86_64/
gpgcheck=0
[epel]
name=epel base
baseurl=http://mirrors.aliyun.com/epel/7/x86_64
gpgcheck=0

Ansible自动部署nginx+keepalived高可用负载均衡_第5张图片

templates:

拷贝配置文件到对应目录下面/etc/ansible/roles/nginx/templates/nginx.conf.j2

Ansible自动部署nginx+keepalived高可用负载均衡_第6张图片

[root@server1 ~]# vim /etc/ansible/roles/nginx/templates/nginx.conf.j2

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    upstream web {
        server 172.25.81.1:80;
        server 172.25.81.2:80;
    }

    server {
        listen       80 default_server;
        server_name  {{ ansible_hostname }};
        root         /usr/share/nginx/html;
        index index.html index.php;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
                proxy_pass http://web;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

}

编写httpd模块:

tasks:

<1>install.yml

[root@server1 ~]# vim /etc/ansible/roles/httpd/tasks/install.yml

- name: install httpd
  yum: name=httpd state=present

- name: install httpd  index.html
  shell: echo {{ ansible_fqdn }} {{ ansible_eth0.ipv4.address }} > /var/www/html/index.html
  notify: restart httpd

- name: install config
  template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf

- name: start httpd
  service: name=httpd state=started enabled=true

Ansible自动部署nginx+keepalived高可用负载均衡_第7张图片

<2>main.yml

[root@server1 ~]# vim /etc/ansible/roles/httpd/tasks/main.yml

- include: tasks/install.yml
  when:  ansible_eth0.ipv4.address == '172.25.81.1' or ansible_eth0.ipv4.address == '172.25.81.2'

handlers:

[root@server1 ~]# vim /etc/ansible/roles/httpd/handlers/main.yml

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

templates:

[root@server1 files]# cp httpd.conf /etc/ansible/roles/httpd/templates/httpd.conf.j2

 

编写keepalived模块:

tasks:

<1>install.yml

[root@server1 ~]# vim /etc/ansible/roles/keepalived/tasks/install.yml

- name: copy ali.repo
  copy: src=ali.repo dest=/etc/yum.repos.d/ali.repo

- name: install keepalived
  yum: name=keepalived state=present

- name: install keepalived config
  template: src=keepalived.conf.j2 dest=/etc/keepalived/keepalived.conf
  notify: restart keepalived

- name: start keepalived
  service: name=keepalived state=started enabled=true

Ansible自动部署nginx+keepalived高可用负载均衡_第8张图片

<2>main.yml

[root@server1 ~]# vim /etc/ansible/roles/keepalived/tasks/main.yml

- include: tasks/install.yml
  when:  ansible_eth0.ipv4.address == '172.25.81.3' or ansible_eth0.ipv4.address == '172.25.81.4'

handlers:

[root@server1 ~]# vim /etc/ansible/roles/keepalived/handlers/main.yml

files:

[root@server1 ~]# cp /etc/ansible/roles/nginx/files/ali.repo /etc/ansible/roles/keepalived/files/

templates:

拷贝配置文件到对应目录下面/etc/ansible/roles/keepalived/templates/keepalived.conf.j2

[root@server1 ~]# vim /etc/ansible/roles/keepalived/templates/keepalived.conf.j2

! Configuration File for keepalived

global_defs {
   notification_email {
     root@localhost
   }
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
   #vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}   

vrrp_instance VI_1 {
    state {{ mb }}
    interface eth0
    virtual_router_id 51
    priority {{ priority }}
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.25.81.100/24            ##LVS的vip,服务启动生效时自动添加
    }
}   

 

[root@server1 ansible]# ansible-playbook service.yml --syntax-check

[root@server1 ansible]# ansible-playbook service.yml

做好免密:

Ansible自动部署nginx+keepalived高可用负载均衡_第9张图片

 

测试:

[root@server1 ansible]# ansible all -m shell -a "ss -tnlp| grep 'nginx\|httpd\|keepalived'"

Ansible自动部署nginx+keepalived高可用负载均衡_第10张图片

此时虚拟ip在server3上:

Ansible自动部署nginx+keepalived高可用负载均衡_第11张图片

负载均衡的实现:

Ansible自动部署nginx+keepalived高可用负载均衡_第12张图片

server3的keepalived down掉之后,虚拟ip自动漂到server4上,可以实现虚拟ip的漂移

Ansible自动部署nginx+keepalived高可用负载均衡_第13张图片

你可能感兴趣的:(Ansible自动部署nginx+keepalived高可用负载均衡)