06-K8S离线依赖库和镜像制作

docker环境下载

1.yum仓库添加

# vim /etc/yum.repos.d/docker-ce.repo 
[docker-ce-stable]
name=Docker CE Stable - $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/$basearch/stable
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

推荐添加阿里云的源,速度非常快。

2.docker环境本地下载

yum install --downloadonly  --downloaddir=. docker

该命令会把docker以及依赖全下载到指定目录。
注:若发现目录下没有下载的rpm包,请到/var/cache/yum/{RepositoryName}/packages/目录中找。

$ ls /var/cache/yum/x86_64/7/
base            docker-ce-nightly  docker-ce-test  extras          timedhosts      updates
docker-ce-edge  docker-ce-stable   epel            saltstack-repo  timedhosts.txt  zabbix

kubeadm环境下载

kubeadm下载后k8s所需的rpm都齐了。

1.yum仓库添加

# vim /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg

推荐添加阿里云的源,速度非常快。

2.kubeadm环境本地下载

# yum install --downloadonly  --downloaddir=. kubeadm

该命令会把docker以及依赖全下载到指定目录。
注:若发现目录下没有下载的rpm包,请到/var/cache/yum/{RepositoryName}/packages/目录中找。

k8s依赖镜像下载

kubeadm安装时会访问特定的tag,若不存在就会走外网下载,所以kubeadm部署前必须将所需的k8s环境以镜像方式下载好,并导入docker中。

1.阿里云海外镜像下载配置

由于各种原因,k8s的依赖kube-proxy,kube-apiserver,kube-controller-manager,kube-scheduler,coredns,etcd和pause都难于下载,本人通过阿里云海外镜像下载方式,分享一下。

1) 将github上建立一个项目,github的配置不再赘述。

2) 将所有依赖建立目录,并在每个目录下建立Dockerfile,并输入FROM命令,每个组件的命令如下[注意是每个组,假如kube-apiserver,Dockerfile只需要FROM k8s.gcr.io/kube-apiserver:v1.16.3,不需要其他的东西]:

FROM k8s.gcr.io/kube-apiserver:v1.16.3

FROM k8s.gcr.io/kube-proxy:v1.16.3

FROM k8s.gcr.io/kube-controller-manager:v1.16.3

FROM k8s.gcr.io/kube-scheduler:v1.16.3

FROM k8s.gcr.io/etcd:3.3.15-0

FROM k8s.gcr.io/coredns:1.6.2

FROM k8s.gcr.io/pause:3.1

其中,每个需要的组件以及版本的信息通过命令
$ kubeadm --kubernetes-version=v1.16.3 config images list
得到。

将这些信息commit到github上,提交方法不再赘述。

3) 创建镜像仓库,并选择公开。下一步选择github帐号关联,并选择之前提交的项目,务必勾选上海外机器构建。



4) 创建完镜像仓库后,进去构建选项卡,将之前上传的几个依赖分别如下图配置,因为一个仓库只能创建5个镜像,所以需要再以相同方法建一个镜像并构建。



5) 创建完后后台登陆阿里云,在阿里云的基本信息中看到登录方式。

sudo docker login --username=<username> registry.cn-hangzhou.aliyuncs.com

并输入密码,用户名和密码是对应阿里云帐号的。

6) 进入构建,对每个镜像点击立即构建,下面的构建日志成功即表示拉到了国内仓库。若失败则可能登陆超时,重新执行第5步。

7) docker拉取镜像。

sudo docker pull registry.cn-hangzhou.aliyuncs.com/zjw-kubernetes/k8s-docker-dep:[镜像版本号]

# sudo docker pull registry.cn-hangzhou.aliyuncs.com/zjw-kubernetes/k8s-docker-dep:kube-controller-manager-1.16.3
# sudo docker pull registry.cn-hangzhou.aliyuncs.com/zjw-kubernetes/k8s-docker-dep:kube-scheduler-1.16.3
# sudo docker pull registry.cn-hangzhou.aliyuncs.com/zjw-kubernetes/k8s-docker-dep:kube-apiserver-1.16.3
# sudo docker pull registry.cn-hangzhou.aliyuncs.com/zjw-kubernetes/k8s-docker-dep:kube-proxy-1.16.3
# sudo docker pull registry.cn-hangzhou.aliyuncs.com/zjw-kubernetes/k8s-docker-dep:coredns-1.6.2
# sudo docker pull registry.cn-hangzhou.aliyuncs.com/zjw-kubernetes/k8s-docker-dep-2:etcd-3.3.15-0
# sudo docker pull registry.cn-hangzhou.aliyuncs.com/zjw-kubernetes/k8s-docker-dep-2:pause-3.1

2.依赖镜像重打tag

拉取镜像后,需要修改成kubeadm拉取的tag,并删除老的tag。首先通过命令查看已经拉取的镜像:

