一、Saltstack搭建高可用集群
1. 在上一篇博客的基础上继续进行,首先将apache/files目录下的模板文件的端口进行修改
vim /srv/salt/apache/files/httpd.conf
Listen 80 #不使用变量的方法设置端口
2. 编写keepalived安装文件
mkdir /srv/salt/keepalived
cd keepalived/
cp ../apache/install.sls .
vim install.sls
install-keepalived:
pkg.installed:
- pkgs:
- keepalived
file.managed:
- name: /etc/keepalived/keepalived.conf
- source: salt://keepalived/files/keepalived.conf
- template: jinja
- context:
STATE: {{ pillar['state'] }}
VRID: {{ pillar['vrid'] }}
PRIORITY: {{ pillar['priority'] }}
service.running:
- name: keepalived
- watch:
- file: install-keepalived
3. 创建files目录,拷贝keepalived.conf模板文件到该目录
yum install -y keepalived #提供keepalived.conf文件
mkdir keepalived/files
cd files
cp /etc/keepalived/keepalived.conf .
vim keepalived.conf
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state {{ STATE }}
interface eth0
virtual_router_id {{ VRID }}
priority {{ PRIORITY }}
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.5.100
}
}
4. 在pillar/web目录下,设置变量的取值
cd /srv/pillar/
vim web/vars.sls
{% if grains['fqdn'] == 'server2' %}
state: MASTER
vrid: 5
priority: 100
{% elif grains['fqdn'] == 'server3' %}
state: BACKUP
vrid: 5
priority: 100
{% endif %}
5. 设置主推送文件top.sls
cd /srv/salt/
vim top.sls
base:
'roles:apache':
- match: grain
- apache.install
- keepalived.install
'roles:nginx':
- match: grain
- nginx.service
- keepalived.install
6. 向server2和server3推送keepalived
salt '*' state.highstate
7. 查看高可用集群是否搭建完毕
server2为master,在server2中查看虚拟ip
server2中编写httpd默认发布文件
echo hello westos > /var/www/html/index.html
浏览器中访问http://172.25.5.100,默认访问的页面是server2中的内容
8. 测试高可用
systemctl stop keepalived
将server2中的keepalived关闭,我们会发现虚拟ip漂移到server3中,且访问172.25.5.100时,显示的页面内容是server3中nginx的欢迎页面
虚拟ip从server2漂移到server3
再次开启server2中的keepalived服务,由于优先级的原因,虚拟ip会回到server2中
二、saltstack缓存测试返回值到数据库
1. 在server1中安装mariadb,用来存测试返回值
yum install -y mariadb-server
yum install -y MySQL-python
systemctl start mariadb
mysql_secure_installation #初始化数据库
2. 导入salt相关数据到数据库中,创建用户并授权
vim test.sql
mysql -p < test.sql
mysql -uroot -p
grant all on salt.* to salt@'localhost' identified by 'salt';
3. 在/etc/salt/master配置文件中设置数据库相关参数
vim /etc/salt/master
master_job_cache: mysql
mysql.host: 'localhost'
mysql.user: 'salt'
mysql.pass: 'salt'
mysql.db: 'salt'
mysql.port: 3306
systemctl restart salt-master
4. 测试是否实现自动缓存
salt-key -L
salt server2 test.ping
mysql -uroot -p
use salt;
show tables;
select * from salt_returns; #查看salt缓存