Linux之企业实训篇——saltstack自动化管理系统推送实现haproxy对apache与nginx负载均衡

一、实验环境

主机名ip服务预配置服务
server1172.25.2.1/24salt-master,salt-minionhaproxy
server2172.25.2.2/24salt-minionhattpd
server3172.25.2.3/24salt-minionnginx

1.实验所用虚拟机系统均为redhat6.5
2.实验前提:物理机配置好rhel6的安装包,虚拟机配置好yum源。
3.server1即充当master也充当minion,且三台minion与master的公钥已交换,master可以向三台minion成功推送任务
以上具体步骤可见我的上篇博客>_< ! 博客链接

二、实 验

2.1部署apache的自动化安装及启动

2.1 .1 部署apache自动化安装
[root@server1 ~]# vim /etc/salt/master //开启文件系统服务器
file_roots:
  base:
    - /srv/salt
[root@server1 ~]# /etc/init.d/salt-master restart
Stopping salt-master daemon:                               [  OK  ]
Starting salt-master daemon:                               [  OK  ]
[root@server1 ~]# mkdir /srv/salt
[root@server1 ~]# cd /srv/salt
[root@server1 master]# ls
[root@server1 master]# mkdir httpd
[root@server1 master]# cd httpd/
[root@server1 httpd]# vim install.sls   //部署安装httpd与php
apache-install:     
  pkg.installed:      //安装包
    - pkgs:           // -pkgs下可写多个安装包
      - httpd
      - php
[root@server1 httpd]# salt server2 state.sls httpd.install   //向server2推送
server2:
----------
          ID: apache-install
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed
     Started: 11:20:27.070271
    Duration: 450.606 ms
     Changes:   

Summary for server2
------------
Succeeded: 1
Failed:    0
------------
Total states run:     1
Total run time: 450.606 ms 

[root@server2 minion]# rpm -q httpd  //查看已安装
httpd-2.2.15-29.el6_4.x86_64
[root@server2 minion]# rpm -q php
php-5.3.3-26.el6.x86_64

[root@server2 ~]# /etc/init.d/httpd status //但是服务并为启动,下面我们自动化部署启动服务
httpd is stopped
[root@server2 ~]# chkconfig --list httpd
httpd           0:off   1:off   2:off   3:off   4:off   5:off   6:off
2.1.2 部署apache自动化启动
[root@server1 httpd]# mkdir files
[root@server1 httpd]# cd files/
[root@server1 files]# ls
httpd.conf   

//这里存放我们http的配置文件,在部署的脚本中我们会用到源文件取自这里,而对此文件直接修改,相当于对server2上的服务配置文件修改

[root@server1 files]# ll
total 36
-rw-r--r-- 1 root root 34418 Aug 17 11:05 httpd.conf
[root@server1 httpd]# vim install.sls 
apache-install:
  pkg.installed:   
    - pkgs:
      - httpd
      - php
  file.managed:    //文件模块块
    - name: /etc/httpd/conf/httpd.conf   //server2配置文件地址
    - source: salt://httpd/files/httpd.conf  //源文件地址
    - mode: 644   //权限
    - user: root  //用户
  service.running:  //服务启动模块
    - name: httpd    //服务名称
    - enable: True   //开机自启
    - reload: True   //修改配置文件后刷新
    - watch:         //监控文件,就是上面的文件模块中的源文件
      - file: apache-install

 //为了测试文件模块块,我们将源文件中的配置文件端口修改为8080
 [root@server1 files]# vim httpd.conf  

这里写图片描述

server1进行推送,server2端查看效果
root@server1 files]# salt server2 state.sls httpd.install

server2:
----------
          ID: apache-install
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed
     Started: 11:16:54.397005
    Duration: 450.873 ms
     Changes:   
----------
          ID: apache-install
    Function: file.managed
        Name: /etc/httpd/conf/httpd.conf
      Result: True
     Comment: File /etc/httpd/conf/httpd.conf is in the correct state
     Started: 11:16:54.849887
    Duration: 50.483 ms
     Changes:   
----------
          ID: apache-install
    Function: service.running
        Name: httpd
      Result: True
     Comment: Service httpd has been enabled, and is running
     Started: 11:16:54.901259
    Duration: 202.485 ms
     Changes:   
              ----------
              httpd:
                  True

Summary for server2
------------
Succeeded: 3 (changed=1)
Failed:    0
------------
Total states run:     3
Total run time: 703.841 ms
//server2;
[root@server2 ~]# /etc/init.d/httpd status  //服务已启动
httpd (pid  3741) is running...
[root@server2 ~]# chkconfig --list httpd   //加载在开机自启项中
httpd           0:off   1:off   2:on    3:on    4:on    5:on    6:off
[root@server2 ~]# netstat -antlp | grep :8080  //查看端口为8080
tcp        0      0 :::8080                     :::*                        LISTEN      3741/httpd          
2.1.2 自动化部署sls文件优化

1. 将文件模块与安装模块并列出来


apache-install:
  pkg.installed:
    - pkgs:
      - httpd
      - php

  service.running:
    - name: httpd
    - enable: True
    - reload: True
    - watch:
      - file: /etc/httpd/conf/httpd.conf

/etc/httpd/conf/httpd.conf:
  file.managed:
    - source: salt://httpd/files/httpd.conf
    - mode: 644
    - user: root

2.将安装模块并列出来

httpd:
  pkg.installed
php:
  pkg.installed