$ docker images -a
REPOSITORY                                                          TAG                              IMAGE ID            CREATED             SIZE
registry.cn-hangzhou.aliyuncs.com/zjw-kubernetes/k8s-docker-dep-2   etcd-3.3.15-0                    b2756210eeab        3 months ago        247MB
registry.cn-hangzhou.aliyuncs.com/zjw-kubernetes/k8s-docker-dep-2   pause-3.1                        da86e6ba6ca1        23 months ago       742kB
registry.cn-hangzhou.aliyuncs.com/zjw-kubernetes/k8s-docker-dep     coredns-1.6.2                    bf261d157914        3 months ago        44.1MB
registry.cn-hangzhou.aliyuncs.com/zjw-kubernetes/k8s-docker-dep     kube-apiserver-1.16.3            df60c7526a3d        3 weeks ago         217MB
registry.cn-hangzhou.aliyuncs.com/zjw-kubernetes/k8s-docker-dep     kube-controller-manager-1.16.3   bb16442bcd94        3 weeks ago         163MB
registry.cn-hangzhou.aliyuncs.com/zjw-kubernetes/k8s-docker-dep     kube-proxy-1.16.3                9b65a0f78b09        3 weeks ago         86.1MB
registry.cn-hangzhou.aliyuncs.com/zjw-kubernetes/k8s-docker-dep     kube-scheduler-1.16.3            98fecf43a54f        3 weeks ago         87.3MB

执行命令:

sudo docker tag registry.cn-hangzhou.aliyuncs.com/zjw-kubernetes/k8s-docker-dep:kube-apiserver-1.16.3 k8s.gcr.io/kube-apiserver:v1.16.3
sudo docker tag registry.cn-hangzhou.aliyuncs.com/zjw-kubernetes/k8s-docker-dep:kube-controller-manager-1.16.3 k8s.gcr.io/kube-controller-manager:v1.16.3
sudo docker tag registry.cn-hangzhou.aliyuncs.com/zjw-kubernetes/k8s-docker-dep:kube-scheduler-1.16.3 k8s.gcr.io/kube-scheduler:v1.16.3
sudo docker tag registry.cn-hangzhou.aliyuncs.com/zjw-kubernetes/k8s-docker-dep:kube-proxy-1.16.3 k8s.gcr.io/kube-proxy:v1.16.3
sudo docker tag registry.cn-hangzhou.aliyuncs.com/zjw-kubernetes/k8s-docker-dep-2:pause-3.1 k8s.gcr.io/pause:3.1
sudo docker tag registry.cn-hangzhou.aliyuncs.com/zjw-kubernetes/k8s-docker-dep-2:etcd-3.3.15-0 k8s.gcr.io/etcd:3.3.15-0
sudo docker tag registry.cn-hangzhou.aliyuncs.com/zjw-kubernetes/k8s-docker-dep:coredns-1.6.2 k8s.gcr.io/coredns:1.6.2

sudo docker rmi registry.cn-hangzhou.aliyuncs.com/zjw-kubernetes/k8s-docker-dep:kube-apiserver-1.16.3
sudo docker rmi registry.cn-hangzhou.aliyuncs.com/zjw-kubernetes/k8s-docker-dep:kube-controller-manager-1.16.3
sudo docker rmi registry.cn-hangzhou.aliyuncs.com/zjw-kubernetes/k8s-docker-dep:kube-scheduler-1.16.3
sudo docker rmi registry.cn-hangzhou.aliyuncs.com/zjw-kubernetes/k8s-docker-dep:kube-proxy-1.16.3
sudo docker rmi registry.cn-hangzhou.aliyuncs.com/zjw-kubernetes/k8s-docker-dep-2:pause-3.1
sudo docker rmi registry.cn-hangzhou.aliyuncs.com/zjw-kubernetes/k8s-docker-dep-2:etcd-3.3.15-0
sudo docker rmi registry.cn-hangzhou.aliyuncs.com/zjw-kubernetes/k8s-docker-dep:coredns-1.6.2

3.网络模块flannel下载

执行命令sudo docker pull jmgao1983/flannel:v0.11.0-amd64

$ mkdir flannel && cd flannel
$ wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

修改kube-flannel.yml配置文件,将镜像地址改成本地(tag也可)

sudo sed -i 's#quay.io/coreos/flannel:v0.11.0-amd64#jmgao1983/flannel:v0.11.0-amd64#g' kube-flannel.yml
# sudo sed -i 's#quay.io/coreos/flannel:v0.11.0-s390x#jmgao1983/flannel:v0.11.0-amd64#g' kube-flannel.yml

4.所有k8s依赖包导出

执行导出命令至k8s-images-1.16.3.tar

sudo docker save $(docker images | grep -v REPOSITORY | awk 'BEGIN{OFS=":";ORS=" "}{print $1,$2}') -o k8s-images-1.16.3.tar

最后,将 docker 以及依赖的 rpm 包,kubeadm 以及依赖的 rpm 包,kubeadm 安装需要的 docker 镜像包,kubeadm.yaml,kube-flannel.yml 一起打包分发到各台待安装的服务器上。

点击量:36

发表评论