1. 程式人生 > >基於滴滴雲的 yum 源伺服器的搭建(一)

基於滴滴雲的 yum 源伺服器的搭建(一)

CentOS 下 yum 源的搭建

我們在安裝完 Linux 作業系統後,往往會需要安裝好多軟體,同時我們在編譯程式碼時也需要安裝很多依賴的 yum軟體包,但是我們的伺服器又不能連線外網的話,這個就很麻煩。

如果我們公司區域網內有自己的 yum 源,我們就可以很方便的使用 yum 來安裝軟體了。如果可以的話,我們也可以對外提供 yum 源,定期更新軟體包,為 IT 工作人員提供公網公開的 yum 源。

以下是國內幾個常用的 yum 源:

今天我們就簡單介紹一下 yum 源的搭建方法。

我們分幾種不同的場景來搭建。

第一種場景

我們伺服器本地就有 ISO 映象,ISO 映象裡帶著我們軟體開發所需要的所有 yum 包,也就是本地檔案的 yum 源。

1. 準備 ISO 映象

這一步可以從 CentOS 官網下載或者從光盤獲取。

官網下載地址:https://www.centos.org/download/

2. 掛載映象

如果是從官網下載的 ISO 檔案,那我們首先需要將 ISO 掛在到系統指定路徑下:

[[email protected]
home]# mkdir /mnt/CentOS [[email protected] home]# mount -t iso9660 -o loop /home/CentOS-7-x86_64-DVD-1810.iso /mnt/CentOS mount: /dev/loop0 防寫,將以只讀方式掛載 [[email protected] home]# cd /mnt/CentOS/ [[email protected] CentOS]# ls CentOS_BuildTag EFI EULA GPL images isolinux LiveOS Packages repodata RPM-GPG-KEY-CentOS-7 RPM-GPG-KEY-CentOS-Testing-7 TRANS.TBL [
[email protected]
CentOS]# mount -t iso9660 -o loop /home/CentOS-7-x86_64-DVD-1810.iso /mnt/CentOS

主要是這條命令:

mount -t iso9660 -o loop /home/CentOS-7-x86_64-DVD-1810.iso /mnt/CentOS

ISO 檔案具體路徑根據你實際存放的路徑為準。

掛載後如下:

[[email protected] CentOS]# pwd
/mnt/CentOS
[[email protected] CentOS]#
[[email protected] CentOS]# ls -al
總用量 689
drwxrwxr-x  8 root root   2048 11月 25 23:53 .
drwxr-xr-x. 3 root root     20 12月 31 04:36 ..
-rw-rw-r--  1 root root     14 11月 25 16:01 CentOS_BuildTag
-rw-r--r--  1 root root     29 11月 25 16:16 .discinfo
drwxr-xr-x  3 root root   2048 11月 25 16:20 EFI
-rw-rw-r--  1 root root    227 8月  30 2017 EULA
-rw-rw-r--  1 root root  18009 12月  9 2015 GPL
drwxr-xr-x  3 root root   2048 11月 25 16:21 images
drwxr-xr-x  2 root root   2048 11月 25 16:20 isolinux
drwxr-xr-x  2 root root   2048 11月 25 16:20 LiveOS
drwxrwxr-x  2 root root 663552 11月 25 23:52 Packages
drwxrwxr-x  2 root root   4096 11月 25 23:53 repodata
-rw-rw-r--  1 root root   1690 12月  9 2015 RPM-GPG-KEY-CentOS-7
-rw-rw-r--  1 root root   1690 12月  9 2015 RPM-GPG-KEY-CentOS-Testing-7
-r--r--r--  1 root root   2883 11月 25 23:54 TRANS.TBL
-rw-r--r--  1 root root    354 11月 25 16:21 .treeinfo
[[email protected] CentOS]#

假如我們使用的裝置是光碟 sr0,則使用如下方式掛載:

mount /dev/sr0 /mnt/CentOS

3. 配置 yum 源

