SaltStack自动化安装配置haproxy的Keepalived
keepalived配置安装
什么是vrrp,阿里云不支持组播,所以阿里云上不能配置keepalived,但是它有自己的slb。
运维的大忌,在命令行复制粘贴,一般是先复制到文本中查看确认以后
salt编写配置管理keepalived之前,首先自己安装一遍
一、下载以及编译安装
wget http://www.keepalived.org/software/keepalived-1.2.19.tar.gz tar zxf keepalived-1.2.19.tar.gz cd keepalived-1.2.19 ./configure --prefix=/usr/local/keepalived --disable-fwmark make && make install
二、编写功能模块配置
1、创建keepalived配置管理相关目录
mkdir /srv/salt/prod/keepalived mkdir /srv/salt/prod/leepalived/files cd /usr/local/src/keepalived-1.2.19/keepalived/etc cp init.d/keepalived.init /srv/salt/prod/keepalived/files cp keeplived/keepalived.conf /srv/salt/prod/leepalived/files keepalived文件位于编译安装后的目录下,该文件是keepalived后台运行配置文件 cp /usr/local/keepalived/etc/sysconfig/keepalived /srv/salt/prod/leepalived/files/keepalived.sysconfig 或在解压目目录下也存在该文件 cp keepalived/etc/init.d/keepalived.sysconfig /srv/salt/prod/keepalived/files/ cp /usr/local/src/keepalived-1.2.19.tar.gz /srv/salt/prod/leepalived/files/
2、修改启动脚本
cd /srv/salt/prod/keepalived/files
vim keeplived.init
daemon keeplived ${KEEPALIVED_OPTIONS}修改为 daemon /usr/local/keepalived/sbin/keepalived ${KEEPALIVED_OPTIONS}
3、keepalived安装配置install.sls
cd /srv/salt/prod/keepalived
vim install.sls
include: - pkg.pkg-init keepalived-install: file.managed: - name: /usr/local/src/keepalived-1.2.19.tar.gz - source: salt://keepalived/files/keepalived-1.2.19.tar.gz - user: root - group: root - mode: 755 cmd.run: - name: cd /usr/local/src && tar zxf keepalived-1.2.19.tar.gz && cd keepalived-1.2.19 && ./configure --prefix=/usr/local/keepalived --disable-fwmark && make && make install - unless: test -d /usr/local/keepalived - require: - pkg: pkg-init - file: keepalived-install keepalived-init: file.managed: - name: /etc/init.d/keepalived - source: salt://keepalived/files/keepalived.init - user: root - group: root - mode: 755 cmd.run: - name: chkconfig --add keepalived - unless: chkconfig --list | grep keepalived - require: - file: keepalived-init /etc/sysconfig/keepalived: file.managed: - source: salt://keepalived/files/keepalived.sysconfig - user: root - group: root - mode: 644 /etc/keepalived: file.directory: - user: root - group: root - mode: 755
4、测试
salt '*' state.sls keepalived.install env=prod test=True
三、编写业务模块配置
1、编写keepalived的配置haproxy-outside-keepalived.conf
cd /srv/salt/prod/cluster/files
vim haproxy-outside-keepalived.conf
! Configuration File for keepalived global_defs { notification_email { saltstack@example.com } notification_email_from keepalived@example.com smtp_server 127.0.0.1 smtp_connect_timeout 30 # jinja模板变量 router_id {{ROUTEID}} } vrrp_instance haproxy_ha { # jinja模板变量 state {{STATEID}} interface eth0 virtual_router_id 36 # jinja模板变量 priority {{PRIORITYID}} advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { #虚ip 192.168.217.155 } }
2、编写管理keepalived配置文件的sls
cd /srv/salt/prod/cluster/
vim haproxy-outside-keepalived.sls
include: - keepalived.install keepalived-service: file.managed: - name: /etc/keepalived/keepalived.conf - source: salt://cluster/files/haproxy-outside-keepalived.conf - user: root - group: root - mode: 644 - template: jinja {% if grains['fqdn'] == 'node1' %} - ROUTEID: haproxy_ha - STATEID: MASTER - PRIORITYID: 150 {% elif grains['fqdn'] == 'node2' %} - ROUTEID: haproxy_ha - STATEID: BACKUP - PRIORITYID: 100 {% endif %} service.running: - name: keepalived - enable: True - watch: - file: keepalived-service
3、配置完后的文件配置管理目录结构
│ ── base
│ ├── init
│ │ ├── audit.sls
│ │ ├── dns.sls
│ │ ├── env_init.sls
│ │ ├── files
│ │ │ └── resolv.conf
│ │ ├── history.sls
│ │ └── sysctl.sls
│ └── top.sls
├── prod
├── cluster
│ ├── files
│ │ ├── haproxy-outside.cfg
│ │ └── haproxy-outside-keepalived.conf
│ ├── haproxy-outside-keepalived.sls
│ └── haproxy-outside.sls
├── haproxy
│ ├── files
│ │ ├── haproxy-1.6.2.tar.gz
│ │ └── haproxy.init
│ └── install.sls
├── keepalived
│ ├── files
│ │ ├── keepalived-1.2.19.tar.gz
│ │ ├── keepalived.conf
│ │ ├── keepalived.init
│ │ └── keepalived.sysconfig
│ └── install.sls
└── pkg
└── pkg-init.sls
4、测试
salt '*' state.sls cluster.haproxy-outside-keepalived env=prod test=True
5、在top.sls中加入keepalived
vim /srv/salt/base/top.sls
base: '*': - init.env_init prod: "node1": - cluster.haproxy-outside - cluster.haproxy-outside-keepalived "node2": - cluster.haproxy-outside - cluster.haproxy-outside-keepalived
四、执行安装和配置keepalived以及vrrp测试查看
1、执行配置管理
salt '*' state.highstate
2、虚ip查看如下截图
web访问结果