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

 

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

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

 

0x01 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

 

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:v4
gcr.io/google-samples/gb-frontend:v4

# 转换pull源头到anjia0532
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

# 将anjia0532的镜像打tag
docker 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/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

 

参考

 

posted @ 2018-12-24 18:15  时光飞逝,逝者如斯  阅读(2116)  评论(0编辑  收藏  举报