首页 > 代码库 > salt部署nginx

salt部署nginx

尝试使用saltstack部署nginx

搭建环境

master:192.168.25.135    centos 7

master:192.168.25.133    centos 7

master端目录:在/etc/salt/x下新建nginx目录用来存放源码包和配置文件,将源码包下载到这个目录,

     #mkdir  //etc/salt/nginx

查看目录:

技术分享

编写salt 入口文件top.sls

    #cat top.sls

base:
  ‘*‘:
    - nginx.install                  //安装sls文件
    - nginx.server                 //配置文件
    - nginx.init                      //启动文件
    - nginx.html                    //自定义的首页文件

首先对系统进行初始化配置,安装必要的依赖包

      #cat nginx/init.sls

system_init:
  pkg.installed:
    - pkgs:                              //同时安装多个报用复数
      - gcc
      - gcc-c++
      - openssl
      - make
      - unzip
      - openssl-devel
      - zlib
      - zlib-devel

安装文件:

         # cat nginx/install.sls

pcre_install:
  file.managed:
    - name: /usr/local/pcre-8.40.zip                       //安装必要的依赖包pcre
    - source: salt://nginx/pcre-8.40.zip
    - user: root
    - group: root
    - mode: 0644
  cmd.run:
    - name: cd /usr/local && unzip pcre-8.40.zip && cd pcre-8.40 && ./configure --prefix=/usr/local/pcre && make && make install
    - unless: test-d /usr/local/pcre
    - require:
      - file: pcre_install
nginx_install:
  file.managed:
    - name: /usr/local/nginx-1.8.1.tar.gz
    - source: salt://nginx/nginx-1.8.1.tar.gz
    - user: root
    - group: root
    - mode: 0644
  cmd.run:
    - name: cd /usr/local && tar zxf nginx-1.8.1.tar.gz && cd nginx-1.8.1 && ./configure --with-pcre=/usr/local/pcre-8.40 --with-http_ssl_module --with-http_stub_status_module --with-file-aio --with-http_dav_module && make && make install
    - unless: test -d /usr/local/nginx
    - require:
      - cmd: pcre_install
      - file: nginx_install

配置文件:

与启动有关的启动文件nginx.service,nginx主配置文件,nginx主配置文件可以先解压nginx源码包后,将配置文件修改后在复制到nginx目录下。

         # cat nginx/nginx.service

[Unit]  
Description=nginx service  
After=network.target

[Service]  
Type=forking   
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit  
PrivateTmp=true  

[Install]  
WantedBy=multi-user.target  
########################################################

         # cat nginx/server.sls

nginx_init:
  file.managed:
    - name: /usr/lib/systemd/system/nginx.service
    - source: salt://nginx/nginx.service
    - user: root
    - group: root
    - mode: 754
nginx_conf:
  file.managed:
    - name: /usr/local/nginx/conf/nginx.conf
    - source: salt://nginx/nginx.conf
    - user: root
    - group: root
    - mode: 644
nginx_service:
  cmd.run:
    - name: systemctl start nginx.service
    - require:
      - file: nginx_init
    - watch:
      - file: /usr/local/nginx/conf/nginx.conf

编写首页文件

       #vim nginx/www.html

hello !
if you see this picture, you are successful
首页sls文件:

       #cat nginx/html.sls

www_html:
  file.managed:
    - name: /usr/local/nginx/html/www.html
    - source: salt://nginx/www.html
    - user: root
    - group: root
    - mode: 644

到此配置完成。进行测试

       #salt ‘192.168.25.133‘ state.highstate test=Ture     

                         test=Ture   参数是测试是否能执行,但是不会真的去执行安装,只用于测试,也方便排查sls文件的错误。

技术分享

Succeeded: 9                     //成功的个数
Failed:    0                          //不成功的个数

------------
Total states run:     9         

接下来可以执行安装部署操作了

        #salt ‘192.168.25.133‘ state.highstate

查看但会结果,只要没有failed的就是执行成功的。

技术分享

最后被控端验证,nginx服务是否启动,页面是否能访问:

技术分享

技术分享

配置完成。

salt部署nginx