SaltStack实践(二)-- 安装配置Keepalived

接上篇博文《SaltStack实践(一)-- 安装配置HAproxy


    1、编写功能模块

#创建keepalived目录
[root@linux-node1 ~]#mkdir -p /srv/salt/prod/keepalived/files
[root@linux-node1 ~]#cd /srv/salt/prod/keepalived/files

#获取并解压keepalived
[root@linux-node1 files]#wget http://www.keepalived.org/software/keepalived-1.2.19.tar.gz
[root@linux-node1 files]#tar xf keepalived-1.2.19.tar.gz
[root@linux-node1 files]#cd keepalived-1.2.19

#从源码包拷贝启动脚本、配置文件到files目录
[root@linux-node1 keepalived-1.2.19]#cp keepalived/etc/init.d/keepalived.init /srv/salt/prod/keepalived/files/
[root@linux-node1 keepalived-1.2.19]#cp keepalived/etc/init.d/keepalived.sysconfig  /srv/salt/prod/keepalived/files/
[root@linux-node1 keepalived-1.2.19]#cp keepalived/etc/keepalived/keepalived.conf /srv/salt/prod/keepalived/files/

#编写install.sls文件
[root@linux-node1 keepalived-1.2.19]# cd /srv/salt/prod/keepalived/
[root@linux-node1 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 xf 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


#测试
[root@linux-node1 keepalived]# salt '*' state.sls keepalived.install env=prod test=True 
linux-node2.example.com:
----------
....
Summary
-------------
Succeeded: 13 (changed=5)
Failed:     0
-------------
Total states run:     13
linux-node2.example.com:
----------
.....
-------------
Succeeded: 13 (changed=6)
Failed:     0
-------------
Total states run:     13


    2、编写业务模块

[root@linux-node1 keepalived]# cd ../cluster/


#编写keepalived配置文件
[root@linux-node1 cluster]# cd files/
[root@linux-node1 files]# vim haproxy-outside-keepalived.conf  
! Configuration File for keepalived
global_defs {
  notification_email {
    [email protected]
  }
  notification_email_from [email protected]
  smtp_server 127.0.0.1
  smtp_connect_timeout 30
  router_id {{ROUTEID}}   # jinja模板变量
}
vrrp_instance haproxy_ha {
state {{STATEID}}   # jinja模板变量
interface eth0
   virtual_router_id 36
priority {{PRIORITYID}}  # jinja模板变量
   advert_int 1
authentication {
auth_type PASS
       auth_pass 1111
   }
   virtual_ipaddress {
      10.0.0.11
   }
}


#编写用于管理keepalived配置文件的SLS
[root@linux-node1 files]#cd ..
[root@linux-node1 cluster]# vim haproxy-outside-keepalived.sls
include:
 - keepalived.install
keepalived-serivce:
 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'] == 'linux-node1.example.com' %}
   - ROUTEID: haproxy_ha
   - STATEID: MASTER
   - PRIORITYID: 150
   {% elif grains['fqdn'] == 'linux-node2.example.com' %}
   - ROUTEID: haproxy_ha
   - STATEID: BACKUP
   - PRIORITYID: 100
   {% endif %}
 service.running:
   - name: keepalived
   - enable: True
   - watch:
     - file: keepalived-serivce

     
#测试
[root@linux-node1 cluster]# salt '*' state.sls cluster.haproxy-outside-keepalived env=prod test=True   
.....
Summary
-------------
Succeeded: 15 (changed=1)
Failed:     0
-------------
Total states run:     15


#在top.sls中加入keepalived
[root@linux-node1 cluster]#cd /srv/salt/base
[root@linux-node1 base]# vim top.sls
base:
 '*':
- init.env_init

prod:
'linux-node[1-2].example.com':
  - cluster.haproxy-outside
  - cluster.haproxy-outside-keepalived


#执行安装和配置keepalived
[root@linux-node1 base]# salt 'linux-node?.example.com' state.highstate
Summary
-------------
Succeeded: 29
Failed:     0
-------------
Total states run:     29


你可能感兴趣的:(linux,keepalived,saltstack)