如何在RHEL系Linux上创建一个本地 YUM 仓库来管理 HAProxy 软件及其依赖项?
# CentOS/RHEL
yum -y install yum-utils createrepo
# yum info yum-utils
Loaded plugins: fastestmirror, langpacks, versionlock
Loading mirror speeds from cached hostfile
Excluding 1 update due to versionlock (use "yum versionlock status" to show it)
Installed Packages
Name : yum-utils
Arch : noarch
Version : 1.1.31
Release : 54.el7_8
Size : 337 k
Repo : installed
From repo : anaconda
Summary : Utilities based around the yum package manager
URL : http://yum.baseurl.org/download/yum-utils/
License : GPLv2+
Description : yum-utils is a collection of utilities and examples for the yum package
: manager. It includes utilities by different authors that make yum easier and
: more powerful to use. These tools include: debuginfo-install,
: find-repos-of-install, needs-restarting, package-cleanup, repoclosure,
: repodiff, repo-graph, repomanage, repoquery, repo-rss, reposync,
: repotrack, show-installed, show-changed-rco, verifytree, yumdownloader,
: yum-builddep, yum-complete-transaction, yum-config-manager, yum-debug-dump,
: yum-debug-restore and yum-groups-manager.
# yum info createrepo
Loaded plugins: fastestmirror, langpacks, versionlock
Loading mirror speeds from cached hostfile
Excluding 1 update due to versionlock (use "yum versionlock status" to show it)
Installed Packages
Name : createrepo
Arch : noarch
Version : 0.9.9
Release : 28.el7
Size : 302 k
Repo : installed
From repo : base
Summary : Creates a common metadata repository
URL : http://createrepo.baseurl.org/
License : GPLv2
Description : This utility will generate a common metadata repository from a directory of rpm
: packages.
mkdir -p haproxy-deps
repotrack haproxy -p haproxy-deps/
createrepo -v haproxy-deps/
# repotrack --help
Usage:
Repotrack: keep current on any given pkg and its deps. It will download the package(s) you
want to track and all of their dependencies
/usr/bin/repotrack [options] package1 [package2] [package..]
Options:
-h, --help show this help message and exit
-c CONFIG, --config=CONFIG
config file to use (defaults to /etc/yum.conf)
-a ARCH, --arch=ARCH check as if running the specified arch (default:
current arch)
-r REPOID, --repoid=REPOID
specify repo ids to query, can be specified multiple
times (default is all enabled)
--repofrompath=REPOFROMPATH
specify repoid & paths of additional repositories -
unique repoid and complete path required, can be
specified multiple times. Example:
--repofrompath=myrepo,/path/to/repo
-t, --tempcache Use a temp dir for storing/accessing yum-cache
-p DESTDIR, --download_path=DESTDIR
Path to download packages to
-u, --urls Just list urls of what would be downloaded, don't
download
-n, --newest Toggle downloading only the newest packages(defaults
to newest-only)
-q, --quiet Output as little as possible
# createrepo --help
Usage: genpkgmetadata.py [options]
Options:
--version show program's version number and exit
-h, --help show this help message and exit
-q, --quiet output nothing except for serious errors
-v, --verbose output more debugging info.
--profile output timing/profile info.
-x EXCLUDES, --excludes=EXCLUDES
files to exclude
--basedir=BASEDIR basedir for path to directories
-u BASEURL, --baseurl=BASEURL
baseurl to append on all files
-g GROUPFILE, --groupfile=GROUPFILE
path to groupfile to include in metadata
-s SUMTYPE, --checksum=SUMTYPE
specify the checksum type to use (default: sha256)
-p, --pretty make sure all xml generated is formatted
-c CACHEDIR, --cachedir=CACHEDIR
set path to cache dir
-C, --checkts check timestamps on files vs the metadata to see if we
need to update
-d, --database create sqlite database files: now default, see --no-
database to disable
--no-database do not create sqlite dbs of metadata
--update use the existing repodata to speed up creation of new
--update-md-path=UPDATE_MD_PATH
use the existing repodata for --update from this path
--skip-stat skip the stat() call on a --update, assumes if the
filename is the same then the file is still the same
(only use this if you're fairly trusting or gullible)
--split generate split media
-i PKGLIST, --pkglist=PKGLIST
use only the files listed in this file from the
directory specified
-n INCLUDEPKG, --includepkg=INCLUDEPKG
add this pkg to the list - can be specified multiple
times
-o OUTPUTDIR, --outputdir=OUTPUTDIR
<dir> = optional directory to output to
-S, --skip-symlinks ignore symlinks of packages
--changelog-limit=CHANGELOG_LIMIT
only import the last N changelog entries
--unique-md-filenames
include the file's checksum in the filename, helps
with proxies (default)
--simple-md-filenames
do not include the file's checksum in the filename
--retain-old-md=RETAIN_OLD_MD
keep around the latest (by timestamp) N copies of the
old repodata
--distro=DISTRO distro tag and optional cpeid:
--distro'cpeid,textname'
--content=CONTENT_TAGS
tags for the content in the repository
--repo=REPO_TAGS tags to describe the repository itself
--revision=REVISION user-specified revision for this repository
--deltas create delta rpms and metadata
--oldpackagedirs=OLDPACKAGE_PATHS
paths to look for older pkgs to delta against
--num-deltas=NUM_DELTAS
the number of older versions to make deltas against
--read-pkgs-list=READ_PKGS_LIST
output the paths to the pkgs actually read useful with
--update
--max-delta-rpm-size=MAX_DELTA_RPM_SIZE
max size of an rpm that to run deltarpm against (in
bytes)
--workers=WORKERS number of workers to spawn to read rpms
--compress-type=COMPRESS_TYPE
which compression type to use
这些命令是在 Linux 系统上创建一个本地 YUM 仓库来管理 HAProxy 软件及其依赖项的完整流程。
下面是对每个命令及其作用的详细解释:
1. mkdir -p haproxy-deps
- 作用:创建一个目录用于存放 HAProxy 及其依赖包。
-p参数表示如果目录不存在则创建,如果已存在也不会报错。- 这里创建了一个名为
haproxy-deps的目录,后续所有文件都会保存在这里。
2. repotrack haproxy -p haproxy-deps/
- 作用:下载 HAProxy 及其所有依赖包到指定目录。
repotrack是一个工具,用于递归下载一个软件包及其所有依赖项。haproxy指定要下载的软件包名称。-p haproxy-deps/指定下载的目标目录(即上一步创建的目录)。- 关键点:
- 会从配置的 YUM 仓库(如 CentOS-Base、EPEL 等)拉取 RPM 包。
- 包含 HAProxy 运行时需要的所有依赖(如 OpenSSL、PCRE 等),适合离线环境使用。
3. createrepo -v haproxy-deps/
- 作用:为下载的 RPM 包创建 YUM 仓库元数据。
createrepo是生成 YUM 仓库元数据的工具,使目录成为可被yum或dnf识别的仓库。-v表示输出详细日志(verbose)。haproxy-deps/是包含 RPM 包的目录路径。- 生成的文件:
repodata/子目录,内含primary.xml、filelists.xml等元数据文件。- 这些文件帮助 YUM 快速解析包之间的依赖关系。
整体流程的意义
- 离线部署:将 HAProxy 及其依赖集中下载到本地,便于在没有互联网连接的环境中安装。
- 版本控制:固定软件版本,避免因网络仓库更新导致版本不一致。
- 批量部署:可在多台机器上通过配置本地 YUM 源快速安装相同的环境。
补充说明
- 依赖工具安装:
- 如果系统没有
repotrack或createrepo,需先安装:
bash
yum install yum-utils createrepo# CentOS/RHEL
dnf install dnf-utils createrepo# Fedora/RHEL 8+
- 使用本地仓库:
- 将
haproxy-deps目录复制到目标机器。 - 创建 YUM 源配置文件(如
/etc/yum.repos.d/local.repo):
ini
[local-haproxy]
name=Local HAProxy Repository
baseurl=file:///path/to/haproxy-deps
enabled=1
gpgcheck=0
- 安装 HAProxy:
bash
yum -y install haproxy
- 替代方案:
- 如果只需下载单个包(不递归依赖),可用
yumdownloader:
bash
yumdownloader haproxy --destdir=haproxy-deps
通过以上步骤,你构建了一个完整的本地 YUM 仓库,便于管理和分发 HAProxy 软件。