• 生活的道路一旦选定,就要勇敢地走到底,决不回头。——左拉
  • 坚强的信心,能使平凡的人做出惊人的事业。——马尔顿
  • 人不可有傲气,但不可无傲骨。 --徐悲鸿
  • 古之立大志者,不惟有超世之才,亦必有坚韧不拔之志。 --苏轼
  • 时间像海绵里的水,只要你愿意挤,总还是有的。 --鲁迅

ansible实现Atlas高可用keepalived管理MHA

MySQL zkinogg 2年前 (2020-07-30) 221次浏览 0个评论

一.环境准备

主机名 内网ip 外网ip 安装服务
db04 172.16.1.54 10.0.0.54 keepalived,Atlas,mysql
db05 172.16.1.55 10.0.0.55 keepalived,Atlas,mysql
m01 172.16.1.61 10.0.0.61 ansible服务端

二.ansible准备阶段

[root@m01 keepalived_Atlas]# ll
total 8
drwxr-xr-x 10 root root 154 Jul 30 16:15 Atlas
-rw-r--r--  1 root root  79 Jul 30 16:29 hosts
-rw-r--r--  1 root root  79 Jul 30 16:41 site.yml


[root@m01 keepalived_Atlas]# cat hosts 
[db_group]
db05 ansible_ssh_host=172.16.1.54
db04 ansible_ssh_host=172.16.1.55


[root@m01 keepalived_Atlas]# cat site.yml 
- hosts: all
  roles:
    - { role: Atlas , when: ansible_fqdn is match 'db*'}
    

[root@m01 files]# ll /root/keepalived_Atlas/Atlas/files
total 398624
-rw-r--r-- 1 root root   4963681 Jul 30 17:01 Atlas-2.2.1.el6.x86_64.rpm #Atlas安装包
-rw-r--r-- 1 root root       528 Jul 30  2020 Atlas_keep.sh      #监控Atlas存活脚本
-rw-r--r-- 1 root root       274 Jul 30 17:06 keepalived2.conf    #Atlas高可用端配置文件
-rw-r--r-- 1 root root       781 Jul 30 17:03 keepalived.conf    #Atlas端keepalive配置文件
-rw-r--r-- 1 root root 403177622 Jul 30 16:16 mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz # mysql5.6.46二进制安装包
-rw-r--r-- 1 root root       325 Jul 30 16:16 mysqld.service  # mysql的systemd启动脚本
-rwxr-xr-x 1 root root     10565 Jul 30 16:16 mysql.server    # mysql启动脚本
-rw-r--r-- 1 root root        39 Jul 30 16:17 mysql.sh        # mysql环境变量配置文件
-rw-r--r-- 1 root root       974 Jul 30 17:01 switch_Atlas.sh   # 实时监控读写分离服务器脚本
-rw-r--r-- 1 root root       402 Jul 30 17:01 test.cnf         # Atlas配置文件


#监控Atlas存活状态的脚本
[root@m01 files]# cat Atlas_keep.sh 
#!/bin/bash
Atlas_status=$(ps -ef |grep -w test|grep -v grep|wc -l)

## 1判断Atlas是否存活,如果不存活则尝试启动Atlas
if [ $Atlas_status -eq 0 ];then
    /usr/local/mysql-proxy/bin/mysql-proxyd test start
    sleep 3
    #2.等待3秒后再次获取一次Atlas状态
    Atlas_status=$(ps -ef |grep -w test|grep -v grep|wc -l)
    #3.再次进行判断, 如Atlas还不存活则停止Keepalived,让地址进行漂移,并退出脚本  
    if [ $Atlas_status -eq 0 ];then
        systemctl stop keepalived
   fi
fi


#keepalived主配置文件
[root@m01 files]# cat keepalived.conf 
global_defs {                   #全局配置
    router_id db04             #标识身份->名称
}

vrrp_script Atlas {
    script "/usr/local/Atlas_keep.sh" # 5秒钟执行一次该脚本
    interval 5
}

vrrp_instance VI_1 {
    state MASTER                #标识角色状态
    interface eth0              #网卡绑定接口
    virtual_router_id 50        #虚拟路由id
    priority 150                #优先级
    advert_int 1                #监测间隔时间
    authentication {            #认证
        auth_type PASS          #认证方式
        auth_pass 2222          #认证密码
    }
    virtual_ipaddress {
        10.0.0.3                #虚拟的VIP地址
    }

    track_script {              #掉用上面配置的脚本
        Atlas
    }
}

#keepalived高可用配置文件
[root@m01 files]# cat keepalived2.conf 
global_defs {
    router_id db05
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 50
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 2222
    }
    virtual_ipaddress {
        10.0.0.3
    }
}

