实验环境:
hostname | ip | 角色 | 安装服务 |
---|---|---|---|
server1 | 172.25.7.1 | salt-master,salt-minion | keepalived,haproxy |
server2 | 172.25.7.2 | salt-minion | httpd |
server3 | 172.25.7.3 | salt-minion | nginx |
server4 | 172.25.7.4 | salt-minion | keepalived,haproxy |
一、源码安装keepalived
base目录的部署
[root@server1 salt]# pwd
/srv/salt
[root@server1 salt]# mkdir keepalived
[root@server1 salt]# cd keepalived/
[root@server1 keepalived]# mkdir files
[root@server1 keepalived]# cd files/
[root@server1 files]# ls
keepalived-2.0.6.tar.gz
编写install.sls
[root@server1 keepalived]# vim install.sls
include:
- pkgs.make
kp-install:
file.managed:
- name: /mnt/keepalived-2.0.6.tar.gz
- source: salt://keepalived/files/keepalived-2.0.6.tar.gz
cmd.run:
- name: cd /mnt && tar zxf keepalived-2.0.6.tar.gz && cd keepalived-2.0.6 && ./configure --prefix=/usr/local/keepalived --with-init=SYSV &> /dev/null && make &> /dev/null && make install &> /dev/null
- creates: /usr/local/keepalived
/etc/keepalived:
file.directory:
- mode: 755
/etc/sysconfig/keepalived:
file.symlink: #软连接方法
- target: /usr/local/keepalived/etc/sysconfig/keepalived
/sbin/keepalived:
file.symlink:
- target: /usr/local/keepalived/sbin/keepalived
pkgs.make
[root@server1 pkgs]# cat make.sls
make:
pkg.installed:
- pkgs:
- pcre-devel
- openssl-devel
- gcc
- malix
进行推送:
[root@server1 keepalived]# salt server4 state.sls keepalived.install
server4:
----------
ID: make-gcc
Function: pkg.installed
Result: True
Comment: All specified packages are already installed
Started: 10:08:28.709285
Duration: 439.666 ms
Changes:
----------
ID: kp-install
Function: file.managed
Name: /mnt/keepalived-2.0.6.tar.gz
Result: True
Comment: File /mnt/keepalived-2.0.6.tar.gz is in the correct state
Started: 10:08:29.150619
Duration: 63.684 ms
Changes:
----------
ID: kp-install
Function: cmd.run
Name: cd /mnt && tar zxf keepalived-2.0.6.tar.gz && cd keepalived-2.0.6 && ./configure --prefix=/usr/local/keepalived --with-init=SYSV &> /dev/null && make &> /dev/null && make install &> /dev/null
Result: True
Comment: /usr/local/keepalived exists
Started: 10:08:29.214997
Duration: 0.376 ms
Changes:
----------
ID: /etc/keepalived
Function: file.directory
Result: True
Comment: Directory /etc/keepalived updated
Started: 10:08:29.215465
Duration: 43.462 ms
Changes:
----------
/etc/keepalived:
New Dir
----------
ID: /etc/sysconfig/keepalived
Function: file.symlink
Result: True
Comment: Created new symlink /etc/sysconfig/keepalived -> /usr/local/keepalived/etc/sysconfig/keepalived
Started: 10:08:29.259057
Duration: 1.264 ms
Changes:
----------
new:
/etc/sysconfig/keepalived
----------
ID: /sbin/keepalived
Function: file.symlink
Result: True
Comment: Created new symlink /sbin/keepalived -> /usr/local/keepalived/sbin/keepalived
Started: 10:08:29.260420
Duration: 67.82 ms
Changes:
----------
new:
/sbin/keepalived
Summary for server4
------------
Succeeded: 6 (changed=3)
Failed: 0
------------
Total states run: 6
Total run time: 616.272 ms
二、实现高可用
将server4的配置文件和脚本发至server1:
[root@server4 ~]# scp /usr/local/keepalived/etc/rc.d/init.d/keepalived server1:/srv/salt/keepalived/files
[root@server4 ~]# scp /usr/local/keepalived/etc/keepalived/keepalived.conf server1:/srv/salt/keepalived/files
[root@server1 files]# ls
keepalived keepalived-2.0.6.tar.gz keepalived.conf
keepalived配置文件修改
[root@server1 keepalived]# cd files/
[root@server1 files]# vim keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
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.7.100/24
}
}
编写服务文件service.sls
添加jinja模板
[root@server1 keepalived]# vim service.sls
include:
- keepalived.install
/etc/keepalived/keepalived.conf:
file.managed:
- source: salt://keepalived/files/keepalived.conf
- template: jinja
- context:
STATE: {{ pillar['state'] }}
VRID: {{ pillar['vrid'] }}
PRIORITY: {{ pillar['priority'] }}
kp-service:
file.managed:
- name: /etc/init.d/keepalived
- source: salt://keepalived/files/keepalived
- mode: 755
service.running:
- name: keepalived
- reload: True
- watch:
- file: /etc/keepalived/keepalived.conf
修改pillar
[root@server1 srv]# cd pillar
[root@server1 pillar]# mkdir keepalived
[root@server1 keepalived]# vim install.sls
{% if grains['fqdn'] == 'server1'%}
state: MASTER
vrid: 77
priority: 100
{% elif grains['fqdn'] == 'server4'%}
state: BACKUP
vrid: 77
priority: 50
{% endif %}
修改base
[root@server1 pillar]# vim top.sls
base:
'*':
- web.install
- keepalived.install
高级推送文件
[root@server1 salt]# ls
_grains haproxy httpd keepalived nginx pkgs top.sls users
[root@server1 salt]# vim top.sls
base:
'server1':
- haproxy.install
- keepalived.service
'server4':
- haproxy.install
- keepalived.service
'roles:apache':
- match: grain
- httpd.install
'roles:nginx':
- match: grain
- nginx.service
开始推送:
[root@server1 salt]# salt '*' state.highstate
server2:
----------
ID: httpd
Function: pkg.installed
Result: True
Comment: Package httpd is already installed
Started: 10:30:57.496258
Duration: 779.37 ms
Changes:
----------
ID: php
Function: pkg.installed
Result: True
Comment: Package php is already installed
Started: 10:30:58.275901
Duration: 1.123 ms
Changes:
----------
ID: /etc/httpd/conf/httpd.conf
Function: file.managed
Result: True
Comment: File /etc/httpd/conf/httpd.conf is in the correct state
Started: 10:30:58.282447
Duration: 229.195 ms
Changes:
----------
ID: apache
Function: service.running
Name: httpd
Result: True
Comment: The service httpd is already running
Started: 10:30:58.511951
Duration: 40.932 ms
Changes:
Summary for server2
------------
Succeeded: 4
Failed: 0
------------
Total states run: 4
Total run time: 1.051 s
server3:
----------
ID: make-gcc
Function: pkg.installed
Result: True
Comment: All specified packages are already installed
Started: 10:30:58.194559
Duration: 813.467 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: 10:30:59.011037
Duration: 136.234 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/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && sed -i.bak 's/#define NGINX_VER "nginx\/" NGINX_VERSION/#define NGINX_VER "nginx"/g' src/core/nginx.h && ./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: 10:30:59.148808
Duration: 3.991 ms
Changes:
----------
ID: nginx-group
Function: group.present
Name: nginx
Result: True
Comment: Group nginx is present and up to date
Started: 10:30:59.153586
Duration: 0.713 ms
Changes:
----------
ID: nginx-user
Function: user.present
Name: nginx
Result: True
Comment: User nginx is present and up to date
Started: 10:30:59.158538
Duration: 1.918 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: 10:30:59.160698
Duration: 147.27 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: 10:30:59.308190
Duration: 77.362 ms
Changes:
----------
ID: nginx-service
Function: service.running
Name: nginx
Result: True
Comment: The service nginx is already running
Started: 10:30:59.386591
Duration: 59.254 ms
Changes:
Summary for server3
------------
Succeeded: 8
Failed: 0
------------
Total states run: 8
Total run time: 1.240 s
server1:
----------
ID: haproxy-install
Function: pkg.installed
Result: True
Comment: All specified packages are already installed
Started: 10:30:58.776878
Duration: 897.935 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: 10:30:59.678006
Duration: 85.707 ms
Changes:
----------
ID: haproxy-install
Function: service.running
Name: haproxy
Result: True
Comment: The service haproxy is already running
Started: 10:30:59.765197
Duration: 68.964 ms
Changes:
----------
ID: make-gcc
Function: pkg.installed
Result: True
Comment: All specified packages are already installed
Started: 10:30:59.834457
Duration: 1.059 ms
Changes:
----------
ID: kp-install
Function: file.managed
Name: /mnt/keepalived-2.0.6.tar.gz
Result: True
Comment: File /mnt/keepalived-2.0.6.tar.gz is in the correct state
Started: 10:30:59.835730
Duration: 77.672 ms
Changes:
----------
ID: kp-install
Function: cmd.run
Name: cd /mnt && tar zxf keepalived-2.0.6.tar.gz && cd keepalived-2.0.6 && ./configure --prefix=/usr/local/keepalived --with-init=SYSV &> /dev/null && make &> /dev/null && make install &> /dev/null
Result: True
Comment: /usr/local/keepalived exists
Started: 10:30:59.914451
Duration: 0.418 ms
Changes:
----------
ID: /etc/keepalived
Function: file.directory
Result: True
Comment: Directory /etc/keepalived is in the correct state
Started: 10:30:59.914982
Duration: 0.532 ms
Changes:
----------
ID: /etc/sysconfig/keepalived
Function: file.symlink
Result: True
Comment: Symlink /etc/sysconfig/keepalived is present and owned by root:root
Started: 10:30:59.915607
Duration: 1.435 ms
Changes:
----------
ID: /sbin/keepalived
Function: file.symlink
Result: True
Comment: Symlink /sbin/keepalived is present and owned by root:root
Started: 10:30:59.917140
Duration: 1.248 ms
Changes:
----------
ID: /etc/keepalived/keepalived.conf
Function: file.managed
Result: True
Comment: File /etc/keepalived/keepalived.conf is in the correct state
Started: 10:30:59.918485
Duration: 37.75 ms
Changes:
----------
ID: kp-service
Function: file.managed
Name: /etc/init.d/keepalived
Result: True
Comment: File /etc/init.d/keepalived is in the correct state
Started: 10:30:59.956367
Duration: 35.367 ms
Changes:
----------
ID: kp-service
Function: service.running
Name: keepalived
Result: True
Comment: The service keepalived is already running
Started: 10:30:59.992383
Duration: 33.99 ms
Changes:
Summary for server1
-------------
Succeeded: 12
Failed: 0
-------------
Total states run: 12
Total run time: 1.242 s
server4:
----------
ID: haproxy-install
Function: pkg.installed
Result: True
Comment: The following packages were installed/updated: haproxy
Started: 10:30:57.965762
Duration: 6185.553 ms
Changes:
----------
haproxy:
----------
new:
1.4.24-2.el6
old:
----------
ID: haproxy-install
Function: file.managed
Name: /etc/haproxy/haproxy.cfg
Result: True
Comment: File /etc/haproxy/haproxy.cfg updated
Started: 10:31:04.153916
Duration: 86.389 ms
Changes:
----------
diff:
---
+++
@@ -60,27 +60,9 @@
#---------------------------------------------------------------------
# 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.7.2:80 check
+ server app2 172.25.7.3:80 check
----------
ID: haproxy-install
Function: service.running
Name: haproxy
Result: True
Comment: Started Service haproxy
Started: 10:31:04.247499
Duration: 222.761 ms
Changes:
----------
haproxy:
True
----------
ID: make-gcc
Function: pkg.installed
Result: True
Comment: All specified packages are already installed
Started: 10:31:04.473303
Duration: 1.457 ms
Changes:
----------
ID: kp-install
Function: file.managed
Name: /mnt/keepalived-2.0.6.tar.gz
Result: True
Comment: File /mnt/keepalived-2.0.6.tar.gz is in the correct state
Started: 10:31:04.474858
Duration: 98.192 ms
Changes:
----------
ID: kp-install
Function: cmd.run
Name: cd /mnt && tar zxf keepalived-2.0.6.tar.gz && cd keepalived-2.0.6 && ./configure --prefix=/usr/local/keepalived --with-init=SYSV &> /dev/null && make &> /dev/null && make install &> /dev/null
Result: True
Comment: /usr/local/keepalived exists
Started: 10:31:04.573982
Duration: 0.382 ms
Changes:
----------
ID: /etc/keepalived
Function: file.directory
Result: True
Comment: Directory /etc/keepalived is in the correct state
Started: 10:31:04.574456
Duration: 0.485 ms
Changes:
----------
ID: /etc/sysconfig/keepalived
Function: file.symlink
Result: True
Comment: Symlink /etc/sysconfig/keepalived is present and owned by root:root
Started: 10:31:04.575030
Duration: 3.815 ms
Changes:
----------
ID: /sbin/keepalived
Function: file.symlink
Result: True
Comment: Symlink /sbin/keepalived is present and owned by root:root
Started: 10:31:04.578959
Duration: 1.461 ms
Changes:
----------
ID: /etc/keepalived/keepalived.conf
Function: file.managed
Result: True
Comment: File /etc/keepalived/keepalived.conf is in the correct state
Started: 10:31:04.580533
Duration: 35.922 ms
Changes:
----------
ID: kp-service
Function: file.managed
Name: /etc/init.d/keepalived
Result: True
Comment: File /etc/init.d/keepalived is in the correct state
Started: 10:31:04.616681
Duration: 66.378 ms
Changes:
----------
ID: kp-service
Function: service.running
Name: keepalived
Result: True
Comment: The service keepalived is already running
Started: 10:31:04.684128
Duration: 46.462 ms
Changes:
Summary for server4
-------------
Succeeded: 12 (changed=3)
Failed: 0
-------------
Total states run: 12
Total run time: 6.749 s
在浏览器访问vip
当master的keepalived宕机
[root@server1 salt]# /etc/init.d/keepalived stop
Stopping keepalived: [ OK ]
负载均衡保持高可用