使用saltstack编译安装keepalived:

创建相应的目录,并在目录下创建相应的sls配置文件

[root@node1 ~]# mkdir /srv/salt/prod/keepalived
[root@node1 ~]# mkdir /srv/salt/prod/keepalived/files

1、使用saltstack进行编译安装keepalived

1.1将下载好的keepalived源码包放置在keepalived目录下面的files目录中(files目录提供需要用的源码包,文件等)

[root@node1 etc]# pwd
/usr/local/src/keepalived-1.3.6/keepalived/etc
[root@node1 etc]# cp keepalived/keepalived.conf /srv/salt/prod/keepalived/files/
[root@node1 etc]# cp init.d/keepalived /srv/salt/prod/keepalived/files/keepalived.init
[root@node1 sysconfig]# pwd
/usr/local/src/keepalived-1.3.6/keepalived/etc/sysconfig
[root@node1 sysconfig]# cp keepalived /srv/salt/prod/keepalived/files/keepalived.sysconfig

查看files目录下面文件:

[root@node1 keepalived]# ll files/
total 696
-rw-r--r-- 1 root root 702570 Oct 10 22:21 keepalived-1.3.6.tar.gz
-rwxr-xr-x 1 root root 1335 Oct 10 22:17 keepalived.init
-rw-r--r-- 1 root root 667 Oct 10 22:28 keepalived.sysconfig

1.2haproxy的源码包和启动脚本准备好后,开始进行安装keepalived

[root@node1 keepalived]# pwd
/srv/salt/prod/keepalived
[root@node1 keepalived]# cat install.sls
include:
- pkg.pkg-init keepalived-install:
file.managed:
- name: /usr/local/src/keepalived-1.3.6.tar.gz
- source: salt://keepalived/files/keepalived-1.3.6.tar.gz
- user: root
- group: root
- mode: 755
cmd.run:
- name: cd /usr/local/src/ && tar xf keepalived-1.3.6.tar.gz && cd keepalived-1.3.6 && ./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: /etc/init.d/keepalived /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

总结上面配置文件包括:1、include进来编译环境 2、编译安装keepalived 3、添加keepalived脚本文件,并添加到系统服务中 4、复制keepalived.sysconfig文件 5、创建keepalived配置文件目录

执行install.sls文件,安装keepalived:

[root@node1 keepalived]# salt 'node1' state.sls keepalived.install saltenv=prod

3、安装完keepalived后,并且keepalived已经有了启动脚本,接下来需要给keepalived提供配置文件,最后将keepalived服务开启,由于根据业务需求的不同,可能用到的keepalived的配置文件会有区别,

所以这里将配置文件与keepalived的安装分隔开进行状态管理配置,以后minion的keepalived可以根据配置文件的不同而提供安装

[root@node1 cluster]# pwd
/srv/salt/prod/cluster
[root@node1 cluster]# cat haproxy-outside-keepalived.sls haproxy与keepalived结合使用的高可用
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 jinja模板调用,使用变量
{% if grains['fqdn'] == 'node1' %} 基于节点的fqdn信息来赋予变量值
- ROUTEID: haproxy_node1
- STATEID: MASTER
- PRIORITYID: 150
{% elif grains['fqdn'] == 'node2' %}
- ROUTEID: haproxy_node2
- STATEID: BACKUP
- PRIORITYID: 100
{% endif %}
service.running:
- name: keepalived
- enable: True
- reload: True
- watch:
- file: keepalived-service

总结上述配置文件内容:1、include进来keepalived的安装 2、给各节点提供不同的配置文件,用到了jinja模板调用grains 3、开启keepalived服务,并开启自启动

最后将keepalived项目添加到top.sls文件中:

[root@node1 base]# cat top.sls
base:
'*':
- init.env_init prod:
'node1':
- cluster.haproxy-outside
- cluster.haproxy-outside-keepalived

整个keepalived项目构架图:

[root@node1 keepalived]# tree
.
├── files
│   ├── keepalived-1.3.6.tar.gz
│   ├── keepalived.init
│   └── keepalived.sysconfig
└── install.sls 1 directory, 4 files
[root@node1 keepalived]# cd ../cluster/
[root@node1 cluster]# tree
.
├── files
│   ├── haproxy-outside.cfg
│   └── haproxy-outside-keepalived.conf
├── haproxy-outside-keepalived.sls
└── haproxy-outside.sls

