使用saltstack完成这个架构图:
配置思路
(1).系统初始化
Base环境下存放所有系统都要执行的状态,调整内核参数,dns,装zabbix-agent等
(2).功能模块(如:上面的haproxy)
如上面的haproxy nginx php memcached等服务,每一个服务都建一个目录,把每一个服务要执行的状态都放在这个目录下.
(3).业务模块
以业务为单位,一个业务里可能包含haproxy,nginx,php等,业务需要什么服务就把功能模块里对应的服务include
1.编辑配置文件修改file_roots,并且建立对应目录
[[email protected] ~]# vim /etc/salt/master
file_roots:
base:
- /srv/salt/base
test:
- /srv/salt/test
prod:
- /srv/salt/prod
[[email protected] ~]# /etc/init.d/salt-master restart
注:top.sls必须放在base环境下
[[email protected] ~]# mkdir /srv/salt/test 测试环境目录
[[email protected] ~]# mkdir /srv/salt/base 基础环境目录
[[email protected] ~]# mkdir /srv/salt/prod 生产环境目录
2.系统初始化模块
[[email protected] ~]# mkdir /srv/salt/base/init/ 创建一个系统初始化的目录
[[email protected] ~]# mkdir /srv/salt/base/init/files/ 创建一个文件目录,存放一些初始化需要的文件
[[email protected] ~]# cd /srv/salt/base/
[[email protected] base]# tree
├── init
│ ├── files
└── top.sls
[[email protected] base]# cd init/
(1).配置dns
[[email protected] init]# cat dns.sls
/etc/resolv.conf: #这里是指定name,这里没有指定ID
file.managed: #文件管理方法
- source: salt://init/files/resolv.conf #这个路径式相当与配置文件中/srv/salt/base/
- user: root
- group: root
- mode : 644
[[email protected] init]# cat files/resolv.conf
nameserver 192.168.10.1
(2)历史命令显示时间
[[email protected] init]# cat history.sls
/etc/profile:
file.append: #文件追加的方法
- text:
- export HISTTIMEFORMAT="%F %T `whoami`" #执行之后该语句会被追加到/etc/profile
(3).log日志记录谁在什么时间使用了什么命令
[[email protected] init]# cat audit.sls
/etc/bashrc:
file.append:
- text:
- export PROMPT_COMMAND=‘{ msg=$(history 1|{ read x y;echo $y; });logger "[euid=$(whoami)]":$(who am i):[`pwd`]"$msg";}‘
(4)内核调优
[[email protected] init]# cat sysctl.sls
vm.swappiness:
sysctl.present:
- value: 0
net.ipv4.ip_local_port_range:
sysctl.present:
- value: 10000 65000
fs.file-max:
sysctl.present:
- value: 100000
注:上面的路径
/proc/sys/net/ipv4/ip_local_port_range #监听端口
/proc/sys/fs/file-max #打开最大文件数
/proc/sys/vm/swappiness #交换分区
(5)将上面的状态include到 env_init.sls
[[email protected] init]# cat env_init.sls
include:
- init.dns
- init.history
- init.audit
- init.sysctl
(6).编写top.sls执行以上状态
[[email protected] init]# vim /srv/salt/base/top.sls
[[email protected] init]# cat /srv/salt/base/top.sls
base:
‘*‘:
- init.env_init #这里只需要执行init目录下的env_init.sls即可,
(7)注:以上环境中用到的一些命令
export HISTTIMEFORMAT="%F %T `whoami`" #该命令是将%F %T `whoami`命令执行的结果赋给变量HISTTIMEFORMAT,用export将其变成环境变量
export PROMPT_COMMAND=‘{ msg=$(history 1|{ read x y;echo $y; });logger "[euid=$(whoami)]":$(who am i):[`pwd`]"$msg";}‘
执行该命令之后会在log日志里记录用户使用命令的情况,如:
[[email protected] base]# uptime
05:17:38 up 4:08, 4 users, load average: 0.00, 0.00, 0.00
[[email protected] base]# tail -1 /var/log/messages
Aug 11 05:17:38 node1 root: [euid=root]:root pts/3 2017-08-11 04:07 (192.168.10.1):[/srv/salt/base]uptime
[[email protected] init]# salt "*" state.highstate test=True #这里可以测试一下前面所配置的是否成功
3.功能模块-------基础包模块
[[email protected] ~]# mkdir /srv/salt/prod/pkg #基础包目录
[[email protected] ~]# cd /srv/salt/prod/pkg/
[[email protected] pkg]# vim pkg-init.sls #安装一些基础的包
[[email protected] pkg]# cat pkg-init.sls
pkg-init: #这里就是ID号,唯一性
pkg.installed: #安装包的模块.方法
- names: #安装包的名字
- gcc
- gcc-c++
- glibc
- make
- autoconf
- openssl
- openssl-devel
4.功能模块--------haproxy模块
#安装haproxy说明:该模块采用编译安装,用模块安装之前先用一台主机测试一下
#cd /usr/local/src/
#tar xf haproxy-1.6.2.tar.gz
#cd haproxy-1.6.2
#make TARGET=linux26 PREFIX=/usr/local/haproxy &&make install PREFIX=/usr/local/haproxy
#vim haproxy.init 启动脚步路径修改
#BIN=/usr/local/haproxy/sbin/$BASENAME
#修改之后
#cp haproxy.init /srv/salt/prod/haproxy/files/
[[email protected] ~]# mkdir /srv/salt/prod/haproxy #haprox模块目录
[[email protected] ~]# mkdir /srv/salt/prod/haproxy/files #存放haprox的一些文件
[[email protected] ~]# ll /srv/salt/prod/haproxy/files/
-rw-r--r--. 1 root root 1538976 Aug 11 2017 haproxy-1.6.2.tar.gzhaproxy #源码包
-rw-r--r--. 1 root root 2395 Aug 11 08:31 haproxy.inithaproxy #启动脚本
[[email protected] ~]# cd /srv/salt/prod/haproxy/
[[email protected] haproxy]# vim install.sls
include:
- pkg.pkg-init #这是个相对目录,相对于配置文件中的/srv/salt/prod/,意思是调用/srv/salt/prod/目录下的pkg目录下的pkg-init.sls
haproxy-install: #ID
file.managed:
- name: /usr/local/src/haproxy-1.6.2.tar.gz #name 声明,没有ID可以把name声明放在ID位置
- source: salt://haproxy/files/haproxy-1.6.2.tar.gz #这里也是相对路径/srv/salt/prod/,源码包所在
- user: root
- group: root
- mode: 755
cmd.run: #将要执行的命令放在这个模块下
- name: cd /usr/local/src/ && tar xf haproxy-1.6.2.tar.gz && cd haproxy-1.6.2 && make TARGET=linux26 PREFIX=/usr/local/haproxy &&make install PREFIX=/usr/local/haproxy
- unless: test -d /usr/local/haproxy #/usr/local/haproxy 目录不存在才执行cmd命令
- require: #指定依赖
- pkg: pkg-init #依赖pkg-init这个ID的pkg模块,这个模块必须执行成功才执行本模块cmd.run
- file: haproxy-install #依赖haproxy-install这个ID的file模块
haproxy-init:
file.managed:
- name: /etc/init.d/haproxy
- source: salt://haproxy/files/haproxy.init
- user: root
- group: root
- mode: 755
- require:
- cmd: haproxy-install
cmd.run:
- name: chkconfig --add haproxy
- unless: chkconfig --list|grep haproxy
- require:
- file: haproxy-init
net.ipv4.ip_nonlocal_bind: #/proc/sys/net/ipv4/ip_nonlocal_bind,这里只需要写相对路径
sysctl.present:#sysctl #模块,管理内核模块
- value: 1 #默认不让监听非本地ip,改为1后可以监听
haproxy-config-dir: #ID
file.directory:
- name: /etc/haproxy #创建配置文件目录
- user: root
- group: root
- mode: 755
[[email protected] haproxy]# salt ‘node1‘ state.sls haproxy.install env=prod #手动测试一下
5.业务模块-------haproxy模块
[[email protected] ~]# mkdir /srv/salt/prod/cluster
[[email protected] ~]# mkdir /srv/salt/prod/cluster/files
[[email protected] ~]# cd /srv/salt/prod/cluster/files
[[email protected] files]# vim haproxy-outside.cfg #负载均衡文件
global
maxconn 100000
chroot /usr/local/haproxy
uid 99
gid 99
daemon
nbproc 1
pidfile /usr/local/haproxy/logs/haproxy.pid
log 127.0.0.1 local3 info
defaults
option http-keep-alive
maxconn 100000
mode http
timeout connect 5000ms
timeout client 5000ms
timeout server 5000ms
listen stats
mode http
bind 0.0.0.0:8888
stats enable
stats uri /haproxy-status
stats auth haproxy:saltstack
frontend frontend_www_example_com
bind 192.168.10.150:80
mode http
option httplog
log global
default_backend backend_www_example_com
backend backend_www_example_com
option forwardfor header X-REAL-IP
option httpchk HEAD / HTTP/1.0
balance roundrobin
server web-node1 192.168.10.129:8080 check inter 2000 rise 30 fall 15
server web-node2 192.168.10.128:8080 check inter 2000 rise 30 fall 15
[[email protected] cluster]# cd /srv/salt/prod/cluster/
[[email protected] cluster]# vim haproxy-outside.sls
[[email protected] cluster]# cat haproxy-outside.sls
include:
- haproxy.install #执行haproxy目录下的install.sls
haproxy-service:#ID
file.managed:
- name: /etc/haproxy/haproxy.cfg #安装之后配置文件的名字
- source: salt://cluster/files/haproxy-outside.cfg #源配置文件,前面已经写好
- user: root
- group: root
- mode: 644
service.running: #service模块下的running方法,作用:启动服务
- name: haproxy #服务名字
- enable: True #是否开机启动
- reload: True #是否reload,如果不加,配置文件变了会restart
- require:
- cmd: haproxy-init #依赖haproxy-init下的cmd,意思是启动脚本那步必须执行成功
- watch: #关注某个文件状态
- file: haproxy-service #关注haproxy-service ID下的file模块里的文件,文件改变会reload
6.编辑top.sls
[[email protected] base]# cd /srv/salt/base/
[[email protected] base]# vim top.sls
base:
‘*‘:
- init.env_init #所有主机都执行init目录下的env_init.sls
prod:
‘node1‘:
- cluster.haproxy-outside #node1执行cluster目录下的haproxy-outside.sls
‘node2‘:
- cluster.haproxy-outside
[[email protected] base]# salt ‘*‘ state.highstate test=True #测试执行一下
[[email protected] prod]# salt ‘*‘ state.highstate
使用httpd测试一下
[[email protected] prod]# vim /etc/httpd/conf/httpd.conf
Listen 8080
[[email protected] prod]# cat /var/www/html/index.html
node1
[[email protected] prod]# /etc/init.d/httpd restart
[[email protected] ~]# vim /etc/httpd/conf/httpd.conf
[[email protected] prod]# cat /var/www/html/index.html
Node2
Listen 8080
[[email protected] ~]# /etc/init.d/httpd restart
在网页输入下面的地址登录:
192.168.10.129:8888/haproxy-status
192.168.10.128:8888/haproxy-status
用户haproxy密码saltstack