您好,登錄后才能下訂單哦!
本篇文章為大家展示了containerd中如何配置鏡像倉(cāng)庫(kù),內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
K3s 是一個(gè)輕量級(jí) Kubernetes 發(fā)行版,二進(jìn)制大小小于100MB,所需內(nèi)存不到Kubernetes的一半。K3s 為了降低資源消耗,將默認(rèn)的 runtime 修改為 containerd,同時(shí)也內(nèi)置了 Kubernetes CLI 工具 crictl和ctr。
K3s 默認(rèn)的 containerd 配置文件目錄為/var/lib/rancher/k3s/agent/etc/containerd/config.toml
,但直接操作 containerd 的配置文件去設(shè)置鏡像倉(cāng)庫(kù)或加速器相比于操作 docker 要復(fù)雜許多。K3s 為了簡(jiǎn)化配置 containerd 鏡像倉(cāng)庫(kù)的復(fù)雜度,K3s 會(huì)在啟動(dòng)時(shí)檢查/etc/rancher/k3s/
中是否存在 registries.yaml 文件,如果存在該文件,就會(huì)根據(jù) registries.yaml 的內(nèi)容轉(zhuǎn)換為 containerd 的配置并存儲(chǔ)到/var/lib/rancher/k3s/agent/etc/containerd/config.toml
,從而降低了配置 containerd 鏡像倉(cāng)庫(kù)的復(fù)雜度。
K3s 鏡像倉(cāng)庫(kù)配置文件由兩大部分組成:mirrors
和configs
:
Mirrors 是一個(gè)用于定義專用鏡像倉(cāng)庫(kù)的名稱和 endpoint 的指令
Configs 部分定義了每個(gè) mirror 的 TLS 和證書配置。對(duì)于每個(gè) mirror,你可以定義auth和/或tls
containerd 使用了類似 K8S 中 svc 與 endpoint 的概念,svc 可以理解為訪問(wèn)名稱,這個(gè)名稱會(huì)解析到對(duì)應(yīng)的 endpoint 上。也可以理解 mirror 配置就是一個(gè)反向代理,它把客戶端的請(qǐng)求代理到 endpoint 配置的后端鏡像倉(cāng)庫(kù)。mirror 名稱可以隨意填寫,但是必須符合IP或域名的定義規(guī)則。并且可以配置多個(gè) endpoint,默認(rèn)解析到第一個(gè) endpoint,如果第一個(gè) endpoint 沒(méi)有返回?cái)?shù)據(jù),則自動(dòng)切換到第二個(gè) 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"
可以通過(guò) crictl pull 172.31.6.200:5000/library/alpine
和 crictl pull rancher.ksd.top:5000/library/alpine
獲取到鏡像,但鏡像都是從同一個(gè)倉(cāng)庫(kù)獲取到的。
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)私有倉(cāng)庫(kù),只需要在 endpoint 中指定 http 協(xié)議頭的地址即可。
在沒(méi)有 TLS 通信的情況下,需要為 endpoints 指定http:// ,否則將默認(rèn)為 https。
如果你使用的是非安全(http)私有倉(cāng)庫(kù),那么可以通過(guò)下面的參數(shù)來(lái)配置 K3s 連接私有倉(cāng)庫(kù):
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
然后可以通過(guò) 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
接下來(lái),在看一下 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)私有倉(cāng)庫(kù)帶有認(rèn)證,那么可以通過(guò)下面的參數(shù)來(lái)配置 k3s 連接私有倉(cāng)庫(kù):
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
通過(guò) 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"
以下示例均啟用了認(rèn)證,所以每個(gè)示例都配置了configs.auth,如果實(shí)際環(huán)境未配置認(rèn)證,刪除configs.auth配置即可。
與非安全(http)私有倉(cāng)庫(kù)配置類似,只需要配置 endpoint 對(duì)應(yīng)的倉(cāng)庫(kù)地址為 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
通過(guò) 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"
如果后端倉(cāng)庫(kù)使用的是自簽名的 ssl 證書,那么需要配置 CA 證書 用于 ssl 證書的校驗(yàn)。
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
通過(guò) 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"
如果鏡像倉(cāng)庫(kù)配置了雙向認(rèn)證,那么需要為 containerd 配置 ssl 證書用于 鏡像倉(cāng)庫(kù)對(duì) containerd 做認(rèn)證。
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
通過(guò) 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 都有默認(rèn)倉(cāng)庫(kù),均為 docker.io 。如果配置中未指定 mirror 為 docker.io,containerd 后會(huì)自動(dòng)加載 docker.io 配置。與 docker 不同的是,containerd 可以修改 docker.io 對(duì)應(yīng)的 endpoint(默認(rèn)為 https://registry-1.docker.io ) ,而 docker 無(wú)法修改。
Docker 中可以通過(guò) registry-mirrors 設(shè)置鏡像加速地址。如果 pull 的鏡像不帶倉(cāng)庫(kù)地址(項(xiàng)目名+鏡像名:tag),則會(huì)從默認(rèn)鏡像倉(cāng)庫(kù)去拉取鏡像。如果配置了鏡像加速地址,會(huì)先訪問(wèn)鏡像加速倉(cāng)庫(kù),如果沒(méi)有返回?cái)?shù)據(jù),再訪問(wèn)默認(rèn)的鏡像倉(cāng)庫(kù)。
Containerd 目前沒(méi)有直接配置鏡像加速的功能,但 containerd 中可以修改 docker.io 對(duì)應(yīng)的 endpoint,所以可以通過(guò)修改 endpoint 來(lái)實(shí)現(xiàn)鏡像加速下載。因?yàn)?endpoint 是輪詢?cè)L問(wèn),所以可以給 docker.io 配置多個(gè)倉(cāng)庫(kù)地址來(lái)實(shí)現(xiàn) 加速地址+默認(rèn)倉(cāng)庫(kù)地址。如下配置示例:
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
上述內(nèi)容就是containerd中如何配置鏡像倉(cāng)庫(kù),你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。