node1节点安装没有问题,那么更改top.sls中节点设置,将node2节点也给添加上:

[root@node1 base]# cat top.sls
base:
'*':
- init.env_init prod:
'*': 只有两个节点,所以这里*代替了
- cluster.haproxy-outside
- cluster.haproxy-outside-keepalived

执行状态配置文件:

[root@node1 base]# salt '*' state.highstate

查看node2状态:

[root@node2 ~]# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 192.168.44.10:80 0.0.0.0:* LISTEN 16791/haproxy
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1279/sshd
tcp 0 0 0.0.0.0:8090 0.0.0.0:* LISTEN 16791/haproxy
tcp 0 0 :::8080 :::* LISTEN 14351/httpd
tcp 0 0 :::22 :::* LISTEN 1279/sshd
udp 0 0 0.0.0.0:68 0.0.0.0:* 1106/dhclient

可以看见haproxy已经监听起来了,监听在了一个不是自己实际ip的地址上

查看node1的vip信息:

[root@node1 files]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:86:2C:63
inet addr:192.168.44.134 Bcast:192.168.44.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe86:2c63/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:230013 errors:0 dropped:0 overruns:0 frame:0
TX packets:172530 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:130350592 (124.3 MiB) TX bytes:19244347 (18.3 MiB) eth0:0 Link encap:Ethernet HWaddr 00:0C:29:86:2C:63
inet addr:192.168.44.10 Bcast:0.0.0.0 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:145196 errors:0 dropped:0 overruns:0 frame:0
TX packets:145196 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:12285984 (11.7 MiB) TX bytes:12285984 (11.7 MiB)

可以看见eth0:0就是vip,手动将keepalived停止,查看vip是否漂移到nide2?

[root@node1 files]# /etc/init.d/keepalived stop
Stopping keepalived: [ OK ]

查看node2状态:

[root@node2 ~]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:34:32:CB
inet addr:192.168.44.135 Bcast:192.168.44.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe34:32cb/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:494815 errors:0 dropped:0 overruns:0 frame:0
TX packets:357301 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:250265303 (238.6 MiB) TX bytes:98088504 (93.5 MiB) eth0:0 Link encap:Ethernet HWaddr 00:0C:29:34:32:CB
inet addr:192.168.44.10 Bcast:0.0.0.0 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:2953 errors:0 dropped:0 overruns:0 frame:0
TX packets:2953 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1272983 (1.2 MiB) TX bytes:1272983 (1.2 MiB)

于是haproxy结合keepalived的高可用基于saltstack安装成功,下面为haproxy和keepalived的简单配置文件:

haproxy配置文件:

[root@node1 files]# pwd
/srv/salt/prod/cluster/files
[root@node1 files]# cat haproxy-outside.cfg
#
# This is a sample configuration. It illustrates how to separate static objects
# traffic from dynamic traffic, and how to dynamically regulate the server load.
#
# It listens on 192.168.1.10:80, and directs all requests for Host 'img' or
# URIs starting with /img or /css to a dedicated group of servers. URIs
# starting with /admin/stats deliver the stats page.
# global
maxconn 10000
stats socket /var/run/haproxy.stat mode 600 level admin
log 127.0.0.1 local0
uid 200
gid 200
chroot /var/empty
daemon defaults
mode http
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms # The public 'www' address in the DMZ
frontend webserver
bind 192.168.44.10:80
default_backend web
#bind 192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem
mode http listen base_stats
bind *:8090
stats enable
stats hide-version
stats uri /haproxy?stats
stats realm "haproxy statistics"
stats auth wadeson:redhat # The static backend backend for 'Host: img', /img and /css.
backend web
balance roundrobin
retries 2
server web1 192.168.44.134:8080 check inter 1000
server web2 192.168.44.135:8080 check inter 1000

keepalived配置文件:

[root@node1 files]# cat haproxy-outside-keepalived.conf
! Configuration File for keepalived global_defs {
notification_email {
json_hc@163.com
}
notification_email_from json_hc@163.com
smtp_server smtp.163.com
smtp_connect_timeout 30
router_id {{ ROUTEID }}
} vrrp_instance VI_1 {
state {{ STATEID }}
interface eth0
virtual_router_id 51
priority {{ PRIORITYID }}
advert_int 1
authentication {
auth_type PASS
auth_pass password
}
virtual_ipaddress {
192.168.44.10/24 dev eth0 label eth0:0
}
}

查看高可用的负载效果:

 

