saltstack部署nginx,


1、前期规划准备

nginx安装准备:

  1)安装必要依赖

  2)准备pcre安装包

  2)源码安装pcre

  3)准备nginx安装包

  4)源码安装nginx

nginx配置:

  1)拷贝nginx.conf配置文件

  2)拷贝启停脚本

  3)添加系统服务并设置开机启动

2、部署过程

1)准备安装包

1 2 3 4 5 6 7 [root@salt-master base]# cd /srv/salt/base [root@salt-master base]# mkdir -p pcre/files [root@salt-master base]# mkdir -p nginx/files [root@salt-master base]# cd /srv/salt/base/pcre/files [root@salt-master base]# wget https://ftp.pcre.org/pub/pcre/pcre-8.37.tar.gz [root@salt-master base]# cd /srv/salt/base/nginx/files [root@salt-master base]# wget http://nginx.org/download/nginx-1.6.3.tar.gz

2)编写sls文件

1 2 3 4 5 [root@salt-master base]# cd nginx/ [root@salt-master nginx]# vi install.sls  [root@salt-master nginx]# vi service.sls [root@salt-master nginx]# cd ../pcre/ [root@salt-master pcre]# vi install.sls

3)测试sls

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 [root@salt-master base]# salt 'salt-minion01.contoso.com' state.sls nginx.service test=True salt-minion01.contoso.com: ----------           ID: pcre-source-install     Function: pkg.installed         Name: zlib-devel       Result: None      Comment: The following packages are set to be installed/updated: zlib-devel      Started: 14:41:07.540210     Duration: 18297.782 ms      Changes:    ----------           ID: pcre-source-install     Function: pkg.installed         Name: glibc-devel       Result: True      Comment: Package glibc-devel is already installed.      Started: 14:41:25.838187     Duration: 0.536 ms      Changes:    ----------           ID: pcre-source-install     Function: pkg.installed         Name: gcc       Result: True      Comment: Package gcc is already installed.      Started: 14:41:25.838783     Duration: 0.294 ms      Changes:    ----------           ID: pcre-source-install     Function: pkg.installed         Name: cmake       Result: True      Comment: Package cmake is already installed.      Started: 14:41:25.839133     Duration: 0.241 ms      Changes:    ----------           ID: pcre-source-install     Function: pkg.installed         Name: glibc       Result: True      Comment: Package glibc is already installed.      Started: 14:41:25.839432     Duration: 0.241 ms      Changes:    ----------           ID: pcre-source-install     Function: pkg.installed         Name: zlib       Result: True      Comment: Package zlib is already installed.      Started: 14:41:25.839727     Duration: 0.305 ms      Changes:    ----------           ID: pcre-source-install     Function: pkg.installed         Name: autoconf       Result: None      Comment: The following packages are set to be installed/updated: autoconf      Started: 14:41:25.840087     Duration: 0.281 ms      Changes:    ----------           ID: pcre-source-install     Function: pkg.installed         Name: gcc-c++       Result: True      Comment: Package gcc-c++ is already installed.      Started: 14:41:25.840422     Duration: 0.239 ms      Changes:    ----------           ID: pcre-source-install     Function: pkg.installed         Name: make       Result: True      Comment: Package make is already installed.      Started: 14:41:25.840709     Duration: 0.282 ms      Changes:    ----------           ID: pcre-source-install     Function: file.managed         Name: /opt/tools/pcre-8.37.tar.gz       Result: None      Comment: The file /opt/tools/pcre-8.37.tar.gz is set to be changed      Started: 14:41:25.888753     Duration: 17.582 ms      Changes:                  ----------               newfile:                   /opt/tools/pcre-8.37.tar.gz ----------           ID: pcre-source-install     Function: cmd.run         Name: cd /opt/tools/ && tar -zxf pcre-8.37.tar.gz && cd pcre-8.37 && ./configure --prefix=/usr/local/pcre && make && make install       Result: None      Comment: Command "cd /opt/tools/ && tar -zxf pcre-8.37.tar.gz && cd pcre-8.37 && ./configure --prefix=/usr/local/pcre && make && make install" would have been executed      Started: 14:41:25.910768     Duration: 10.909 ms      Changes:    ----------           ID: nginx-source-install     Function: pkg.installed         Name: zlib-devel       Result: None      Comment: The following packages are set to be installed/updated: zlib-devel      Started: 14:41:25.922254     Duration: 2.483 ms      Changes:    ----------           ID: nginx-source-install     Function: pkg.installed         Name: gcc       Result: True      Comment: Package gcc is already installed.      Started: 14:41:25.925010     Duration: 0.926 ms      Changes:    ----------           ID: nginx-source-install     Function: pkg.installed         Name: make       Result: True      Comment: Package make is already installed.      Started: 14:41:25.926183     Duration: 0.975 ms      Changes:    ----------           ID: nginx-source-install     Function: pkg.installed         Name: zlib       Result: True      Comment: Package zlib is already installed.      Started: 14:41:25.927407     Duration: 1.276 ms      Changes:    ----------           ID: nginx-source-install     Function: pkg.installed         Name: openssl       Result: True      Comment: Package openssl is already installed.      Started: 14:41:25.929287     Duration: 1.331 ms      Changes:    ----------           ID: nginx-source-install     Function: pkg.installed         Name: openssl-devel       Result: None      Comment: The following packages are set to be installed/updated: openssl-devel      Started: 14:41:25.930830     Duration: 1.027 ms      Changes:    ----------           ID: nginx-source-install     Function: pkg.installed         Name: automake       Result: None      Comment: The following packages are set to be installed/updated: automake      Started: 14:41:25.931943     Duration: 0.392 ms      Changes:    ----------           ID: nginx-source-install     Function: pkg.installed         Name: autoconf       Result: None      Comment: The following packages are set to be installed/updated: autoconf      Started: 14:41:25.932402     Duration: 0.385 ms      Changes:    ----------           ID: nginx-source-install     Function: pkg.installed         Name: gcc-c++       Result: True      Comment: Package gcc-c++ is already installed.      Started: 14:41:25.932851     Duration: 0.33 ms      Changes:    ----------           ID: nginx-source-install     Function: group.present         Name: nginx       Result: None      Comment: The following group attributes are set to be changed:               gid: 601      Started: 14:41:25.934673     Duration: 0.425 ms      Changes:    ----------           ID: nginx-source-install     Function: user.present         Name: nginx       Result: None      Comment: The following user attributes are set to be changed:               homeDoesNotExist: /home/nginx               gid: 601               fullname: nginx               uid: 601      Started: 14:41:25.943129     Duration: 10.523 ms      Changes:    ----------           ID: nginx-source-install     Function: file.managed         Name: /opt/tools/nginx-1.6.3.tar.gz       Result: None      Comment: The file /opt/tools/nginx-1.6.3.tar.gz is set to be changed      Started: 14:41:25.953761     Duration: 3.461 ms      Changes:                  ----------               newfile:                   /opt/tools/nginx-1.6.3.tar.gz ----------           ID: nginx-source-install     Function: cmd.run         Name: cd /opt/tools/ && tar -zxf nginx-1.6.3.tar.gz && mkdir -p /usr/local/nginx/tmp/{client,proxy,fcgi} && cd nginx-1.6.3 && ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_gzip_static_module --http-client-body-temp-path=/usr/local/nginx/tmp/client/ --http-proxy-temp-path=/usr/local/nginx/tmp/proxy/ --http-fastcgi-temp-path=/usr/local/nginx/tmp/fcgi/ --with-poll_module --with-file-aio --with-http_realip_module --with-http_addition_module --with-http_addition_module --with-http_random_index_module --with-http_stub_status_module --http-uwsgi-temp-path=/usr/local/nginx/uwsgi_temp --http-scgi-temp-path=/usr/local/nginx/scgi_temp --with-pcre=/opt/tools/pcre-8.37 && make && make install && chown -R nginx:nginx /usr/local/nginx/       Result: None      Comment: Command "cd /opt/tools/ && tar -zxf nginx-1.6.3.tar.gz && mkdir -p /usr/local/nginx/tmp/{client,proxy,fcgi} && cd nginx-1.6.3 && ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_gzip_static_module --http-client-body-temp-path=/usr/local/nginx/tmp/client/ --http-proxy-temp-path=/usr/local/nginx/tmp/proxy/ --http-fastcgi-temp-path=/usr/local/nginx/tmp/fcgi/ --with-poll_module --with-file-aio --with-http_realip_module --with-http_addition_module --with-http_addition_module --with-http_random_index_module --with-http_stub_status_module --http-uwsgi-temp-path=/usr/local/nginx/uwsgi_temp --http-scgi-temp-path=/usr/local/nginx/scgi_temp --with-pcre=/opt/tools/pcre-8.37 && make && make install && chown -R nginx:nginx /usr/local/nginx/" would have been executed      Started: 14:41:25.958142     Duration: 4.665 ms      Changes:    ----------           ID: nginx-init     Function: file.managed         Name: /etc/init.d/nginx       Result: None      Comment: The file /etc/init.d/nginx is set to be changed      Started: 14:41:25.963238     Duration: 3.005 ms      Changes:                  ----------               newfile:                   /etc/init.d/nginx ----------           ID: nginx-init     Function: cmd.run         Name: chkconfig --add nginx       Result: None      Comment: Command "chkconfig --add nginx" would have been executed      Started: 14:41:25.966786     Duration: 293.959 ms      Changes:    ----------           ID: /usr/local/nginx/conf/nginx.conf     Function: file.managed       Result: None      Comment: The file /usr/local/nginx/conf/nginx.conf is set to be changed      Started: 14:41:26.261344     Duration: 2.984 ms      Changes:                  ----------               newfile:                   /usr/local/nginx/conf/nginx.conf ----------           ID: nginx-service     Function: file.directory         Name: /usr/local/nginx/conf.d       Result: None      Comment: The following files will be changed:               /usr/local/nginx/conf.d: directory - new      Started: 14:41:26.264670     Duration: 0.371 ms      Changes:    ----------           ID: nginx-service     Function: service.running         Name: nginx       Result: None      Comment: Service is set to be started      Started: 14:41:26.286912     Duration: 53.579 ms      Changes:    Summary ------------- Succeeded: 29 (unchanged=17, changed=4) Failed:     0 ------------- Total states run:     29

