Ansible:一鍵部署 Keepalived 高可用集羣

使用 Ansible 工具一鍵部署 Keepalived 服務

需求分析

使用 ansible 部署 keepalived 服務的過程中,需要實現:

根據不同的系統,自動選擇對應的安裝方式。可通過相關的環境變量結合when實現。

考慮到 rpm 安裝的方式會需要依賴一些基礎環境,以及版本較低,將採用源碼編譯的方式進行安裝。

爲保證適用更多的應用場景,將以環境變量的方式指定版本號,使用時修改變量值爲對應版本即可。


編寫 Ansible Role

2.1 初始化 role

創建相關的目錄存放對應的 yml 清單文件。

$ mkdir -p keepalived-install-role/{files,tasks,vars,templates}

2.2 設置環境變量

keepalived-install-role/vars/main.yml文件中設定相關的環境變量,使用時只需要修改值即可。

$ cat keepalived-install-role/vars/main.yml
---
#Keepalived版本
keepalived_version: 2.2.7
#安裝目錄
install_path: /etc/keepalived
#VIP地址:
virtual_ipaddress: 192.168.2.88/24
#虛擬路由ID號(主備必須一致),每個實例唯一
virtual_router_id: 216

2.3 下載文件

從官網下載源碼包,放入keepalived-install-role/files/目錄下。

cd keepalived-install-role/files/
$ wget https://keepalived.org/software/keepalived-2.2.7.tar.gz
$ md5sum keepalived-2.2.7.tar.gz
5f310b66a043a1fb31acf65af15e95bc  keepalived-2.2.7.tar.gz

2.4 編寫 tasks

keepalived-install-role/tasks目錄下創建對應的 tasks 任務文件。

$  cat keepalived-install/tasks/main.yml
---
- include_tasks: host-init.yml
- include_tasks: install-keepalived.yml
$ cat keepalived-install/tasks/host-init.yml 
---
#配置主機環境,如安裝依賴包等

- name: "安裝依賴包(Debian系統)"
  when: ansible_os_family == "Debian"
  apt:
    name:
      - build-essential   #編譯所需的基本工具
      - libssl-dev        #可能需要的 SSL 庫
      - libnl-3-dev       #libnl 庫
      - libnl-genl-3-dev  #libnl-genl 庫
    state: present
  
- name: "安裝依賴包(CentOS系統)"
  when: ansible_distribution == "CentOS"
  yum:
    name:
      - gcc
      - gcc-c++
      - make
      - openssl
      - openssl-devel
      - iproute
      - libnl
      - libnl-devel
      - libnfnetlink-devel
    state: present

#設置環境變量
- name: "收集目標主機IP清單"
  set_fact:
    target_hosts: "{{ ansible_play_hosts | map('extract', hostvars, 'ansible_host') | list }}"
- debug:
    msg: "{{ target_hosts }}"

- name: "生成環境變量(集羣列表)"
  set_fact:
    nacos_servers: "{{ target_hosts | map('regex_replace', '^(.*)$', '\\1:8848') | join(',') }}"

#設定MASTER(第一臺host)
- name: "設定MASTER(第一臺host)"
  set_fact:
    master_host:  "{{ target_hosts[0] }}"
- debug:
    msg: "MASTER節點爲:{{ master_host }}"

#查找 internal IP 所在網卡並設置爲環境變量
- name: "查找 internal IP 所在網卡"
  shell: "ip a s to {{ ansible_host }} | awk -F ': ' 'NF > 1 {print $2}'"
  register: ip_address_result
- name: "設置變量"
  set_fact:
    network_interface: "{{ ip_address_result.stdout | regex_replace('@.*', '') }}"
- debug:
    msg: "IP對應的網卡爲:{{ network_interface }}"

$ tree keepalived-install/
keepalived-install/
├── files
│   ├── keepalived-2.2.7.tar.gz
│   └── keepalived.service
├── tasks
│   ├── host-init.yml
│   ├── install-keepalived.yml
│   └── main.yml
├── templates
│   └── keepalived.conf.j2
└── vars
    └── main.yml

4 directories, 7 files

$ cat keepalived-install/tasks/install-keepalived.yml
---
#源碼編譯安裝keepalived
#若已經安裝,則跳過安裝
- name: "檢查服務是否安裝 <keepalived>"
  command: which keepalived
  register: keepalived_check
  ignore_errors: true

- name: "判斷服務安裝則進行安裝操作"
  when: keepalived_check.rc != 0
  block:
  - name: "創建安裝目錄 <{{ install_path }}>"
    file:
      name: "{{ install_path }}"
      state: directory
  - name: "分發安裝包"
    unarchive: 
      src: "keepalived-{{ keepalived_version }}.tar.gz"
      dest: "{{ install_path }}"
  - name: "編譯並安裝Keepalived"
    shell:
      cmd: "./configure --prefix=/usr/local/keepalived"
      chdir: "{{ install_path }}/keepalived-{{ keepalived_version }}"
  - name: "製作並安裝Keepalive"
    shell:
      cmd: "make && make install"
      chdir: "{{ install_path }}/keepalived-{{ keepalived_version }}"

- name: "分發配置文件"
  template: 
    src: keepalived.conf.j2
    dest: /etc/keepalived/keepalived.conf

- name: "啓動服務<keepalived>"
  systemd: 
    name: keepalived
    state: started
    enabled: true
    daemon_reload: yes

2.5 定製配置文件模板

基於Jinja2模板文件,根據變量自動配置MASTERBACKUP節點的配置參數,默認將第一個host作爲MASTER,其餘的均爲BACKUP

$ cat keepalived-install/templates/keepalived.conf.j2
! Configuration File for keepalived

global_defs {
   smtp_server localhost
   smtp_connect_timeout 30
   router_id {{ ansible_host }}
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

# VRRP 實例定義
vrrp_instance VI_1 {
{if ansible_host == master_host %}
    state MASTER
    priority 100
{else %}
    state BACKUP
    priority 80
{% endif %}

    nopreempt
    interface {{ network_interface }}
    virtual_router_id {{ virtual_router_id }}

    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }

    virtual_ipaddress {
        {{ virtual_ipaddress }}
    }
    track_script {
        check_nginx
    }
}

#健康檢查
vrrp_script check_nginx {
    script "/etc/keepalived/check_nginx.sh"
    interval 5
    weight -60
    fall 2
    rise 2
}

:相關參數可根據實際業務環境進行調整!


調用 role 部署任務

在編寫上述 role 完成後,就可以調用 roles 進行實際的部署應用了。

$ cat /etc/ansible/hosts
[lidabai]
192.168.2.61
192.168.2.62
192.168.2.63

[lidabai:vars]
ansible_ssh_user=root
ansible_ssh_pass=1
ansible_ssh_port=22
#ansible_python_interpreter=/usr/bin/python2
$ cat run-keepalived.yml
---
- hosts: lidabai
  become: yes
  roles:
  - keepalived-install
$ ansible-plabook run-keepalived.yml
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/drFUv6BQ9BOMLu_O3E7leQ