ansible实现keepalived和nginx高可用

实验环境

ansible节点

keepalived+nginx节点1    ansible自动安装配置

keepalived+nginx节点2    ansible自动安装配置

httpd节点1

httpd节点2

ansible配置

yum install epel-release
yum install ansible

安装ansible

vi /etc/ansible/hosts

[nginxsrv]
172.20.128.42
172.20.128.43

[keepalivedsrv]
172.20.128.42
172.20.128.43

#配置免密钥登录连接
[[email protected] .ssh]# ssh-keygen
[[email protected] .ssh]# ssh-copy-id 172.20.128.42
[[email protected] .ssh]# ssh-copy-id 172.20.128.43
[[email protected] .ssh]# ansible nginxsrv -m ping

#指定使用密码连接测试 -k
[[email protected] .ssh]# ansible nginxsrv -m ping -k

添加主机清单

编写ansible角色脚本

[[email protected] ~]# ls
 HAnginx
[[email protected] ~]# cd HAnginx/
[[email protected] HAnginx]# ls
roles  start.yaml
[[email protected] HAnginx]# cd roles
[[email protected] roles]# ls
keepalived  nginx
[[email protected] roles]# cd nginx
[[email protected] nginx]# ls
tasks  templates  vars  handlers
[[email protected] roles]# cd keepalived/
[[email protected] keepalived]# ls
tasks  templates  vars handlers

角色目录结构

keepalived角色配置

vips:
 - vip1:
   master: node1
   interface: ens33
   vip: 172.20.103.88/16
   instance: VI_1
   routeid: 91
 - vip2:
   master: node2
   interface: ens33
   vip: 172.20.103.99/16
   instance: VI_2
   routeid: 92

这个文件中变量由ansible自动传递到模板中,因此可以在template中直接引用

vars/main.yaml

! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_iptables
   vrrp_garp_interval 0
   vrrp_gna_interval 0
   vrrp_mcast_group4 224.0.103.103
}

vrrp_script ngxhealth {
   script "killall -0 nginx && exit 0 || exit 1"
   interval 1
   weight -10

}

vrrp_script checkdown {
   script "/bin/bash -c ‘[[ -f /etc/keepalived/down ]]‘ && exit 1 || exit 0"
   interval 1
   weight -20
}

{% for v in vips %}
vrrp_instance {{ v["instance"] }} {
  {% if v["master"] == ansible_nodename  %}
   state MASTER
   priority 100
  {% else %}
   state BACKUP
   priority 95
  {% endif %}
    interface {{ v["interface"] }}
    virtual_router_id {{ v["routeid"] }}
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        {{ v["vip"] }}
    }

    track_interface {
       {{ v["interface"] }}
    }
    track_script {
     ngxhealth
     checkdown
   }
}
{% endfor %}

templates/keepalived.conf.j2

vi  yum.yaml

 - name: install keepalived
  yum: name=keepalived

vi  tmpl.yaml

  - name: copy keepalived conf
  template: src=keepalived.conf.j2 dest=/etc/keepalived/keepalived.conf

vi  start.yaml

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

vi main.yaml

  - include: yum.yaml
  - include: tmpl.yaml
  - include: start.yaml

tasks目录下文件

nginx角色配置

websrv1: 172.20.128.33
webport1: 80
websrv2: 172.20.128.34
webport2: 80

vars/main.yaml

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

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;

    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;

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

        location / {
        }

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

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

templates/nginx.conf.j2

upstream websrvs {
  server {{ websrv1 }}:{{ webport1 }};
  server {{ websrv2 }}:{{ webport2 }};
}

server {
    listen 80  default_server;
    server_name www.a.com;
    root /usr/share/nginx/html;
    location /{
      proxy_pass http://websrvs;
    }
}

templates/www.conf.j2

vi yum.yaml
  - name: install nginx
    yum:  name=nginx

[[email protected] tasks]# cat templ.yaml
- name: copy nginx conf
  template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf

- name: copy www conf
  template: src=www.conf.j2 dest=/etc/nginx/conf.d/www.conf

[[email protected] tasks]# cat start.yaml
- name: start nginx
  service: name=nginx state=started enabled=yes

[[email protected] tasks]# cat main.yaml
- include: yum.yaml
- include: templ.yaml
- include: start.yaml

tasks目录下文件

启动执行角色

[[email protected] HAnginx]# ls
roles  start.yaml
[[email protected] HAnginx]# ansible-playbook start.yaml

#roles目录下的文件
[[email protected] HAnginx]# cd roles
[[email protected] roles]# ls
keepalived  nginx
[[email protected] roles]# 

启动目录

注意点

在模板中  {% if v["master"] ==  ansible_nodename  %}  不能写成  {% if v["master"] ==  {{ ansible_nodename }}  %}

只有需要输出变量的值的时候才能写成   {{ ansible_nodename }}   进行变量比较的时候直接写成 ansible_nodename 即可

可以把所有的变量定义在每个角色目录下的/vars/main.yaml文件中,然后可以直接在j2模板中引用这里面的变量名称,j2根据变量和相关业务动态生成各种配置文件传送到相关主机

