• 通过rpmbuild构建Elasticsearch-7.14.2-search-guard的RPM包


    系列文章目录

    rpmbuild从入门到放弃
    search-guard插件使用入门手册



    前言

    不管是源码安装elasticsearch还是通过elastic官网的rpm包进行安装,在安装完成后都需要进行手动配置elastic的安全认证,这样不管从安装效率还是维护都比较麻烦,为了让安装和开启鉴权一起完成,本篇文章就使用rpmbuild自行构建elasticsearch-7.14.2的RPM包,安全认证插件使用的是search-guard,具体构建方式看下方文章即可。阅读下方文章前,请先阅读顶部的两章链接文件,先对整体知识有个大体上的了解。


    提示:已在本地centos 7环境执行过构建测试、安装、使用流程,目前未发现异常

    一、资源准备

    	1、rpmbuild工具安装此处不再描述,详情查看顶部文章链接即可
    
    	2、下载elasticsearch-7.14.2的tar包
    	[root@python2 SOURCES]# wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.14.2-linux-x86_64.tar.gz
    
    	3、下载elasticsearch版本对应的search-guard插件
    	[root@python2 SOURCES]# wget https://maven.search-guard.com/search-guard-suite-release/com/floragunn/search-guard-suite-plugin/7.14.2-52.3.0/search-guard-suite-plugin-7.14.2-52.3.0.zip
    
    	[root@python2 SOURCES]# https://docs.search-guard.com/latest/search-guard-installation
    

    至此源码包准备完成,下面开始编写spec文件

    二、spec文件

    1.基础信息

    文件如下(示例):

    #自定义宏,相当于Linux中"Key-Value"变量形式
    #--->名称
    %define Name elasticsearch
    #--->版本
    %define Version 7.14.2
    #--->本rpm包中需更换的配置文件
    
    #--->本rpm包默认安装的路径
    %define InstallPath /export/server/elasticsearch-7.14.2
    #-->rpm包封装进去的脚本
    %define configYaml elasticsearch.yml
    %define search_guard  search-guard-suite-plugin-7.14.2-52.3.0.zip
    %define search_tlstools search-guard-tlstool-1.7.tar.gz
    %define __spec_install_post %{nil}
    %define _build_id_links none
    # 软件包的名称 
    Name: %{Name}
    # 软件包的版本 
    Version: %{Version}
    # 软件包发布序列号,1表示第几次打包 %{?dist} 会再包名中添加操作系统系统
    Release: 1
    # 软件包的概要信息,不要超过50个 
    # 软件授权方式 
    License: BSD
    
    # 软件分类
    Group: System Middleware
    # 源代码软件包的官方地址或源码包的下载地址 
    URL: https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.14.2-linux-x86_64.tar.gz
    
    Summary: The elasticsearch for centos 7.14.2 x86_64.
    # 源代码软件包的名字 
    Source0: %{Name}-%{Version}-linux-x86_64.tar.gz
    Source3: %{configYaml}
    Source4: %{search_guard}
    Source5: %{search_tlstools}
    
    # install使用的虚拟目录,安装后就在该目录下打包 
    AutoReqProv: no
    #制作过程中用到的软件包
    Requires: bash
    
    #软件包详细描述信息 
    %description
    This is %{Name}
    

    2.%prep

    解压源码包部分
    如下(示例):

    # 安装前的准备工作,一般用于解压源码包
    %prep
    #解压并cd到相关目录  tar xf SOURCES/xxx.tar.gz -C BUILD && cd BUILD
    %setup -q -n elasticsearch-7.14.2  #-q 静默 不输出信息  -n 指定解压后的名称
    

    3.%Install

    此部分主要用于解压后的源码安装及目录创建等工作
    在该文件中主要实现目录创建、search-guard插件的安装及密钥文件的创建工作,并将准备好的文件和目录拷贝到BUILDROOT目录下

    # 源码安装
    #目前还是在/export/rpmbuild/BUILD/目录中,执行以下操作
    #rm -rf /export/rpmbuild/BUILDROOT
    %install
    %{_rm} -rf %{buildroot} # 清理之前的安装
    mkdir -p /tmp/search-guard-tlstool
    tar -zxvf %{SOURCE5} -C /tmp/search-guard-tlstool
    cd /tmp/search-guard-tlstool/config/ && cp example.yml tlsconfig.yml
    sed -i '/^#/d; /^$/d' tlsconfig.yml
    sed -i '/root\.ca\.example\.com/c\      dn: CN=root.ca.com.local,OU=CA,O=com,DC=com,DC=local' tlsconfig.yml
    sed -i '/signing\.ca\.example\.com/c\      dn: CN=root.ca.com.local,OU=CA,O=com,DC=com,DC=local' tlsconfig.yml
    sed -i 's/#nodesDn/nodesDn/' tlsconfig.yml
    sed -i 's/#- "CN=\*.example.com,OU=Ops,O=Example Com\\\\, Inc.,DC=example,DC=com"/- "CN=\*.com.local,OU=Ops,O=com,DC=com,DC=local"/' tlsconfig.yml
    sed -i 's/# nodeOid: "1.2.3.4.5.5"/nodeOid: "1.2.3.4.5.5"/' tlsconfig.yml
    sed -i '88,99d' tlsconfig.yml
    sed -i 's/- name: node1/- name: esnode/' tlsconfig.yml
    sed -i '/dn: CN=node1\.example\.com*/c\    dn: CN=esnode.com.local,OU=Ops,O=com,DC=com,DC=local' tlsconfig.yml
    sed -i 's/dns: node1.example.com/dns: esnode/' tlsconfig.yml
    sed -i '/dn: CN=spock\.example\.com/c\    dn: CN=spock.com.local,OU=Ops,O=com,DC=com,DC=local' tlsconfig.yml
    sed -i '/dn: CN=kirk\.example\.com/c\    dn: CN=kirk.com.local,OU=Ops,O=com,DC=com,DC=local' tlsconfig.yml
    sed -i 's/pkPassword: auto/pkPassword: none/' tlsconfig.yml
    cd /tmp/search-guard-tlstool/tools/
    ./sgtlstool.sh -c ../config/tlsconfig.yml -ca
    ./sgtlstool.sh -c ../config/tlsconfig.yml -crt
    cd out/ && chmod 644 root-ca.* signing-ca.* esnode* kirk.*
    mkdir -p %{buildroot}%{InstallPath}/plugins/search-guard-7
    mkdir -p %{buildroot}%{InstallPath}/{modules,logs,data,lib,config,bin}
    cp -rp /tmp/search-guard-tlstool/tools/out/{root-ca.pem,kirk.pem,kirk.key,esnode.pem,esnode.key} %{buildroot}%{InstallPath}/config
    
    # 复制自定义的或额外的配置文件和脚本
    unzip %{SOURCE4} -d %{buildroot}%{InstallPath}/plugins/search-guard-7/
    cp -rp %{_builddir}/%{Name}-%{Version}/* %{buildroot}%{InstallPath}/
    rm -rf %{buildroot}%{InstallPath}/jdk
    

    4.%file

    主要用于安装后目录下都包含哪些文件或子目录

    %files
    %defattr(-,elasticsearch,elasticsearch,-)
    %dir %{InstallPath}
    %dir %{InstallPath}/bin
    %dir %{InstallPath}/config
    %dir %{InstallPath}/lib
    %dir %{InstallPath}/logs
    %dir %{InstallPath}/data
    %dir %{InstallPath}/modules
    %dir %{InstallPath}/plugins
    %{InstallPath}/bin/*
    %{InstallPath}/config/*
    %{InstallPath}/lib/*
    %{InstallPath}/modules/*
    %{InstallPath}/plugins/*
    %{InstallPath}/NOTICE.txt
    %{InstallPath}/LICENSE.txt
    %{InstallPath}/README.asciidoc
    

    5.%post

    主要用于安装rpm后做的相关操作,比如启动进程、创建用户等工作。在此文件中,该部分主要用于替换elasticsearch.yml文件、授权、设置所需要的内核参数、初始化search-guard插件并重置es密码

    %post
    #!/bin/sh
    cat >%{InstallPath}/config/elasticsearch.yml<cluster.name: cityos
    
    node.name: wangyingkai-test-01
    
    path.data: %{InstallPath}/data
    path.logs: %{InstallPath}/logs
    # Lock the memory on startup:
    #bootstrap.memory_lock: true
    network.host: $local_ip
    http.port: 9200
    
    discovery.seed_hosts: ["$local_ip"]
    cluster.initial_master_nodes: ["$local_ip"]
    #discovery.zen.minimum_master_nodes:
    
    #gateway.recover_after_nodes: 3
    
    # Search Duard Configure
    action.auto_create_index: true
    node.master: true
    node.data: true
    searchguard.ssl.transport.pemcert_filepath: esnode.pem
    searchguard.ssl.transport.pemkey_filepath: esnode.key
    searchguard.ssl.transport.pemtrustedcas_filepath: root-ca.pem
    searchguard.ssl.transport.enforce_hostname_verification: false
    searchguard.ssl.transport.resolve_hostname: false
    searchguard.authcz.admin_dn:
    - CN=kirk.com.local,OU=Ops,O=com,DC=com,DC=local
    searchguard.cert.oid: 1.2.3.4.5.5
    EOF
    
    # edit config file 修改yml文件
    sed -i "s/node.name:.*/node.name: $local_ip/g" %{InstallPath}/config/elasticsearch.yml
    
    # useradd elasticsearch 创建用户
    id elasticsearch > /dev/null 2>&1
    if [ $? == 0 ];then
            echo 'elasticsearch user exist.'
    else
            useradd -s /sbin/nologin elasticsearch
    fi
    
    #chown 设置目录权限
    chown -R elasticsearch:elasticsearch %{InstallPath}
    
    #设置kernel参数
    if ! grep -w "fs.file-max" /etc/sysctl.conf |grep -v ^# >/dev/null
    then
       sed -i '$a\\nfs.file-max = 65536' /etc/sysctl.conf
    fi
    
    if ! grep -w "vm.max_map_count" /etc/sysctl.conf |grep -v ^#  >/dev/null
    then
       sed -i '$a\\nvm.max_map_count = 262144' /etc/sysctl.conf
    fi
    sysctl -p > /dev/null
    # 启动 Elasticsearch 服务
    su elasticsearch -c "%{InstallPath}/bin/elasticsearch -d -p %{InstallPath}/%{Name}.pid"
    # 等待 Elasticsearch 启动,修改密码
    until curl -s http://$local_ip:9200 > /dev/null; do
        echo "Waiting for Elasticsearch to start..."
        sleep 10
    done
    
    # 生成哈希密码并更新配置文件
    HASHED_PASSWORD=$(%{InstallPath}/plugins/search-guard-7/tools/hash.sh -p "0gvzJr66iNs5")
    # 转义特殊字符
    ESCAPED_HASHED_PASSWORD=$(echo "$HASHED_PASSWORD" | sed 's/[\/&]/\\&/g')
    # 更新 sg_internal_users.yml 文件
    if [ -n "$ESCAPED_HASHED_PASSWORD" ]; then
       sed -i 's/^  hash: ".*"/  hash: "'"$ESCAPED_HASHED_PASSWORD"'"/' %{InstallPath}/plugins/search-guard-7/sgconfig/sg_internal_users.yml
       sed -i 's/\r//g' %{InstallPath}/plugins/search-guard-7/sgconfig/sg_internal_users.yml
    fi
    
    #修改完配置文件并执行初始化
    cd %{InstallPath}/plugins/search-guard-7/tools && sh sgadmin.sh -h $local_ip -cd ../sgconfig -key ../../../config/kirk.key -cert ../../../config/kirk.pem -cacert ../../../config/root-ca.pem -nhnv -icl
    

    6.%postun

    这部分主要用于卸载后需要做的任务,比如:删除目录等任务工作,在该spec文件中,这部分主要用于根据pid是否存在停止程序、删除目录,完成相应的卸载工作

    %postun -p /bin/sh
    #!/bin/bash
    PID_FILE="%{InstallPath}/%{Name}.pid"
    DIR_TO_REMOVE="%{InstallPath}"
    
    # Check if the PID file exists and read the PID from it
    if [ -f "$PID_FILE" ]; then
        PID=$(cat "$PID_FILE")
        # Check if a process with the given PID exists
        if ps -p $PID > /dev/null 2>&1; then
            # Kill the process with the PID found in the PID file
            kill $PID
            # Wait a moment to allow the process to terminate
            sleep 1
        fi
        # Remove the PID file
        rm -f "$PID_FILE"
    fi
    
    # Remove the directory
    rm -rf "$DIR_TO_REMOVE"
    

    三、成果演示

    1.执行构建过程图示例

    在这里插入图片描述
    在这里插入图片描述

    2.执行安装RPM包示例

    在这里插入图片描述
    在这里插入图片描述

    3.进程检查

    在这里插入图片描述

    4.访问esApi

    在这里插入图片描述在这里插入图片描述


    总结

    通过完成本篇文章,已经对rombuild有了60%的理解和使用,熟能生巧,希望也对大家的实际工作能起到帮助作用。加油!!!

  • 相关阅读:
    基于SSM的学生疫情信息管理系统设计与实现
    【BOOST C++ 7 内部进程】(3)同步
    【毕业设计】单片机远程wifi红外无接触体温测量系统 - 物联网 stm32
    苹果笔记本电脑可以玩steam游戏吗 MacBook支持玩steam游戏吗 在Steam上玩黑神话悟空3A大作 苹果Mac怎么下载steam
    CAP理论和BASE思想
    Linux命令之文件管理相关命令
    【SA8295P 源码分析 (二)】46 - OpenWFD Server 启动流程 之 /dev/openwfd_server_0 管道事务源码分析
    数据库系统 整体结构化 的理解
    java高考填报志愿综合参考系统springboot+vue
    03-数据链路层
  • 原文地址:https://blog.csdn.net/weixin_50902636/article/details/140300029