google gcr.io、k8s.gcr.io 国内镜像

服务器

浏览数:224

2019-5-15

1、首先添加docker官方的国内镜像

sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://registry.docker-cn.co"]
}
EOF

2、gcr.io镜像

根据开源项目:

https://github.com/anjia0532/gcr.io_mirror

作者将gcr.io相关镜像pull下来,然后push到docker官方仓库,相关转换语法如下:

语法

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 <<EOF
# gcr.io/google-containers/federation-controller-manager-arm64:v1.3.1-beta.1
# gcr.io/google-containers/federation-controller-manager-arm64:v1.3.1-beta.1
# gcr.io/google-containers/federation-controller-manager-arm64:v1.3.1-beta.1
#EOF
#)

eval $(echo ${images}|
        sed 's/k8s\.gcr\.io/anjia0532\/google-containers/g;s/gcr\.io/anjia0532/g;s/\//\./g;s/ /\n/g;s/anjia0532\./anjia0532\//g' |
        uniq |
        awk '{print "docker pull "$1";"}'
       )

# this code will retag all of anjia0532's image from local  e.g. anjia0532/google-containers.federation-controller-manager-arm64:v1.3.1-beta.1 
# to gcr.io/google-containers/federation-controller-manager-arm64:v1.3.1-beta.1
# k8s.gcr.io/{image}/{tag} <==> 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

3、 pull-google-container 工具脚本

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

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:v4
gcr.io/google-samples/gb-frontend:v4
docker pull anjia0532/google-samples.gb-frontend:v4
v4: Pulling from anjia0532/google-samples.gb-frontend
Digest: sha256:aaa5b327ef3b4cb705513ab674fa40df66981616950c7de4912a621f9ee03dd4
Status: Image is up to date for anjia0532/google-samples.gb-frontend:v4
docker tag anjia0532/google-samples.gb-frontend:v4 gcr.io/google-samples/gb-frontend:v4

4. 替换已有的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/v1beta1
kind: Deployment
metadata:
  name: frontend
spec:
  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

作者:Jadepeng
出处:jqpeng的技术记事本–http://www.cnblogs.com/xiaoqi
您的支持是对博主最大的鼓励,感谢您的认真阅读。
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

作者:JadePeng