K8S学习笔记之将Google的gcr.io、k8s.gcr.io 换为国内镜像

网友投稿 310 2022-09-10

K8S学习笔记之将Google的gcr.io、k8s.gcr.io 换为国内镜像

0x00 添加docker官方的国内镜像

sudo tee /etc/docker/daemon.json <<-'EOF'{ "registry-mirrors": ["一般语法gcr.io/namespace/image_name:image_tag <====> anjia0532/namespace.image_name:image_tag# 特别语法k8s.gcr.io/{image}/{tag} <==> gcr.io/google-containers/{image}/{tag} <==> anjia0532/google-containers.{image}/{tag}

批量转换脚本

# replace gcr.io/google-containers/federation-controller-manager-arm64:v1.3.1-beta.1 to real image# this will convert gcr.io/google-containers/federation-controller-manager-arm64:v1.3.1-beta.1 # to anjia0532/google-containers.federation-controller-manager-arm64:v1.3.1-beta.1 and pull it# k8s.gcr.io/{image}/{tag} <==> gcr.io/google-containers/{image}/{tag} <==> anjia0532/google-containers.{image}/{tag}images=$(cat img.txt)#or #images=$(cat < gcr.io/google-containers/{image}/{tag} <==> anjia0532/google-containers.{image}/{tag}for img in $(docker images --format "{{.Repository}}:{{.Tag}}"| grep "anjia0532"); do n=$(echo ${img}| awk -F'[/.:]' '{printf "gcr.io/%s",$2}') image=$(echo ${img}| awk -F'[/.:]' '{printf "/%s",$3}') tag=$(echo ${img}| awk -F'[:]' '{printf ":%s",$2}') docker tag $img "${n}${image}${tag}" [[ ${n} == "gcr.io/google-containers" ]] && docker tag $img "k8s.gcr.io${image}${tag}"done

0x02 pull-google-container 工具脚本

K8S的exapmle里的yaml默认是k8s.gcr.io的镜像,为了方便运行我们可以预先拉取相关镜像:

pull-google脚本

# vim pull-google.sh

image=$1 echo $1 img=`echo $image | sed 's/k8s\.gcr\.io/anjia0532\/google-containers/g;s/gcr\.io/anjia0532/g;s/\//\./g;s/ /\n/g;s/_/-/g;s/anjia0532\./anjia0532\//g' | uniq | awk '{print ""$1""}'` echo "docker pull $img" docker pull $img echo "docker tag $img $image" docker tag $img $image~

然后放到/usr/local/bin

# chmod +x pull-google.sh && cp pull-google.sh /usr/local/bin/pull-google-container

就可以愉快的使用pull-google-container 命令了

pull-google-container gcr.io/google-samples/gb-frontend:v4gcr.io/google-samples/gb-frontend:v4# 转换pull源头到anjia0532docker pull anjia0532/google-samples.gb-frontend:v4v4: Pulling from anjia0532/google-samples.gb-frontendDigest: sha256:aaa5b327ef3b4cb705513ab674fa40df66981616950c7de4912a621f9ee03dd4Status: Image is up to date for anjia0532/google-samples.gb-frontend:v4# 将anjia0532的镜像打tagdocker tag anjia0532/google-samples.gb-frontend:v4 gcr.io/google-samples/gb-frontend:v4

0x03 利用替换已有的yaml的方法

除了预先拉取镜像,我们还可以将k8s.gcr.io 替换为可执行镜像

为了方便替换,我们编写一个repair_yaml 脚本:

# vim /usr/local/bin/repair_yaml

cp $1 "$1.bak" cat $1.bak | sed 's/k8s\.gcr\.io\//anjia0532\/google-containers./g;s/gcr\.io\/\(.*\)\//anjia0532\/\1./g;s/google_/google-/g;' > $1 rm -f "$1.bak"

保存退出,然后

# chmod +x /usr/local/bin/repair_yaml

效果:

$ repair_yaml frontend-deployment.yaml $ cat frontend-deployment.yaml apiVersion: apps/v1 # for k8s versions before 1.9.0 use apps/v1beta2 and before 1.8.0 use extensions/v1beta1kind: Deploymentmetadata: name: frontendspec: selector: matchLabels: app: guestbook tier: frontend replicas: 3 template: metadata: labels: app: guestbook tier: frontend spec: # 更换镜像源 containers: - name: php-redis image: anjia0532/google-samples.gb-frontend:v4 resources: requests: cpu: 100m memory: 100Mi env: - name: GET_HOSTS_FROM value: dns # If your cluster config does not include a dns service, then to # instead access environment variables to find service host # info, comment out the 'value: dns' line above, and uncomment the # line below: # value: env ports: - containerPort: 80

​​参考​​

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Kafka学习笔记之K8S内filebeat传输到kafka报错带解决方案
下一篇:K8S学习笔记之CentOS7集群使用Chrony实现时间同步
相关文章

 发表评论

暂时没有评论,来抢沙发吧~