利用salt stack pillar安装多组keepalived


环境描述

在生产环境中,需要搭建三套keepalived环境,3个master和3个backup,要安装的软件和配置文件,虽然不是很多,一个一个配置也工作量不大,最近看了一下salt的状态模块,感觉可以通过salt来直接进行安装并自定义配置,下面记录一下操作的过程。

操作过程

首先先分析一下keepalive的配置文件,主要需要修改的地方有6处:

Keepalived_Router_ID: router_id
Keepalived_Interface: interface
Keepalived_Mcast_src_ip: mcast_src_ip
Keepalived_Virtual_Router_ID: virtual_router_id
Keepalived_status: state
Keepalived_priority: priority
Keepalived_Virtual_Ipaddress: virtual_ipaddress

最开始想到的是,利用master和backup的区分,然后基于virtual_router_id来设置ip地址,所以想的是每一组keepalived做两个状态文件,一个文件是master主机用,一个是backup主机用,这样就需要做6个状态文件,虽然是能实现自动化配置,但是需要创建和修改的配置文件也是不少,划不来。

所以想到了pillar的方式,在salt中,为每一个主机,定义keepalived相关的属性参数,然后在安装配置keepalived的时候,基于这些属性参数,自定义修改配置文件,然后生效。首先对6台主机进行配置规划,也就是这6个字段。

Minion 192.168.100.76 192.168.100.77 192.168.100.106 192.168.100.107 192.168.100.115 192.168.100.116
Keepalived_Router_ID 76 77 106 107 115 116
Keepalived_Interface eth2 eth2 eth1 eth1 eth1 eth1
Keepalived_Mcast_src_ip 192.168.100.76 192.168.100.77 192.168.100.106 192.168.100.107 192.168.100.115 192.168.100.116
Keepalived_Virtual_Router_ID 118 118 110 110 108 108
Keepalived_status master backup master backup master backup
Keepalived_priority 120 100 120 100 120 100
Keepalived_Virtual_Ipaddress 192.168.100.118/26 192.168.100.118/26 192.168.100.110/26 192.168.100.110/26 192.168.100.108/26 192.168.100.108/26

在master上,为每个minion配置这些pillar属性。顶层创建top.sls文件,然后在top文件中定义minion和对应pillar的对应关系,然后创建状态文件。

启用pillar,在/etc/salt/master文件中,修改pillar_roots,设置base的路径为/srv/salt/base/pillar

pillar_roots:
base:
- /srv/salt/base/pillar

创建keepalived目录,在该目录中,放置对应节点的pillar定义,编写top文件

[root@mysqlbackup pillar]# cat top.sls
base:
'192.168.100.76':
- keepalived.Keepalived_76
[root@mysqlbackup pillar]# cat keepalived/Keepalived_76.sls
Keepalived_Router_ID: 76
Keepalived_Interface: eth2
Keepalived_Mcast_src_ip: 192.168.100.76
Keepalived_Virtual_Router_ID: 118
Keepalived_status: master
Keepalived_priority: 120
Keepalived_Virtual_Ipaddress: 192.168.100.118/26

编写完成后,查看生效情况

[root@mysqlbackup pillar]# salt '192.168.100.76' pillar.ls
192.168.100.76:
- Keepalived_Interface
- Keepalived_priority
- Keepalived_Virtual_Router_ID
- Keepalived_Router_ID
- Keepalived_status
- Keepalived_Virtual_Ipaddress
- Keepalived_Mcast_src_ip
[root@mysqlbackup pillar]# salt '192.168.100.76' pillar.items
192.168.100.76:
----------
Keepalived_Interface:
eth2
Keepalived_Mcast_src_ip:
192.168.100.76
Keepalived_Router_ID:
76
Keepalived_Virtual_Ipaddress:
192.168.100.118/26
Keepalived_Virtual_Router_ID:
118
Keepalived_priority:
120
Keepalived_status:
master

minion的pillar生效之后,就开始修改默认的配置文件,将相关的变量,设置为pillar取值。

