Chuyển tới nội dung chính

Pull Image trong K8s

Để chạy được 1 service trong K8s thì sau các bước cài đặt và cấu hình cụm K8s ta cần phải pull image từ registry về. Registry ta có hiểu là nơi lưu trữ các image. Nó có thể public registry như Docker hub hoặc có thể là Private Regitry do ta tự cài đặt hoặc do công ty cung cấp.

Một số App thường được sử dụng làm private registry như: Harbor, Nexus, ...

::: -

Về bản chất khi ta pull image trong K8s thì sẽ là máy ảo chạy node pull image. Nên khi gặp lỗi K8s không pull được image thì ta có thể kiểm tra xem máy ảo có pull được image không

Để lấy ví dụ, ở đây ta sẽ sử dụng Harbor làm registry

1. File pod config

Thông thường ta sẽ thấy image được setup trong pod config sẽ là image: nginx:1.14.2 hoặc node:12.xx,.... Với dạng này thì K8s sẽ tự tìm image ở các registry mặc định như Docker registry hoặc default được cấu hình sẵn của K8s. Và nó sẽ là public registry

apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80

Ở môi trường doanh nghiệp, image sẽ được pull từ private registry là Harbor hoặc nexsus.

apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: harbor.company.vn\nginx:1.14.2
ports:
- containerPort: 80

Như ta thấy ở đây image registry sẽ là harbor.example.vn\nginx:1.14.2

Nó được Pull từ server harbor.example.vn

Như vậy các máy ảo chạy node k8s đều phải có quyền pull image từ server này.

2. Chỉnh sửa file hosts trên các node k8s

Nếu harbor hoặc nexus server không có DNS thì ta cần chỉnh sửa file hosts trên các node chạy k8s

vi /etc/hosts

Thêm dòng sau vào file hosts
10.6.1.111 harbor.company.vn

Với 10.9.8.99 là server Harbor bạn đã có
harbor.company.vn là DNS bạn đặt cho server đó

3. Đăng nhập vào server harbor

Hiện nay các server chạy k8s chủ yếu chạy bằng containerd. Như vậy ta sẽ phải sử dụng command của containerd để đăng nhập vào server Harbor

3.1 Export default config containerd

    containerd config default > /etc/containerd/config.toml 

3.2 Sửa file config default thêm Auth

vi /etc/containerd/config.toml 
 [plugins."io.containerd.grpc.v1.cri".image_decryption]
key_model = "node"

[plugins."io.containerd.grpc.v1.cri".registry]
config_path = ""

[plugins."io.containerd.grpc.v1.cri".registry.auths]

[plugins."io.containerd.grpc.v1.cri".registry.configs]

[plugins."io.containerd.grpc.v1.cri".registry.headers]

[plugins."io.containerd.grpc.v1.cri".registry.mirrors]
## Thêm vào từ dòng này
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]
endpoint = ["https://registry-1.docker.io"]
[plugins."io.containerd.grpc.v1.cri".registry.configs."10.9.8.99".tls]
insecure_skip_verify=true
[plugins."io.containerd.grpc.v1.cri".registry.configs."10.9.8.99".auth]
auth = "ccccccccccccccccccccc="
## End
[plugins."io.containerd.grpc.v1.cri".x509_key_pair_streaming]
tls_cert_file = ""
tls_key_file = ""

[plugins."io.containerd.internal.v1.opt"]
path = "/opt/containerd"

Warning: Không thể test pull image trực tiếp được bằng ctr pull <server harbor>

# Command của containerd
ctr image pull harbor.test.vn/prod/sm-api-micro:1.0.0

4. Kiểm tra trên K8s xem có Pull được không

Sau khi hoàn thành xong bước này ta có thể run pod

Successfully pulled image "harbor.company.vn/dev/nginx:1.14.2 " in 8.79059954s