SaltStack自动化部署httpd和nginx

前言

工作机制:

  • Master和Minion都以守护进程的方式运行
  • Master监听配置文件里定义的ret_port(接收minion请求),和publish_port(发布消息)的端口
  • 当Minion运行时,它会自动连接到配置文件里定义的Master地址ret_port端口进行连接认证
  • 当Master和Minion可以正常通信后,就可以进行各种各样的配置管理工作了

一、saltstack的安装部署

172.25.1.4 server4 master
172.25.1.5 server5 minion
172.25.1.6 server6 minion

1.配置yum源,在各节点安装软件

SaltStack自动化部署httpd和nginx_第1张图片
SaltStack自动化部署httpd和nginx_第2张图片
server4安装salt-master

SaltStack自动化部署httpd和nginx_第3张图片

在两个从节点安装salt-minion

SaltStack自动化部署httpd和nginx_第4张图片

在两个从节点

[root@server5 ~]# cd /etc/salt
[root@server5 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@server5 salt]# vim minion
 15 # resolved, then the minion will fail to start.
 16 master: 172.25.1.4
[root@server5 salt]# systemctl start salt-minion
[root@server5 salt]# systemctl enable salt-minion
Created symlink from /etc/systemd/system/multi-user.target.wants/salt-minion.service to /usr/lib/systemd/system/salt-minion.service.

[root@server6 ~]# cd /etc/salt
[root@server6 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@server6 salt]# vim minion
 15 # resolved, then the minion will fail to start.
 16 master: 172.25.1.4
[root@server6 salt]# systemctl start salt-minion
[root@server6 salt]# systemctl enable salt-minion
Created symlink from /etc/systemd/system/multi-user.target.wants/salt-minion.service to /usr/lib/systemd/system/salt-minion.service.

SaltStack自动化部署httpd和nginx_第5张图片

SaltStack自动化部署httpd和nginx_第6张图片
2.添加连接密钥(查看密钥并加入)/在master添加minion
SaltStack自动化部署httpd和nginx_第7张图片
测试:

SaltStack自动化部署httpd和nginx_第8张图片
二、通过saltstack自动化部署httpd
1.修改master端的配置文件

file_roots:
  base:
    - /srv/salt/     ##这个目录没有需要自己建立

2。安装appache

[root@server4 ~]# mkdir /srv/salt
[root@server4 ~]# cd /srv/salt
[root@server4 salt]# mkdir apache
[root@server4 salt]# 
[root@server4 salt]# cd apache/
[root@server4 apache]# vim install.sls
httpd-install:
  pkg.installed:
    - pkgs:
      - httpd

  service.running:
    - name: httpd
    - enable: true
    - reload: true

[root@server4 apache]# salt server5 state.sls apache.install
SaltStack自动化部署httpd和nginx_第9张图片

SaltStack自动化部署httpd和nginx_第10张图片
3。指定配置文件并运行

httpd-install:
  pkg.installed:
    - pkgs:
      - httpd

  service.running:
    - name: httpd
    - enable: true
    - reload: true
  file.managed:
    - name: /etc/httpd/conf/httpd.conf 
    - source: salt://apache/files/httpd.conf 
    - user: root 
    - group: root 
    - mode: 644

SaltStack自动化部署httpd和nginx_第11张图片
[root@server4 files]# salt server5 state.sls apache.installSaltStack自动化部署httpd和nginx_第12张图片

SaltStack自动化部署httpd和nginx_第13张图片
4.将安装脚本和运行脚本分离

[root@server4 apache]# cat install.sls 
httpd-install:
  pkg.installed:
    - pkgs:
      - httpd

  file.managed: 
    - name: /etc/httpd/conf/httpd.conf 
    - source: salt://apache/files/httpd.conf 
    - user: root 
    - group: root 
    - mode: 644

[root@server4 apache]# vim service.sls 
[root@server4 apache]# cat service.sls 
include:       ##运行时先执行安装脚本
  - apache.install  

httpd-service:
  service.running:
    - name: httpd
    - enable: true
    - reload: true
    - watch:
      - file: httpd-install

[root@server4 apache]# salt server5 state.sls apache.service

SaltStack自动化部署httpd和nginx_第14张图片

SaltStack自动化部署httpd和nginx_第15张图片

三、配置nginx的自动化部署

1.配置安装脚本

[root@server4 salt]# mkdir nginx
[root@server4 salt]# cd nginx/
[root@server4 nginx]# ls
[root@server4 nginx]# mkdir files
[root@server4 nginx]# cd files/
[root@server4 files]# ls
nginx-1.15.8.tar.gz

[root@server4 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=“KaTeX parse error: Expected 'EOF', got '#' at position 12: 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
2.配置执行脚本

[root@server4 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

3.files中添加所要的组件
[root@server4 files]# ls
nginx-1.15.8.tar.gz nginx.conf nginx.service
[root@server4 files]# cat 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
4.执行脚本
[root@server4 nginx]# salt server6 state.sls nginx.service
SaltStack自动化部署httpd和nginx_第16张图片

SaltStack自动化部署httpd和nginx_第17张图片

5.测试
SaltStack自动化部署httpd和nginx_第18张图片

nginx部署成功
五、指定节点部署

1.编写sls文件,指定节点安装指定服务
[root@server4 ~]# cd /srv/salt
[root@server4 salt]# pwd
/srv/salt
[root@server4 salt]# vim top.sls
[root@server4 salt]# cat top.sls
base:
‘server5’:
- apache.service
‘server6’:
- nginx.service
2.运行sls文件,查看是否可以生效
[root@server4 salt]# salt ‘*’ state.highstate

SaltStack自动化部署httpd和nginx_第19张图片

SaltStack自动化部署httpd和nginx_第20张图片

你可能感兴趣的:(SaltStack自动化部署httpd和nginx)