源码编译安装nginx
1、在master端
mkdir /srv/salt/nginx/files -p
将nginx的源码包放入files目录
编写源码编译安装脚本:
cd /srv/salt/nginx
vim install.sls
nginx-install:
# 安装源码编译nginx所需要的依赖
pkg.installed:
- pkgs:
- gcc
- pcre-devel
- openssl-devel
file.managed:
- name: /opt/nginx-1.10.1.tar.gz # 将源码包推送至minion端后的存放位置
- source: salt://nginx/files/nginx-1.10.1.tar.gz # 源码包的来源(在master端的位置)
cmd.run:
# 源码编译开始之前的操作,隐藏nginx版本号等,随后进行源码编译安装
- name: cd /opt && tar zxf nginx-1.10.1.tar.gz && cd nginx-1.10.1 && sed -i .
bak 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && sed -i .bak 's/#define NGINX_VER "nginx\/" NGINX_VERSION/#define NGINX_VER "nginx"/g' src/cor
e/nginx.h && ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-
http_stub_status_module --with-threads --with-file-aio &>/dev/null && make >/dev/null && make install >/dev/null
# 如果minion端没有这个目录,则创建出来
- creates: /usr/local/nginx
2、使用salt执行脚本
为了方便以及后面的负载均衡集群需要我们向server3中安装nginx
salt server3 state.sls nginx.install
3、在server3中查看安装情况
4、salt自动重新加载nginx的配置文件
a、编辑自动建立nginx用户的sls文件:
mkdir /srv/salt/users
vim nginx.sls
nginx-group:
group.present:
- name: nginx
- gid: 800
nginx-user:
user.present:
- name: nginx
- uid: 800
- gid: 800
- shell: /sbin/nologin
- createhome: False
- home: /usr/local/nginx
b、编辑自动加载nginx配置文件的脚本
cd /srv/salt/nginx
vim service.sls
include:
- users.nginx # 调用nginx的建立用户
- nginx.install # 调用nginx的安装
/usr/local/nginx/conf/nginx.conf:
file.managed:
- source: salt://nginx/files/nginx.conf # 将server3中的nginx.conf文件拷贝至master中
nginx-service:
file.managed:
- name: /etc/init.d/nginx
- source: salt://nginx/files/nginx # 编写nginx启动脚本,放在/srv/salt/nginx/files目录中
- mode: 755
service.running:
- name: nginx
- reload: True
- watch:
- file: /usr/local/nginx/conf/nginx.conf
c、执行service.sls脚本
6、在server3中查看nginx用户是否创建、是否可以通过/etc/init.d/nginx控制nginx服务
salt一键部署负载均衡
1、在master端安装salt-minion,使server1也作为一个minion,并接收server-minion的公钥
2、在/srv/salt目录下创建haproxy目录,编写haproxy的安装脚本
mkdir /srv/salt/haproxy
cd /srv/salt/haproxy
vim install.sls
haproxy-install:
file.managed:
- name: /mnt/haproxy-1.4.24-1.x86_64.rpm
- source: salt://haproxy/files/haproxy-1.4.24-1.x86_64.rpm
cmd.run:
- name: yum install -y /mnt/haproxy-1.4.24-1.x86_64.rpm
- unless: rpm -q haproxy
/usr/share/haproxy:
file.directory:
- mode: 755
3、编辑自动配置负载均衡脚本:
cd /srv/salt/haproxy
vim service.sls
include:
- haproxy.install # 调用haproxy安装脚本,安装haproxy
haproxy-service:
file.managed:
- name: /etc/haproxy/haproxy.cfg
- source: salt://haproxy/files/haproxy.cfg # 配置负载均衡
service.running:
- name: haproxy
- enable: true
- reload: true
- watch:
- file: haproxy-service
4、将haproxy配置文件haproxy.cfg放入/srv/salt/haproxy/files中,并配置好负载均衡机制
vim /srv/salt/haproxy/files/haproxy.cfg
在最后写入:
frontend main *:80
default_backend app
backend app
balance roundrobin
server app1 172.25.66.2:8080 check
server app2 172.25.66.3:80 check
5、在/srv/salt/目录下编辑top.sls脚本,用来一键部署负载均衡,即在server1上部署haproxy,在server2上部署http,在server3上部署nginx
vim /srv/salt/top.sls
base:
'server1':
- haproxy.service # 调用haproxy的部署脚本
'server2':
- httpd.apache # 调用apache的部署脚本
'server3':
- nginx.service # 调用nginx的部署脚本
6、使用salt一键推送
**salt ‘*’ state.highstate **
7、在浏览器中访问www.westos.org会出现负载均衡