配置 containerd 鏡像倉庫完全攻略

作者簡介

王海龍,Rancher中國社區技術經理,負責Rancher中國技術社區的維護和運營。擁有6年的云計算領域經驗,經歷了OpenStack到Kubernetes的技術變革,無論底層操作系統Linux,還是虛擬化KVM或是Docker容器技術都有豐富的運維和實踐經驗。

前 言

Kubernetes 在 Changelog 中宣布自 Kubernetes 1.20 之后將棄用 Docker 作為容器運行時之后,containerd成為下一個容器運行時的熱門選項。雖然 containerd 很早就已經是 Docker 的一部分,但是純粹使用 containerd 還是給大家帶來了諸多困擾,本文將介紹如何使用 containerd 配置鏡像倉庫和加速器。

本文將以K3s為例對containerd進行配置,如果您的環境未使用 K3s 而是使用的 Kubernetes,你也可以參考本文來配置 containerd 的鏡像倉庫,因為 containerd 的配置是通用的。

關于 K3s 和 containerd

K3s 是一個輕量級 Kubernetes 發行版,二進制大小小于100MB,所需內存不到Kubernetes的一半。K3s 為了降低資源消耗,將默認的 runtime 修改為 containerd,同時也內置了 Kubernetes CLI 工具 crictl和ctr。

K3s 默認的 containerd 配置文件目錄為/var/lib/rancher/k3s/agent/etc/containerd/config.toml,但直接操作 containerd 的配置文件去設置鏡像倉庫或加速器相比于操作 docker 要復雜許多。K3s 為了簡化配置 containerd 鏡像倉庫的復雜度,K3s 會在啟動時檢查/etc/rancher/k3s/中是否存在 registries.yaml 文件,如果存在該文件,就會根據 registries.yaml 的內容轉換為 containerd 的配置并存儲到/var/lib/rancher/k3s/agent/etc/containerd/config.toml,從而降低了配置 containerd 鏡像倉庫的復雜度。

使用 K3s 配置私有鏡像倉庫

K3s 鏡像倉庫配置文件由兩大部分組成:mirrorsconfigs:

  • Mirrors 是一個用于定義專用鏡像倉庫的名稱和 endpoint 的指令

  • Configs 部分定義了每個 mirror 的 TLS 和證書配置。對于每個 mirror,你可以定義auth和/或tls

containerd 使用了類似 K8S 中 svc 與 endpoint 的概念,svc 可以理解為訪問名稱,這個名稱會解析到對應的 endpoint 上。也可以理解 mirror 配置就是一個反向代理,它把客戶端的請求代理到 endpoint 配置的后端鏡像倉庫。mirror 名稱可以隨意填寫,但是必須符合IP或域名的定義規則。并且可以配置多個 endpoint,默認解析到第一個 endpoint,如果第一個 endpoint 沒有返回數據,則自動切換到第二個 endpoint,以此類推。

比如以下配置示例:

mirrors:
  "172.31.6.200:5000":
    endpoint:
      - "http://172.31.6.200:5000"
  "rancher.ksd.top:5000":
    endpoint:
      - "http://172.31.6.200:5000"
  "docker.io":
    endpoint:
      - "https://fogjl973.mirror.aliyuncs.com"
      - "https://registry-1.docker.io"

可以通過 crictl pull 172.31.6.200:5000/library/alpinecrictl pull rancher.ksd.top:5000/library/alpine獲取到鏡像,但鏡像都是從同一個倉庫獲取到的。

root@rancher-server:/etc/rancher/k3s# systemctl restart k3s.service
root@rancher-server:/etc/rancher/k3s# crictl pull 172.31.6.200:5000/library/alpine
Image is up to date for sha256:a24bb4013296f61e89ba57005a7b3e52274d8edd3ae2077d04395f806b63d83e
root@rancher-server:/etc/rancher/k3s# crictl pull rancher.ksd.top:5000/library/alpine
Image is up to date for sha256:a24bb4013296f61e89ba57005a7b3e52274d8edd3ae2077d04395f806b63d83e
root@rancher-server:/etc/rancher/k3s#

非安全(http)私有倉庫配置

配置非安全(http)私有倉庫,只需要在 endpoint 中指定 http 協議頭的地址即可。

在沒有 TLS 通信的情況下,需要為 endpoints 指定http:// ,否則將默認為 https。

無認證

如果你使用的是非安全(http)私有倉庫,那么可以通過下面的參數來配置 K3s 連接私有倉庫:

root@ip-172-31-13-117:~# cat >> /etc/rancher/k3s/registries.yaml <<EOF
mirrors:
  "172.31.6.200:5000":
    endpoint:
      - "http://172.31.6.200:5000"
EOF
systemctl restart k3s

然后可以通過 crictl 去 pull 鏡像:

