实验环境

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 #配置免密钥登录连接
[root@localhost .ssh]# ssh-keygen
[root@localhost .ssh]# ssh-copy-id 172.20.128.42
[root@localhost .ssh]# ssh-copy-id 172.20.128.43
[root@localhost .ssh]# ansible nginxsrv -m ping #指定使用密码连接测试 -k
[root@localhost .ssh]# ansible nginxsrv -m ping -k

添加主机清单

编写ansible角色脚本

[root@localhost ~]# ls
HAnginx
[root@localhost ~]# cd HAnginx/
[root@localhost HAnginx]# ls
roles start.yaml
[root@localhost HAnginx]# cd roles
[root@localhost roles]# ls
keepalived nginx
[root@localhost roles]# cd nginx
[root@localhost nginx]# ls
tasks templates vars handlers
[root@localhost roles]# cd keepalived/
[root@localhost keepalived]# ls
tasks templates vars handlers

角色目录结构

keepalived角色配置

vips:
- vip1:
master: node1
interface: ens33
vip: 172.20.103.88/
instance: VI_1
routeid:
- vip2:
master: node2
interface: ens33
vip: 172.20.103.99/
instance: VI_2
routeid: 这个文件中变量由ansible自动传递到模板中,因此可以在template中直接引用

vars/main.yaml

! Configuration File for keepalived

