一、安装salt
配置环境:
lucky4 master端
lucky5和lucky6都是minion端
确保firewalld是关闭的
1、配置yum源
三台主机都是需要saltstack的yum源,在此我不过多叙述
[root@lucky4 yum.repos.d]# vim slat.repo
[salt]
name=salt
baseurl=file:///root/2018
gpgcheck=0
2、配置salt的master端和slave端
a.在lucky4上yum install -y salt-master
在lucky5和lucky6 上yum install -y salt-minion
b.在master端修改配置文件
[root@lucky4 ~]# cd /etc/salt/
[root@lucky4 salt]# vim master
674 file_roots:
675 base:
676 - /srv/salt 打开注释
[root@lucky4 salt]# systemctl start salt-master.service
[root@lucky4 salt]# netstat -antlp 查看端口是否打开
4505端口:用来链接slave,发布订阅
4506端口:请求响应,模式为:zmq(消息队列)
3.在minion端修改配置文件
[root@lucky5 ~]# cd /etc/salt/cd
[root@lucky5 salt]# vim minion
16 master: 172.25.66.4# 指向的master端
[root@lucky5 salt]# systemctl start salt-minion.service 打开服务
如果修改ip或者hostname的话,必须删除该文件重新生成
生成一个新的minon_id的文件
这就表示到我们的saltstack的master端和minon端都已经是指定成功的。
4.在master添加minion(master和minion交换公钥的过程)
salt-key -L
salt-key -A
# 添加显示的所有主机
给所有的minon端添加密钥
5.分别在master和minion端查看两者的公钥
master:
[root@lucky4 ~]# cd /etc/salt/pki/master/
[root@lucky4 master]# ls
master.pem master.pub minions minions_autosign minions_denied minions_pre minions_rejected
[root@lucky4 master]# md5sum master.pub
e60d0dc7c570c28dc9637e29d8394ac0 master.pub 生成的加密文件
列出树状关系
[root@lucky4 2018]# yum install -y tree
[root@lucky4 2018]# cd /etc/salt/pki/
[root@lucky4 pki]# tree .
.
├── master
│ ├── master.pem
│ ├── master.pub
│ ├── minions
│ │ ├── lucky5
│ │ └── lucky6
│ ├── minions_autosign
│ ├── minions_denied
│ ├── minions_pre
│ └── minions_rejected
└── minion
7 directories, 4 files
minon端:
[root@lucky5 ~]# cd /etc/salt/pki/minion/
[root@lucky6 minion]# md5sum minion_master.pub
fc7ab466a89d00c3f549892ef4a15ada minion_master.pub 生产的加密文件
[root@lucky6 salt]# cd /etc/salt/pki/minion/
[root@lucky6 minion]# md5sum minion_master.pub
fc7ab466a89d00c3f549892ef4a15ada minion_master.pub 生成的加密文件
发现两个生成的公钥是一样的
6.交换公钥完成之后,在master端可以直接调用python方法查看lucky5中的信息,.安装python-setproctitle,查看python端口情况
[root@lucky5 ~]# yum install -y python-setproctitle
[root@lucky5 ~]# systemctl restart salt-master
测试:
二、配置自动化部署
(一)安装httpd服务
1.修改master端的配置文件
cd /etc/salt/
vim master
file_roots:
base:
- /srv/salt # 这个目录没有,自己建立
[root@lucky4 ~]# mkdir /srv/salt
[root@lucky4 ~]# systemctl restart salt-master.service 重启服务
2.配置自动化安装httpd的脚本
[root@lucky4 salt]# cd /srv/salt/
[root@lucky4 salt]# mkdir apache
[root@lucky4 salt]# cd apache/
[root@lucky4 apache]# vim install.sls
httpd-install:
pkg.installed:
- pkgs:
- httpd
- php
- httpd-tools
service.running:
- name: httpd
- enable: true
- reload: true
[root@lucky4 apache]# salt lucky5 state.sls apache.install
3.指定配置文件并运行服务
[root@lucky4 apache]# vim install.sls
httpd-install:
pkg.installed:
- pkgs:
- httpd
- php
- httpd-tools
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@lucky4 apache]# mkdir files
[root@lucky4 apache]# scp lucky5:/etc/httpd/conf/httpd.conf .
root@lucky5's password:
httpd.conf 100% 11KB 11.5KB/s 00:00
[root@lucky4 apache]# mv httpd.conf files/
[root@lucky4 apache]# cd files/
[root@lucky4 files]# ls
httpd.conf
[root@lucky4 files]# salt lucky5 state.sls apache.install
在从节点上查看是否修改了默认配置文件在从节点上查看是否修改了默认配置文件
4.将安装脚本和运行脚本分离
[root@lucky4 apache]# vim install.sls
[root@lucky4 apache]# cat 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@lucky4 apache]# vim service.sls
include:
- apache.install
httpd-service:
service.runing: 运行前先安装
- name: httpd
- enable: true
- reload: true
- watch:
- file:httpd-install
[root@lucky4 apache]# salt lucky5 state.sls apache.install
到这里可以看到的是先安装在运行
三、配置nginx的自动化部署,基于saltstack实现负载均衡
1.配置安装脚本
[root@lucky4 apache]# cd ..
[root@lucky4 salt]# pwd
/srv/salt
[root@lucky4 salt]# mkdir nginx
[root@lucky4 salt]# cd nginx/
[root@lucky4 nginx]# mkdir files
[root@lucky4 nginx]# cd files/
[root@lucky4 files]# ls
nginx-1.15.8.tar.gz
[root@lucky4 files]# cd ..
[root@lucky4 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
2.配置执行脚本(分离)
[root@lucky4 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@lucky4 nginx]# cd files/
[root@lucky4 files]# vim nginx.service
[root@lucky4 files]# ls
nginx-1.15.8.tar.gz nginx.conf nginx.service nginx.conf 文件可以从别的主机里面进行拷贝
[root@lucky4 files]# salt lucky6 state.sls nginx.service
lucky6:
----------
ID: nginx-install
Function: pkg.installed
Result: True
Comment: 3 targeted packages were installed/updated.
The following packages were already installed: make
Started: 09:53:28.210899
Duration: 14899.923 ms
Changes:
----------
ID: nginx-install
Function: file.managed
Name: /mnt/nginx-1.15.8.tar.gz
Result: True
Comment: File /mnt/nginx-1.15.8.tar.gz updated
Started: 09:53:43.140214
Duration: 271.954 ms
Changes:
----------
ID: /usr/local/nginx/conf/nginx.conf
Function: file.managed
Result: True
Comment: File /usr/local/nginx/conf/nginx.conf is in the correct state
Started: 09:54:02.228497
Duration: 24.306 ms
Changes:
----------
ID: nginx-service
Function: file.managed
Name: /etc/systemd/system/nginx.service
Result: True
Comment: File /etc/systemd/system/nginx.service updated
Started: 09:54:02.253049
Duration: 24.45 ms
Changes:
----------
diff:
New file
mode:
0644
----------
ID: nginx-service
Function: service.running
Name: nginx
Result: True
Comment: Service nginx has been enabled, and is running
Started: 09:54:03.253261
Duration: 163.318 ms
Changes:
----------
nginx:
True
Summary for lucky6
------------
Succeeded: 6 (changed=5)
Failed: 0
------------
Total states run: 6
Total run time: 34.198 s
可以看到nginx服务已经部署完成
5.更改nginx配置文件再运行(因为在运行脚本中写了默认重启服务,所以再次执行脚本就生效啦)
[root@lucky4 files]# vim nginx.conf
worker_processes auto; ##修改文件第三行,根据主机的cpu数目分配worker进程
[root@lucky4 files]# salt lucky6 state.sls nginx.service