備份系統原有的 yum 源
mkdir /etc/yum.repos.d/bak
mv /etc/yum.repos.d/* /etc/yum.repos.d/bak/
然後清除本地所有快取
yum clean all
編寫自己的 repo 檔案
[centos7]
name=CentOS-$releasever - Media
baseurl=file:///mnt/CentOS/
gpgcheck=0
enabled=1
gpgkey=file:///mnt/CentOS/RPM-GPG-KEY-CentOS-7

我們已經把所有原來的 repo 檔案做好了備份,最後把自己的 repo 放進來,然後重建快取如下:

yum makecache 

重建快取,將它儲存為 centos7.repo,放在 /etc/yum.repos.d/ 目錄下。GPGkey 可以不指定,只是你開啟 GPGcheck 後需要指定路徑。

重建 yum 快取(元資料)
[[email protected] yum.repos.d]# yum makecache
已載入外掛:fastestmirror
centos7                                                                                                                                | 3.6 kB  00:00:00     
(1/4): centos7/group_gz                                                                                                                | 166 kB  00:00:00     
(2/4): centos7/primary_db                                                                                                              | 3.1 MB  00:00:00     
(3/4): centos7/other_db                                                                                                                | 1.3 MB  00:00:00     
(4/4): centos7/filelists_db                                                                                                            | 3.2 MB  00:00:00     
Determining fastest mirrors
元資料快取已建立

4. 測試一下

[[email protected] yum.repos.d]# yum search vim
已載入外掛:fastestmirror
Loading mirror speeds from cached hostfile
====================================================================== N/S matched: vim ======================================================================
vim-X11.x86_64 : The VIM version of the vi editor for the X Window System
vim-common.x86_64 : The common files needed by any version of the VIM editor
vim-enhanced.x86_64 : A version of the VIM editor which includes recent enhancements
vim-filesystem.x86_64 : VIM filesystem layout
vim-minimal.x86_64 : A minimal version of the VIM editor

  名稱和簡介匹配 only,使用“search all”試試。
[[email protected] yum.repos.d]#

5. 再比如

[[email protected] yum.repos.d]# yum install gdb
已載入外掛:fastestmirror
Loading mirror speeds from cached hostfile
正在解決依賴關係
--> 正在檢查事務
---> 軟體包 gdb.x86_64.0.7.6.1-100.el7_4.1 將被 升級
---> 軟體包 gdb.x86_64.0.7.6.1-114.el7 將被 更新
--> 解決依賴關係完成

依賴關係解決

==============================================================================================================================================================
 Package                          架構                                版本                                         源                                    大小
==============================================================================================================================================================
正在更新:
 gdb                              x86_64                              7.6.1-114.el7                                centos7                              2.4 M

事務概要
==============================================================================================================================================================
升級  1 軟體包

總下載量:2.4 M
Is this ok [y/d/N]:

第二種場景

我們伺服器本地沒有 ISO 映象,但是我們有所需要的 RPM 包檔案列表,我們也可以利用這些 RPM 搭建屬於我們自己的本地 yum 源。

1. 準備 RPM 包

假如我們的 RPM 包已經準備好放在如下目錄下:/home/Packages/

ls 檢視如下:

[email protected] Packages]# ls
389-ds-base-1.3.8.4-15.el7.x86_64.rpm                                 libtranslit-m17n-0.0.2-6.el7.x86_64.rpm
389-ds-base-libs-1.3.8.4-15.el7.x86_64.rpm                            libudisks2-2.7.3-8.el7.x86_64.rpm
abattis-cantarell-fonts-0.0.25-1.el7.noarch.rpm                       libunistring-0.9.3-9.el7.x86_64.rpm
abrt-2.1.11-52.el7.centos.x86_64.rpm                                  libunwind-1.2-2.el7.x86_64.rpm
abrt-addon-ccpp-2.1.11-52.el7.centos.x86_64.rpm                       libusal-1.1.11-25.el7.x86_64.rpm
abrt-addon-kerneloops-2.1.11-52.el7.centos.x86_64.rpm                 libusb-0.1.4-3.el7.x86_64.rpm
abrt-addon-pstoreoops-2.1.11-52.el7.centos.x86_64.rpm                 libusbmuxd-1.0.10-5.el7.x86_64.rpm
abrt-addon-python-2.1.11-52.el7.centos.x86_64.rpm                     libusbx-1.0.21-1.el7.x86_64.rpm
abrt-addon-vmcore-2.1.11-52.el7.centos.x86_64.rpm                     libusbx-devel-1.0.21-1.el7.x86_64.rpm
...

2. 準備 yum 源配置檔案

[mytest]
name=[mytest]
baseurl=file:///home/Packages
gpgcheck=0
enabled=1

將它儲存為 mytest.repo,放在 /etc/yum.repos.d/ 目錄下:

[[email protected] yum.repos.d]# ls
bak  mytest.repo
[[email protected] yum.repos.d]#

[[email protected] yum.repos.d]# cat mytest.repo 
[mytest]
name=[mytest]
baseurl=file:///home/Packages
gpgcheck=0
enabled=1
[[email protected] yum.repos.d]#

3. 安裝 createrepo 工具

[[email protected] ~]# yum install createrepo -y

4. 建立本地 yum 源資料庫檔案

[[email protected] ~]# createrepo /home/Packages/
Spawning worker 0 with 10 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete
[[email protected] ~]#

5. 清除原有 yum 配置

[[email protected] ~]# yum clean all

6. 重建快取(元資料)

[[email protected] ~]# yum makecache 重建快取
[[email protected] home]# yum makecache
已載入外掛:fastestmirror
mytest                                                                                                                                 | 2.9 kB  00:00:00     
(1/3): mytest/filelists_db                                                                                                             | 5.3 kB  00:00:00     
(2/3): mytest/other_db                                                                                                                 | 5.9 kB  00:00:00     
(3/3): mytest/primary_db                                                                                                               | 7.5 kB  00:00:00     
Determining fastest mirrors
元資料快取已建立
[[email protected] home]#

7. 測試一下

[[email protected] ~]# yum search yum-utils
已載入外掛:fastestmirror
Loading mirror speeds from cached hostfile
=================================================================== N/S matched: yum-utils ===================================================================
yum-utils.noarch : Utilities based around the yum package manager

  名稱和簡介匹配 only,使用“search all”試試。
[[email protected] ~]#

第三種場景

對外公開的 yum 源,要求:

  1. 這個 yum 源伺服器要能夠定期自動更新 yum 源
  2. 使用 CentOS 官方標準源保持更新
  3. Linux ( mini ) 客戶端可以在使用的時候直接使用源,比如直接命令:yum –y install gdb*

1. 開始配置 yum 源伺服器

2. 安裝 Nginx 服務

我們使用 Nginx 配置 yum 源 Web 伺服器:

[[email protected] yum.repos.d]# yum  install nginx -y
[[email protected] nginx]# pwd
/etc/nginx
[[email protected] nginx]# vim nginx.conf

在這裡插入圖片描述

[[email protected] html]# systemctl  status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: inactive (dead)

12月 31 13:36:05 10-255-1-40 systemd[1]: Unit nginx.service cannot be reloaded because it is inactive.
[[email protected] html]#
[[email protected] html]# systemctl  start nginx
[[email protected] html]# systemctl  status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since 一 2018-12-31 13:48:21 UTC; 4s ago
  Process: 3803 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 3800 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 3798 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 3806 (nginx)
   CGroup: /system.slice/nginx.service
           ├─3806 nginx: master process /usr/sbin/nginx
           └─3807 nginx: worker process

12月 31 13:48:21 10-255-1-40 systemd[1]: Starting The nginx HTTP and reverse proxy server...
12月 31 13:48:21 10-255-1-40 nginx[3800]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
12月 31 13:48:21 10-255-1-40 nginx[3800]: nginx: configuration file /etc/nginx/nginx.conf test is successful
12月 31 13:48:21 10-255-1-40 systemd[1]: Failed to read PID from file /run/nginx.pid: Invalid argument
12月 31 13:48:21 10-255-1-40 systemd[1]: Started The nginx HTTP and reverse proxy server.
[[email protected] html]#

3. 測試 Nginx 服務

方法1:

http://116.85.5.228 ,如圖:

在這裡插入圖片描述

方法2:

[[email protected] html]# curl http://127.0.0.1:80/

4. 開始配置 CentOS 映象

在 Nginx HTML 下面建立以下目錄:
[[email protected] nginx]# tree
.
├── html
│   ├── 404.html
│   ├── 50x.html
│   ├── centos
│   │   └── didiyun
│   │       ├── 5
│   │       ├── 6
│   │       └── 7
│   │           ├── 7.0
│   │           ├── 7.1
│   │           ├── 7.2
│   │           ├── 7.3
│   │           ├── 7.4
│   │           ├── 7.5
│   │           └── 7.6
│   │               ├── x86
│   │               └── x86_64
...

5. 修改 Nginx HTML

增加如下內容:

...
        <h1>Welcome to <strong>didiyun</strong> on Internet!</h1>

    <div class="content">
    <p style="font-weight:bolder;color:blue;font-size:30px;">ALL of the packages in the below:</p>
    <br/>

    <a href="http://didiyun.com/centos/didiyun">DiDiyun</a><br/>
    These packagers from of CentOS ISO.<br/>
   </div>
    ...

6. 重啟 Nginx服務

[[email protected] yum.repos.d]# systemctl  restart nginx

使用滴滴雲分配的 EIP 訪問如下:

在這裡插入圖片描述

在這裡插入圖片描述

7. 配置 repo

[[email protected] yum.repos.d]# cat Base7.repo 
#########################################
\#          Centos SYSTEM YUM            #
#########################################
[base7]
name=CentOS-7 - Base
baseurl=http://mirrors.intra.didiyun.com/centos/7/os/x86_64
gpgkey=http://mirrors.intra.didiyun.com/centos/RPM-GPG-KEY-CentOS-7
enabled=1
gpgcheck=1

[updates7]
name=CentOS-7 - Updates
baseurl=http://mirrors.intra.didiyun.com/centos/7/updates/x86_64/
gpgkey=http://mirrors.intra.didiyun.com/centos/RPM-GPG-KEY-CentOS-7
enabled=1
gpgcheck=1

[extras7]
name=CentOS-7 - Extras
baseurl=http://mirrors.intra.didiyun.com/centos/7/extras/x86_64/
gpgkey=http://mirrors.intra.didiyun.com/centos/RPM-GPG-KEY-CentOS-7
enabled=1
gpgcheck=1

[centosplus7]
name=CentOS-7 - Plus
baseurl=http://mirrors.intra.didiyun.com/centos/7/centosplus/x86_64/
gpgkey=http://mirrors.intra.didiyun.com/centos/RPM-GPG-KEY-CentOS-7
gpgcheck=1
enabled=0

[[email protected] yum.repos.d]#

8. 同步映象

reposync -n --repoid=extras7 --repoid=updates7 --repoid=base7 -p /usr/share/nginx/html/centos/didiyun/7/7.6/x86_64/

同步完後如下:

[[email protected] x86_64]# ls
base7  extras7  updates7
[[email protected] x86_64]#

如果出現同步後沒有發現對應的檔案和目錄,請檢查 repo 檔案是否有錯誤。

在這裡插入圖片描述

在這裡插入圖片描述

這樣 yum 源的 Server 端就建立好了。然後進行建倉,即 yum 源倉庫,供 Client 端檢索使用。

9. 配置 yum 源快取元資料

[[email protected] yum.repos.d]# createrepo -p /usr/share/nginx/html/centos/didiyun/7/7.6/x86_64/base7/Packages/
Spawning worker 0 with 9591 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete
您在 /var/spool/mail/root 中有新郵件
[[email protected] yum.repos.d]#

如圖:

在這裡插入圖片描述

接下來 Client 端配置後就可以直接使用這些 didiyun yum 源安裝軟體了。

10. 準備測試客戶端

我們再啟動一臺滴滴雲主機,開始做如下配置:

客戶端的 yum 配置:

  1. 安裝優先順序控制軟體:

使用 yum 來安裝 yum install yum-plugin-priorities -y

這個是優先順序控制工具,可以控制 Client 端進行 yum 源的先後使用查詢。

  1. 備份客戶端系統原有的 yum 源,此處要注意,域名需要自己購買,這裡只是舉例。
mkdir –p /etc/yum.repos.d/bak

mv /etc/yum.repos.d/*  /etc/yum.repos.d/bak/

vim /etc/yum.repos.d/Base.repo
[centos7]
name=source_from_didiyun
baseurl=http://didiyun.com/centos/didiyun/7/7.6/x86_64/base7/Packages
gpgcheck=0
enable=1
priority=1
  1. 清除本地快取和重新建立快取
yum clean all 清除所有快取

yum makecache 建立快取

yum repolist 檢視yum源列表

現在我們新的滴滴雲 yum 源已經搭建好了,而且也設定了優先順序,我們可以測試安裝軟體了。

我們來測試一下安裝 xz 相關。

[[email protected] yum.repos.d]# yum search xz-devel
已載入外掛:fastestmirror
Loading mirror speeds from cached hostfile
=================================================================== N/S matched: xz-devel ====================================================================
xz-devel.i686 : Devel libraries & headers for liblzma
xz-devel.x86_64 : Devel libraries & headers for liblzma

  名稱和簡介匹配 only,使用“search all”試試。
[[email protected] yum.repos.d]# yum install xz-devel
已載入外掛:fastestmirror
Loading mirror speeds from cached hostfile
正在解決依賴關係
--> 正在檢查事務
---> 軟體包 xz-devel.x86_64.0.5.2.2-1.el7 將被 安裝
--> 解決依賴關係完成

依賴關係解決

==============================================================================================================================================================
 Package                              架構                               版本                                       源                                   大小
==============================================================================================================================================================
正在安裝:
 xz-devel                             x86_64                             5.2.2-1.el7                                centos7                              46 k

事務概要
==============================================================================================================================================================
安裝  1 軟體包

總下載量:46 k
安裝大小:165 k
Is this ok [y/d/N]:

11. 配置滴滴雲 yum 源自動更新機制

這裡我們先簡單寫個指令碼定期更新 CentOS 7.4 的映象:

 [[email protected] opt]# ls
updateyurepo.sh
[[email protected] opt]# cat updateyurepo.sh 
\#!/bin/bash

reposync -n --repoid=extras7 --repoid=updates7 --repoid=base7 -p /usr/share/nginx/html/centos/didiyun/7/7.4/x86_64/


[[email protected] opt]#
 [[email protected] opt]# crontab -e
 利用-e寫入30 18 * * 6 bash /opt/updateyurepo.sh內容
crontab: installing new crontab
檢視:
[[email protected] opt]# crontab -l
30 18 * * 6 bash /opt/updateyurepo.sh
[[email protected] opt]#

這樣每週 6 六點半自動更新 CentOS 7.4 yum 源。

我們後續會根據具體的版本來設定更新,做到每個版本獨立更新。