简体   繁体   English

如何使用私有Docker注册表定义Kubernetes Job?

[英]How to define Kubernetes Job using a private docker registry?

I have a simple Kubernetes job (based on the http://kubernetes.io/docs/user-guide/jobs/work-queue-2/ example) which uses a Docker image that I have placed as a public image on my dockerhub account. 我有一个简单的Kubernetes作业(基于http://kubernetes.io/docs/user-guide/jobs/work-queue-2/示例),该作业使用放置在dockerhub上的公共映像作为Docker映像。帐户。 It all loks like this: 一切都像这样:

job.yaml : job.yaml

apiVersion: batch/v1
kind: Job
metadata:
  name: job-wq-2
spec:
  parallelism: 2
  template:
    metadata:
      name: job-wq-2
    spec:
      containers:
      - name: c
        image: jonalv/job-wq-2
      restartPolicy: OnFailure

Now I want to try to instead use a private Docker registry which requires authentication as in: 现在,我想尝试改为使用需要身份验证的私有Docker注册表,如下所示:

docker login https://myregistry.com

But I can't find anything about how I add username and password to my job.yaml file. 但是我找不到有关如何在我的job.yaml文件中添加用户名和密码的任何信息。 How is it done? 怎么做?

You need to use ImagePullSecrets . 您需要使用ImagePullSecrets

Once you create a secret object, you can refer to it in your pod spec (the spec value that is the parent of containers : 创建秘密对象后,您可以在pod规范(作为containers父级的spec值)中引用该秘密对象:

apiVersion: batch/v1
kind: Job
metadata:
  name: job-wq-2
spec:
  parallelism: 2
  template:
    metadata:
      name: job-wq-2
    spec:
      imagePullSecrets:
      - name: myregistrykey
      containers:
      - name: c
        image: jonalv/job-wq-2
      restartPolicy: OnFailure

Ofcourse, you'll have to create the secret (as per the docs). 当然,您必须创建秘密(根据文档)。 This is what this will look like: 这是这样的:

apiVersion: v1
kind: Secret
metadata:
  name: myregistrykey
  namespace: mynamespace
data:
  .dockerconfigjson: UmVhbGx5IHJlYWxseSByZWVlZWVlZWVlZWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGx5eXl5eXl5eXl5eXl5eXl5eXl5eSBsbGxsbGxsbGxsbGxsbG9vb29vb29vb29vb29vb29vb29vb29vb29vb25ubm5ubm5ubm5ubm5ubm5ubm5ubm5ubmdnZ2dnZ2dnZ2dnZ2dnZ2dnZ2cgYXV0aCBrZXlzCg==
type: kubernetes.io/dockerconfigjson

The value of .dockerconfigjson is a base64 encoding of this file: .docker/config.json . .dockerconfigjson的值是此文件的base64编码: .docker/config.json

The key point: A job spec contains a pod spec . 关键点: 工作规范包含pod规范 So whatever knowledge you gain about pod specs can be applied to jobs as well. 因此,您所获得的有关Pod规格的任何知识也可以应用于工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM