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模板,配置不一样
01-07 15:54