[[email protected] HAnginx]# ansible all -m setup | grep hostname

"ansible_hostname": "node2",
    "ansible_hostname": "node1",

原文地址:https://www.cnblogs.com/yxh168/p/9302103.html

时间: 2024-10-10 01:47:55

ansible实现keepalived和nginx高可用的相关文章

keepalived对nginx高可用演练脚本

keepalived对nginx高可用演练脚本 参考文章:http://deidara.blog.51cto.com/400447/302402/ 1.安装nginx.keepalived.epel-release源 yum install -y epel-release yum install -y nginx yum install -y keepalived 2.配置好nginx 3.设置keepalived配置文件 #主机 vi /etc/keepalived/keepalived.co

keepalived实现nginx高可用,一主一备

keepalived实现nginx高可用(HA) 安装直接yum安装就可以,版本可能比官网落后,但是够用了, yum -y install keepalived 编辑配置文件,默认路径 /etc/keepalived/keepalived.conf,这里将默认配置文件重命名,新建一个空的配置文件 mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak vim /etc/keepalived/keepalived

Nginx+Keepalived实现Nginx高可用

在架构设计中,可以利用NGINX的反向代理和负载均衡实现后端应用的高可用性,同时我们还需要考虑Nginx的单点故障.真正做到架构高可用性. 主要考虑以下几点: 1.Nginx服务因为意外现象挂掉 2.服务器宕机导致NGINX不可用 目前主流的解决方案就是keepalived+nginx 实现nginx的故障转移,同时做好监控报警.在自动故障转移的同时能通知到相关的应用负责人检查相关应用,排查隐患,彻底解决问题. 模拟环境: 序号 环境名称 IP地址 环境介绍 1 访问客户端1 10.57.3.2

基于keepalived的nginx高可用

#nginx,keepalived安装略过 MASTER 节点配置文件(192.168.1.11) vi /etc/keepalived/keepalived.conf global_defs { ##keepalived自带的邮件提醒需要开启sendmail服务.建议用独立的监控或第三方SMTP ##标识本节点的字条串,通常为 hostname router_id 192.168.1.11 } ##keepalived会定时执行脚本并对脚本执行的结果进行分析,动态调整vrrp_instance

nginx和keepalived实现nginx高可用

首先介绍一下Keepalived,它是一个高性能的服务器高可用或热备解决方案,Keepalived主要来防止服务器单点故障的发生问题,可以通过其与Nginx的配合实现web服务端的高可用. Keepalived以VRRP协议为实现基础,用VRRP协议来实现高可用性(HA).VRRP (Virtual Router Redundancy Protocol)协议是用于实现路由器冗余的协议,VRRP协议将两台或多台路由器设备虚拟成一个设备,对外提供虚拟路由器IP(一个或多个),如下图所示: 这张图的意

Keepalived 实现 nginx 高可用

一.什么是 keepalived Keepalived是一个免费开源的,用C编写的类似于layer3, 4 & 7交换机制软件,具备我们平时说的第3层.第4层和第7层交换机的功能.主要提供loadbalancing(负载均衡)和 high-availability(高可用)功能,负载均衡实现需要依赖Linux的虚拟服务内核模块(ipvs),而高可用是通过VRRP协议实现多台机器之间的故障转移服务.   上图是Keepalived的功能体系结构,大致分两层:用户空间(user space)和内核空

keepalived+lvs+nginx 高可用

keepalived是一款用C编写的,旨在给linux系统和基于linux的设施提供简单.稳固的高可用和负载均衡功能的软件.它基于linux内核的ipvs模块实现4层负载均衡,能应用一系列的健康状态检测机制基于VRRP协议实现服务的高可用. 一.VRRP协议 VRRP(Virtual Router Redundancy Protocol,虚拟路由冗余协议)是一种容错协议.通常,一个网络内的所有主机都设置一条默认路由,这样,主机发出的目的地址不在本网段的报文将被通过默认路由发往路由器RouterA

keepalived实现nginx高可用

1.环境说明 IP 服务 作用 192.168.1.101 nginx + keepalived master 192.168.1.102 nginx + keepalived backup 192.168.1.103 虚拟ip(VIP) 说明:系统:CentOS 6.10master配一个,backup可以配置多个:虚拟ip(VIP):192.168.1.103,对外提供服务的ip,也可称作浮动ip 2.nginx 安装与配置 2.1.安装nginx master和backup所有节点都安装配

Nginx配置upstream实现负载均衡及keepalived实现nginx高可用

(原文链接:http://www.studyshare.cn/blog-front//blog/details/1159/0 ) 一.准备工作 1.准备两个项目,发布到不同的服务器上,此处使用2个虚拟机发布了两个项目分别为:http://192.168.28.128:8081, http://192.168.28.129:8081 2.在两个虚拟机上都安装好nginx 二.配置upstream 1.在任意一台虚拟机上所安装的nginx的nginx.conf配置文件中配置upstream如下: 以