apache-install:
  service.running:
    - name: httpd
    - enable: True
    - reload: True
    - watch:
      - file: /etc/httpd/conf/httpd.conf

/etc/httpd/conf/httpd.conf:
  file.managed:
    - source: salt://httpd/files/httpd.conf
    - mode: 644
    - user: root

2.2 部署nginx的自动化安装及启动

2.2.1 部署nginx自动化源码编译安装
[root@server1 salt]# cd /srv/salt/
[root@server1 salt]# ls  //在文件系统中创建nginx目录
httpd  nginx
[root@server1 salt]# cd nginx/
[root@server1 nginx]# ls   //创建用于安装files,用于储存安装包
files
[root@server1 nginx]# cd files/
[root@server1 files]# ls
nginx-1.14.0.tar.gz
[root@server1 files]# cd ..
[root@server1 nginx]# vim install.sls   //书写自动化安装脚本
nginx-install:
  pkg.installed:
    - pkgs:      //编译安装依赖型软件
      - pcre-devel  
      - openssl-devel
      - gcc
  file.managed:   //文件模块
    - name: /mnt/nginx-1.14.0.tar.gz  //tar包存储地址
    - source: salt://nginx/files/nginx-1.14.0.tar.gz  //源地址
  cmd.run:      //编译安装,这里的命令,就是你在源码编译是一步步进行的动作,每个动作中间用&&来连接
    - name: cd /mnt && tar zxf nginx-1.14.0.tar.gz && cd nginx-1.14.0 && sed -i.bak 's/#define NGINX_VER          "nginx\/" NGINX_VERSION/#define NGINX_VER          "nginx"/g' src/core/nginx.h && sed -i.bak 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./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
- create: /usr/local/nginx  
//文件检测,若目的机中已存在这个目录,则说明已安装好,就不用再进行编译

这里写图片描述

2.2.2 创建用户自动化创建sls文件
//这里我们再创建一个目录用于存储建立用户的sls文件。
[root@server1 salt]# ls
httpd  nginx   users
[root@server1 salt]# cd users/
[root@server1 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    //不给用户创建shell
    - createhome: False       //不自动创建家目录
    - home: /user/local/nginx  //指定家目录

这里写图片描述

2.2.3 部署服务自启动及修改配置文件
root@server1 nginx]# cd files/
[root@server1 files]# ls
nginx  nginx-1.14.0.tar.gz  nginx.conf   //这里将nginx的启动脚本及配置文件放置过来
[root@server1 files]# cd ..
[root@server1 nginx]# vim service.sls 

include:     //include模块可以将前边写的两个文件包括进来
  - users.nginx
  - nginx.install

/user/local/nginx/conf/nginx.conf:  //文件模块
  file.managed:
    - source: salt://nginx/files/nginx.conf //源文件

nginx-service:
  file.managed:
    - name: /etc/init.d/nginx    //启动文件地址
    - source: salt://nginx/files/nginx   //源文件(启动脚本)
    - mode: 755
  service.running:
    - name: nginx      //服务名称
    - reload: True     //刷新
    - watch:           //监控文件
      - file: /user/local/nginx/conf/nginx.conf

//推送任务

[root@server1 nginx]# salt server3 state.sls  nginx.service

//成功
这里写图片描述
//Server3端查看

[root@server3 salt]# ps ax|grep nginx
28983 ?        Ss     0:00 nginx: master process /user/local/nginx/sbin/nginx -c /user/local/nginx/conf/nginx.conf
28986 ?        S      0:00 nginx: worker process                                            
28987 ?        S      0:00 nginx: worker process                                            
29082 pts/0    S+     0:00 grep nginx

2.3 部署haproxy的自动化安装启动及实现自动化安装

//实验前请设置好你的yum源,将LoadBalancer模块加入server1
这里写图片描述

2.3.1 自动化部署安装haproxy及自启动
[root@server1 ~]# cd /srv/salt/
[root@server1 salt]# cd haproxy/  //这里我们创建一个目录,用于haproxy的部署文件的存放
[root@server1 haproxy]# ls
files  install.sls
[root@server1 haproxy]# cd files/    //创建files用于存储源文件
[root@server1 files]# ls
haproxy.cfg    //haproxy的配置文件
[root@server1 files]# cd ..
[root@server1 haproxy]# vim install.sls 
haproxy-install:
  pkg.installed:       //安装软件包
    - pkgs:
      - haproxy    
  file.managed:
    - name: /etc/haproxy/haproxy.cfg   //配置文件地址
    - source: salt://haproxy/files/haproxy.cfg  //源文件
  service.running:
    - name: haproxy
    - reload: True
    - watch:      //监控
      - file: haproxy-install  

这里写图片描述

2.3.2 负载均衡设置
[root@server1 /]# vim /srv/salt/haproxy/files/haproxy.cfg 

//前面设置了httpd为8080端口,这里为了统一,提前把端口改过来,并设置一个发布页面用于测试
这里写图片描述

//推送
[root@server1 files]# salt server1 state.sls haproxy.install

这里写图片描述

2.3.3 测 试

浏览器访问server1
这里写图片描述
这里写图片描述

2.4 一键式部署安装,实现负载均衡

[root@server1 salt]# vim top.sls  //在/srv/salt直接书写top.sls,这个文件的用于将前三个服务囊括起来
base:            
  'server1':
    - haproxy.install
  'server2':
    - httpd.install
  'server3':
    - nginx.service
[root@server1 salt]# salt '*' state.highstate  //运行 

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值