Ansible自動化部署安裝openGauss3.1企業版單機

語言: CN / TW / HK

 

文章目錄

  • 一、背景
  • 二、環境準備
  • 三、具體實施步驟
    • 3.1、安裝ansible
    • 3.2、配置主機清單
    • 3.3、測試主機連通性
    • 3.4、創建相關目錄
    • 3.5、下載openGauss軟件包到files目錄
    • 3.6、創建變量文件
    • 3.7、創建安裝時需要的xml模板
    • 3.8、創建任務文件
  • 四、執行自動化安裝
    • 4.1、校驗語法
    • 4.2、自動化安裝openGauss
    • 4.3、安裝完成後驗證

 

一、背景

由於IT建設的快速發展,當數據中心業務突增,需要快速部署多套的數據庫時,給運維工作帶來了不小的壓力和挑戰,作為運維人員該如何面對面對這種困境呢?另外由於個人的習慣等也會導致所部署的環境不一定與規劃完全一致,那麼對以後的運維也會產生一定的負面影響。很顯然,這種傳統的方式已經無法適應當前的情景了,自動化運維應運而生,ansible在自動化運維和devops 的應用中嶄露頭角。

本文基於ansible工具實現 openGauss 的一鍵批量部署,傳統的部署方式是先修改系統配置、安裝依賴包、創建omm用户和組、配置環境變量、上傳安裝包以及解壓、安裝等步驟。

按照這個流程和思路,我們把這些操作弄成劇本編排(playbook),交給ansible來做。

二、環境準備

2台主機:
一台為Ansible的管理主機(10.10.10.142),操作系統為CentOS Linux release 7.9.2009 (Core);
另外一台為需要部署openGauss的主機(10.10.10.150),操作系統為CentOS Linux release 7.9.2009 (Core)。

三、具體實施步驟

3.1、安裝ansible

–在10.10.10.142上進行安裝Ansible
yum install epel-release -y
yum install ansible –y

–配置/etc/ansible/ansible.cfg

# grep -v '^#' /etc/ansible/ansible.cfg |sed '/^$/d'

[defaults]
host_key_checking = False
callback_whitelist = timer,profile_roles,log_plays
log_path = /var/log/ansible.log
strategy = free
bin_ansible_callbacks = True
[inventory]
[privilege_escalation]
[paramiko_connection]
[ssh_connection]
[persistent_connection]
[accelerate]
[selinux]
[colors]
[diff]
[callback_log_plays]
log_folder=/tmp/ansible/hosts/

3.2、配置主機清單

修改主機清單/etc/ansible/hosts,添加主機列表

# cat /etc/ansible/hosts
[openGaussdb]
10.10.10.150 ansible_ssh_user=root ansible_ssh_pass=123456

###10.10.10.150為本次需要安裝openGauss的主機

3.3、測試主機連通性

# ansible -i /etc/ansible/hosts openGaussdb -m ping

在這裏插入圖片描述

3.4、創建相關目錄

[root@cs79-mysql:~]# cd /etc/ansible/roles/
[root@cs79-mysql:/etc/ansible/roles]# mkdir -p openGauss_Install/{files,vars,tasks,templates}
[root@cs79-mysql:/etc/ansible/roles]# tree openGauss_Install/
openGauss_Install/
├── files
├── tasks
├── templates
└── vars

4 directories, 0 files

上述目錄主要作用如下:
files:存放需要同步到異地服務器的安裝文件或者配置文件;
tasks:openGauss安裝過程需要進行的執行的任務;
templates:用於執行openGauss安裝的模板文件,一般為腳本;
vars:安裝openGauss定義的變量;

3.5、下載openGauss軟件包到files目錄

安裝包下載地址:https://opengauss.org/zh/download.html

[root@cs79-mysql:/etc/ansible/roles]# cd openGauss_Install/files/
[root@cs79-mysql:/etc/ansible/roles/openGauss_Install/files]# # wget https://opengauss.obs.cn-south-1.myhuaweicloud.com/3.1.0/x86/openGauss-3.1.0-CentOS-64bit-all.tar.gz
–2022-10-09 21:42:01-- https://opengauss.obs.cn-south-1.myhuaweicloud.com/3.1.0/x86/openGauss-3.1.0-CentOS-64bit-all.tar.gz
Resolving opengauss.obs.cn-south-1.myhuaweicloud.com (opengauss.obs.cn-south-1.myhuaweicloud.com)… 121.37.63.38, 139.159.208.64, 139.159.208.243
Connecting to opengauss.obs.cn-south-1.myhuaweicloud.com (opengauss.obs.cn-south-1.myhuaweicloud.com)|121.37.63.38|:443… connected.
HTTP request sent, awaiting response… 200 OK
Length: 123022609 (117M) [application/gzip]
Saving to: ‘openGauss-3.1.0-CentOS-64bit-all.tar.gz’

100%[==================================================================================================================================================================================================>] 123,022,609 38.4MB/s in 3.2s

2022-10-09 21:42:04 (37.1 MB/s) - ‘openGauss-3.1.0-CentOS-64bit-all.tar.gz’ saved [123022609/123022609]

3.6、創建變量文件

[root@cs79-mysql:~]# vi /etc/ansible/roles/openGauss_Install/vars/main.yml

#安裝包名稱
openGauss_software: openGauss-3.1.0-CentOS-64bit-all.tar.gz
#解壓目錄
install_dir: /opt/software/openGauss
#omm用户密碼
omm_password: openGauss@123
#數據庫密碼
db_password: openGauss@123

3.7、創建安裝時需要的xml模板

[root@cs79-mysql:~]# vi /etc/ansible/roles/openGauss_Install/templates/cluster_config.j2

<?xml version="1.0" encoding="UTF-8"?>
<ROOT>
    <!-- openGauss整體信息 -->
    <CLUSTER>
        <!-- 數據庫名稱 -->
        <PARAM name="clusterName" value="dbCluster" />
        <!-- 數據庫節點名稱(hostname) -->
        <PARAM name="nodeNames" value="{ { ansible_hostname }}" />
        <!-- 數據庫安裝目錄-->
        <PARAM name="gaussdbAppPath" value="/opt/huawei/install/app" />
        <!-- 日誌目錄-->
        <PARAM name="gaussdbLogPath" value="/var/log/omm" />
        <!-- 臨時文件目錄-->
        <PARAM name="tmpMppdbPath" value="/opt/huawei/tmp" />
        <!-- 數據庫工具目錄-->
        <PARAM name="gaussdbToolPath" value="/opt/huawei/install/om" />
        <!-- 數據庫core文件目錄-->
        <PARAM name="corePath" value="/opt/huawei/corefile" />
        <!-- 節點IP,與數據庫節點名稱列表一一對應 -->
        <PARAM name="backIp1s" value="{ { inventory_hostname }}"/>
    </CLUSTER>
    <!-- 每台服務器上的節點部署信息 -->
    <DEVICELIST>
        <!-- 節點1上的部署信息 -->
        <DEVICE sn="1000001">
            <!-- 節點1的主機名稱 -->
            <PARAM name="name" value="{ { ansible_hostname }}"/>
            <!-- 節點1所在的AZ及AZ優先級 -->
            <PARAM name="azName" value="AZ1"/>
            <PARAM name="azPriority" value="1"/>
            <!-- 節點1的IP,如果服務器只有一個網卡可用,將backIP1和sshIP1配置成同一個IP -->
            <PARAM name="backIp1" value="{ { inventory_hostname }}"/>
            <PARAM name="sshIp1" value="{ { inventory_hostname }}"/>

            <!--dbnode-->
            <PARAM name="dataNum" value="1"/>
            <PARAM name="dataPortBase" value="26000"/>
            <PARAM name="dataNode1" value="/opt/huawei/install/data/dn01"/>
            <PARAM name="dataNode1_syncNum" value="0"/>
        </DEVICE>
    </DEVICELIST>
</ROOT>

3.8、創建任務文件

[root@cs79-mysql:~]# vi /etc/ansible/roles/openGauss_Install/tasks/main.yml
- name: 關閉防火牆
  shell: systemctl disable firewalld.service && systemctl stop firewalld.service
  ignore_errors: true
  tags: 01_os_syscfg
- name: 關閉selinux
  shell: sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
  ignore_errors: true
  tags: 01_os_syscfg
- name: 設置時區
  shell: timedatectl set-timezone Asia/Shanghai
  tags: 01_os_syscfg
- name: 關閉RemoveIPC
  lineinfile:
    path:  /etc/systemd/logind.conf
    state: present
    line: "RemoveIPC=no"
  tags: 01_os_syscfg
- name: 重啟systemd-logind服務
  shell: systemctl daemon-reload && systemctl restart systemd-logind
  tags: 01_os_syscfg
- name: 創建組
  group: name=dbgrp gid=2000
  tags: 02_user_add
- name: 創建用户
  user:
    name=omm  uid=2000 group=dbgrp
  tags: 02_user_add
- name: 修改密碼
  shell: echo "{
   {omm_password}}" | passwd --stdin omm
  tags: 02_user_add
- name: 新建目錄
  file: path="{
   {item}}"  state=directory mode=0755 owner=omm group=dbgrp
  with_items:
      - /opt/software/
      - /opt/software/openGauss
  tags: 03_unzip_db
- name: 上傳安裝包
  copy: src={
   {
   openGauss_software}} dest={
   {
   install_dir}}  owner=omm group=dbgrp mode=0644
  tags: install
  tags: 03_unzip_db
- name: "解壓軟件包"
  shell: cd {
   {
   install_dir}} && tar -zxvf *all.tar.gz && tar -zxvf *om.tar.gz
  become: yes
  become_user: omm
  tags: 03_unzip_db
- name: "安裝依賴包"
  yum: name="libaio-devel,flex,bison,ncurses-devel,glibc-devel,patch,redhat-lsb-core,python3,bzip2,readline-devel,net-tools,tar,gcc,gcc-c++" state=installed 
  tags: 04_os_yum
- name: 替換python3版本
  shell: mv /usr/bin/python  /usr/bin/python2_bak && ln -s /usr/bin/python3 /usr/bin/python && python -V
  tags: 05_replace_py
- name: 配置xml文件
  template: src=cluster_config.j2 dest={
   {
   install_dir}}/clusterconfig.xml
  tags: 06_config_xml
- name: 執行預安裝腳本
  shell: '{
   {install_dir}}/script/gs_preinstall -U omm -G dbgrp -X {
   {install_dir}}/clusterconfig.xml --non-interactive'
  register: preinstall
  tags: 07_pre_install
- debug: var=preinstall.stdout_lines
  ignore_errors: true
  tags: 07_pre_install
- name: 檢查預安裝環境
  shell: '{
   {install_dir}}/script/gs_checkos -i A -h {
   { ansible_hostname }} --detail'
  register: checkos
  tags: 08_check_os
- debug: var=checkos.stdout_lines
  ignore_errors: true
  tags: 08_check_os
- name: 更改權限
  shell: chmod -R 755 {
   {
   install_dir}}
  tags: 09_gs_install
- name: 執行gs_install
  shell: su - omm -c "{
   {install_dir}}/script/gs_install -X {
   {install_dir}}/clusterconfig.xml --gsinit-parameter="--pwpasswd={
   {
   db_password}}""
  register: gsinstall
  tags: 09_gs_install
- debug: var=gsinstall.stdout_lines
  ignore_errors: true
  tags: 09_gs_install
- name: 啟動數據庫
  shell: ss -anpt|grep 26000 && su - omm -c "gs_ctl restart " || su - omm -c "gs_om -t start "
  tags: 10_db_start
- name: "登錄數據庫"
  shell: ss -anpt|grep 26000 && su - omm -c "gsql -d postgres -p26000 -r -l"
  tags: 10_db_start

3.9、創建劇本調用文件
[root@cs79-mysql:~]# vi /etc/ansible/playbook/InstallopenGauss.yml

  • name: Install openGauss
    hosts: openGaussdb
    remote_user: root
    roles:
    • openGauss_Install

四、執行自動化安裝

4.1、校驗語法

# ansible-playbook -C /etc/ansible/playbook/InstallopenGauss.yml 

在這裏插入圖片描述

校驗語法通過後,執行下一步安裝

4.2、自動化安裝openGauss

# ansible-playbook /etc/ansible/playbook/InstallopenGauss.yml

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

4.3、安裝完成後驗證

在這裏插入圖片描述

至此,整個自動化部署openGauss完畢,如果有多台機器需要部署,添加主機相關信息到/etc/ansible/hosts,再執行ansible-playbook即可。

作者:鴻驚九天
openGauss: 一款高性能、高安全、高可靠的企業級開源關係型數據庫。

🍒如果您覺得博主的文章還不錯或者有幫助的話,請關注一下博主,如果三連點贊評論收藏就更好啦!謝謝各位大佬給予的支持!