自动化运维——saltstack自动化部署apache服务和nginx服务

1. minion端安装部署apache

1.master端配置
1.)创建目录

[root@server1 salt]# mkdir /srv/salt	

2.)进入该创建apache目录

[root@server1 salt]# cd /srv/salt/
[root@server1 salt]# mkdir apache

3.)在apache目录,建立install.sls

[root@server1 salt]# cd apache/
[root@server1 apache]# vim install.sls
[root@server1 apache]# cat install.sls 
httpd:
  pkg.installed

4.)推送:server2安装apache

[root@server1 apache]# salt server2 state.sls apache.install  ##server2安装apache

5.)推送:server2安装httpd,php,httpd-tools

[root@server1 apache]# vim install.sls 
httpd:
  pkg.installed:
    - pkgs:
      - httpd
      - php
      - httpd-tools
[root@server1 apache]# salt server2 state.sls apache.install

自动化运维——saltstack自动化部署apache服务和nginx服务_第1张图片

7.)推送server2安装apache服务并设置开机启动

[root@server1 apache]# vim install.sls 
httpd:
  pkg.installed:
    - pkgs:
      - httpd
      - php
      - httpd-tools
  service.running:
    - name: httpd
    - enable: true
    - reload: true
[root@server1 apache]# salt server2 state.sls apache.install

自动化运维——saltstack自动化部署apache服务和nginx服务_第2张图片

8.)推送:server2 apache修改完配置文件后重启动apache服务

[root@server1 apache]# mkdir files
[root@server1 apache]# cd files/
[root@server1 files]# scp server2:/etc/httpd/conf/httpd.conf .
root@server2's password: 
httpd.conf                                                   100%   11KB  11.5KB/s   00:00    
[root@server1 files]# ls
httpd.conf
[root@server1 apache]# vim install.sls 
httpd:
  pkg.installed:
    - pkgs:
      - httpd
      - php
      - httpd-tools
  service.running:
    - name: httpd
    - enable: true
    - reload: true
    - watch:
      - file: /etc/httpd/conf/http.conf

/etc/httpd/conf/http.conf:
  file.managed:
    - source: salt://apache/files/httpd.conf
    - user: root
    - group: root
    - mode: 644
[root@server1 apache]# salt server2 state.sls apache.install

9.)server2查看配置

[root@server2 minion]# yum install -y tree
[root@server2 ~]# cd /var/cache/salt/minion/  ##master端所推送的内容保存在该目录
[root@server2 minion]# ls
accumulator  extmods  files  highstate.cache.p  pkg_refresh  proc  sls.p
[root@server2 minion]# tree .

自动化运维——saltstack自动化部署apache服务和nginx服务_第3张图片

10.)也可以将apache服务安装和配置推送分开

[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
[root@server1 apache]# salt server2 state.sls apache.service

2. minion端安装部署nginx

1.在nginx目录下创建files目录

[root@server1 nginx]# mkdir files

2.nginx-1.15.8.tar.gz源码包放到该目录下

[root@server1 nginx]# cd files/
[root@server1 files]# pwd
/srv/salt/nginx/files
[root@server1 files]# ls  ##nginx-1.15.8.tar.gz源码包放到该目录下
nginx-1.15.8.tar.gz

3.推送server3源码安装nginx

[root@server1 salt]# cd nginx/
[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
[root@server1 nginx]# salt server3 state.sls nginx.install

4.将nginx.conf配置文件放到master端/srv/salt/nginx/files目录

[root@server3 ~]# ll /usr/local/nginx/conf/nginx.conf
-rw-r--r-- 1 root root 2656 Jun 11 17:16 /usr/local/nginx/conf/nginx.conf
[root@server3 ~]# scp /usr/local/nginx/conf/nginx.conf server1:/srv/salt/nginx/files
root@server1's password: 
nginx.conf                                    100% 2656     2.6KB/s   00:00 

5.master端在/srv/salt/nginx/files将配置nginx.service文件

[root@server1 nginx]# cd files/
[root@server1 files]# 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

6.推送nginx服务配置

[root@server1 files]# cd ..
[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: /usr/lib/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
[root@server1 nginx]# salt server3 state.sls nginx.service

在server3查看nginx部署情况

[root@server3 system]# ps ax

自动化运维——saltstack自动化部署apache服务和nginx服务_第4张图片

3. 高级方式部署

[root@server1 salt]# vim top.sls
base:
  'server2':
   - apache.service
  'server3':
   - nginx.service
[root@server1 salt]# salt '*' state.highstate

你可能感兴趣的:(saltstack)