1. 实战项目GitHub地址
该项目已经放在了GitHub上,地址如下:
https://github.com/zhanglianghhh/salt-example-lnmp
2. 主机规划
序号 | 操作系统版本 | 内网IP | 外网IP(模拟) | Hostname | 部署模块 |
---|---|---|---|---|---|
------ | ------ | ------ | 10.0.0.100 | ------ | Keepalived虚拟IP地址 |
01 | CentOS7.5 | 172.16.1.111 | 10.0.0.111 | salt01-haproxy | salt-minion、zabbix-agent、haproxy |
02 | CentOS7.5 | 172.16.1.112 | 10.0.0.112 | salt02-haproxy | salt-minion、zabbix-agent、haproxy |
03 | CentOS7.5 | 172.16.1.113 | 10.0.0.113 or「无」 | salt03-web | salt-minion、zabbix-agent、nginx+php、mysql(master) |
04 | CentOS7.5 | 172.16.1.114 | 10.0.0.114 or「无」 | salt04-web | salt-minion、zabbix-agent、nginx+php、mysql(slave) |
05 | CentOS7.5 | 172.16.1.115 | 10.0.0.115 or「无」 | salt05-web | salt-minion、zabbix-agent、nginx+php、mysql(slave)、memcached |
06 | CentOS7.5 | 172.16.1.150 | 10.0.0.150 or「无」 | salt150-master | salt-master、salt-minion、zabbix-server、zabbix-agent、mariadb【zabbix使用】 |
07 | CentOS7.5 | 172.16.1.15 | 10.0.0.15 | GateWay00 | 网关服务器使salt03-web、salt04-web、salt05-web通过网关服务器,实现公网访问目的 |
3. pillar的整体目录结构
[root@salt150-master pillar]# pwd
/srv/pillar
[root@salt150-master pillar]# ll
total 4
drwxr-xr-x 2 root root 62 Mar 10 17:27 base
drwxr-xr-x 2 root root 83 Mar 10 17:39 prod
-rw-r--r-- 1 root root 183 Mar 10 17:42 top.sls
[root@salt150-master pillar]# tree
.
├── base
│ ├── database.sls
│ ├── monitor.sls
│ └── nosql.sls
├── prod
│ ├── high_available.sls
│ ├── program.sls
│ ├── proxy.sls
│ └── web.sls
└── top.sls
2 directories, 8 files
4. Pillar的top.sls 文件信息
[root@salt150-master pillar]# cat top.sls
base:
'*':
# base 目录
- base.database
- base.nosql
- base.monitor
# prod 目录
- prod.web
- prod.program
- prod.proxy
- prod.high_available
5. base基础信息
[root@salt150-master base]# pwd
/srv/pillar/base
[root@salt150-master base]# ll
total 12
-rw-r--r-- 1 root root 258 Mar 10 17:27 database.sls
-rw-r--r-- 1 root root 272 Mar 10 17:20 monitor.sls
-rw-r--r-- 1 root root 99 Mar 10 17:25 nosql.sls
[root@salt150-master base]# cat database.sls
# 数据库信息
# 包含配置文件信息
# 注意和 zabbix pillar 不同 的写法
{% if grains['host'] == 'salt03-web' %}
database_info:
database: mysql
role: master
server-id: 1
read_only: "OFF"
{% elif grains['host'] == 'salt04-web' %}
database_info:
database: mysql
role: slave
server-id: 2
read_only: "ON"
{% elif grains['host'] == 'salt05-web' %}
database_info:
database: mysql
role: slave
server-id: 3
read_only: "ON"
{% elif grains['host'] == 'salt150-master' %}
database_info:
database: mariadb
read_only: "OFF"
{% endif %}
[root@salt150-master base]# cat nosql.sls
# nosql 信息
{% if grains['host'] == 'salt05-web' %}
nosql_info:
nosql: memcached
{% endif %}
[root@salt150-master base]# cat monitor.sls
# 其中 salt150-maste 172.16.1.150 机器,不仅是 zabbix 客户端,还是服务端
# 只包含 zabbix 的属性 【服务端 客户端】
monitor_info:
monitor:
{% if grains['host'] == 'salt150-master' %}
zabbix_server: True
{% endif %}
zabbix_agent: True
zabbix_server_hostname: 'salt150-master'
6. prod业务组件信息
[root@salt150-master prod]# pwd
/srv/pillar/prod
[root@salt150-master prod]# ll
total 16
-rw-r--r-- 1 root root 326 Mar 10 17:39 high_available.sls
-rw-r--r-- 1 root root 174 Mar 10 17:34 program.sls
-rw-r--r-- 1 root root 148 Mar 10 17:38 proxy.sls
-rw-r--r-- 1 root root 160 Mar 10 17:33 web.sls
[root@salt150-master prod]# cat web.sls
# web 服务器
{% if grains['host'] == 'salt03-web' or grains['host'] == 'salt04-web' or grains['host'] == 'salt05-web' %}
web_info:
web: nginx
{% endif %}
[root@salt150-master prod]# cat program.sls
# 使用的编程语言
{% if grains['host'] == 'salt03-web' or grains['host'] == 'salt04-web' or grains['host'] == 'salt05-web' %}
program_info:
program: php
{% endif %}
[root@salt150-master prod]# cat proxy.sls
# 负载均衡服务器
{% if grains['host'] == 'salt01-haproxy' or grains['host'] == 'salt02-haproxy' %}
proxy_info:
proxy: haproxy
{% endif %}
[root@salt150-master prod]# cat high_available.sls
# 高可用keepalived 的 pillar 信息
{% if grains['host'] == 'salt01-haproxy' %}
high_available_info:
high_available: keepalived
high_state: MASTER
high_priority: 100
{% elif grains['host'] == 'salt02-haproxy' %}
high_available_info:
high_available: keepalived
high_state: BACKUP
high_priority: 50
{% endif %}
7. pillar信息生效
[root@salt150-master pillar]# salt '*' saltutil.refresh_pillar
salt02-haproxy:
True
salt01-haproxy:
True
salt05-web:
True
salt03-web:
True
salt150-master:
True
salt04-web:
True
8. pillar信息查看
[root@salt150-master pillar]# salt '*' pillar.items
salt05-web:
----------
database_info:
----------
database:
mysql
read_only:
ON
role:
slave
server-id:
3
monitor_info:
----------
monitor:
----------
zabbix_agent:
True
zabbix_server_hostname:
salt150-master
nosql_info:
----------
nosql:
memcached
program_info:
----------
program:
php
web_info:
----------
web:
nginx
salt02-haproxy:
----------
high_available_info:
----------
high_available:
keepalived
high_priority:
50
high_state:
BACKUP
monitor_info:
----------
monitor:
----------
zabbix_agent:
True
zabbix_server_hostname:
salt150-master
proxy_info:
----------
proxy:
haproxy
salt03-web:
----------
database_info:
----------
database:
mysql
read_only:
OFF
role:
master
server-id:
1
monitor_info:
----------
monitor:
----------
zabbix_agent:
True
zabbix_server_hostname:
salt150-master
program_info:
----------
program:
php
web_info:
----------
web:
nginx
salt04-web:
----------
database_info:
----------
database:
mysql
read_only:
ON
role:
slave
server-id:
2
monitor_info:
----------
monitor:
----------
zabbix_agent:
True
zabbix_server_hostname:
salt150-master
program_info:
----------
program:
php
web_info:
----------
web:
nginx
salt01-haproxy:
----------
high_available_info:
----------
high_available:
keepalived
high_priority:
100
high_state:
MASTER
monitor_info:
----------
monitor:
----------
zabbix_agent:
True
zabbix_server_hostname:
salt150-master
proxy_info:
----------
proxy:
haproxy
salt150-master:
----------
database_info:
----------
database:
mariadb
read_only:
OFF
monitor_info:
----------
monitor:
----------
zabbix_agent:
True
zabbix_server:
True
zabbix_server_hostname:
salt150-master
9. 说明
其他的就不单独写文章了,都是之前讲解过的。
该实战项目中有些细节补充,结合之前所学,即可达到整体实战目的。项目实战代码请参见开头的GitHub地址。
如果阅读后有些地方不清楚,可以结合之前的《Saltstack_使用指南》进行参考。