[root@mysqlbackup keepalived]# cat ../files/keepalived/keepalived.conf
! Configuration File for keepalived global_defs {
notification_email {
acassen@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id {{ pillar['Keepalived_Router_ID'] }}
} vrrp_script chk_mysql_port {
script "/opt/chk_mysql.sh"
interval 2
weight -5
fall 2
rise 1
} vrrp_instance VI_1 {
state {{ pillar['Keepalived_status'] }}
nopreempt
interface {{ pillar['Keepalived_Interface'] }}
mcast_src_ip {{ pillar['Keepalived_Mcast_src_ip'] }}
virtual_router_id {{ pillar['Keepalived_Virtual_Router_ID'] }}
priority {{ pillar['Keepalived_priority'] }}
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
{{ pillar['Keepalived_Virtual_Ipaddress'] }}
}
# track_script {
# chk_mysql_port
# }
}

下一步是编写状态文件,一个状态文件中,包含keepalived的安装、配置文件管理和服务管理。

[root@mysqlbackup keepalived]# cat keepalived.sls
include:
- keepalived.keepalived-install
- keepalived.configuration
- keepalived.services
[root@mysqlbackup keepalived]# cat keepalived-install.sls
keepalived-install:
pkg.installed:
- names:
- keepalived
[root@mysqlbackup keepalived]# cat configuration.sls
configuration:
file.managed:
- name: /etc/keepalived/keepalived.conf
- source: salt://files/keepalived/keepalived.conf
- user: root
- group: root
- mode: 644
- backup: minion
- template: jinja
[root@mysqlbackup keepalived]# cat services.sls
keepalived-service:
service.running:
- name: keepalived
- enable: True
- watch:
- file: /etc/keepalived/keepalived.conf
- reload: True

下一步就是在指定的minion上执行该状态文件,检查安装的结果。

[root@mysqlbackup keepalived]# salt '192.168.100.76' state.sls keepalived.keepalived
192.168.100.76:
----------
ID: keepalived-install
Function: pkg.installed
Name: keepalived
Result: True
Comment: All specified packages are already installed
Started: 08:06:42.776064
Duration: 1295.253 ms
Changes:
----------
ID: configuration
Function: file.managed
Name: /etc/keepalived/keepalived.conf
Result: True
Comment: File /etc/keepalived/keepalived.conf updated
Started: 08:06:44.075458
Duration: 125.626 ms
Changes:
----------
diff:
---
+++
@@ -23,7 +23,7 @@
nopreempt
interface eth2
mcast_src_ip 192.168.100.76
- virtual_router_id 76
+ virtual_router_id 118
priority 120
advert_int 1
authentication {
----------
ID: keepalived-service
Function: service.running
Name: keepalived
Result: True
Comment: Service reloaded
Started: 08:06:44.278083
Duration: 91.662 ms
Changes:
----------
keepalived:
True Summary for 192.168.100.76
------------
Succeeded: 3 (changed=2)
Failed: 0
------------
Total states run: 3
Total run time: 1.513 s

在minion上查看配置文件,相关的变量参数,都是根据系统的pillar进行自定义配置的。

! Configuration File for keepalived

global_defs {
notification_email {
acassen@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id 76
} vrrp_script chk_mysql_port {
script "/opt/chk_mysql.sh"
interval 2
weight -5
fall 2
rise 1
} vrrp_instance VI_1 {
state master
nopreempt
interface eth2
mcast_src_ip 192.168.100.76
virtual_router_id 118
priority 120
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.100.78/26
}
# track_script {
# chk_mysql_port
# }
}

利用salt stack pillar安装多组keepalived的更多相关文章

  1. 七天学会 SALT STACK 自动化运维 (1)

    七天学会 SALT STACK 自动化运维 (1) 简单理解 SALTSTACK 安装与配置 基本的使用方法 结束语 引用资源 简单理解 SALT STACK 笔者是初次接触 自动化运维 这一技术领域 ...

  2. salt stack 工具之一——远程命令

    salt stack 远程命令 salt stack是一种自动化的运维工具,可以同时对N台服务器进行配置管理.远程命令执行等操作. salt stack分为两个部分: salt-master,部署在控 ...

  3. Salt Stack 官方文档翻译 - 一个想做dba的sa - 博客频道 - CSDN.NET

    OSNIT_百度百科 Salt Stack 官方文档翻译 - 一个想做dba的sa - 博客频道 - CSDN.NET Salt Stack 官方文档翻译 分类: 自动运维 2013-04-02 11 ...

  4. salt stack 远程命令

    salt stack 远程命令 salt stack是一种自动化的运维工具,可以同时对N台服务器进行配置管理.远程命令执行等操作. salt stack分为两个部分 salt-master,部署在控制 ...

  5. salt之pillar组件

    pillar也是salt最重要的组件之一,其作用是定义与被控主机相关的任何数据,定义好的数据可以被其他组件使用,如模板.state.API等.在pillar中定义的数据与不同业务特征的被控主机相关联, ...

  6. RabbitMQ集群安装配置+HAproxy+Keepalived高可用

    RabbitMQ集群安装配置+HAproxy+Keepalived高可用 转自:https://www.linuxidc.com/Linux/2016-10/136492.htm rabbitmq 集 ...

  7. centos7安装配置LVS+keepalived高可用

    Centos7-Lvs+Keepalived架构 LVS+Keepalived 介绍 1 .   LVS LVS 是一个开源的软件,可以实现 LINUX 平台下的简单负载均衡. LVS 是 Linux ...

  8. Mac下利用(xcode)安装git

    Mac下利用(xcode)安装git 一.AppStore 最安全途径:搜索下载Xcode,(需要AppleID). 其他:直接百度Xcode下载. 二.Xcode 打开Xcode-->Pref ...

  9. python 利用 setup.py 手动安装第三方类库

    python 利用 setup.py 手动安装第三方类库 由于我在mac使用时,装了python3,默认有python2的环境,使用 pip 安装第三方类库时,老是安装到 python2的环境上: 在 ...

随机推荐

  1. Linux系统部署JavaWeb项目(超详细tomcat,nginx,mysql)

    转载自:Linux系统部署JavaWeb项目(超详细tomcat,nginx,mysql) 我的系统是阿里云的,香港的系统,本人选择的是系统镜像:CentOS 7.3 64位. 具体步骤: 配置Jav ...

  2. HTML转义字符&url编码表

    ISO Latin-1字符集:  — 制表符Horizontal tab  — 换行Line feed  — 回车Carriage Return  — Space ! ! — 惊叹号Exclamati ...

  3. JS代码静态分析及挖掘

    JavaScript 已经成为现代 Web 浏览器开发中最普遍的技术之一.使用客户端 JavaScript 框架(如 AngularJS,ReactJS 和 Vue.js)构建的应用程序已向前端输送了 ...

  4. Chisel3 - Tutorial - VendingMachine

    https://mp.weixin.qq.com/s/tDpUe9yhwC-2c1VqisFzMw   演示如何使用状态机.   参考链接: https://github.com/ucb-bar/ch ...

  5. Java实现 LeetCode 790 多米诺和托米诺平铺(递推)

    790. 多米诺和托米诺平铺 有两种形状的瓷砖:一种是 2x1 的多米诺形,另一种是形如 "L" 的托米诺形.两种形状都可以旋转. XX <- 多米诺 XX <- &q ...

  6. Java实现 LeetCode 650 只有两个键的键盘(递归 || 数学)

    650. 只有两个键的键盘 最初在一个记事本上只有一个字符 'A'.你每次可以对这个记事本进行两种操作: Copy All (复制全部) : 你可以复制这个记事本中的所有字符(部分的复制是不允许的). ...

  7. Java实现 蓝桥杯VIP 算法训练 链表数据求和操作

    算法训练 9-7链表数据求和操作 时间限制:1.0s 内存限制:512.0MB 读入10个复数,建立对应链表,然后求所有复数的和. 样例输入 1 2 1 3 4 5 2 3 3 1 2 1 4 2 2 ...

  8. 疫情期间我是如何拿到20k的offer,2020年php面试题汇总

    推荐视频:面试10家公司,收获9个offer,2020年PHP 面试问题 第一阶段1-2年 我认为1-2年对于PHP程序员来说是第一个门槛,这一阶段菜鸟正式从理论迈向企业级开发.我们知道如何使用工具. ...

  9. python—迭代器,生成器与for循环机制

    一:什么是迭代器协议 1.迭代器协议是指:对象必须提供一个next方法,执行该方法要么返回迭代器中的下一项,要么就引起一个stoplteration异常,以终止协议(只能往后走不能往前) 2.可迭代对 ...

  10. Linux内核配置编译及基本调试方法

    一.Linux内核配置编译 1. 交叉编译设置:make ARCH=arm CROSS_COMPILE=arm-linux- 注:也可以直接修改顶层Makefile ARCH ?= arm CROSS ...