Docker-for-desktop kubernetes从私有存储库中提取图像

时间:2020-11-07 23:46:53

标签: docker kubernetes google-cloud-platform docker-for-mac

我是kubernetes和GCP的新手。我正在尝试在本地部署。我有一张图片,它在Google注册中心的私有存储库中。

我能够在GCP集群中进行部署,但是当我尝试应用部署时,在本地得到ErrImagePull

我尝试了以下步骤

  1. 使用角色Service Account创建了一个Viewer并下载了json文件

  2. 我使用以下命令openssl base64 -in file.json -out encodedfile.json

    对文件进行了编码
  3. 我删除了编码文件上的返回字符(将编码内容放在一行中)

  4. 我创建了一个带有Yaml的机密,以便能够访问Docker注册表,并将已编码文件的内容粘贴到.dockerconfigjson

    apiVersion:v1 种类:秘密 元数据: 名称:gcr-json-key 命名空间:开发 数据: .dockerconfigjson:xxxxx 类型:kubernetes.io/dockerconfigjson

  5. 在我添加的部署中

    imagePullSecrets:

    • 名称:gcr-json-key

我遇到了同样的错误,它无法从Google私有注册表中提取到我的本地计算机中


更新1

我使用此命令对json文件进行了编码

base64 -i myorg-8b8eea93246a.json -o encoded-myorg-8b8eea93246a.json

然后我检查了此编码文件是否有效

cat encoded-myorg-8b8eea93246a.json | docker login -u _json_key_base64 --password-stdin \
https://us-docker.pkg.dev

成功了

Login Succeeded

这是我用来创建机密的yaml文件

apiVersion: v1
kind: Secret
metadata:
  name: gcr-json-key
  namespace: development
data:
  .dockerconfigjson: <XXXX content of encoded myorg-8b8eea93246a.json file XXXX>
type: kubernetes.io/dockerconfigjson

在部署中,我有

...
spec:
  ...
  imagePullSecrets:
  - name: gcr-json-key
...

已创建部署,但未提取映像。在kubectl get all中,我可以看到状态ImagePullBackOff

当我对吊舱进行描述

Failed to pull image "gcr.io/xxx/yyy": rpc error: code = Unknown desc = Error response from daemon: unauthorized: You don't have the needed permissions to perform this operation, and you may have invalid credentials.

1 个答案:

答案 0 :(得分:2)

您在正确的道路上。您需要为注册表登录创建密码。这对我有用:

import requests
from bs4 import BeautifulSoup as bs
import pandas as pd
import datetime

def get_date(date_string):
    date_parts = date_string.split(' ')
    article_date = '-'.join([date_parts[-1], month_numbers[date_parts[1].lower()], date_parts[0].zfill(2)])
    d = datetime.datetime.strptime(article_date, "%Y-%m-%d").date()
    return d

month_numbers =  { 'gennaio' : '01',
                    'febbraio' : '02',
                    'marzo' : '03',
                    'aprile' : '04',
                    'maggio' : '05',
                    'giugno' : '06',
                    'luglio' : '07',
                    'agosto' : '08',
                    'settembre' : '09',
                    'ottobre' : '10',
                    'novembre' : '11',
                    'dicembre' : '12',
        }

next_page = True
final = pd.DataFrame()

def main(page):
    global final
    global next_page
    results = []

    with requests.Session() as s:

        soup =  bs(s.get(f'http://www.lavocedellevoci.it/category/inchieste/page/{page}').content, 'lxml')
    
        for article in soup.select('article'):  #soup.select('article:has(a:contains("Inchieste"))') if need to be more restrictive in future
            title = article.select_one('h1').text
            date = get_date(article.select_one('.homepage_post-date').text)
            link = article.select_one('.read-more')['href']
            soup2 =  bs(s.get(link).content, 'lxml')
            text = '\n'.join([i.text for i in soup2.select('article p:not([class])')])
            results.append([title, date, text])

        df = pd.DataFrame(results, columns = ['Title', 'Date', 'Content'])
        
        if df.empty:
            final = df
        else:
            final = pd.concat([final, df], sort = False)
        
        next_page = soup.select_one('.next') is not None
        
if __name__ == '__main__':
    page = 1

    while next_page: # page < 3: 
        main(page)
        page+=1
        
    final = final.sort_values('Date').reset_index()
    print(final)

然后我将这个秘密用于部署:

kubectl create secret docker-registry <secret_name> --docker-server=<your.registry.domain.name> --docker-username=<user> --docker-password=<password> --docker-email=<your_email>