Kubernetes k8s拉取镜像失败最简单最快最完美解决方法 [ERROR ImagePull]: failed to pull image k8s.gcr.io/kube-apiserver

问题

由于国内网络原因,kubeadm init会卡住不动,一卡就是半个小时,然后报出这种问题:
[ERROR ImagePull]: failed to pull image k8s.gcr.io/kube-apiserver:v1.18.5: output: Error response from daemon: Get https://k8s.gcr.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)

完整的错误日志如下:

root@ubuntu:/home/c# kubeadm init
W0709 04:14:45.954974   70230 configset.go:202] WARNING: kubeadm cannot validate component configs for API groups [kubelet.config.k8s.io kubeproxy.config.k8s.io]
[init] Using Kubernetes version: v1.18.5
[preflight] Running pre-flight checks
[preflight] Pulling images required for setting up a Kubernetes cluster
[preflight] This might take a minute or two, depending on the speed of your internet connection
[preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
error execution phase preflight: [preflight] Some fatal errors occurred:
        [ERROR ImagePull]: failed to pull image k8s.gcr.io/kube-apiserver:v1.18.5: output: Error response from daemon: Get https://k8s.gcr.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
, error: exit status 1
        [ERROR ImagePull]: failed to pull image k8s.gcr.io/kube-controller-manager:v1.18.5: output: Error response from daemon: Get https://k8s.gcr.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
, error: exit status 1
        [ERROR ImagePull]: failed to pull image k8s.gcr.io/kube-scheduler:v1.18.5: output: Error response from daemon: Get https://k8s.gcr.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
, error: exit status 1
        [ERROR ImagePull]: failed to pull image k8s.gcr.io/kube-proxy:v1.18.5: output: Error response from daemon: Get https://k8s.gcr.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
, error: exit status 1
        [ERROR ImagePull]: failed to pull image k8s.gcr.io/pause:3.2: output: Error response from daemon: Get https://k8s.gcr.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
, error: exit status 1
        [ERROR ImagePull]: failed to pull image k8s.gcr.io/etcd:3.4.3-0: output: Error response from daemon: Get https://k8s.gcr.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
, error: exit status 1
        [ERROR ImagePull]: failed to pull image k8s.gcr.io/coredns:1.6.7: output: Error response from daemon: Get https://k8s.gcr.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
, error: exit status 1
[preflight] If you know what you are doing, you can make a check non-fatal with `--ignore-preflight-errors=...`
To see the stack trace of this error execute with --v=5 or higher

原因显而易见,是因为要下载k8s.gcr.io的docker镜像,但是国内连不上https://k8s.gcr.io/v2/。

还有其它问题可以看我上一篇文章使用VMware虚拟机搭建Kubernetes的Master和Worker节点 最全教程(1)准备工作,有很详细的解决过程。接下来进入正题:

解决方法

使用阿里云镜像

运行kubeadm init时加上阿里云镜像的参数--image-repository=registry.aliyuncs.com/google_containers,如下:(版本改为自己需要的)

kubeadm init --image-repository=registry.aliyuncs.com/google_containers --pod-network-cidr=10.244.0.0/16 --kubernetes-version=v1.18.5

如果还有问题,那可以用下面这个比较复杂的方法

从DockerHub的其它仓库拉取

网上搜了半天,很多人说从别的仓库一个一个拉取再改名,但是这些教程仓库名称不一,有的教程已经很老了,仓库很多年没更新,这里直接授之以渔,自己学怎么找仓库。
并且一个一个拉取改名太累了,可以写个脚本。

过程如下:

首先使用下面的命令获取需要的docker镜像名称:

kubeadm config images list

结果如下:

k8s.gcr.io/kube-apiserver:v1.18.5
k8s.gcr.io/kube-controller-manager:v1.18.5
k8s.gcr.io/kube-scheduler:v1.18.5
k8s.gcr.io/kube-proxy:v1.18.5
k8s.gcr.io/pause:3.2
k8s.gcr.io/etcd:3.4.3-0
k8s.gcr.io/coredns:1.6.7

首先要看看该在哪个地方拉取,可以去docker hub搜一搜哪里有kube-proxy之类的组件
进入dockerhub搜索:
https://hub.docker.com/search?q=kube-proxy&type=image
按照最近更新排序,结果如下,可以发现一个下载次数10k+,更新也很频繁的仓库:
Kubernetes k8s拉取镜像失败最简单最快最完美解决方法 [ERROR ImagePull]: failed to pull image k8s.gcr.io/kube-apiserver_第1张图片

然后开始编写脚本:

vim pull_k8s_images.sh
set -o errexit
set -o nounset
set -o pipefail
##这里定义版本
KUBE_VERSION=v1.18.5
KUBE_PAUSE_VERSION=3.2
ETCD_VERSION=3.4.3-0
DNS_VERSION=1.6.7

GCR_URL=k8s.gcr.io
##这里就是写你要使用的仓库
DOCKERHUB_URL=gotok8s
##这里是镜像列表
images=(
kube-proxy:${KUBE_VERSION}
kube-scheduler:${KUBE_VERSION}
kube-controller-manager:${KUBE_VERSION}
kube-apiserver:${KUBE_VERSION}
pause:${KUBE_PAUSE_VERSION}
etcd:${ETCD_VERSION}
coredns:${DNS_VERSION}
)
##这里是拉取和改名的循环语句
for imageName in ${images[@]} ; do
  docker pull $DOCKERHUB_URL/$imageName
  docker tag $DOCKERHUB_URL/$imageName $GCR_URL/$imageName
  docker rmi $DOCKERHUB_URL/$imageName
done

然后授予执行权限

chmod +x ./pull_k8s_images.sh

执行:

./pull_k8s_images.sh

执行过程中就会拉取镜像,完成后,使用docker images命令查看所有镜像,如下:
Kubernetes k8s拉取镜像失败最简单最快最完美解决方法 [ERROR ImagePull]: failed to pull image k8s.gcr.io/kube-apiserver_第2张图片
如果还有镜像没有拉取下来,比如我第一次用的不是gotok8s仓库,而是另一个仓库,但是它里面并没有最新的etcd和coredns,所以这两个没有拉取下来,那就再去找一些仓库,直接搜索etcd,进入仓库后点击Tag,看是不是有你需要的版本,如下图:
Kubernetes k8s拉取镜像失败最简单最快最完美解决方法 [ERROR ImagePull]: failed to pull image k8s.gcr.io/kube-apiserver_第3张图片

如果有,假如这个仓库叫corelab,然后使用vim命令再新建一个可执行文件。

vim pull_k8s_images2.sh

添加内容:

set -o errexit
set -o nounset
set -o pipefail

## 这里只需要留下还需要下载的组件的版本号
ETCD_VERSION=3.4.3-0
DNS_VERSION=1.6.7

GCR_URL=k8s.gcr.io
## 这里写新的仓库地址
DOCKERHUB_URL=corelab

## 这里也只留下需要下载的镜像
images=(
etcd:${ETCD_VERSION}
coredns:${DNS_VERSION}
)

for imageName in ${images[@]} ; do
  docker pull $DOCKERHUB_URL/$imageName
  docker tag $DOCKERHUB_URL/$imageName $GCR_URL/$imageName
  docker rmi $DOCKERHUB_URL/$imageName
done

然后授予执行权限

chmod +x ./pull_k8s_images2.sh

执行:

./pull_k8s_images2.sh

这下就应该没问题了

结果

再进行kubeadm init,这次没有任何问题(如果有其它问题可以看我的另一篇文章使用VMware虚拟机搭建Kubernetes的Master和Worker节点 最全教程(1)准备工作,有很详细的解决过程。)
Kubernetes k8s拉取镜像失败最简单最快最完美解决方法 [ERROR ImagePull]: failed to pull image k8s.gcr.io/kube-apiserver_第4张图片

你可能感兴趣的:(docker,kubernetes,kubernetes,K8s,k8s集群,安装手册,搭建手册,kubernetes,k8s,docker,ubuntu)