注意:编写配置要顶头写,一级一级之间空两格
一、安装apache
1、
[root@server1 ~]# cd /etc/salt/
[root@server1 salt]# ls
cloud cloud.maps.d master minion.d proxy.d
cloud.conf.d cloud.profiles.d master.d pki roster
cloud.deploy.d cloud.providers.d minion proxy
[root@server1 salt]# vim master
[root@server1 salt]# mkdir /srv/salt
[root@server1 salt]# systemctl restart salt-master
创建角色,编写sls
[root@server1 salt]# cd /srv/salt/
[root@server1 salt]# pwd
/srv/salt
[root@server1 salt]# mkdir apache
[root@server1 salt]# cd apache/
[root@server1 apache]# vim install.sls
[root@server1 apache]# cat install.sls
httpd-install:
pkg.installed: #下载
- pkgs:
- httpd
- php
- httpd-tools
service.running: ## 运行
- name: httpd
- enable: true
- reload: true
[root@server1 apache]# salt server2 state.sls apache.install ##apache执行install.sls(sls可以省略)
此时查看sevrer2,已经安装httpd并且可以运行。
[root@server2 minion]# systemctl is-enabled httpd
enabled
添加监控文件
[root@server1 apache]# vim install.sls
httpd-install:
pkg.installed:
- pkgs:
- httpd
- php
- httpd-tools
service.running:
- name: httpd
- enable: true
- reload: True
- watch:
- file: /etc/httpd/conf/httpd.conf
/etc/httpd/conf/httpd.conf:
file.managed:
- source: salt://apache/files/httpd.conf
- user: root
- group: root
- mode: 644
[root@server1 apache]# mkdir files
[root@server1 apache]# cd files/
[root@server1 files]# scp server2:/etc/httpd/conf/http.conf .
[root@server1 apache]# salt server2 state.sls apache.install
在servre2上查看
[root@server2 minion]# yum install tree -y
[root@server2 minion]# cd /var/cache/salt/minion/
[root@server2 minion]# ls
[root@server2 minion]# tree .
.
|-- accumulator
|-- extmods
|-- files
| `-- base
| `-- apache
| |-- files
| | `-- httpd.conf
| `-- install.sls
|-- highstate.cache.p
|-- pkg_refresh
|-- proc
`-- sls.p
7 directories, 5 files
将安装和服务的启动分开
[root@server1 apache]# vim install.sls
httpd-install:
pkg.installed:
- pkgs:
- httpd
- php
- httpd-tools
file.managed:
- name: /etc/httpd/conf/httpd.conf
- source: salt://apache/files/httpd.conf
- user: root
- group: root
- mode: 644
[root@server1 apache]# vim service.sls
include:
- apache.install
httpd-service:
service.running:
- name: httpd
- enable: true
- reload: True
- watch:
- file: httpd-install
测试:
关闭server2上的httpd服务
[root@server2 minion]# systemctl stop httpd
执行开启服务的文件
[root@server1 apache]# salt server2 state.sls apache.service
查看server2上的httpd的状态:
[root@server2 minion]# systemctl status httpd
二、安装配置nginx
[root@server1 salt]# pwd
/srv/salt
[root@server1 salt]# mkdir nginx
[root@server1 salt]# cd nginx/
[root@server1 nginx]# mkdir files
[root@server1 nginx]# cd files/
[root@server1 files]# ls
nginx-1.15.8.tar.gz
[root@server1 nginx]# vim install.sls
nginx-install:
pkg.installed:
- pkgs:
- gcc
- make
- pcre-devel
- zlib-devel
file.managed:
- name: /mnt/nginx-1.15.8.tar.gz
- source: salt://nginx/files/nginx-1.15.8.tar.gz
cmd.run:
- name: cd /mnt && tar zxf nginx-1.15.8.tar.gz && cd nginx-1.15.8 && sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --prefix=/usr/local/nginx &> /dev/null && make &> /dev/null && make install &> /dev/null
- creates: /usr/local/nginx
#执行,server3上安装nginx
[root@server1 nginx]# salt server3 state.sls nginx.install
在server3上查看
[root@server3 ~]# cd /mnt/nginx-1.15.8
[root@server3 nginx-1.15.8]# du -sh /usr/local/nginx/
880K /usr/local/nginx/
#启动脚本设置
root@server1 nginx]# vim files/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@server1 nginx]# vim service.sls
include:
- nginx.install
/usr/local/nginx/conf/nginx.conf:
file.managed:
- source: salt://nginx/files/nginx.conf
nginx-service:
file.managed:
- name: /etc/systemd/system/nginx.service
- source: salt://nginx/files/nginx.service
service.running:
- name: nginx
- enable: true
- reload: true
- watch:
- file: /usr/local/nginx/conf/nginx.conf
#执行nginx服务脚本
[root@server1 nginx]# salt server3 state.sls nginx.service
#server3上查看nginx的状态
[root@server3 nginx-1.15.8]# systemctl status nginx
[root@server3 ~]# curl localhost
[root@server1 files]# pwd
/srv/salt/nginx/files
[root@server1 files]# vim nginx.conf
worker_processes auto ##根据cpu核数自动创建响应worker_processes个数
[root@server1 nginx]# salt server3 state.sls nginx.service
如果想要一次性就给指定的主机执行指定配置,可以写一个top文件。执行这个文件,就可以自动安装了
[root@server1 salt]# vim top.sls
[root@server1 salt]# cat top.sls
base:
'server2':
- apache.service
'server3':
- nginx.service
[root@server1 salt]# ls
apache nginx top.sls
[root@server1 salt]# salt '*' state.highstate ##apache和nginx必须要有service(没有就用自己有的)