分享一个很香的k8s.gcr.io Docker镜像拉取方法

当遇到无法拉取k8s.gcr.io等国外镜像时,可以利用Github Actions配合Dockerhub实现镜像的自动同步。创建Github仓库,设置Dockerhub Secrets,编写CI配置文件,让Github服务器拉取镜像,重新打标签并推送到Dockerhub,从而解决国内访问限制问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

经常k8s.gcr.io ,quay.io之类的国外镜像拉取不过来,那怎么办呢?

本文安利一种使用github action拉取镜像推送到Dockerhub的方法。推送好了之后,自己拉取镜像retag一下就能用了

编译kubernetes源码需要好几个国外镜像,我的构建服务器根本连不上k8s.gcr.io, -_-!!!

比如我现在要拉取的镜像叫做k8s.gcr.io/build-image/kube-cross:v1.21.0-go1.16.15-buster.0.

1. 创建一个Github仓库

创建一个github仓库,并初始化Action.

在这里插入图片描述

2. 创建DockerHub Secret

在这里插入图片描述

一定要用Repository Secret类型,上面的Environment Secret是我建错了,根本不好使!!

3. 编写你的ci yaml文件为

底下的代码都有注释,大概思路就是

  1. 让github的服务器拉取k8s.gcr.io的镜像
  2. 登录你的dockerhub账户
  3. 重新tag一下拉取的镜像为你仓库的tag
  4. 推送到dockerhub
# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the workflow will run
on:
  # Triggers the workflow on push or pull request events but only for the "main" branch
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v3

      # Runs a set of commands using the runners shell
      - name: Run a multi-line script
        run: |
          docker pull k8s.gcr.io/build-image/kube-cross:v1.21.0-go1.16.15-buster.0
          docker tag k8s.gcr.io/build-image/kube-cross:v1.21.0-go1.16.15-buster.0 oneslide/k8s.gcr.io:kube-cross-v1.21.0-go1.16.15-buster.0
      
      - name: Log in to Docker Hub
        uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
      
      # Runs a set of commands using the runners shell
      - name: Push to Docker Registry
        run: |
          docker push oneslide/k8s.gcr.io:kube-cross-v1.21.0-go1.16.15-buster.0

4. 观察gitlab CI输出

在这里插入图片描述

Reference List

  1. https://docs.github.com/en/actions/publishing-packages/publishing-docker-images
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值