ansible 实现keepalived基于nginx的高可用

实验

timg

实验环境
192.168.63.131 ansible服务器
192.168.63.137 keepalived 服务器基于nginx
192.168.63.140 keepalived 服务器基于nginx
192.168.63.134 HTTP服务器
192.168.63.135 HTTP服务器

1.安装ansible,基于epel源
yum install ansible -y
2.配置主配置文件,在主配置文件中加入要远程的ip
vim /etc/ansible/hosts
[keepalived]
192.168.63.137
192.168.63.140

[httpdserver]
192.168.63.134
192.168.63.135

[server]
192.168.63.134
192.168.63.135
192.168.63.137
192.168.63.140

3.基于ssh验证
ssh-keygen 生成秘钥文件
ssh-copy-id 192.168.63.131 拷贝到自己主机
拷贝到其他主机
scp -r /root/.ssh root@192.168.63.137:/root/.ssh/
scp -r /root/.ssh root@192.168.63.140:/root/.ssh/
scp -r /root/.ssh root@192.168.63.135:/root/.ssh/
scp -r /root/.ssh root@192.168.63.134:/root/.ssh/

4.测试ansible是否可以ping通其他主机
ansible 192.168.63.137 -m ping
ansible 192.168.63.134 -m ping
ansible 192.168.63.135 -m ping
ansible 192.168.63.140 -m ping

ansible server -m ping

5.创建一个ansible目录用来组名存放ansible脚本
mkdir ansible

6.创建yml
[root@contes7 ansible]# vim keepalived.yml


– hosts: keepalived
remote_user: root

tasks:
– name: install
yum: name=keepalived
– name: install
yum: name=nginx
– name: install
yum: name=psmisc
– name: install
yum: name=mail*
– name: copy conf file
copy: src=/root/ansible/templates/keepalived.conf.bak.j2 dest=/etc/keepalived/keepalived.conf
– name: copy conf file
copy: src=/root/ansible/templates/notify.sh.j2 dest=/etc/keepalived/notify.sh
– name: copy conf file
copy: src=/root/ansible/templates/nginx.conf.bak.j2 dest=/etc/nginx/nginx.conf
– name: copy conf file
copy: src=/root/ansible/templates/www.conf.j2 dest=/etc/nginx/conf.d/www.conf
– name: shell
shell: chmod +x /etc/keepalived/notify.sh
– name: shell
shell: iptables -F
– name: start service
service: name=keepalived.service state=started enabled=yes
– name: start service
service: name=nginx state=started enabled=yes
– name: shell
shell: iptables -F

– hosts: 192.168.63.140
remote_user: root

tasks:
– name: copy conf file
copy: src=/root/ansible/templates/keepalived2.conf.bak.j2 dest=/etc/keepalived/keepalived.conf backup=yes

– hosts: httpdserver
remote_user: root

tasks:
– name: install package
yum: name=httpd
– name: copy
copy: src=/root/ansible/templates/index.html.j2 dest=/var/www/html/index.html
– name: shell
shell: iptables -F
– name: start service
service: name=httpd state=started enabled=yes

– hosts: 192.168.63.135
remote_user: root

tasks:
– name: copy
copy: src=/root/ansible/templates/index.html.j3 dest=/var/www/html/index.html
– name: shell
shell: iptables -F

7. 创建模板文件
vim ansible/templates/keepalived.conf.bak.j2
! Configuration File for keepalived

global_defs {
notification_email {
root@localhost
}
notification_email_from Alexandre.Cassen@firewall.loc
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_group 224.0.98.98
}

vrrp_script ngxhealth { #定义独立的脚本,名字自己定义
#killall -0 nginx 命令是检查nginx服务是否正常
script “killall -0 nginx && exit 0 || exit 1” #指明要配置脚本了,script可以是放置在系统中的脚本文件路径,也可以是一条命令
interval 1 #一秒钟检测一次
weight -5 #如果nginx故障权重减5,要确定减完以后低于备用节点
fall 2
rise 1
}

vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 66
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 12345678
}
virtual_ipaddress {
192.168.63.98/24
}

track_script {
ngxhealth #在vrrp_instance VI_1中调用ngxhealth脚本
}

track_interface {
ens33 #跟踪ens33接口
}

notify_master “/etc/keepalived/notify.sh master” # 当前节点成为主节点时触发的脚本
notify_backup “/etc/keepalived/notify.sh backup” # 当前节点转为备节点时触发的脚本
notify_fault “/etc/keepalived/notify.sh fault” # 当前节点转为失败状时触发的脚本
}

vrrp_instance VI_2 { #唯一标识,如果有vrrp,两个标识不一样,可以自己指定
state BACKUP #优先级最高的为MASTER,其他级别为BACKUP
interface ens33 #在哪个接口工作
virtual_router_id 77 #id是0-255主机的十进制数字都可以,id和自己的主服务器一样
priority 98 #优先级,数字越大,优先级就越高
advert_int 1 #自己的心跳信息,通过组播每隔多长时间通告一次
authentication { #为了安全需要验证
auth_type PASS #PASS是密码验证,默认是8位
auth_pass pvfe4HZi #密码8位,可以通过“openssl rand -base64 8” 命令生成随机的8位字串
}
virtual_ipaddress { #设置虚拟ip地址
192.168.63.100/24 #虚拟的ip,不是真实的
}

track_script {
ngxhealth #在vrrp_instance VI_1中调用ngxhealth脚本
}

notify_master “/etc/keepalived/notify.sh master” # 当前节点成为主节点时触发的脚本
notify_backup “/etc/keepalived/notify.sh backup” # 当前节点转为备节点时触发的脚本
notify_fault “/etc/keepalived/notify.sh fault” # 当前节点转为失败状时触发的脚本
}

