1. 程式人生 > >容器學習筆記之將Google的gcr.io、k8s.gcr.io 換為國內映象

容器學習筆記之將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

 

參考