实验环境:
172.25.23.7 haproxy master
172.25.23.8 httpd
172.25.23.9 nginx
服务器上下载
[root@server7 salt]# salt-key -a server7
The key glob 'server7' does not match any unaccepted keys.
[root@server7 salt]# salt-key
Accepted Keys:
server8
server9
Denied Keys:
Unaccepted Keys:
Rejected Keys:
[root@server7 salt]# salt-key -a server7
The key glob 'server7' does not match any unaccepted keys.
[root@server7 salt]# vim /etc/hosts
[root@server7 salt]# /etc/init.d/salt-minion state
Usage: /etc/init.d/salt-minion {start|stop|status|restart|condrestart|try-restart|reload}
[root@server7 salt]# /etc/init.d/salt-minion status
salt-minion:root:server7 is running: 20450
[root@server7 salt]# salt-key -a server7
The key glob 'server7' does not match any unaccepted keys.
[root@server7 salt]# vim /etc/salt/minion
[root@server7 salt]# /etc/init.d/salt-minion restart
Stopping salt-minion:root:server7 daemon: OK
Starting salt-minion:root:server7 daemon: OK
[root@server7 salt]# salt-key -a server7
The following keys are going to be accepted:
Unaccepted Keys:
server7
Proceed? [n/Y] y
Key for minion server7 accepted.
[root@server7 salt]# yum install haproxy -y
[root@server7 salt]# mkdir haproxy
[root@server7 salt]# cd haproxy/
[root@server7 haproxy]# vim install.sls
haproxy-install:
pkg.installed:
- pkgs:
- haproxy
file.managed:
- name: /etc/haproxy/haproxy.cfg
- source: salt://haproxy/files/haproxy.cfg
service.running:
- name: haproxy
- reload: True
- watch:
- file: haproxy-install
[root@server7 haproxy]# salt server7 state.sls haproxy.install
测试:
[root@server7 haproxy]# rpm -q haproxy
haproxy-1.4.24-2.el6.x86_64
[root@server7 haproxy]# mkdir files
[root@server7 haproxy]# cd files/
[root@server7 files]# cp /etc/haproxy/haproxy.cfg .
[root@server7 files]# vim haproxy.cfg
frontend main *:80
default_backend app
backend app
balance roundrobin
server app1 172.25.23.8:80 check
server app2 172.25.23.9:80 check
[root@server7 salt]# vim top.sls
base:
'server7':
- haproxy.install
'roles:apache':
- match: grain
- httpd.install
'roles:nginx':
- match: grain
- nginx.service
[root@server7 salt]# salt server? test.ping
server8:
True
server9:
True
server7:
True
[root@server7 salt]# salt server* test.ping
server9:
True
server8:
True
server7:
True
测试:
[root@server7 salt]# salt server[7-9]] test.ping
No minions matched the target. No command was sent, no jid was assigned.
ERROR: No return received
[root@server7 salt]# salt server[7-9] test.ping
server7:
True
server8:
True
server9:
True
[root@server7 pkgs]# salt '*' state.highstate
server7:
----------
ID: haproxy-install
Function: pkg.installed
Result: True
Comment: All specified packages are already installed
Started: 15:12:19.789372
Duration: 407.607 ms
Changes ID: haproxy-install
Function: file.managed
Name: /etc/haproxy/haproxy.cfg
Result: True
Comment: File /etc/haproxy/haproxy.cfg is in the correct state
Started: 15:12:20.200431
Duration: 48.254
[root@server8 ~]# cd /var/www/html
[root@server8 html]# vim index.html
server8
测试
浏览器输入172.25.23.7