vim ansible/templates/keepalived2.conf.bak.j2
! Configuration File for keepalived

global_defs {
notification_email {
root@localhost
}
notification_email_from Alexandre.Cassen@firewall.loc
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_group 224.0.98.98
}

vrrp_script ngxhealth { #定义独立的脚本,名字自己定义
#killall -0 nginx 命令是检查nginx服务是否正常
script “killall -0 nginx && exit 0 || exit 1” #指明要配置脚本了,script可以是放置在系统中的脚本文件路径,也可以是一条命令
interval 1 #一秒钟检测一次
weight -5 #如果nginx故障权重减5,要确定减完以后低于备用节点
fall 2
rise 1
}

vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 66
priority 98
advert_int 1
authentication {
auth_type PASS
auth_pass 12345678
}
virtual_ipaddress {
192.168.63.98/24
}

track_interface {
ens33 #跟踪ens33接口
}

track_script {
ngxhealth #在vrrp_instance VI_1中调用ngxhealth脚本
}

notify_master “/etc/keepalived/notify.sh master” # 当前节点成为主节点时触发的脚本
notify_backup “/etc/keepalived/notify.sh backup” # 当前节点转为备节点时触发的脚本
notify_fault “/etc/keepalived/notify.sh fault” # 当前节点转为失败状时触发的脚本
}

vrrp_instance VI_2 { #唯一标识,如果有vrrp,两个标识不一样,可以自己指定
state MASTER #优先级最高的为MASTER,其他级别为BACKUP
interface ens33 #在哪个接口工作
virtual_router_id 77 #id是0-255主机的十进制数字都可以,id和自己的主服务器一样
priority 100 #优先级,数字越大,优先级就越高
advert_int 1 #自己的心跳信息,通过组播每隔多长时间通告一次
authentication { #为了安全需要验证
auth_type PASS #PASS是密码验证,默认是8位
auth_pass pvfe4HZi #密码8位,可以通过“openssl rand -base64 8” 命令生成随机的8位字串
}
virtual_ipaddress { #设置虚拟ip地址
192.168.63.100/24 #虚拟的ip,不是真实的
}

track_script {
ngxhealth #在vrrp_instance VI_1中调用ngxhealth脚本
}

notify_master “/etc/keepalived/notify.sh master” # 当前节点成为主节点时触发的脚本
notify_backup “/etc/keepalived/notify.sh backup” # 当前节点转为备节点时触发的脚本
notify_fault “/etc/keepalived/notify.sh fault” # 当前节点转为失败状时触发的脚本
}

vim ansible/templates/index.html.j2
RS1
vim ansible/templates/index.html.j3
RS2

vim ansible/templates/nginx.conf.bak.j2
include /etc/nginx/conf.d/*.conf;

server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;

vim ansible/templates/www.conf.j2
upstream websrvs {
server 192.168.63.134:80; #http服务器的ip地址
server 192.168.63.135:80; #http服务器的ip地址

}

server {
listen 80 default_server; #默认访问这个网站
server_name nginx2.zhouyfei.com; #定义域名
root /user/share/nginx/html; #网站所在的目录
location / { #转换网站所在的目录
proxy_pass http://websrvs; #定义websrvs组名
}
}

vim ansible/templates/notify.sh.j2
#!/bin/bash
#通知脚本
#
contact=’root@localhost’

notify() {
local mailsubject=”$(hostname) to be $1, vip floating”
local mailbody=”$(date +’%F %T’): vrrp transition, $(hostname) changed to be $1″
echo “$mailbody” | mail -s “$mailsubject” $contact
}

case $1 in
master)
notify master
;;
backup)
notify backup
;;
fault)
notify fault
;;
*)
echo “Usage: $(basename $0) {master|backup|fault}”
exit 1
;;
esac

本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/103046

(0)
上一篇 2018-07-15 21:00
下一篇 2018-07-16 00:50

相关推荐

  • 源码编译安装 MariaDB

    基础软件 yum install bison bison-devel zlib-devel libcurl-devel libarchive-devel boost-devel gcc gcc-c++ cmake ncurses-devel gnutls-devel libxml2-devel openssl-devel libevent-devel lib…

    Linux笔记 2018-06-24
  • Linux菜鸟,放弃了曾经的那份“鸡肋”!!!

    有志者、事竟成,破釜沉舟,百二秦关终属楚;
    苦心人、天不负,卧薪尝胆,三千越甲可吞吴。

    2018-07-19
  • Linux系统原理浅谈

    CPU   CPU和各个存储器   寄存器:接近于CPU的工作频率,是CPU的本地存储器,位于运算器和控制器中,在进程的切换时,寄存器会被清空 一级缓存:比寄存器稍慢,频率同样接近于CPU,只能用于缓存数据,不能像寄存器一样修改数据,各个核心都有自己的私有一级缓存,进程切换时不被清空 二级缓存:比一级缓存慢,多个核心都有自己私有的二级缓存…

    Linux笔记 2018-04-01
  • Centos7默认网卡名如何修改成eth*

    Centos7 新添加网卡默认的识别名ens*,用起来很不习惯,为了方便系统的统一化管理,如何将默认命名规则改成eth*?

    2018-04-29
  • 吐血整理,国内常见的几种Linux认证

      因为在培训机构工作过,身边很多人咨询我Linux认证的事宜。现在国内各种培训机构的确很多,各家都说Linux认证多么多么重要,能获得多少薪水……这些听听就好了。 如果想在开源行业有所作为,必须要投入到实战中去。当你不是发烧友,不是开源社区骨干、不经常泡国外的坛子,那么实战是你唯一的成长路径。尤其是对基于Linux的开发、运维、纠错,都有上手经验…

    2018-06-01