自动化运维saltstack安装部署及用keepalived实现高可用

这篇博客中写到了用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

一、配置

1、配置server4

  • 配置server4的yum源
[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
  • 安装并启动salt-minion,并修改配置文件,将server4添加到master端
[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

自动化运维saltstack安装部署及用keepalived实现高可用_第1张图片

二、源码安装keepalivedd

  • /srv/salt目录的部署:
[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
  • 编写安装脚本,在/srv/salt/keepalived/目录下
[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

自动化运维saltstack安装部署及用keepalived实现高可用_第2张图片

三、实现高可用

  • 将keepalived的配置文件和启动脚本发给/srv/salt/keepalived/files目录下
[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
    }
}
  • 编写服务文件service.sls
[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
    • 修改pillar
[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 %}
  • 将pillar中写的添加到pillar的top.sls中去
[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

自动化运维saltstack安装部署及用keepalived实现高可用_第3张图片

四、在浏览器中测试负载均衡和高可用

自动化运维saltstack安装部署及用keepalived实现高可用_第4张图片
在浏览器中访问vip,可以看到实现了负载均衡
自动化运维saltstack安装部署及用keepalived实现高可用_第5张图片
自动化运维saltstack安装部署及用keepalived实现高可用_第6张图片
当把server1上的keepalived关掉

[root@server1 keepalived]# /etc/init.d/keepalived stop
Stopping keepalived:                                       [  OK  ]

自动化运维saltstack安装部署及用keepalived实现高可用_第7张图片
测试,可以看出在客户端仍然可以访问,实现了高可用
自动化运维saltstack安装部署及用keepalived实现高可用_第8张图片

你可能感兴趣的:(saltstack)