root@ip-172-31-13-117:~# crictl pull 172.31.6.200:5000/my-ubuntu
Image is up to date for sha256:9499db7817713c4d10240ca9f5386b605ecff7975179f5a46e7ffd59fff462ee

接下來,在看一下 containerd 的配置,可以看到文件末尾追加了如下配置:

root@ip-172-31-13-117:~# cat /var/lib/rancher/k3s/agent/etc/containerd/config.toml
[plugins.cri.registry.mirrors]

[plugins.cri.registry.mirrors."172.31.6.200:5000"]
  endpoint = ["http://172.31.6.200:5000"]

[plugins.cri.registry.mirrors."rancher.ksd.top:5000"]
  endpoint = ["http://172.31.6.200:5000"]

有認證

如果你的非安全(http)私有倉庫帶有認證,那么可以通過下面的參數來配置 k3s 連接私有倉庫:

mirrors:
  "35.182.134.80":
    endpoint:
      - "http://35.182.134.80"
configs:
  "35.182.134.80":
    auth:
      username: admin # this is the registry username
      password: Harbor12345 # this is the registry password
EOF
systemctl restart k3s

通過 crictl 去 pull 鏡像:

root@ip-172-31-13-117:~# crictl pull 35.182.134.80/ksd/ubuntu:16.04
Image is up to date for sha256:9499db7817713c4d10240ca9f5386b605ecff7975179f5a46e7ffd59fff462ee

Containerd 配置文件末尾追加了如下配置:

[plugins.cri.registry.mirrors]

[plugins.cri.registry.mirrors."35.182.134.80"]
  endpoint = ["http://35.182.134.80"]

[plugins.cri.registry.configs."35.182.134.80".auth]
  username = "admin"
  password = "Harbor12345"

安全(https)私有倉庫配置

以下示例均啟用了認證,所以每個示例都配置了configs.auth,如果實際環境未配置認證,刪除configs.auth配置即可。

使用授信 ssl 證書

與非安全(http)私有倉庫配置類似,只需要配置 endpoint 對應的倉庫地址為 https 即可。

root@ip-172-31-13-117:~# cat >> /etc/rancher/k3s/registries.yaml <<EOF
mirrors:
  "harbor.kingsd.top":
    endpoint:
      - "https://harbor.kingsd.top"
configs:
  "harbor.kingsd.top":
    auth:
      username: admin # this is the registry username
      password: Harbor12345 # this is the registry password
EOF
systemctl restart k3s

通過 crictl 去 pull 鏡像:

root@ip-172-31-13-117:~# crictl pull harbor.kingsd.top/ksd/ubuntu:16.04
Image is up to date for sha256:9499db7817713c4d10240ca9f5386b605ecff7975179f5a46e7ffd59fff462ee

Containerd 配置文件末尾追加了如下配置:

root@ip-172-31-13-117:~# cat /var/lib/rancher/k3s/agent/etc/containerd/config.toml
[plugins.cri.registry.mirrors]

[plugins.cri.registry.mirrors."harbor.kingsd.top"]
  endpoint = ["https://harbor.kingsd.top"]

[plugins.cri.registry.configs."harbor.kingsd.top".auth]
  username = "admin"
  password = "Harbor12345"

使用自簽 ssl 證書

如果后端倉庫使用的是自簽名的 ssl 證書,那么需要配置 CA 證書 用于 ssl 證書的校驗。

mirrors:
  "harbor-ksd.kingsd.top":
    endpoint:
      - "https://harbor-ksd.kingsd.top"
configs:
  "harbor-ksd.kingsd.top":
    auth:
      username: admin # this is the registry username
      password: Harbor12345 # this is the registry password
    tls:
      ca_file: /opt/certs/ca.crt
EOF
systemctl restart k3s

通過 crictl 去 pull 鏡像:

root@ip-172-31-13-117:~# crictl pull harbor-ksd.kingsd.top/ksd/ubuntu:16.04
Image is up to date for sha256:9499db7817713c4d10240ca9f5386b605ecff7975179f5a46e7ffd59fff462ee

Containerd 配置文件末尾追加了如下配置:

root@ip-172-31-13-117:~# cat /var/lib/rancher/k3s/agent/etc/containerd/config.toml
[plugins.cri.registry.mirrors]

[plugins.cri.registry.mirrors."harbor-ksd.kingsd.top"]
  endpoint = ["https://harbor-ksd.kingsd.top"]

[plugins.cri.registry.configs."harbor-ksd.kingsd.top".auth]
  username = "admin"
  password = "Harbor12345"

[plugins.cri.registry.configs."harbor-ksd.kingsd.top".tls]
  ca_file = "/opt/certs/ca.crt"

ssl 雙向認證

