saltstack搭建集群2

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了saltstack搭建集群2相关的知识,希望对你有一定的参考价值。

功能模块-----keepalived模块


写之前先找一台主机源码安装测试

http://www.keepalived.org/software/keepalived-1.2.19.tar.gz

[[email protected] tools]# tar xf keepalived-1.2.19.tar.gz

[[email protected] tools]# cd keepalived-1.2.19

[[email protected] keepalived-1.2.19]# ./configure --prefix=/usr/local/keepalived --disable-fwmark

[[email protected] keepalived-1.2.19]# make && make install

keepalived-1.2.19/keepalived/etc/init.d/keepalived.init       #启动脚本

keepalived-1.2.19/keepalived/etc/keepalived/keepalived.conf    #配置文件

 

配置keepalived模块路径及相关文件

[[email protected] ~]# mkdir /srv/salt/prod/keepalived

[[email protected] ~]# mkdir /srv/salt/prod/keepalived/files

[[email protected] keepalived]# cp ~/tools/keepalived-1.2.19.tar.gz /srv/salt/prod/keepalived/

files/

[[email protected] tools]#cp keepalived-1.2.19/keepalived/etc/init.d/keepalived.init /srv/salt/prod/keepalived/files/        #复制启动脚本

[[email protected] tools]#cp keepalived-1.2.19/keepalived/etc/keepalived/keepalived.conf /srv/salt/prod/keepalived/files/           #复制配置文件

[[email protected] tools]# cp keepalived-1.2.19/keepalived/etc/init.d/keepalived.sysconfig /srv/salt/prod/keepalived/files/

[[email protected] tools]# cd /srv/salt/prod/keepalived/files/

[[email protected] files]# vim keepalived.init     #修改启动脚本路径

daemon /usr/local/keepalived/sbin/keepalived ${KEEPALIVED_OPTIONS}


1.keepalived功能模块

[[email protected] keepalived]# cd /srv/salt/prod/keepalived/

[[email protected] keepalived]# cat 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

 

[[email protected] files]# salt ‘*‘ state.sls keepalived.install env=prod     #手动测试一下


2.keepalived业务模块

[[email protected] ~]# cd /srv/salt/prod/cluster/files/

[[email protected] files]# cat haproxy-outside-keepalived.cfg #keepalived配置文件,里面用到了jinja变量

#configutation file for keepalive

globlal_defs {

    notification_email {

      [email protected]

}

    notification_email_from [email protected]

    smtp_server 127.0.0.1

    smtp_connect_timeout 30

    router_id {{ROUTEID}}

}

 

vrrp_instance haproxy_ha {

state {{STATEID}}

interface eth2

    virtual_router_id 36

priority {{PRIORITYID}}

    advert_int 1

authentication {

auth_type PASS

    auth_pass 1111

  }

  virtual_ipaddress {

    192.168.10.130

  }

}

 

[[email protected] ~]# cd /srv/salt/prod/cluster/

[[email protected] cluster]# cat haproxy-outside-keepalived.sls

include:

 - keepalived.install

 

keepalived-service:

  file.managed:

    - name: /etc/keepalived/keepalived.conf

    - source: salt://cluster/files/haproxy-outside-keepalived.cfg

    - 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

 

 

[[email protected] cluster]# salt ‘*‘ state.sls cluster.haproxy-outside-keepalived env=prod  #测试一下


指定服务器执行keepalived模块

[[email protected] salt]# cat /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

 

 

[[email protected] salt]# salt ‘*‘ state.highstate    #到这步执行成功的话就实现了keepalived+haproxy

 

遇到问题:发现keepalived 虚拟vip写不上去

查看日志 cat /var/log/messages,发现下面一句

Aug 11 15:10:12 node1 Keepalived_vrrp[29442]: VRRP_Instance(haproxy_ha{) sending 0 priority

haproxy_ha后面打了个空格解决

vrrp_instance haproxy_ha {


本文出自 “feng” 博客,请务必保留此出处http://fengxiaoli.blog.51cto.com/12104465/1958138

以上是关于saltstack搭建集群2的主要内容,如果未能解决你的问题,请参考以下文章

saltstack搭建集群3

saltstack搭建集群详解1

SaltStck 搭建Web集群运用示例

SaltStck 搭建Web集群-Job 管理

5cobbler搭建本地saltstack yum仓库

关于saltstack的job问题