nginx实例安装:
在salt的默认目录/src/salt建立一个目录nginx,建立init.sls
install-nginx:
pkg.installed:
- pkgs: #安装nigx所依赖的包
- gcc
- make
- openssl-devel
- pcre-devel
archive.extracted: #解压
- name: /mnt
- source: salt://nginx/
- archive_format: tar
解压过去测试一下:
[root@node1 nginx]# salt node3 state.sls nginx
启动服务:
[root@node1 nginx]# vim nginx.service
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.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
[root@node1 nginx]# ls
init.sls nginx-1.16.1.tar.gz nginx.conf nginx.service service.sls
[root@node1 nginx]# vim init.sls
install-nginx:
pkg.installed:
- pkgs:
- gcc
- make
- openssl-devel
- pcre-devel
archive.extracted:
- name: /mnt
- source: salt://nginx/nginx-1.16.1.tar.gz
- archive_format: tar
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 && 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
在node1上更改端口,使node3上生效
再编写一个文件service.sls。在nginx目录里有init.sls.和servicr.sls.默认执行init.sls,如何两个都执行
第一种方法:top.sls
[root@node1 salt]# pwd
/srv/salt
[root@node1 salt]# ls
apache _modules nginx top.sls
[root@node1 salt]# vim top.sls
base:
"node2":
- apache
"node3":
- nginx.init
- nginx.service
[root@node1 salt]# cd nginx/
[root@node1 nginx]# ls
init.sls nginx-1.16.1.tar.gz nginx.conf nginx.service service.sls
[root@node1 nginx]# vim init.sls
install-nginx:
pkg.installed:
- pkgs:
- gcc
- make
- openssl-devel
- pcre-devel
archive.extracted:
- name: /mnt
- source: salt://nginx/nginx-1.16.1.tar.gz
- archive_format: tar
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 && make install &> /dev/null
- creates: /usr/local/nginx
file.managed:
- name: /lib/systemd/system/nginx.service
- source: salt://nginx/nginx.service
[root@node1 nginx]# vim service.sls
service-nginx:
file.managed:
- source: salt://nginx/nginx.conf
- name: /usr/local/nginx/conf/nginx.conf
service.running:
- name: nginx
- watch:
- file: service-nginx
[root@node1 salt]# salt node3 state.highstate #先执行init.sls,再执行service.sls
第二种:include #在service.sls里加init.sls。nginx默认就是nginx.init
[root@node1 nginx]# vim service.sls
include:
- nginx
service-nginx:
file.managed:
- source: salt://nginx/nginx.conf
- name: /usr/local/nginx/conf/nginx.conf
service.running:
- name: nginx
- watch:
- file: service-nginx
[root@node1 nginx]# salt node3 state.sls nginx.service #nginx默认就是nginx.init,在此时我们是service.sls包括了init,所以执行的时候要加nginx.service
第三种:init.sls包括service.sls
[root@node1 nginx]# vim service.sls
/usr/local/nginx/conf/nginx.conf: #既当ID声明又当file.name。一个sls文件里可以有多个声明
file.managed:
- source: salt://nginx/nginx.conf
- require:
- sls: nginx
service.running:
- name: nginx
- watch:
- file: /usr/local/nginx/conf/nginx.conf
[root@node1 nginx]# vim init.sls
install-nginx:
pkg.installed:
- pkgs:
- gcc
- make
- openssl-devel
- pcre-devel
archive.extracted:
- name: /mnt
- source: salt://nginx/nginx-1.16.1.tar.gz
- archive_format: tar
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 && make install &> /dev/null
- creates: /usr/local/nginx
file.managed:
- name: /lib/systemd/system/nginx.service
- source: salt://nginx/nginx.service
include:
- nginx.service
[root@node1 nginx]# salt node3 state.sls nginx
接第三种方法:第三种方法的sevice也可以这样写,一个sls里可以有多个ID声明:
[root@node1 nginx]# vim service.sls
[root@node1 nginx]# pwd
/srv/salt/nginx
configure:
file.managed:
- source: salt://nginx/nginx.conf
- name: /usr/local/nginx/conf/nginx.conf
- require:
- sls: nginx
qidong:
service.running:
- name: nginx
- watch:
- file: /usr/local/nginx/conf/nginx.conf
[root@node1 nginx]# salt node3 state.sls nginx