企业实战--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部署成功。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值