4)正式部署

1 [root@salt-master base]# salt 'salt-minion01.contoso.com' state.sls nginx.service

5)sls代码

目录结构如下:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 [root@salt-master salt]# tree /srv/salt/base/ /srv/salt/base/ ├── nginx │   ├── files │   │   ├── nginx │   │   ├── nginx-1.6.3.tar.gz │   │   └── nginx.conf │   ├── install.sls │   └── service.sls └── pcre     ├── files     │   └── pcre-8.37.tar.gz     └── install.sls 4 directories, 7 files

pcre install.sls代码:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 [root@salt-master base]# cat pcre/install.sls  pcre-source-install:   pkg.installed:     - names:       - gcc       - gcc-c++       - autoconf       - zlib       - zlib-devel       - glibc       - glibc-devel       make       - cmake   file.managed:     source: salt://pcre/files/pcre-8.37.tar.gz     - name: /opt/tools/pcre-8.37.tar.gz     - user: root     - group: root     - mode: 755     - makedirs: True     - dir_mode: 644   cmd.run:     - name: cd /opt/tools/ && tar -zxf pcre-8.37.tar.gz && cd pcre-8.37 && ./configure --prefix=/usr/local/pcre && make && make install      - unless: test -d /usr/local/pcre     - require:       file: pcre-source-install

