企业实战--saltstack自动化运维(使用 Saltstack 远程部署 nginx)

1、准备源码包:

[root@server1 salt]# pwd
/srv/salt
[root@server1 salt]# ls
apache  _modules  nfs  nginx  top.sls
[root@server1 salt]# cd nginx/
[root@server1 nginx]# ls
nginx-1.16.1.tar.gz

2、编辑init文件安装依赖性及解压源码包:

[root@server1 nginx]# vim init.sls
[root@server1 nginx]# cat init.sls
install-nginx:
  pkg.installed:
    - pkgs:
      - gcc
      - make
      - pcre-devel
      - openssl-devel

  archive.extracted:
    - name: /mnt				#解压目录
    - source: salt://nginx/nginx-1.16.1.tar.gz		#源码包位置

3、推送获得nginx。conf配置文件:

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

在server3查看:

[root@server3 salt]# ls /mnt/
nginx-1.16.1
[root@server3 salt]# cd /mnt/nginx-1.16.1/
[root@server3 nginx-1.16.1]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src

4、编辑init文件对nginx编译:

[root@server1 nginx]# vim init.sls
install-nginx:
  pkg.installed:
    - pkgs:
      - gcc
      - make
      - pcre-devel
      - openssl-devel

  archive.extracted:
    - name: /mnt
    - source: salt://nginx/nginx-1.16.1.tar.gz

  cmd.run:
    - name: cd /mnt/nginx-1.16.1 && sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --prefix=/usr/local/nginx --with-http_ssl_module &> /dev/null && make &> /dev/null && make install &> /dev/null
    - creates: /usr/local/nginx

其中cmd.run下面的creates表示判断文件是否存在,如果存在就不再运行,避免再次调用这个文件的时候重复运行。

4、nginx启动脚本:

[root@server1 nginx]# vim nginx.service
[root@server1 nginx]# cat nginx.service 
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

以上内容参考官网:https://www.nginx.com/resources/wiki/start/topics/examples/systemd/

5、继续编辑init文件推送启动脚本:

[root@server1 nginx]# vim init.sls
[root@server1 nginx]# cat init.sls
install-nginx:
  pkg.installed:
    - pkgs:
      - gcc
      - make
      - pcre-devel
      - openssl-devel

  archive.extracted:
    - name: /mnt
    - source: salt://nginx/nginx-1.16.1.tar.gz

  cmd.run:
    - name: cd /mnt/nginx-1.16.1 && sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --prefix=/usr/local/nginx --with-http_ssl_module &> /dev/null && make &> /dev/null && make install &> /dev/null
    - creates: /usr/local/nginx

  file.managed:
    - name: /lib/systemd/system/nginx.service
    - source: salt://nginx/nginx.service

6.编辑init中nginx启动服务

[root@server1 nginx]# vim init.sls 
[root@server1 nginx]# cat init.sls
install-nginx:
  pkg.installed:
    - pkgs:
      - gcc
      - make
      - pcre-devel
      - openssl-devel

  archive.extracted:
    - name: /mnt
    - source: salt://nginx/nginx-1.16.1.tar.gz

  cmd.run:
    - name: cd /mnt/nginx-1.16.1 && sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --prefix=/usr/local/nginx --with-http_ssl_module &> /dev/null && make &> /dev/null && make install &> /dev/null
    - creates: /usr/local/nginx

  file.managed:
    - name: /lib/systemd/system/nginx.service
    - source: salt://nginx/nginx.service

  service.running:
    - name: nginx
    - enable: true
    - reload: true
    - require:
      - file: /usr/local/nginx/conf/nginx.conf
    - watch:
      - file: /usr/local/nginx/conf/nginx.conf

/usr/local/nginx/conf/nginx.conf:
  file.managed:
    - source: salt://nginx/nginx.conf

同时需要准备配置文件:

[root@server1 nginx]# scp server3:/usr/local/nginx/conf/nginx.conf .
[root@server1 nginx]# ls
init.sls nginx-1.16.1.tar.gz nginx.conf nginx.service service.sls

如果使用include,要注意执行sls文件顺序

[root@server1 nginx]# vim init.sls 
[root@server1 nginx]# cat init.sls 
install-nginx:
  pkg.installed:
    - pkgs:
      - gcc
      - make
      - pcre-devel
      - openssl-devel

  archive.extracted:
    - name: /mnt
    - source: salt://nginx/nginx-1.16.1.tar.gz

  cmd.run:
    - name: cd /mnt/nginx-1.16.1 && sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --prefix=/usr/local/nginx --with-http_ssl_module &> /dev/null && make &> /dev/null && make install &> /dev/null
    - creates: /usr/local/nginx

  file.managed:
    - name: /lib/systemd/system/nginx.service
    - source: salt://nginx/nginx.service

include:
  - nginx.service

其中include字段表示在运行init.sls文件时还会运行nginx.service文件。

 [root@server1 nginx]# vim service.sls 
[root@server1 nginx]# cat service.sls 
/usr/local/nginx/conf/nginx.conf:
  file.managed:
    - source: salt://nginx/nginx.conf
    - require:
      - sls: nginx
 
  service.running:
    - name: nginx
    - enable: true
    - reload: true
    - watch:
      - file: /usr/local/nginx/conf/nginx.conf

其中include字段表示在运行init.sls文件时还会运行nginx.service文件。

推送:

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

此时远程nginx部署成功。

你可能感兴趣的:(企业实战saltstack)