这篇博客中写到了用saltstack推送了一台主机上的haproxy为了实现了负载均衡,https://blog.csdn.net/qq_42241932/article/details/83828110
此篇博客基于上次实验,用keepalived实现高可用
server1 172.25.66.1 maseter/minion keepalived/haproxy
server4 172.25.66.4 minion keepalived/haproxy
server2 172.25.66.2 minion httpd
server3 172.25.66.3 minion nginx
[root@server4 keepalived]# cat /etc/yum.repos.d/rhel-source.repo
[rhel6.5]
name=rhel6.5
baseurl=http://172.25.66.250/rhel6.5
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
[saltstack]
name=saltstack
baseurl=http://172.25.66.250/saltstack/rhel6
gpgcheck=0
[root@server4 ~]# yum install -y salt-minion
[root@server4 ~]# cd /etc/salt/
[root@server4 salt]# vim minion
16 master: 172.25.66.1
[root@server4 salt]# /etc/init.d/salt-minion restart
Stopping salt-minion:root:server4 daemon: OK
Starting salt-minion:root:server4 daemon: OK
[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
[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
编辑安装中所需要的依赖包文件,vim /srv/salt/pkgs/install.sls
[root@server1 pkgs]# vim make.sls
make:
pkg.installed:
- pkgs:
- pcre-devel
- openssl-devel
- gcc
[root@server1 keepalived]# salt server1 state.sls keepalived.install
[root@server1 keepalived]# salt server4 state.sls keepalived.install
[root@server1 files]# cd /usr/local/keepalived/etc/rc.d/init.d/
[root@server1 init.d]# cp keepalived /srv/salt/keepalived/files
[root@server1 init.d]# cd /usr/local/keepalived/etc/keepalived/
[root@server1 keepalived]# cp keepalived.conf /srv/salt/keepalived/files
[root@server1 files]# ls
keepalived keepalived-2.0.6.tar.gz keepalived.conf
[root@server1 files]# vim keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from [email protected]
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
}
}
[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
[root@server1 srv]# cd pillar
[root@server1 pillar]# mkdir keepalived
[root@server1 keepalived]# vim install.sls
{% if grains['fqdn'] == 'server1'%}
state: MASTER ##server1设置为主机
vrid: 77
priority: 100
{% elif grains['fqdn'] == 'server4'%}
state: BACKUP #将server4设置为备用机
vrid: 77
priority: 50
{% endif %}
[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.service
- keepalived.service
"server4":
- haproxy.service
- keepalived.service
"server2":
- apache.service
"server3":
- nginx.service
开始推送
[root@server1 salt]# salt '*' state.highstate
在浏览器中访问vip,可以看到实现了负载均衡
当把server1上的keepalived关掉
[root@server1 keepalived]# /etc/init.d/keepalived stop
Stopping keepalived: [ OK ]