分类
devops

使用repotrack和createrepo创建一个yum本地仓库


Last Updated on 2022-08-30

创建仓库

step1

准备一个可以联网的机器
设置必要的远程仓库
比如 yum install epel-release

yum install epel-release
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
cat > /etc/yum.repos.d/kubernetes.repo <<EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF


yum makecache fast -y

step2

安装yum-utils和createrepo工具包

yum install createrepo yum-utils

step3

准备目录

mkdir -p /opt/localrepo/centos/7/os/x86_64/Packages
cd /opt/localrepo/centos/7/os/x86_64/Packages

for i in htop bridge-utils nfs-utils unzip tree ca-certificates traceroute iptables whois psmisc bc nmap openssh-server lsof net-tools curl wget git vim-common jq socat conntrack-tools ipvsadm ipset sysstat libseccomp bzip2 libtool flex bison pcre-devel zlib-devel openssl openssl-devel bind-utils libnl3 libnl3-devel systemd-devel libuuid-devel device-mapper-persistent-data lvm2;do
    echo $i && repotrack $i -q
done


repotrack \
bind-export-libs \
dhclient \
bind-libs \
e2fsprogs-libs \
libcom_err \
pcre-devel \
e2fsprogs \
libselinux libselinux-python libselinux-utils


#repotrack docker-ce-20.10.17-3.el7.x86_64
#repotrack kubeadm-1.23.9
#repotrack kubelet-1.23.9
#repotrack kubectl-1.23.9

yum install -y --nogpgcheck --downloadonly --downloaddir=/opt/localrepo/centos/7/os/x86_64/Packages docker-ce-20.10.17-3.el7.x86_64
yum install -y --nogpgcheck --downloadonly --downloaddir=/opt/localrepo/centos/7/os/x86_64/Packages kubelet-1.23.9-0.x86_64 kubeadm-1.23.9-0.x86_64 kubectl-1.23.9-0.x86_64


# reposync --repoid=docker-ce-stable --newest-only

step4

createrepo --database /opt/localrepo/centos/7/os/x86_64

step5

启动一个http服务,这里使用docker也可以使用nginx等

docker run -d --network host -v /opt/localrepo:/var/web/html --name yum python:3.7-slim python3 -m http.server --bind 0.0.0.0 --directory /var/web/html 42344

也可以使用caddy

curl -fsSL -o caddy.tar.gz https://github.com/caddyserver/caddy/releases/download/v2.5.2/caddy_2.5.2_linux_amd64.tar.gz
tar -xvzf caddy.tar.gz
./caddy file-server --root /opt/localrepo/ --browse --listen 0.0.0.0:42344

也可以使用static-file-server

curl -fsSL -o static-file-server https://github.com/halverneus/static-file-server/releases/download/v1.8.8/static-file-server-v1.8.8-linux-amd64
chmod +x static-file-server
FOLDER=/opt/localrepo PORT=42344 HOST=0.0.0.0 SHOW_LISTING=true ./static-file-server

使用仓库

step6

使用local仓库

cat > /etc/yum.repos.d/local.repo <<EOF
[local]
name=local
baseurl=http://192.168.9.217:42344/centos/7/os/x86_64
#baseurl=file:///opt/localrepo/centos/7/os/x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
EOF

yum --disablerepo=\* --enablerepo="local" makecache fast -y

yum install -y --disablerepo=\* --enablerepo="local" \
htop bridge-utils nfs-utils unzip tree ca-certificates traceroute iptables whois psmisc bc nmap openssh-server lsof net-tools curl wget git vim-common jq socat conntrack-tools ipvsadm ipset sysstat libseccomp bzip2 libtool flex bison pcre-devel zlib-devel openssl openssl-devel bind-utils libnl3 libnl3-devel systemd-devel libuuid-devel device-mapper-persistent-data lvm2