09 Haproxy+keepalived

Posted venicid

tags:

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

1、如何理解haproxy+keepalived

https://www.cnblogs.com/zs-wei/p/9213961.html

HaProxy可以通过监听一个统一的端口对外提供能力,然后内部进行分发,相当于nginx

何保证前端程序使用单个ip又能保证后端的实际处理机器为多台,这就是KeepAlived的作用。

技术图片   技术图片

 

 技术图片

 2、负载均衡

视频配套的资源:https://github.com/unixhot/saltbook-code

技术图片

1、pkg

init.sls依赖包安装

[root@linux-node1 /srv/salt/prod/modules]# cat pkg/init.sls 
pkg-init:
  pkg.installed:
    - names:
      - gcc
      - gcc-c++
      - glibc
      - make
      - autoconf
      - openssl
      - openssl-devel

 2、haproxy

技术图片

安装文件 init.sls

[root@linux-node1 /srv/salt/prod/modules]# cat haproxy/init.sls 
include:
  - modules.pkg.init

haproxy-install:
  file.managed:
    - name: /usr/local/src/haproxy-1.5.3.tar.gz
    - source: salt://modules/haproxy/files/haproxy-1.5.3.tar.gz
    - mode: 755
    - user: root
    - group: root

  cmd.run:
    - name: cd /usr/local/src && tar zxf haproxy-1.5.3.tar.gz && cd haproxy-1.5.3 && make TARGET=linux26 PREFIX=/usr/local/haproxy && make install PREFIX=/usr/local/haproxy
    - unless: test -d /usr/local/haproxy
    - require:
      - pkg: pkg-init
      - file: haproxy-install

/etc/init.d/haproxy:
  file.managed:
    - source: salt://modules/haproxy/files/haproxy.init
    - mode: 755
    - user: root
    - group: root
    - require:
      - cmd: haproxy-install

net.ipv4.ip_nonlocal_bind:
  sysctl.present:
    - value: 1

haproxy-config-dir:
  file.directory:
    - name: /etc/haproxy
    - mode: 755
    - user: root
    - group: root

haproxy-init:
  cmd.run:
    - name: chkconfig --add haproxy
    - unless: chkconfig --list | grep haproxy
    - require:
      - file: /etc/init.d/haproxy

 

 

内核参数

默认不能监听不在本地的ipv4

技术图片

 

file配置

技术图片

3、keepalived

技术图片

 

 

 配置文件 init.sls

[root@linux-node1 /srv/salt/prod/modules]# cat keepalived/init.sls 
include:
  - modules.pkg.init

keepalived-install:
  file.managed:
    - name: /usr/local/src/keepalived-1.2.17.tar.gz
    - source: salt://modules/keepalived/files/keepalived-1.2.17.tar.gz
    - mode: 755
    - user: root
    - group: root
  cmd.run:
    - name: cd /usr/local/src && tar zxf keepalived-1.2.17.tar.gz && cd keepalived-1.2.17 && ./configure --prefix=/usr/local/keepalived --disable-fwmark && make && make install
    - unless: test -d /usr/local/keepalived
    - require:
      - file: keepalived-install

/etc/sysconfig/keepalived:
  file.managed:
    - source: salt://modules/keepalived/files/keepalived.sysconfig
    - mode: 644
    - user: root
    - group: root

/etc/init.d/keepalived:
  file.managed:
    - source: salt://modules/keepalived/files/keepalived.init
    - mode: 755
    - user: root
    - group: root

keepalived-init:
  cmd.run:
    - name: chkconfig --add keepalived
    - unless: chkconfig --list | grep keepalived
    - require:
      - file: /etc/init.d/keepalived

/etc/keepalived:
  file.directory:
    - user: root
    - group: root

 

 

 

 

 配置文件

技术图片

4、test

[root@linux-node1 ~]# salt * state.sls modules.haproxy.init saltenv=prod

技术图片

 

 

 

[root@linux-node1 /srv/salt/prod/modules]# salt * state.sls modules.keepalived.init saltenv=prod

技术图片

3、如何使用负载均衡

每个业务一个负载均衡

每个主机的负载均衡

负载均衡对内 对外

技术图片

作业

  1. 管理   Haproy+Keepalive 配置文件
  2. 必须使用jianja模板,配置不一样

 

以上是关于09 Haproxy+keepalived的主要内容,如果未能解决你的问题,请参考以下文章

七层负载均衡HAproxy生产环境LVS+Keepalived+HAproxy

keepalived+haproxy搭建LNMP架构并做数据同步

keepalived+haproxy搭建LNMP架构并做数据同步

Keepalived+Haproxy负载均衡

Haproxy负载均衡+Keepalived高可用web群集

haproxy+keepalived实现负载均衡及高可用