saltstack之keepalived的安装配置的更多相关文章

  1. keepalived+haproxy 安装配置

    1.安装配置keepalived 修改配置文件/etc/keepalived/keepalived.conf ! Configuration File for keepalived global_de ...

  2. keepalived+nginx安装配置

    软件版本号: pcre8.36 ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.36.tar.gz keepalived1.2 ...

  3. saltstack之haproxy的安装配置

    使用saltstack编译安装haproxy: 由于编译安装haproxy,所以安装之前需要建立编译环境,将编译环境需要安装的包单独放置在一个目录中,当编译haproxy或其他时,直接include这 ...

  4. Saltstack系列1:安装配置

    安装 安装EPEL作为安装Ansible的yum源(CentOS6.4) rpm -Uvh http://ftp.linux.ncsu.edu/pub/epel/6/i386/epel-release ...

  5. Linux上安装配置Keepalived

    Linux上安装配置Keepalived 1.下载 自行去Keepalived官网进行下载,也可以通过如下链接进行下载2.0.18版本(目前的稳定版) 链接:https://pan.baidu.com ...

  6. saltstack自动化运维系列⑥SaltStack实践安装配置HAproxy的Keepalived

    saltstack自动化运维系列⑥SaltStack实践安装配置HAproxy的Keepalived 安装配置Keepalived 1.编写功能模块 #创建keepalived目录# mkdir -p ...

  7. SaltStack自动化安装配置haproxy的Keepalived

    keepalived配置安装 什么是vrrp,阿里云不支持组播,所以阿里云上不能配置keepalived,但是它有自己的slb.运维的大忌,在命令行复制粘贴,一般是先复制到文本中查看确认以后salt编 ...

  8. saltstack安装配置(halite)

    saltstack官方提供了一个简单的web UI--halite.但是给出的安装配置方法实在没法实现,在网上找了几篇博客,见文章末尾的参考链接,可以用起来了.但是功能有点简单.这篇文章记录安装配置h ...

  9. Keepalived安装配置

    一.  介绍 keepalived:是一个类似于 layer3, 4 & 7 交换机制的软件,也就是我们平时说的第 3 层.第 4 层和第 7层交换. Keepalived 的作用是检测 we ...

随机推荐

  1. 领悟 JavaScript 中的面向对象

    JavaScript是基于对象的语言,我们可以使用面向对象的思想去开发js代码. JavaScript是基于对象的语言. 可以使用面向对象的思想,但是不少人对这一点理解得并不全面. 在 JavaScr ...

  2. Bootstrap CSS组组件架构的设计思想

    w AO模式 Append Overwrite 附加重写

  3. A TCP connection is distinguished by four values

    4个值唯一地定义一条TCP连接. HTTP The Definitive Guide A computer might have several TCP connections open at any ...

  4. php和jsCOOKIE实现前端交互

    w如何精简? <script> document.cookie = 'wjs_cookie=' + 'amz_reviews'; function w(id) { document.coo ...

  5. closure--- 闭包与并行运算

      闭包有效的减少了函数所需定义的参数数目.这对于并行运算来说有重要的意义.在并行运算的环境下,我们可以让每台电脑负责一个函数,然后将一台电脑的输出和下一台电脑的输入串联起来.最终,我们像流水线一样工 ...

  6. Flask视图函数与模板语法

    1.Django中的CBV模式 ​ 2.Flask中的CBV和FBV def auth(func):     def inner(*args, **kwargs):         result =  ...

  7. javascript 之 valueOf

    var m = { i:10, toString:function () { console.log('toString'); return this.i; }, valueOf:function ( ...

  8. python web框架 django 用pycharm 添加django项目

    用pycharm 创建django项目 用pycharm 启动django  用项目名启动 点击蓝色连接的url 直接跳转到页面 修改 运行django 程序 设置  可以改端口 可以在创建djang ...

  9. phpcms调用子栏目名称/文章怎么操作

    phpcms调用子栏目名称相对比较简单一些,也是用{pc:content}来调用,只是把action设置为category,catid如果为0的话是调用所有一级栏目,如果是其他数字的话,则调用相应栏目 ...

  10. Apache Lucene初探

    讲解之前,先来分享一些资料 首先,学习任何一门新的亦或是旧的开源技术,百度其中一二是最简单的办法,先了解其中的大概,思想等等.这里就贡献一个讲解很到位的ppt 这是Lucene4.0的官网文档:htt ...