三.ansible编写阶段

1.安装mysql

[root@m01 tasks]# cat Yum_mysql.yml 
- name: yum rely on
  yum:
    name: "{{ item }}"
    state: present
  loop:
    - ncurses-devel
    - libaio-devel
    - autoconf
  when: ansible_fqdn is match 'db*'

- name: useradd mysql
  user:
    name: mysql
    shell: /sbin/nologin
    create_home: false
    state: present
  when: ansible_fqdn is match 'db*'

- name: Unarchive mysql tgz
  unarchive:
    src: mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz
    dest: /usr/local/
    owner: mysql
    group: mysql
  tags: unarchive
  when: ansible_fqdn is match 'db*'

- name: Link mysql
  file:
    src: /usr/local/mysql-5.6.46-linux-glibc2.12-x86_64
    dest: /usr/local/mysql
    owner: mysql
    group: mysql
    state: link
  when: ansible_fqdn is match 'db*'

- name: mysql config overwrite
  template:
    src: my-default.cnf.j2
    dest: /etc/my.cnf
  when: ansible_fqdn is match 'db*'

- name: Push sh mysql server
  copy: 
    src: mysql.server
    dest: /etc/init.d/mysqld
  when: ansible_fqdn is match 'db*'

- name: initialization mysql
  shell: 'cd /usr/local/mysql/scripts/ && ./mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --socket=/usr/local/mysql/mysql.sock'
  when: ansible_fqdn is match 'db*'

- name: Push mysql sh
  copy: 
    src: mysql.sh
    dest: /etc/profile.d/mysql.sh
  when: ansible_fqdn is match 'db*'

- name: source
  shell: 'source /etc/profile'
  when: ansible_fqdn is match 'db*'

- name: Push mysqld service
  copy:
    src: mysqld.service
    dest: /usr/lib/systemd/system/mysqld.service
  when: ansible_fqdn is match 'db*'

- name: daemon reload
  shell: 'systemctl daemon-reload'
  when: ansible_fqdn is match 'db*'

- name: start mysqld
  service:
    name: mysqld
    state: started
    enabled: true
  when: ansible_fqdn is match 'db*'

2.安装并配置Atlas

[root@m01 tasks]# cat Yum_Atlas.yml 
- name: Push Atlas rpm   # 推Atlas 的rpm 包
  copy:
    src: Atlas-2.2.1.el6.x86_64.rpm
    dest: /root/Atlas-2.2.1.el6.x86_64.rpm
  when: ansible_fqdn is match 'db*'

- name: Yum Atlas  #安装Atlas
  yum:
    name: /root/Atlas-2.2.1.el6.x86_64.rpm
    state: present
  when: ansible_fqdn is match 'db*'

- name: Push Atlas config  #推Atlas配置文件
  copy:
    src: test.cnf
    dest: /usr/local/mysql-proxy/conf/test.cnf
  when: ansible_fqdn is match 'db*'

- name: start Atlas  # 开启Atlas
  shell: '/usr/local/mysql-proxy/bin/mysql-proxyd test start'
  when: ansible_fqdn is match 'db*'

- name: Push Atlas sh  #推Atlas实时同步库信息脚本
  copy:
    src: switch_Atlas.sh
    dest: /root/switch_Atlas.sh
  when: ansible_fqdn is match 'db*'

3.安装并配置keepalived

[root@m01 tasks]# cat Yum_keepalive.yml 
- name: Yum keepalived               # yum 安装keepalived
  yum:
    name: keepalived
    state: present
  when: ansible_fqdn is match 'db*'

- name: Push keepalive sh                  # 推Atlas监控脚本,这里一定要加执行权限,不然       keepalived启动以后不能自动执行该脚本
  copy:
    src: Atlas_keep.sh
    dest: /usr/local/Atlas_keep.sh
    mode: 0755
  when: ansible_fqdn == 'db04'

- name: Push keepalived config                     # 推keepalived主配置文件
  copy:
    src: keepalived.conf
    dest: /etc/keepalived/keepalived.conf
  when: ansible_fqdn == 'db04'

- name: Push keepalived2 config                     #推keepalived高可用配置文件
  copy:
    src: keepalived2.conf
    dest: /etc/keepalived/keepalived.conf
  when: ansible_fqdn == 'db05'

- name: Start keepalived                 # 启动keepalived
  service:
    name: keepalived
    state: started
    enabled: true
  when: ansible_fqdn is match 'db*'

启动成功后查看atlas状态并测试。

 

 

 

 

 

 

 

 

 

 

 

 

喜欢 (0)
[17551054905]
分享 (0)

您必须 登录 才能发表评论!