场景:rpm包安装的nginx服务,无法满足定制模块的需求,故线上环境使用nginx源码进行安装。本片文章详细介绍如何使用saltstack的配置管理功能实现nginx软件的源码安装。
下载源码:pcre
http://exim.mirror.fr/pcre/pcre-8.38.tar.gz
下载源码:nginx
http://nginx.org/download/nginx-1.9.12.tar.gz
1.修改master配置文件,指定环境为prod。
[root@node1 prod]# egrep -A 6 ^file_roots /etc/salt/master file_roots: base: - /srv/salt/base dev: - /srv/salt/dev prod: - /srv/salt/prod
2.编写state文件,最终的目录树如下所示:
[root@node1 prod]# tree . ├── initpkg │ └── install.sls ├── nginx │ ├── files │ │ ├── nginx │ │ └── nginx-1.9.12.tar.gz │ └── install.sls ├── pcre │ ├── files │ │ └── pcre-8.38.tar.gz │ └── install.sls └── top.sls 5 directories, 7 files
3.state文件的详细内容如下:
[root@node1 prod]# cat initpkg/install.sls init-pkg-install: pkg.installed: - names: - gcc - gcc-c++ - make - autoconf - openssl - openssl-devel - lsof - tree - lrzsz [root@node1 prod]# cat pcre/install.sls include: - initpkg.install pcre-source-install: file.managed: - name: /usr/local/src/pcre-8.38.tar.gz - source: salt://pcre/files/pcre-8.38.tar.gz - user: root - group: root - mode: 755 cmd.run: - name: cd /usr/local/src && tar xf pcre-8.38.tar.gz && cd pcre-8.38 && ./configure --prefix=/usr/local/pcre && make && make install - unless: test -d /usr/local/pcre - reuqire: - file: pcre-source-install [root@node1 prod]# cat nginx/install.sls include: - initpkg.install - pcre.install nginx-source-install: file.managed: - name: /usr/local/src/nginx-1.9.12.tar.gz - source: salt://nginx/files/nginx-1.9.12.tar.gz - user: root - group: root - mode: 755 cmd.run: - name: cd /usr/local/src && tar xf nginx-1.9.12.tar.gz && cd nginx-1.9.12 && ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-file-aio --with-http_dav_module --with-pcre=/usr/local/src/pcre-8.38 && make && make install - unless: test -d /usr/local/nginx - require: - file: nginx-source-install - pkg: init-pkg-install - file: pcre-source-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 - reuqire: - cmd: nginx-source-install - file: nginx-init nginx-service: service.running: - name: nginx - enable: nginx - reload: true - require: - cmd: nginx-init [root@node1 prod]# cat top.sls prod: 'node2.xkops.com': - nginx.install - pcre.install - initpkg.install
4.执行state模块在node2.xkopa.com客户端源码安装nginx。
[root@node1 prod]# salt '*' state.highstate env=prod test
[root@node1 prod]# salt '*' state.highstate env=prod
5.在node2检查客户端是否完成nginx安装
[root@node2 ~]# service nginx status
提示:此处列出的为state文件的编写内容,记得查看一下tree的结果,在pcre和nginx目录下还各自有files目录,存放着pcre-8.38.tar.gz、nginx-1.9.12.tar.gz源码包以及nginx启动服务脚本文件。
附录:
nginx启动脚本文件内容。
[root@node1 prod]# cat nginx/files/nginx #!/bin/sh # # nginx - this script starts and stops the nginx daemin # # chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse # proxy and IMAP/POP3 proxy server # processname: nginx # config: /usr/local/nginx/conf/nginx.conf # pidfile: /usr/local/nginx/logs/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/local/nginx/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" lockfile=/var/lock/subsys/nginx start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { configtest || return $? stop start } reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo } force_reload() { restart } configtest() { $nginx -t -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2 esac