如果鏡像倉庫配置了雙向認證,那么需要為 containerd 配置 ssl 證書用于 鏡像倉庫對 containerd 做認證。

root@ip-172-31-13-117:~# cat >> /etc/rancher/k3s/registries.yaml <<EOF
mirrors:
  "harbor-ksd.kingsd.top":
    endpoint:
      - "https://harbor-ksd.kingsd.top"
configs:
  "harbor-ksd.kingsd.top":
    auth:
      username: admin # this is the registry username
      password: Harbor12345 # this is the registry password
    tls:
      ca_file: /opt/certs/ca.crt # path to the ca file used in the registry
      cert_file: /opt/certs/harbor-ksd.kingsd.top.cert # path to the cert file used in the registry
      key_file: /opt/certs/harbor-ksd.kingsd.top.key # path to the key file used in the registry
EOF
systemctl restart k3s

通過 crictl 去 pull 鏡像:

root@ip-172-31-13-117:~# crictl pull harbor-ksd.kingsd.top/ksd/ubuntu:16.04
Image is up to date for sha256:9499db7817713c4d10240ca9f5386b605ecff7975179f5a46e7ffd59fff462ee

Containerd 配置文件末尾追加了如下配置:

[plugins.cri.registry.mirrors]

[plugins.cri.registry.mirrors."harbor-ksd.kingsd.top"]
  endpoint = ["https://harbor-ksd.kingsd.top"]

[plugins.cri.registry.configs."harbor-ksd.kingsd.top".auth]
  username = "admin"
  password = "Harbor12345"

[plugins.cri.registry.configs."harbor-ksd.kingsd.top".tls]
  ca_file = "/opt/certs/ca.crt"
  cert_file = "/opt/certs/harbor-ksd.kingsd.top.cert"
  key_file = "/opt/certs/harbor-ksd.kingsd.top.key"

加速器配置

Containerd 與 docker 都有默認倉庫,均為 docker.io 。如果配置中未指定 mirror 為 docker.io,containerd 后會自動加載 docker.io 配置。與 docker 不同的是,containerd 可以修改 docker.io 對應的 endpoint(默認為 https://registry-1.docker.io ) ,而 docker 無法修改。

Docker 中可以通過 registry-mirrors 設置鏡像加速地址。如果 pull 的鏡像不帶倉庫地址(項目名+鏡像名:tag),則會從默認鏡像倉庫去拉取鏡像。如果配置了鏡像加速地址,會先訪問鏡像加速倉庫,如果沒有返回數據,再訪問默認的鏡像倉庫。

Containerd 目前沒有直接配置鏡像加速的功能,但 containerd 中可以修改 docker.io 對應的 endpoint,所以可以通過修改 endpoint 來實現鏡像加速下載。因為 endpoint 是輪詢訪問,所以可以給 docker.io 配置多個倉庫地址來實現 加速地址+默認倉庫地址。如下配置示例:

mirrors:
  "docker.io":
    endpoint:
      - "https://fogjl973.mirror.aliyuncs.com"
      - "https://registry-1.docker.io"
EOF

systemctl restart k3s

Containerd 配置文件末尾追加了如下配置:

root@ip-172-31-13-117:~# cat /var/lib/rancher/k3s/agent/etc/containerd/config.toml
[plugins.cri.registry.mirrors]

[plugins.cri.registry.mirrors."docker.io"]
  endpoint = ["https://fogjl973.mirror.aliyuncs.com", "https://registry-1.docker.io"]

完整配置示例

  "192.168.50.119":
    endpoint:
      - "http://192.168.50.119"
  "docker.io":
    endpoint:
      - "https://fogjl973.mirror.aliyuncs.com"
      - "https://registry-1.docker.io"
configs:
  "192.168.50.119":
    auth:
      username: '' # this is the registry username
      password: '' # this is the registry password
    tls:
      cert_file: '' # path to the cert file used in the registry
      key_file: '' # path to the key file used in the registry
      ca_file: '' # path to the ca file used in the registry
  "docker.io":
    auth:
      username: '' # this is the registry username
      password: '' # this is the registry password
    tls:
      cert_file: '' # path to the cert file used in the registry
      key_file: '' # path to the key file used in the registry
      ca_file: '' # path to the ca file used in the registry

參考鏈接

K3s私有鏡像倉庫配置:

https://docs.rancher.cn/docs/k3s/installation/private-registry/_index

Containerd配置鏡像倉庫:

https://github.com/containerd/cri/blob/master/docs/registry.md

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,333評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,491評論 3 416
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,263評論 0 374
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,946評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,708評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,186評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,255評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,409評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,939評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,774評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,976評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,518評論 5 359
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,209評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,641評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,872評論 1 286
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,650評論 3 391
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,958評論 2 373

推薦閱讀更多精彩內容