k8s 使用本地镜像启动pod操作指南

34 阅读1分钟

针对containerd 的镜像源的不稳定问题,使用docker下载镜像后,转换为tar包,重新部署到ctr的镜像库来让k8s使用

1. 准备docker

# 准备好docker
sudo apt update && sudo apt install -y docker.io
sudo systemctl enable --now docker
sudo usermod -aG docker $USER 

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{ "registry-mirrors": 
    [ "https://docker.m.daocloud.io", 
    "https://docker.imgdb.de", 
    "https://docker-0.unsee.tech",
    "https://docker.hlmirror.com",
    "https://docker.1ms.run", 
    "https://func.ink", 
    "https://hub-mirror.c.163.com", 
    "https://mirror.baidubce.com" 
    ] 
}
EOF

2. 重启 Docker

sudo systemctl daemon-reload
sudo systemctl restart docker
newgrp docker

3. 构建

mkdir /home/hw/my_image && cd /home/hw/my_image

4. 写入最小 Dockerfile

cat > Dockerfile <<'EOF'
FROM alpine
CMD ["echo", "Hello from my first image!"]
EOF

docker build -t my-first-image:latest .

5. 保存 & 导入 k8s

docker save my-first-image:latest -o my-first-image.tar
sudo ctr -n k8s.io images import my-first-image.tar
sudo crictl images

6. 运行

# 自己构建的镜像可以这么操作
kubectl run my-first-pod --image=my-first-image --restart=Never
# 禁止使用远程仓库,从本地拉取镜像
kubectl run nginx-pod   --image=docker.io/library/nginx:latest   --restart=Never   --image-pull-policy=Never

7. 验证

kubectl get pods