1.配置【server1】:
【1】也安装salt-minion工具:
[root@server1 ~]# yum install salt-minion -y
[root@server1 ~]# cd /etc/salt/
[root@server1 salt]# ls
cloud cloud.deploy.d cloud.profiles.d master minion pki proxy.d
cloud.conf.d cloud.maps.d cloud.providers.d master.d minion.d proxy roster
[root@server1 salt]# vim minion
[root@server1 salt]# /etc/init.d/salt-minion start
Starting salt-minion:root:server1 daemon: OK
【2】发送钥匙免密连接:
[root@server1 salt]# salt-key -a server1
The following keys are going to be accepted:
Unaccepted Keys:
server1
Proceed? [n/Y] y
Key for minion server1 accepted.
【3】配置高可用yum源:
[root@server1 yum.repos.d]# vim rhel-source.repo
【4】编辑haproxy自动连接执行脚本:
[root@server1 ~]# cd /srv/salt/
[root@server1 salt]# ls
httpd nginx pkgs users
[root@server1 salt]# mkdir haproxy
[root@server1 salt]# cd haproxy/
[root@server1 haproxy]# vim install.sls
[root@server1 haproxy]# salt server1 state.sls haproxy.install
[root@server1 haproxy]# pwd
/etc/haproxy
[root@server1 haproxy]# ls
haproxy.cfg
[root@server1 haproxy]# mkdir /srv/salt/haproxy/files
[root@server1 haproxy]# cp haproxy.cfg /srv/salt/haproxy/files/
[root@server1 haproxy]# cd /srv/salt/haproxy/
[root@server1 haproxy]# ls
files install.sls
[root@server1 haproxy]# vim install.sls
【5】配置haproxy服务文件:
[root@server1 files]# vim haproxy.cfg
[root@server1 files]# /etc/init.d/haproxy start
Starting haproxy: [ OK ]
[root@server1 files]# /etc/init.d/haproxy stop
Stopping haproxy: [ OK ]
【6】编写三台主机服务调用脚本:
[root@server1 salt]# pwd
/srv/salt
[root@server1 salt]# ls
haproxy httpd nginx pkgs users
[root@server1 salt]# vim top.sls
测试:
【7】【server2】中书写默认发布页:
[root@server2 salt]# cd
[root@server2 ~]# cd /var/www/html
[root@server2 html]# vim index.html
【8】启动高可用脚本:
注意:【server2】的httpd服务端口必须是80端口
[root@server1 salt]# salt '*' state.highstate
server2:
----------
ID: apache-install
Function: pkg.installed
Result: True
Comment: All specified packages are already installed
Started: 16:57:15.348111
Duration: 453.255 ms
Changes:
----------
ID: apache-install
Function: file.managed
Name: /etc/httpd/conf/httpd.conf
Result: True
Comment: File /etc/httpd/conf/httpd.conf is in the correct state
Started: 16:57:15.804233
Duration: 47.762 ms
Changes:
----------
ID: apache-install
Function: service.running
Name: httpd
Result: True
Comment: The service httpd is already running
Started: 16:57:15.852982
Duration: 48.042 ms
Changes:
Summary for server2
------------
Succeeded: 3
Failed: 0
------------
Total states run: 3
Total run time: 549.059 ms
server1:
----------
ID: haproxy-install
Function: pkg.installed
Result: True
Comment: All specified packages are already installed
Started: 16:57:15.080500
Duration: 397.551 ms
Changes:
----------
ID: haproxy-install
Function: file.managed
Name: /etc/haproxy/haproxy.cfg
Result: True
Comment: File /etc/haproxy/haproxy.cfg updated
Started: 16:57:15.480667
Duration: 71.722 ms
Changes:
----------
diff:
---
+++
@@ -60,27 +60,11 @@
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
-frontend main *:5000
- acl url_static path_beg -i /static /images /javascript /stylesheets
- acl url_static path_end -i .jpg .gif .png .css .js
-
- use_backend static if url_static
+frontend main *:80
default_backend app
-#---------------------------------------------------------------------
-# static backend for serving up images, stylesheets and such
-#---------------------------------------------------------------------
-backend static
- balance roundrobin
- server static 127.0.0.1:4331 check
-
-#---------------------------------------------------------------------
-# round robin balancing between the various backends
-#---------------------------------------------------------------------
backend app
balance roundrobin
- server app1 127.0.0.1:5001 check
- server app2 127.0.0.1:5002 check
- server app3 127.0.0.1:5003 check
- server app4 127.0.0.1:5004 check
+ server app1 172.25.39.1:80 check
+ server app2 172.25.39.2:80 check
----------
ID: haproxy-install
Function: service.running
Name: haproxy
Result: True
Comment: Started Service haproxy
Started: 16:57:15.553338
Duration: 166.267 ms
Changes:
----------
haproxy:
True
Summary for server1
------------
Succeeded: 3 (changed=2)
Failed: 0
------------
Total states run: 3
Total run time: 635.540 ms
server3:
----------
ID: nginx-group
Function: group.present
Name: nginx
Result: True
Comment: Group nginx is present and up to date
Started: 16:57:14.585684
Duration: 11.412 ms
Changes:
----------
ID: nginx-user
Function: user.present
Name: nginx
Result: True
Comment: User nginx is present and up to date
Started: 16:57:14.598217
Duration: 73.361 ms
Changes:
----------
ID: make-gcc
Function: pkg.installed
Result: True
Comment: All specified packages are already installed
Started: 16:57:15.750162
Duration: 462.11 ms
Changes:
----------
ID: nginx-install
Function: file.managed
Name: /mnt/nginx-1.14.0.tar.gz
Result: True
Comment: File /mnt/nginx-1.14.0.tar.gz is in the correct state
Started: 16:57:16.214767
Duration: 90.09 ms
Changes:
----------
ID: nginx-install
Function: cmd.run
Name: cd /mnt && tar zxf nginx-1.14.0.tar.gz && cd nginx-1.14.0 && sed -i.bak 's/#define NGINX_VER "nginx\/" NGINX_VERSION/#define NGINX_VER "nginx"/g' src/core/nginx.h && sed -i.bak 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio &> /dev/null && make &> /dev/null && make install &> /dev/null
Result: True
Comment: /usr/local/nginx exists
Started: 16:57:16.305764
Duration: 0.439 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: 16:57:16.306321
Duration: 41.313 ms
Changes:
----------
ID: nginx-service
Function: file.managed
Name: /etc/init.d/nginx
Result: True
Comment: File /etc/init.d/nginx is in the correct state
Started: 16:57:16.347845
Duration: 37.276 ms
Changes:
----------
ID: nginx-service
Function: service.running
Name: nginx
Result: True
Comment: The service nginx is already running
Started: 16:57:16.386379
Duration: 48.06 ms
Changes:
Summary for server3
------------
Succeeded: 8
Failed: 0
------------
Total states run: 8
Total run time: 764.061 ms
2.真机网页上测试高可用:
【*】当停止【server2】中httpd服务时,网页上只能测试到welcome to nginx!
【】再进行推送[root@server1 files]# salt ‘’ state.highstate
然后高可用重新加载,网页测试正常
Gains模块:
1.【server2】GAINS模块中添加apache服务:
[root@server2 ~]# /etc/init.d/httpd stop
Stopping httpd: [ OK ]
[root@server2 ~]# vim /etc/salt/minion
[root@server2 ~]# /etc/init.d/salt-minion restart
Stopping salt-minion:root:server2 daemon: OK
Starting salt-minion:root:server2 daemon: OK
2.【server1】查看server2端是否添加apache服务:
3.【server3】GAINS模块中添加nginx服务:
[root@server3 ~]# vim /etc/salt/grains
[root@server3 ~]# cat /etc/salt/grains
roles:
nginx
4.【server1】查看server3端是否添加nginx服务:
5.【server1】启动服务更改启动脚本:
[root@server1 ~]# cd /srv/salt/
[root@server1 salt]# vim top.sls
[root@server1 salt]# cat top.sls
base:
'server1':
- haproxy.install
'roles:apache':
- match: grain
- httpd.install
'roles:nginx':
- match: grain
- nginx.service
[root@server1 salt]# salt '*' grains.item roles
[root@server1 salt]# salt '*' state.highstate
[root@server1 salt]# pwd
/srv/salt
[root@server1 salt]# mkdir _grains
[root@server1 salt]# cd _grains/
[root@server1 _grains]# vim my_grains.py
[root@server1 _grains]# cat my_grains.py
#!/usr/bin/env python
def my_grains():
grains = {}
grains['hello'] = 'world'
grains['salt'] = 'stack'
return grains
[root@server1 _grains]# salt server2 saltutil.sync_grains
server2:
- grains.my_grains
6.【server2】查看GAINS模块添加:
[root@server2 salt]# pwd
/var/cache/salt
[root@server2 salt]# tree minion/
minion/
├── accumulator
├── extmods
│ └── grains
│ ├── my_grains.py
│ └── my_grains.pyc
├── files
│ └── base
│ ├── _grains
│ │ └── my_grains.py
│ ├── httpd
│ │ ├── files
│ │ │ └── httpd.conf
│ │ └── install.sls
│ └── top.sls
├── highstate.cache.p
├── module_refresh
├── pkg_refresh
├── proc
└── sls.p
9 directories, 10 files
7.测试: