一、问题描述及相关提示:
在命令行输入# kubeadm init --pod-network-cidr=10.244.0.0/16 --ignore-preflight-errors=NumCPU 后,会提示镜像无法获取
[root@master ~]# kubeadm init --pod-network-cidr=10.244.0.0/16 --ignore-preflight-errors=NumCPU
W0915 04:09:11.676468 2925 version.go:98] could not fetch a Kubernetes version from the internet: unable to get URL "https://dl.k8s.io/release/stable-1.txt": Get https://dl.k8s.io/release/stable-1.txt: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
W0915 04:09:11.676529 2925 version.go:99] falling back to the local client version: v1.15.3
[init] Using Kubernetes version: v1.15.3
[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.15.3: output: Trying to pull repository k8s.gcr.io/kube-apiserver ...
Get https://k8s.gcr.io/v1/_ping: dial tcp 108.177.97.82:443: connect: connection refused
, error: exit status 1
[ERROR ImagePull]: failed to pull image k8s.gcr.io/kube-controller-manager:v1.15.3: output: Trying to pull repository k8s.gcr.io/kube-controller-manager ...
Get https://k8s.gcr.io/v1/_ping: dial tcp 108.177.97.82:443: connect: connection refused
, error: exit status 1
[ERROR ImagePull]: failed to pull image k8s.gcr.io/kube-scheduler:v1.15.3: output: Trying to pull repository k8s.gcr.io/kube-scheduler ...
Get https://k8s.gcr.io/v1/_ping: dial tcp 108.177.97.82:443: connect: connection refused
, error: exit status 1
[ERROR ImagePull]: failed to pull image k8s.gcr.io/kube-proxy:v1.15.3: output: Trying to pull repository k8s.gcr.io/kube-proxy ...
Get https://k8s.gcr.io/v1/_ping: dial tcp 108.177.97.82:443: connect: connection refused
, error: exit status 1
[ERROR ImagePull]: failed to pull image k8s.gcr.io/pause:3.1: output: Trying to pull repository k8s.gcr.io/pause ...
Get https://k8s.gcr.io/v1/_ping: dial tcp 108.177.97.82:443: connect: connection refused
, error: exit status 1
[ERROR ImagePull]: failed to pull image k8s.gcr.io/etcd:3.3.10: output: Trying to pull repository k8s.gcr.io/etcd ...
Get https://k8s.gcr.io/v1/_ping: dial tcp 108.177.97.82:443: connect: connection refused
, error: exit status 1
[ERROR ImagePull]: failed to pull image k8s.gcr.io/coredns:1.3.1: output: Trying to pull repository k8s.gcr.io/coredns ...
Get https://k8s.gcr.io/v1/_ping: dial tcp 108.177.97.82:443: connect: connection refused
, error: exit status 1
[preflight] If you know what you are doing, you can make a check non-fatal with `--ignore-preflight-errors=...`
二、问题解决方案
1、 如一所示,我将提示复制到一个文件里面(此处是require.txt),然后用awk、grep、cut等命令过滤出我想要的格式:如下所示
[root@master ~]# awk -F" " '{print $7}' require.txt | grep -v "^$" | grep -v "connection" |grep -v "fatal"| cut -d ":" -f 1,2
k8s.gcr.io/kube-apiserver:v1.15.3
k8s.gcr.io/kube-controller-manager:v1.15.3
k8s.gcr.io/kube-scheduler:v1.15.3
k8s.gcr.io/kube-proxy:v1.15.3
k8s.gcr.io/pause:3.1
k8s.gcr.io/etcd:3.3.10
k8s.gcr.io/coredns:1.3.1
2、接下来就是将需要的镜像保存到文件里然后读取文件,用for循环来拉取相应的镜像
[root@master ~]# awk -F" " '{print $7}' require.txt | grep -v "^$" | grep -v "connection" |grep -v "fatal"| cut -d ":" -f 1,2 >require.sh
[root@master ~]# cat require.sh
k8s.gcr.io/kube-apiserver:v1.15.3
k8s.gcr.io/kube-controller-manager:v1.15.3
k8s.gcr.io/kube-scheduler:v1.15.3
k8s.gcr.io/kube-proxy:v1.15.3
k8s.gcr.io/pause:3.1
k8s.gcr.io/etcd:3.3.10
k8s.gcr.io/coredns:1.3.1
[root@master ~]# cp require.sh require2.sh
[root@master ~]# sed -i '/k8s.gcr.io/registry.aliyuncs.com/google_containers/g' require2.sh
[root@master ~]# cat require2.sh
k8s.gcr.io/kube-apiserver:v1.15.3
k8s.gcr.io/kube-controller-manager:v1.15.3
k8s.gcr.io/kube-scheduler:v1.15.3
k8s.gcr.io/kube-proxy:v1.15.3
k8s.gcr.io/pause:3.1
k8s.gcr.io/etcd:3.3.10
k8s.gcr.io/coredns:1.3.1
[root@master ~]# sed -i 's/k8s.gcr.io/registry.aliyuncs.com/google_containers/g' require2.sh
sed: -e expression #1, char 37: unknown option to `s'
[root@master ~]# sed -i 's/k8s.gcr.io/registry.aliyuncs.com\/google_containers/g' require2.sh
[root@master ~]# cat require2.sh
registry.aliyuncs.com/google_containers/kube-apiserver:v1.15.3
registry.aliyuncs.com/google_containers/kube-controller-manager:v1.15.3
registry.aliyuncs.com/google_containers/kube-scheduler:v1.15.3
registry.aliyuncs.com/google_containers/kube-proxy:v1.15.3
registry.aliyuncs.com/google_containers/pause:3.1
registry.aliyuncs.com/google_containers/etcd:3.3.10
registry.aliyuncs.com/google_containers/coredns:1.3.1
[root@master ~]# for i in `cat require2.sh`;do echo $i;done
registry.aliyuncs.com/google_containers/kube-apiserver:v1.15.3
registry.aliyuncs.com/google_containers/kube-controller-manager:v1.15.3
registry.aliyuncs.com/google_containers/kube-scheduler:v1.15.3
registry.aliyuncs.com/google_containers/kube-proxy:v1.15.3
registry.aliyuncs.com/google_containers/pause:3.1
registry.aliyuncs.com/google_containers/etcd:3.3.10
registry.aliyuncs.com/google_containers/coredns:1.3.1
[root@master ~]# for i in `cat require2.sh`;do docker pull $i;done
Trying to pull repository registry.aliyuncs.com/google_containers/kube-apiserver ...
v1.15.3: Pulling from registry.aliyuncs.com/google_containers/kube-apiserver
39fafc05754f: Pull complete
b329b37f08d3: Pull complete
Digest: sha256:a21bcbcd23f7dbc6a331583645b56e639ec256cc6e2283a647ddd86505a4783e
Status: Downloaded newer image for registry.aliyuncs.com/google_containers/kube-apiserver:v1.15.3
Trying to pull repository registry.aliyuncs.com/google_containers/kube-controller-manager ...
v1.15.3: Pulling from registry.aliyuncs.com/google_containers/kube-controller-manager
39fafc05754f: Already exists
ea7b418624d2: Pull complete
Digest: sha256:f84c849e3c2105136b210d31018e3050744817a993d3c7b9f5ce3a03b0d82ff5
Status: Downloaded newer image for registry.aliyuncs.com/google_containers/kube-controller-manager:v1.15.3
Trying to pull repository registry.aliyuncs.com/google_containers/kube-scheduler ...
v1.15.3: Pulling from registry.aliyuncs.com/google_containers/kube-scheduler
39fafc05754f: Already exists
f506ab803e71: Pull complete
Digest: sha256:77a630518b31525d85e9de3b4946eb0a54c9df56960a4056925ac14bb1d12a97
Status: Downloaded newer image for registry.aliyuncs.com/google_containers/kube-scheduler:v1.15.3
Trying to pull repository registry.aliyuncs.com/google_containers/kube-proxy ...
v1.15.3: Pulling from registry.aliyuncs.com/google_containers/kube-proxy
39fafc05754f: Already exists
db3f71d0eb90: Pull complete
ed8f57f30bde: Pull complete
Digest: sha256:fbeb72931327b1d66b6d481bc0e0a8c531a055432fc01ef28012d5746ad07877
Status: Downloaded newer image for registry.aliyuncs.com/google_containers/kube-proxy:v1.15.3
Trying to pull repository registry.aliyuncs.com/google_containers/pause ...
3.1: Pulling from registry.aliyuncs.com/google_containers/pause
cf9202429979: Pull complete
Digest: sha256:759c3f0f6493093a9043cc813092290af69029699ade0e3dbe024e968fcb7cca
Status: Downloaded newer image for registry.aliyuncs.com/google_containers/pause:3.1
Trying to pull repository registry.aliyuncs.com/google_containers/etcd ...
3.3.10: Pulling from registry.aliyuncs.com/google_containers/etcd
90e01955edcd: Pull complete
6369547c492e: Pull complete
bd2b173236d3: Pull complete
Digest: sha256:240bd81c2f54873804363665c5d1a9b8e06ec5c63cfc181e026ddec1d81585bb
Status: Downloaded newer image for registry.aliyuncs.com/google_containers/etcd:3.3.10
Trying to pull repository registry.aliyuncs.com/google_containers/coredns ...
1.3.1: Pulling from registry.aliyuncs.com/google_containers/coredns
e0daa8927b68: Pull complete
3928e47de029: Pull complete
Digest: sha256:638adb0319813f2479ba3642bbe37136db8cf363b48fb3eb7dc8db634d8d5a5b
Status: Downloaded newer image for registry.aliyuncs.com/google_containers/coredns:1.3.1
至此需要的镜像都已经拉取下来了,可以用#docker images查看
[root@master ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
registry.aliyuncs.com/google_containers/kube-proxy v1.15.3 232b5c793146 3 weeks ago 82.4 MB
registry.aliyuncs.com/google_containers/kube-apiserver v1.15.3 5eb2d3fc7a44 3 weeks ago 207 MB
registry.aliyuncs.com/google_containers/kube-controller-manager v1.15.3 e77c31de5547 3 weeks ago 159 MB
registry.aliyuncs.com/google_containers/kube-scheduler v1.15.3 703f9c69a5d5 3 weeks ago 81.1 MB
registry.aliyuncs.com/google_containers/coredns 1.3.1 eb516548c180 8 months ago 40.3 MB
registry.aliyuncs.com/google_containers/etcd 3.3.10 2c4adeb21b4f 9 months ago 258 MB
registry.aliyuncs.com/google_containers/pause 3.1 da86e6ba6ca1 21 months ago 742 kB
3、对拉取的镜像打tag,变为问题提示的那种格式,即k8s.gcr.io/的格式。
此处由于docker tag 后边需要接两个参数,用前边的for循环会有些问题。于是我换成了shell脚本,及require4.sh
#!/bin/bash
docker tag registry.aliyuncs.com/google_containers/kube-apiserver:v1.15.3 k8s.gcr.io/kube-apiserver:v1.15.3
docker tag registry.aliyuncs.com/google_containers/kube-controller-manager:v1.15.3 k8s.gcr.io/kube-controller-manager:v1.15.3
docker tag registry.aliyuncs.com/google_containers/kube-scheduler:v1.15.3 k8s.gcr.io/kube-scheduler:v1.15.3
docker tag registry.aliyuncs.com/google_containers/kube-proxy:v1.15.3 k8s.gcr.io/kube-proxy:v1.15.3
docker tag registry.aliyuncs.com/google_containers/pause:3.1 k8s.gcr.io/pause:3.1
docker tag registry.aliyuncs.com/google_containers/etcd:3.3.10 k8s.gcr.io/etcd:3.3.10
docker tag registry.aliyuncs.com/google_containers/coredns:1.3.1 k8s.gcr.io/coredns:1.3.1
执行脚本:#sh require4.sh
[root@master ~]# sh require4.sh
[root@master ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
k8s.gcr.io/kube-proxy v1.15.3 232b5c793146 3 weeks ago 82.4 MB
registry.aliyuncs.com/google_containers/kube-proxy v1.15.3 232b5c793146 3 weeks ago 82.4 MB
k8s.gcr.io/kube-apiserver v1.15.3 5eb2d3fc7a44 3 weeks ago 207 MB
registry.aliyuncs.com/google_containers/kube-apiserver v1.15.3 5eb2d3fc7a44 3 weeks ago 207 MB
registry.aliyuncs.com/google_containers/kube-scheduler v1.15.3 703f9c69a5d5 3 weeks ago 81.1 MB
k8s.gcr.io/kube-scheduler v1.15.3 703f9c69a5d5 3 weeks ago 81.1 MB
k8s.gcr.io/kube-controller-manager v1.15.3 e77c31de5547 3 weeks ago 159 MB
registry.aliyuncs.com/google_containers/kube-controller-manager v1.15.3 e77c31de5547 3 weeks ago 159 MB
k8s.gcr.io/coredns 1.3.1 eb516548c180 8 months ago 40.3 MB
registry.aliyuncs.com/google_containers/coredns 1.3.1 eb516548c180 8 months ago 40.3 MB
k8s.gcr.io/etcd 3.3.10 2c4adeb21b4f 9 months ago 258 MB
registry.aliyuncs.com/google_containers/etcd 3.3.10 2c4adeb21b4f 9 months ago 258 MB
k8s.gcr.io/pause 3.1 da86e6ba6ca1 21 months ago 742 kB
registry.aliyuncs.com/google_containers/pause 3.1 da86e6ba6ca1 21 months ago 742 kB
4、删除多余的镜像
[root@master ~]# vim require2.sh
registry.aliyuncs.com/google_containers/kube-apiserver:v1.15.3
registry.aliyuncs.com/google_containers/kube-controller-manager:v1.15.3
registry.aliyuncs.com/google_containers/kube-scheduler:v1.15.3
registry.aliyuncs.com/google_containers/kube-proxy:v1.15.3
registry.aliyuncs.com/google_containers/pause:3.1
registry.aliyuncs.com/google_containers/etcd:3.3.10
registry.aliyuncs.com/google_containers/coredns:1.3.1
[root@master ~]# for i in `cat require2.sh`;do docker rmi $i;done
Untagged: registry.aliyuncs.com/google_containers/kube-apiserver:v1.15.3
Untagged: registry.aliyuncs.com/google_containers/kube-apiserver@sha256:a21bcbcd23f7dbc6a331583645b56e639ec256cc6e2283a647ddd86505a4783e
Untagged: registry.aliyuncs.com/google_containers/kube-controller-manager:v1.15.3
Untagged: registry.aliyuncs.com/google_containers/kube-controller-manager@sha256:f84c849e3c2105136b210d31018e3050744817a993d3c7b9f5ce3a03b0d82ff5
Untagged: registry.aliyuncs.com/google_containers/kube-scheduler:v1.15.3
Untagged: registry.aliyuncs.com/google_containers/kube-scheduler@sha256:77a630518b31525d85e9de3b4946eb0a54c9df56960a4056925ac14bb1d12a97
Untagged: registry.aliyuncs.com/google_containers/kube-proxy:v1.15.3
Untagged: registry.aliyuncs.com/google_containers/kube-proxy@sha256:fbeb72931327b1d66b6d481bc0e0a8c531a055432fc01ef28012d5746ad07877
Untagged: registry.aliyuncs.com/google_containers/pause:3.1
Untagged: registry.aliyuncs.com/google_containers/pause@sha256:759c3f0f6493093a9043cc813092290af69029699ade0e3dbe024e968fcb7cca
Untagged: registry.aliyuncs.com/google_containers/etcd:3.3.10
Untagged: registry.aliyuncs.com/google_containers/etcd@sha256:240bd81c2f54873804363665c5d1a9b8e06ec5c63cfc181e026ddec1d81585bb
Untagged: registry.aliyuncs.com/google_containers/coredns:1.3.1
Untagged: registry.aliyuncs.com/google_containers/coredns@sha256:638adb0319813f2479ba3642bbe37136db8cf363b48fb3eb7dc8db634d8d5a5b
[root@master ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
k8s.gcr.io/kube-proxy v1.15.3 232b5c793146 3 weeks ago 82.4 MB
k8s.gcr.io/kube-apiserver v1.15.3 5eb2d3fc7a44 3 weeks ago 207 MB
k8s.gcr.io/kube-scheduler v1.15.3 703f9c69a5d5 3 weeks ago 81.1 MB
k8s.gcr.io/kube-controller-manager v1.15.3 e77c31de5547 3 weeks ago 159 MB
k8s.gcr.io/coredns 1.3.1 eb516548c180 8 months ago 40.3 MB
k8s.gcr.io/etcd 3.3.10 2c4adeb21b4f 9 months ago 258 MB
k8s.gcr.io/pause 3.1 da86e6ba6ca1 21 months ago 742 kB
5、到此,前边最开始提示的镜像全部准备好了,接下来就回到最开始的命令吧。
[root@master ~]# kubeadm init --pod-network-cidr=10.244.0.0/16 --ignore-preflight-errors=NumCPU
W0915 05:04:14.258964 6290 version.go:98] could not fetch a Kubernetes version from the internet: unable to get URL "https://dl.k8s.io/release/stable-1.txt": Get https://dl.k8s.io/release/stable-1.txt: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
W0915 05:04:14.259024 6290 version.go:99] falling back to the local client version: v1.15.3
[init] Using Kubernetes version: v1.15.3
[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'
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Activating the kubelet service
[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver-kubelet-client" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [master kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 192.168.150.20]
[certs] Generating "front-proxy-ca" certificate and key
[certs] Generating "front-proxy-client" certificate and key
[certs] Generating "etcd/ca" certificate and key
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [master localhost] and IPs [192.168.150.20 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [master localhost] and IPs [192.168.150.20 127.0.0.1 ::1]
[certs] Generating "sa" key and public key
[kubeconfig] Using kubeconfig folder "/etc/kubernetes"
[kubeconfig] Writing "admin.conf" kubeconfig file
[kubeconfig] Writing "kubelet.conf" kubeconfig file
[kubeconfig] Writing "controller-manager.conf" kubeconfig file
[kubeconfig] Writing "scheduler.conf" kubeconfig file
[control-plane] Using manifest folder "/etc/kubernetes/manifests"
[control-plane] Creating static Pod manifest for "kube-apiserver"
[control-plane] Creating static Pod manifest for "kube-controller-manager"
[control-plane] Creating static Pod manifest for "kube-scheduler"
[etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
[apiclient] All control plane components are healthy after 37.502121 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.15" in namespace kube-system with the configuration for the kubelets in the cluster
[upload-certs] Skipping phase. Please see --upload-certs
[mark-control-plane] Marking the node master as control-plane by adding the label "node-role.kubernetes.io/master=''"
[mark-control-plane] Marking the node master as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: v0c13b.foa68xh8qjtbygyt
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstrap-token] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstrap-token] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy
Your Kubernetes control-plane has initialized successfully!
To start using your cluster, you need to run the following as a regular user:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
https://kubernetes.io/docs/concepts/cluster-administration/addons/
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 192.168.150.20:6443 --token v0c13b.foa68xh8qjtbygyt \
--discovery-token-ca-cert-hash sha256:4521a13c88cc80a662361163d506c9d91f5fa8303106c82baff439f80c6c3f4a
上面的生成的token需要保存一下。
至此,kubernetes的踩坑告一段落,接下来有啥新问题我会继续在博客上面记录下来。