前言
工作机制:
172.25.1.4 server4 master
172.25.1.5 server5 minion
172.25.1.6 server6 minion
1.配置yum源,在各节点安装软件
在两个从节点安装salt-minion
在两个从节点
[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.
2.添加连接密钥(查看密钥并加入)/在master添加minion
测试:
二、通过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
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
[root@server4 files]# salt server5 state.sls apache.install
[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
三、配置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:
/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
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