nginx install.sls代码:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 [root@salt-master base]# cat nginx/install.sls  include:   - pcre.install nginx-source-install:   pkg.installed:     - names:       - gcc       - gcc-c++       - autoconf       - automake       - zlib       - zlib-devel       make       - openssl       - openssl-devel   group.present:     - name: nginx     - gid: 601   user.present:     - name: nginx     - fullname: nginx     - shell: /sbin/nologin     - uid: 601     - gid: 601   file.managed:     source: salt://nginx/files/nginx-1.6.3.tar.gz     - name: /opt/tools/nginx-1.6.3.tar.gz     - user: root     - group: root     - mode: 755   cmd.run:     - name: cd /opt/tools/ && tar -zxf nginx-1.6.3.tar.gz && mkdir -p /usr/local/nginx/tmp/{client,proxy,fcgi} && cd nginx-1.6.3 && ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_gzip_static_module --http-client-body-temp-path=/usr/local/nginx/tmp/client/ --http-proxy-temp-path=/usr/local/nginx/tmp/proxy/ --http-fastcgi-temp-path=/usr/local/nginx/tmp/fcgi/ --with-poll_module --with-file-aio --with-http_realip_module --with-http_addition_module --with-http_addition_module --with-http_random_index_module --with-http_stub_status_module --http-uwsgi-temp-path=/usr/local/nginx/uwsgi_temp --http-scgi-temp-path=/usr/local/nginx/scgi_temp --with-pcre=/opt/tools/pcre-8.37 && make && make install && chown -R nginx:nginx /usr/local/nginx/     - unless: test -e /usr/local/nginx/sbin/nginx     - require:       file: nginx-source-install       - cmd: pcre-source-install

