如果使用containerd拉取不到镜像的话,就使用docker
1.修改decker的镜像源
[root@k8s-master ~]# vim /etc/docker/daemon.json
{
"registry-mirrors": [
"https://do.nark.eu.org",
"https://dc.j8.work",
"https://docker.m.daocloud.io",
"https://dockerproxy.com",
"https://docker.mirrors.ustc.edu.cn",
"https://docker.nju.edu.cn"
]
}
2.加载启动docker服务
[root@k8s-master ~]# systemctl daemon-reload
[root@k8s-master ~]# systemctl start docker
3.拉取常用的镜像
[root@k8s-master ~]# docker pull centos
[root@k8s-master ~]# docker pull nginx
[root@k8s-master ~]# docker pull haproxy
[root@k8s-master ~]# docker pull mysql:5.7.44
[root@k8s-master pods]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
haproxy latest a782c02b8259 10 days ago 103MB
nginx latest 39286ab8a5e1 4 weeks ago 188MB
mysql 5.7.44 5107333e08a8 9 months ago 501MB
centos latest 5d0da3dc9764 2 years ago 231MB
4.使用docker save指令打包镜像
[root@k8s-master ~]# docker save -o centos.tar centos:latest
[root@k8s-master ~]# docker save -o nginx.tar nginx:latest
[root@k8s-master ~]# docker save -o mysql.tar mysql:5.7.44
[root@k8s-master ~]# docker save -o haproxy.tar haproxy:latest
[root@k8s-master ~]# ls
anaconda-ks.cfg haproxy.tar kubeadm-config.yaml new.yaml pods
centos.tar k8s-ha-install mysql.tar nginx.tar
5.使用ctr指令将tar包导入到containerd的镜像中
[root@k8s-master ~]# ctr -n k8s.io images import centos.tar --platform=linux/amd64 //将tar包导入
unpacking docker.io/library/centos:latest (sha256:27f8ba8f837c2b1ef079a25095ea7cd4c778f31a33c09eb845a5be35fc207a3c)...done
[root@k8s-master ~]# ctr -n k8s.io images import nginx.tar --platform=linux/amd64
unpacking docker.io/library/nginx:latest (sha256:006a20213cb7f7d8edfadffd6791139d25283f590c5066e3fcd803e61a9c0b74)...done
[root@k8s-master ~]# ctr -n k8s.io images import mysql.tar --platform=linux/amd64
unpacking docker.io/library/mysql:5.7.44 (sha256:0b14b8e5c533fb43d07327fa8fe6c7393358002310d9f0d01976d387822a1bd3)...done
[root@k8s-master ~]# ctr -n k8s.io images import haproxy.tar --platform=linux/amd64
unpacking docker.io/library/haproxy:latest (sha256:4f4750d54310c5a3dbc94f3862cd3d8a266cbf1bfe806cf8eab64ace8501731b)...done
6.查看containerd镜像列表
[root@k8s-master ~]# crictl images
IMAGE TAG IMAGE ID SIZE
docker.io/library/centos latest 5d0da3dc97646 239MB
docker.io/library/haproxy latest a782c02b82595 106MB
docker.io/library/mysql 5.7.44 5107333e08a87 520MB
docker.io/library/nginx latest 39286ab8a5e14 192MB
registry.cn-beijing.aliyuncs.com/dotbalo/cni v3.26.1 9dee260ef7f59 93.4MB
registry.cn-beijing.aliyuncs.com/dotbalo/kube-controllers v3.26.1 1919f2787fa70 32.8MB
registry.cn-beijing.aliyuncs.com/dotbalo/node v3.26.1 8065b798a4d67 86.6MB
registry.cn-hangzhou.aliyuncs.com/google_containers/coredns v1.10.1 ead0a4a53df89 16.2MB
registry.cn-hangzhou.aliyuncs.com/google_containers/etcd 3.5.9-0 73deb9a3f7025 103MB
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver v1.28.2 cdcab12b2dd16 34.7MB
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager v1.28.2 55f13c92defb1 33.4MB
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy v1.28.2 c120fed2beb84 24.6MB
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler v1.28.2 7a5d9d67a13f6 18.8MB
registry.cn-hangzhou.aliyuncs.com/google_containers/pause 3.9 e6f1816883972 322kB
注意:如果出现这样的报错
是因为配置 crictl 客户端连接的运⾏位置错误
[root@k8s-master ~]# vim /etc/crictl.yaml
runtime-endpoint: unix:///run/containerd/containerd.sock
image-endpoint: unix:///run/containerd/containerd.sock
timeout: 10
debug: false
[root@k8s-master ~]# systemctl daemon-reload
[root@k8s-master ~]# systemctl restart containerd
7.在node01和node02节点上引入tar包
[root@k8s-master ~]# scp nginx.tar root@192.168.1.22:/root
nginx.tar 100% 183MB 45.7MB/s 00:04
[root@k8s-master ~]# scp nginx.tar root@192.168.1.33:/root
nginx.tar 100% 183MB 46.8MB/s 00:03
[root@k8s-master ~]# scp mysql.tar root@192.168.1.22:/root
mysql.tar 100% 496MB 38.7MB/s 00:12
[root@k8s-master ~]# scp mysql.tar root@192.168.1.33:/root
mysql.tar 100% 496MB 42.5MB/s 00:11
[root@k8s-master ~]# scp centos.tar root@192.168.1.22:/root
centos.tar 100% 228MB 31.6MB/s 00:07
[root@k8s-master ~]# scp centos.tar root@192.168.1.33:/root
centos.tar 100% 228MB 46.6MB/s 00:04
[root@k8s-master ~]# scp haproxy.tar root@192.168.1.22:/root
haproxy.tar 100% 101MB 39.4MB/s 00:02
[root@k8s-master ~]# scp haproxy.tar root@192.168.1.33:/root
haproxy.tar 100% 101MB 33.8MB/s 00:03
8.去node节点导入这些包(node1和node2都做)
[root@k8s-node1 ~]# ctr -n k8s.io images import centos.tar --platform=linux/amd64
unpacking docker.io/library/centos:latest (sha256:27f8ba8f837c2b1ef079a25095ea7cd4c778f31a33c09eb845a5be35fc207a3c)...done
[root@k8s-node1 ~]# ctr -n k8s.io images import nginx.tar --platform=linux/amd64
unpacking docker.io/library/nginx:latest (sha256:006a20213cb7f7d8edfadffd6791139d25283f590c5066e3fcd803e61a9c0b74)...done
[root@k8s-node1 ~]# ctr -n k8s.io images import mysql.tar --platform=linux/amd64
unpacking docker.io/library/mysql:5.7.44 (sha256:0b14b8e5c533fb43d07327fa8fe6c7393358002310d9f0d01976d387822a1bd3)...done
[root@k8s-node1 ~]# ctr -n k8s.io images import haproxy.tar --platform=linux/amd64
unpacking docker.io/library/haproxy:latest (sha256:4f4750d54310c5a3dbc94f3862cd3d8a266cbf1bfe806cf8eab64ace8501731b)...done
[root@k8s-node1 ~]# crictl images
IMAGE TAG IMAGE ID SIZE
docker.io/library/centos latest 5d0da3dc97646 239MB
docker.io/library/haproxy latest a782c02b82595 106MB
docker.io/library/mysql 5.7.44 5107333e08a87 520MB
docker.io/library/nginx latest 39286ab8a5e14 192MB
registry.cn-beijing.aliyuncs.com/dotbalo/cni v3.26.1 9dee260ef7f59 93.4MB
registry.cn-beijing.aliyuncs.com/dotbalo/metrics-scraper v1.0.8 115053965e86b 19.7MB
registry.cn-beijing.aliyuncs.com/dotbalo/metrics-server v0.6.3 817bbe3f2e517 29.8MB
registry.cn-beijing.aliyuncs.com/dotbalo/nginx latest 1403e55ab369c 56.9MB
registry.cn-beijing.aliyuncs.com/dotbalo/node v3.26.1 8065b798a4d67 86.6MB
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy v1.28.2 c120fed2beb84 24.6MB
registry.cn-hangzhou.aliyuncs.com/google_containers/pause 3.9 e6f1816883972 322kB
9.使用kubectl run 创建pod
[root@k8s-master ~]# kubectl run test001 --image docker.io/library/nginx:latest --image-pull-policy=IfNotPresent //--image-pull-policy=IfNotPresent 是 Kubernetes 中用于控制容器镜像拉取策略的一个参数,当容器启动时,Kubernetes 会检查本地是否已经存在所需的镜像。如果本地存在该镜像,则不会从镜像仓库拉取新的镜像;如果本地不存在,则会从镜像仓库拉取。
pod/test001 created
[root@k8s-master ~]# kubectl get po
NAME READY STATUS RESTARTS AGE
cluster-test-66bb44bd88-nk46t 1/1 Running 11 (34m ago) 33h
test001 1/1 Running 0 26s
[root@k8s-master ~]# kubectl get po -Aowide|grep test001
default test001 1/1 Running 0 4m28s 172.16.169.137 k8s-node2 <none> <none>
10.使用配置文件创建pod
[root@k8s-master ~]# cd pods/
[root@k8s-master pods]# vim test007.yaml
apiVersion: v1
kind: Pod
metadata:
name: test007
labels:
name: test007
spec:
containers:
- name: test007-nginx
image: docker.io/library/nginx:latest
imagePullPolicy: IfNotPersent
ports:
- name: nginxport
containerPort: 80
[root@k8s-master pods]# kubectl create -f test007.yaml
pod/test007 created
[root@k8s-master pods]# kubectl get po
NAME READY STATUS RESTARTS AGE
cluster-test-66bb44bd88-nk46t 1/1 Running 11 (47m ago) 33h
test001 1/1 Running 0 14m
test007 1/1 Running 0 28s