利用saltstack管理工具部署nginx

部署nginx

再重新打开一个虚拟机server3并配置好yum源(添加salt)

[root@server3 ~]# yum install -y salt-minion
[root@server3 ~]# cd /etc/salt/
[root@server3 salt]# vim minion
####################
 16 master: 172.25.52.1

[root@server3 salt]# /etc/init.d/salt-minion start
Starting salt-minion:root:server3 daemon: OK
[root@server1 ~]# salt-key -L
Accepted Keys:
server2
Denied Keys:
Unaccepted Keys:
server3
Rejected Keys:
[root@server1 ~]# salt-key -a server3
The following keys are going to be accepted:
Unaccepted Keys:
server3
Proceed? [n/Y] Y
Key for minion server3 accepted.
##用命令的方式直接配置nginx
[root@foundation52 Desktop]# scp nginx-1.14.0.tar.gz server3:/mnt/
root@server3's password: 
nginx-1.14.0.tar.gz                          100%  992KB 992.5KB/s   00:00  

[root@server3 mnt]# ls
nginx-1.14.0.tar.gz
[root@server3 mnt]# tar zxf nginx-1.14.0.tar.gz 
[root@server3 mnt]# ls
nginx-1.14.0  nginx-1.14.0.tar.gz
[root@server3 mnt]# cd nginx-1.14.0
[root@server3 nginx-1.14.0]# ls
auto     CHANGES.ru  configure  html     man     src
CHANGES  conf        contrib    LICENSE  README
[root@server3 nginx-1.14.0]# vim src/core/nginx.h 

这里写图片描述

[root@server3 nginx-1.14.0]# sed -i.bak 's/#define NGINX_VER          "nginx\/" NGINX_VERSION/#define NGINX_VER          "nginx"/g' src/core/nginx.h
[root@server3 nginx-1.14.0]# vim src/core/nginx.h 

利用saltstack管理工具部署nginx_第1张图片

[root@server3 nginx-1.14.0]# vim auto/cc/gcc 

利用saltstack管理工具部署nginx_第2张图片

[root@server3 nginx-1.14.0]# sed -i.bak 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc
[root@server3 nginx-1.14.0]# vim auto/cc/gcc 

利用saltstack管理工具部署nginx_第3张图片

[root@server1 salt]# mkdir nginx
[root@server1 salt]# cd nginx/
[root@server1 nginx]# ls
[root@server1 nginx]# pwd
/srv/salt/nginx
##命令的方式
[root@server1 nginx]# vim install.sls
#####################
  1 nginx-install:     ##安装软件包(解决依赖性)   
  2   pkg.installed:
  3     - pkg:
  4       - pcre-devel
  5       - openssl-devel
  6       - gcc
  7 
  8   file.managed:   ##文件
  9     - name: /mnt/nginx-1.14.0.tar.gz
 10     - source: salt://nginx/files/nginx-1.14.0.tar.gz
 11 
 12   cmd.run:       ##run方法 用于直接执行命令
 ## &> /dev/null表示将输出内容导入到/dev/null里
 ## 配置nginx文件以及源码编译
 13     - name: cd /mnt && tar zxf nginx-1.14.0.tar.gz && cd nginx-1.14.0 &&  s    ed -i.bak 's/#define NGINX_VER          "nginx\/" NGINX_VERSION/#define NGI    NX_VER          "nginx"/g' src/core/nginx.h && sed -i.bak 's/CFLAGS="$CFLAG    S -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --prefix=/usr/loca    l/nginx --with-http_ssl_module --with-http_stub_status_module --with-thread    s --with-file-aio &> /dev/null && make &> /dev/null && make install &> /dev    /null

利用saltstack管理工具部署nginx_第4张图片

[root@server1 nginx]# ls
install.sls
[root@server1 nginx]# mkdir files
[root@server1 nginx]# cd files/
[root@server1 files]# ls
[root@server1 files]# pwd
/srv/salt/nginx/files

[root@foundation52 Desktop]# scp nginx-1.14.0.tar.gz server1:/srv/salt/nginx/files 
root@server1's password: 
nginx-1.14.0.tar.gz                             100%  992KB 992.5KB/s   00:00   

[root@server1 files]# ls
nginx-1.14.0.tar.gz

[root@server1 nginx]# salt server3 state.sls nginx.install
server3:
----------
          ID: nginx-install
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed
     Started: 13:34:53.028643
    Duration: 422.918 ms
     Changes:   
----------
          ID: nginx-install
    Function: file.managed
        Name: /mnt/nginx-1.14.0.tar.gz
      Result: True
     Comment: File /mnt/nginx-1.14.0.tar.gz is in the correct state
     Started: 13:34:53.454075
    Duration: 71.122 ms
     Changes:   
----------
          ID: nginx-install
    Function: 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
      Result: True
     Comment: Command "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" run
     Started: 13:34:53.526058
    Duration: 27499.581 ms
     Changes:   
              ----------
              pid:
                  3298
              retcode:
                  0
              stderr:
              stdout:

Summary for server3
------------
Succeeded: 3 (changed=1)
Failed:    0
------------
Total states run:     3
Total run time:  27.994 s
##测试;
##在推送过程中查看到正在编译
[root@server3 mnt]# ps ax
  PID TTY      STAT   TIME COMMAND
  ...................
 5531 ?        S      0:00 make
 5532 ?        S      0:00 make -f objs/Makefile
 5655 ?        S      0:00 cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused
 5656 ?        R      0:00 /usr/libexec/gcc/x86_64-redhat-linux/4.4.7/cc1 -qui
 5657 ?        S      0:00 as -Qy -o objs/src/event/ngx_event_pipe.o -
##推送完成后查看到编译完成
 [root@server3 mnt]# ps ax
   PID TTY      STAT   TIME COMMAND
  ...................
 1372 ?        Ss     0:00 sshd: root@pts/0 
 1374 pts/0    Ss     0:00 -bash
 1721 ?        Sl     0:04 /usr/bin/python2.6 /usr/bin/salt-minion -c /etc/sal
 1722 ?        S      0:00 /usr/bin/python2.6 /usr/bin/salt-minion -c /etc/sal
 1929 ?        Ss     0:00 /usr/sbin/anacron -s
 5973 pts/0    R+     0:00 ps ax
##启动服务
[root@server1 nginx]# vim service.sls
#####################
  1 include:        ##调用nginx目录下的install.sls脚本
  2   - nginx.install
  3 
  4 nginx-service:  ##开启服务
  5   cmd.run:
  6     - name: /usr/local/nginx/sbin/nginx
  ##  creates 用于判断nginx是否完成编译 如果已经编译过就不再重新编译
  7     - creates: /usr/local/nginx/logs/nginx.pid  

利用saltstack管理工具部署nginx_第5张图片

##推送给server3
[root@server1 nginx]# salt server3 state.sls nginx.service
测试:
##查看到nginx的默认端口为80 说明nginx成功启动
[root@server3 logs]# netstat -antpl
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      8658/nginx          
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      907/sshd            
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      984/master          
tcp        0      0 172.25.52.3:58079           172.25.52.1:4506            TIME_WAIT   -                   
tcp        0      0 172.25.52.3:58082           172.25.52.1:4506            TIME_WAIT   -                   
tcp        0      0 172.25.52.3:58078           172.25.52.1:4506            TIME_WAIT   -                   
tcp        0      0 172.25.52.3:45539           172.25.52.1:4505            ESTABLISHED 1721/python2.6      
tcp        0      0 172.25.52.3:58077           172.25.52.1:4506            TIME_WAIT   -                   
tcp        0      0 172.25.52.3:58080           172.25.52.1:4506            TIME_WAIT   -                   
tcp        0      0 172.25.52.3:22              172.25.52.250:50736         ESTABLISHED 1372/sshd           
tcp        0      0 172.25.52.3:58081           172.25.52.1:4506            TIME_WAIT   -                   
tcp        0      0 172.25.52.3:58083           172.25.52.1:4506            TIME_WAIT   -                   
tcp        0      0 :::22                       :::*                        LISTEN      907/sshd            
tcp        0      0 ::1:25                      :::*                        LISTEN      984/master     
[root@server3 conf]# scp nginx.conf server1:/srv/salt/nginx/files
root@server1's password: 
westos
Permission denied, please try again.
root@server1's password: 
nginx.conf                                  100% 2656     2.6KB/s   00:00    
[root@server3 conf]# pwd
/usr/local/nginx/conf
##发送nginx脚本
[root@foundation52 Desktop]# scp nginx server1:/srv/salt/nginx/files
root@server1's password: 
nginx                                       100% 3136     3.1KB/s   00:00 

[root@server1 nginx]# pwd
/srv/salt/nginx
[root@server1 nginx]# ls
files  install.sls  service.sls
[root@server1 nginx]# cd files/
[root@server1 files]# ls
nginx  nginx-1.14.0.tar.gz  nginx.conf
[root@server1 files]# cd ..
##脚本的方式
[root@server1 nginx]# vim service.sls
#####################
  1 include:
  2   - nginx.install
  3 
  4 /usr/local/nginx/conf/nginx.conf:
  5   file.managed:
  6     - source: salt://nginx/files/nginx.conf
  7 
  8 nginx-service:
  9   file.managed:
 10     - name: /etc/init.d/nginx
 11     - source: salt://nginx/files/nginx
 12     - mode: 755
 13   service.running:
 14     - name: nginx
 15     - reload: True
 16     - watch:
 17       - file: /usr/local/nginx/conf/nginx.conf

利用saltstack管理工具部署nginx_第6张图片