global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
root@localhost
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_iptables
vrrp_garp_interval
vrrp_gna_interval
vrrp_mcast_group4 224.0.103.103
} vrrp_script ngxhealth {
script "killall -0 nginx && exit 0 || exit 1"
interval
weight - } vrrp_script checkdown {
script "/bin/bash -c '[[ -f /etc/keepalived/down ]]' && exit 1 || exit 0"
interval
weight -
} {% for v in vips %}
vrrp_instance {{ v["instance"] }} {
{% if v["master"] == ansible_nodename %}
state MASTER
priority
{% else %}
state BACKUP
priority
{% endif %}
interface {{ v["interface"] }}
virtual_router_id {{ v["routeid"] }}
advert_int
authentication {
auth_type PASS
auth_pass
}
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:
websrv2: 172.20.128.34
webport2:

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 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 [root@localhost 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 [root@localhost tasks]# cat start.yaml
- name: start nginx
service: name=nginx state=started enabled=yes [root@localhost tasks]# cat main.yaml
- include: yum.yaml
- include: templ.yaml
- include: start.yaml

tasks目录下文件

启动执行角色

[root@localhost HAnginx]# ls
roles start.yaml
[root@localhost HAnginx]# ansible-playbook start.yaml #roles目录下的文件
[root@localhost HAnginx]# cd roles
[root@localhost roles]# ls
keepalived nginx
[root@localhost roles]#

启动目录

注意点

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

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

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

[root@localhost HAnginx]# ansible all -m setup | grep hostname

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

ansible all -m setup -a "filter=ansible_hostname"

使用default()默认值

当我们定义了变量的值时,采用变量的值,当我们没有定义变量的值时,那么使用默认给定的值

[root@master ansible]# cat roles/temp/templates/test_default.j2
Listen: {{ server_port|default(80) }}
 

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

  1. keepalived对nginx高可用演练脚本

    keepalived对nginx高可用演练脚本 参考文章:http://deidara.blog.51cto.com/400447/302402/ .安装nginx.keepalived.epel-r ...

  2. Keepalived保证Nginx高可用配置

    Keepalived保证Nginx高可用配置部署环境 keepalived-1.2.18 nginx-1.6.2 VM虚拟机redhat6.5-x64:192.168.1.201.192.168.1. ...

  3. linux中keepalived实现nginx高可用配置

    linux中keepalived实现nginx高可用配置 安装keepalived 运行如下命令即可 tar -zxvf keepalived-2.0.8.tar.gz -C /usr/src cd ...

  4. 配置keepalived支持nginx高可用

    实验环境 序号 主机名 IP地址 1 nginx1 192.168.204.11 2 nginx2 192.168.204.12 安装nginx 安装nginx yum install -y epel ...

  5. keepalived实现nginx高可用

    keepalived是什么 keepalived直译就是保持存活,在网络里面就是保持在线了,也就是所谓的高可用或热备,用来防止单点故障(单点故障是指一旦某一点出现故障就会导致整个系统架构的不可用)的发 ...

  6. Keepalived+LVS-DR+Nginx高可用故障切换模式

    LVS架构中,不管是NAT模式还是DR模式,当后端的RS宕掉后,调度器依然会把请求转发到宕掉的RS上,这样的结果并不是我们想要的.其实,keepalived就可以解决问题,它不仅仅有高可用的功能,还有 ...

  7. 基于keepalived的nginx高可用

    #nginx,keepalived安装略过 MASTER 节点配置文件(192.168.1.11) vi /etc/keepalived/keepalived.conf global_defs { # ...

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

    (原文链接:http://www.studyshare.cn/blog-front//blog/details/1159/0 ) 一.准备工作 1.准备两个项目,发布到不同的服务器上,此处使用2个虚拟 ...

  9. 使用Keepalived实现Nginx高可用

    Keepalived是一个路由软件,可以提供linux系统和linux系统上的组件的负载均衡和高可用,高可用基于VRRP(Virtual Router Redundancy Protocol,虚ip) ...

随机推荐

  1. duilib进阶教程 -- Container控件 (3)

    前面两个教程的目的是教大家与MFC结合,那么从这篇起,将不再使用MFC,而使用纯win32项目,本文的所有知识已经在<duilib入门教程>里面讲过了,因此基础知识不再赘述. 代码下载:h ...

  2. electron安装+运行+打包成桌面应用+打包成安装文件+开机自启动

    1.初始化node项目,生成package.json文件 npm init 2.安装electron,并保存为开发依赖项 npm install electron -D 3.根目录下新建index.j ...

  3. N76E003 工程创建教程

    一.准备工作: 1.下载编译工具keil c51 2.下载N76E003提供的板级支持包(BSP),可到nuvoton上下载   二.开发环境搭建 1.安装keil c51,然后和谐...不能随便发链 ...

  4. lua迭代器和泛型for浅析

    (一) 首要概念要理清: 1. 在lua中,函数是一种"第一类值",他们具有特定的词法域."第一类值"表示在lua中函数与其他传统类型的值(例如数字和字符串)具 ...

  5. centos7 安装zookeeper3.4.8集群

    1.下载上传文件到centos中 2.解压文件夹 3.cd conf 文件下,cp  zoo_sample.cfg  zoo.cfg 4.vim zoo.cfg # The number of mil ...

  6. 跟bWAPP学WEB安全(PHP代码)--XSS跨站脚本攻击

    背景 这个系列有很多题,但是其实考察的相近,类似的就不在多说,我们来看吧.主要分几个点来讲: 反射型 存储型 JSON XM 头部字段相关 分类介绍 反射型 在请求中构造了XSS的Payload,一般 ...

  7. 低耦合高内聚 - 不要把所有东西都放在 vuex中

    我就举一个例子.比如,我想看电视,是否需要遥控器??请认真思考这个问题. 看似电视与“我”已经解耦了.然而,我需要通过遥控器去看电视,我的目的是看电视,但是我却需要依赖遥控器这个中间件.这就变相地将“ ...

  8. ubuntu安装使用ffmpeg

    环境:ubuntu 12.04 LTS (1)到http://www.ffmpeg.org/download.html下载最新版ffmpeg 也可以用这个命令: git clone git://sou ...

  9. win怎么设置最快捷的下滑关机

    win怎么设置最快捷的下滑关机 1.在C:\Windows\System32下找到SlideToShutDown.exe文件发送一份到桌面快捷方式 2.右键此快捷方式--属性--更换图表--更换一个自 ...

  10. 网络通信协议八之(传输层)TCP协议详解

    传输层协议 分段是为了提高传输效率,封装是指给每个数据段添加一个编号 端到端的传输是逻辑上的端到端,并不是真正意义上的发送方某层与接收方某层之间的传输 IP协议只是保证数据报文发送到目的地,为主机之间 ...