nginx service.sls代码:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 [root@salt-master base]# cat nginx/service.sls  include:   - nginx.install nginx-init:   file.managed:     - name: /etc/init.d/nginx     source: salt://nginx/files/nginx     - user: root     - group: root     - mode: 755   cmd.run:     - name: chkconfig --add nginx     - unless: chkconfig --list|grep nginx     - require:        file: nginx-init /usr/local/nginx/conf/nginx.conf:   file.managed:     source: salt://nginx/files/nginx.conf     - user: nginx     - group: nginx     - mode: 644 nginx-service:   file.directory:     - name: /usr/local/nginx/conf.d     - require:       - cmd: nginx-source-install   service.running:     - name: nginx     enable: True     - reload: True     - require:       - cmd: nginx-init     watch:       file/usr/local/nginx/conf/nginx.conf

3、部署结果验收

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 [root@salt-minion01 ~]# service nginx status nginx (pid 11406 11405 11404 11403 11400) is running... [root@salt-minion01 ~]# ps -ef|grep nginx root      11494      1  0 14:22 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nginx     11496  11494  0 14:22 ?        00:00:00 nginx: worker process                                           nginx     11497  11494  0 14:22 ?        00:00:00 nginx: worker process                                           nginx     11498  11494  0 14:22 ?        00:00:00 nginx: worker process                                           nginx     11499  11494  0 14:22 ?        00:00:00 nginx: worker process                                           root      11503   1049  0 14:22 pts/0    00:00:00 grep nginx [root@salt-minion01 ~]# chkconfig --list|grep nginx nginx          0:off1:off2:on3:on4:on5:on6:off [root@salt-minion01 ~]# service nginx check Checking config file: nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@salt-minion01 ~]# service nginx reload Reloading nginx: [  OK  ] [root@salt-minion01 ~]# service nginx restart Stopping nginx: [  OK  ] Starting nginx: [  OK  ]


本文转自 jerry1111111 51CTO博客,原文链接:http://blog.51cto.com/jerry12356/1933090,如需转载请自行联系原作者

相关内容