##推送
[root@server1 nginx]# salt server3 state.sls nginx.service
测试:
##在推送过程中查看到正在编译(前提是之前未编译过)
[root@server3 conf]# ps ax
  PID TTY      STAT   TIME COMMAND
10934 ?        S      0:00 make
10935 ?        S      0:00 make -f objs/Makefile
11262 ?        S      0:00 cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused
11264 ?        R      0:00 /usr/libexec/gcc/x86_64-redhat-linux/4.4.7/cc1 -qui
11266 ?        S      0:00 as -Qy -o objs/src/http/modules/ngx_http_limit_conn
11269 pts/0    R+     0:00 ps ax
[root@server3 nginx]# /usr/local/nginx/sbin/nginx -s stop
##重新推送
[root@server1 nginx]# salt server3 state.sls nginx.service
##测试:
##查看到nginx进程
[root@server3 nginx]# ps ax
  PID TTY      STAT   TIME COMMAND
14079 ?        Ss     0:00 nginx: master process /usr/local/nginx/sbin/nginx -
14082 ?        S      0:00 nginx: worker process                             
14097 pts/0    R+     0:00 ps ax
##更改工作进程数为2
[root@server1 nginx]# vim files/nginx.conf 
#####################
3 worker_processes  2;

[root@server1 nginx]# salt server3 state.sls nginx.service
##测试:
##查看到worker数为2
[root@server3 nginx]# ps ax
  PID TTY      STAT   TIME COMMAND
16797 ?        S      0:00 nginx: worker process                         
16798 ?        S      0:00 nginx: worker process                         
16801 pts/0    R+     0:00 ps ax
##添加用户,组
[root@server1 nginx]# vim files/nginx.conf 
#####################
 2 user  nginx nginx;

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

[root@server1 nginx]# cd ..
[root@server1 salt]# ls
httpd  nginx
[root@server1 salt]# mkdir users
[root@server1 salt]# ls
httpd  nginx  users
[root@server1 salt]# cd users/
[root@server1 users]# ls
[root@server1 users]# vim nginx.sls
####################
 1 nginx-group:      ##定义组
  2   group.present:
  3     - name: nginx
  4     - gid: 800
  5 
  6 nginx-user:      ##定义用户
  7   user.present:
  8     - name: nginx
  9     - uid: 800
 10     - gid: 800
 11     - shell: /sbin/nologin
 12     - createhome: False
 13     - home: /usr/local/nginx

利用saltstack管理工具部署nginx_第7张图片

[root@server1 users]# cd ..
[root@server1 salt]# cd nginx/
[root@server1 nginx]# pwd
/srv/salt/nginx
[root@server1 nginx]# vim service.sls 
#####################
  1 include:
  2   - users.nginx   ##调用user目录里的nginx脚本
  3   - nginx.install   
  4 
  5 /usr/local/nginx/conf/nginx.conf:
  6   file.managed:
  7     - source: salt://nginx/files/nginx.conf
  8 
  9 nginx-service:
 10   file.managed:
 11     - name: /etc/init.d/nginx
 12     - source: salt://nginx/files/nginx
 13     - mode: 755
 14   service.running:
 15     - name: nginx
 16     - reload: True
 17     - watch:
 18       - file: /usr/local/nginx/conf/nginx.conf
 19     - creates: /usr/local/nginx

利用saltstack管理工具部署nginx_第8张图片

[root@server1 nginx]# salt server3 state.sls nginx.service
测试:
[root@server3 nginx]# id nginx
uid=800(nginx) gid=800(nginx) groups=800(nginx)
[root@server1 nginx]# cd ..
[root@server1 salt]# mkdir pkgs
[root@server1 salt]# cd pkgs/
[root@server1 pkgs]# ls
[root@server1 pkgs]# vim make.sls
###################
  1 make-gcc:       ##安装软件包
  2   pkg.installed:
  3     - pkgs:
  4       - pcre-devel
  5       - openssl-devel
  6       - gcc

利用saltstack管理工具部署nginx_第9张图片

[root@server1 pkgs]# ls
make.sls
[root@server1 pkgs]# cd ..
[root@server1 salt]# cd nginx/
[root@server1 nginx]# ls
files  install.sls  service.sls
[root@server1 nginx]# vim install.sls
#################### 
  1 include:
  2   - pkgs.make   ##调用pkgs目录下的make脚本
  3 
  4 nginx-install:
  5   file.managed:
  6     - name: /mnt/nginx-1.14.0.tar.gz
  7     - source: salt://nginx/files/nginx-1.14.0.tar.gz
  8 
  9   cmd.run:
 10     - 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 && ./configur    e --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_st    atus_module --with-threads --with-file-aio &> /dev/null && make &> /de    v/null && make install &> /dev/null
 11     - creates: /usr/local/nginx

利用saltstack管理工具部署nginx_第10张图片

##不报错即可
[root@server1 nginx]# salt server3 state.sls nginx.service

你可能感兴趣的:(利用saltstack管理工具部署nginx)