saltstack自动化编译安装haproxy,实现nginx负载均衡
本帖最后由 yun 于 2019-8-30 15:05 编辑此文主要是通过haproxy实现nginx负载均衡
环境:centos6.7
172.31.225.250 node1.example.com nginx(端口8080) haproxy安装在这台主机上,占用的端口是80端口
172.31.225.251 node2.example.com nginx(端口8080)
我们所需要用到的相关文件都在 /srv/salt/prod/haproxy/files/ 这个目录下面,大家直接下载即可:
master相关配置:(/etc/salt/master)
file_roots:
base:
- /srv/salt/base
dev:
- /srv/salt/dev
prod:
- /srv/salt/prod
pillar_roots:
base:
- /srv/salt/base/pillar
dev:
- /srv/salt/dev/pillar
prod:
- /srv/salt/prod/pillar1、部署haproxy(这里个人是编译安装)
首先下载源码包:
wget http://www.haproxy.org/download/1.9/src/haproxy-1.9.10.tar.gz
以上是官网的,官网被墙了,如果大家没办法下载,就去国内的网站下载,如下:
https://src.fedoraproject.org/repo/pkgs/haproxy/
初始化安装:(/srv/salt/prod/init/haproxy-init.sls)pkg-init:
pkg.installed:
- names:
- gcc
- gcc-c++
- glibc
- make
- autoconf
- openssl
- openssl-devel
- libnl
- libnl-devel
haproxy安装:(/srv/salt/prod/haproxy/haproxy.sls)include:
- init.haproxy-init
haproxy-source:
file.managed:
- name: /usr/local/src/haproxy-1.9.10.tar.gz
- source: salt://haproxy/files/haproxy-1.9.10.tar.gz
- user: root
- group: root
- mode: 644
- unless: test -f /usr/local/src/haproxy-1.9.10.tar.gz
haproxy-install:
cmd.run:
- name: cd /usr/local/src && tar xf haproxy-1.9.10.tar.gz && cd haproxy-1.9.10 && make TARGET=linux2628 PREFIX=/usr/local/haproxy && make install PREFIX=/usr/local/haproxy
- unless: test -d /usr/local/haproxy
- require:
- pkg: pkg-init
- file: haproxy-source
/etc/haproxy:
file.directory:
- user: root
- group: root
- mode: 755
/etc/haproxy/haproxy.cfg:
file.managed:
- source: salt://haproxy/files/haproxy.cfg
- user: root
- group: root
- mode: 644
- require:
- file: /etc/haproxy
/etc/init.d/haproxy:
file.managed:
- source: salt://haproxy/files/haproxy.init
- user: root
- group: root
- mode: 755
- watch_in:
- service: haproxy-service
user-create:
cmd.run:
- name: groupadd haproxy && useradd -r -g haproxy haproxy -s /sbin/nologin
- unless: id haproxy
- require_in:
- service: haproxy-service
haproxy-service:
service.running:
- name: haproxy
- enable: True
- require:
- cmd: haproxy-install
- watch:
- file: /etc/haproxy/haproxy.cfg
haproxy编译参数说明:TARGET=linux26 #内核版本,使用uname -r查看内核,如:2.6.18-371.el5,此时该参数就为linux26;kernel 大于2.6.28的用:TARGET=linux2628
ARCH=x86_64 #系统位数
PREFIX=/usr/local/haprpxy #/usr/local/haprpxy为haprpxy安装路径
下面正式安装:
安装之前最好先进行测试安装,检查有没有错误,因为在生产环境下直接执行安装错误,会导致对外服务的相关组件有可能出现问题,所以先进性测试安装salt '*' state.sls haproxy.haproxy env=prod test=True这里我测试没有问题,就直接安装了salt '*' state.sls haproxy.haproxy env=prod查看haproxy服务
验证负载均衡:(haproxy里面我用的是roundrobin),node1权重2,node2权重1
172.31.225.250:8080 内容如下:
172.31.225.251:8080 内容如下:
172.31.225.250 内容如下:
至此就结束了。原创不易,转载请注明原创来处,谢谢!
相关文章:
haproxy配置文件详解
页:
[1]