简体   繁体   English

kubernetes 使用 tar docker 映像部署

[英]kubernetes deploy with tar docker image

I have a problem to deploy docker image via kubernetes.我在通过 kubernetes 部署 docker 映像时遇到问题。

One issue is that, we cannot use any docker image registry service eg docker hub or any cloud services.一个问题是,我们不能使用任何 docker 映像注册表服务,例如 docker 集线器或任何云服务。 But, yes I have docker images as.tar file.但是,是的,我有 docker 图像作为 .tar 文件。

However, it always fails with following message但是,它总是失败并显示以下消息

Warning  Failed     1s         kubelet, dell20    

Failed to pull image "test:latest": rpc
error: code = Unknown
desc = failed to resolve image "docker.io/library/test:latest":
failed to do request: Head https://registry-1.docker.io/v2/library/test/manifests/latest: dial tcp i/o timeout

I also change deployment description with IfNotPresent or Never.我还使用 IfNotPresent 或 Never 更改部署描述。 In this case it will fail anyway with ErrImageNeverPull.在这种情况下,ErrImageNeverPull 无论如何都会失败。

My guess is: kubernetes tries to use Docker Hub anyway, since it https://registry-1.docker.io in order to pull the image. My guess is: kubernetes tries to use Docker Hub anyway, since it https://registry-1.docker.io in order to pull the image. I just want to use tar docker image in local disk, rather than pulling from some services.我只想在本地磁盘中使用 tar docker 映像,而不是从某些服务中提取。

And yes the image is in docker:是的,图像在 docker 中:

docker images
REPOSITORY  TAG    IMAGE ID      CREATED     SIZE
test        latest 9f4916a0780c  6 days ago  1.72GB

Can anyone give me any advices on this problem?任何人都可以就这个问题给我任何建议吗?

I was successful with using local image with Kubernetes cluster.我成功地将本地映像与 Kubernetes 集群一起使用。 I provided the explanation with example below:我用下面的例子提供了解释:

The only prerequisite is that you need to make sure you have access to upload this image directly to nodes.唯一的先决条件是您需要确保您有权将此图像直接上传到节点。

Create the image创建图像

Pull the default nginx image from docker registry with below command:使用以下命令从 docker 注册表中提取默认 nginx 映像:

$ docker pull nginx:1.17.5

Nginx image is used only for demonstration purposes. Nginx 图像仅用于演示目的。

Tag this image with new name as nginx-local with command:使用以下命令将此图像标记为nginx-local的新名称:

$ docker tag nginx:1.17.5 nginx-local:1.17.5

Save this image as nginx-local.tar executing command:将此图像保存为 nginx-local.tar 执行命令:

$ docker save nginx-local:1.17.5 > nginx-local.tar

Link to documentation: docker save文档链接: docker 保存

File nginx-local.tar is used as your image.文件nginx-local.tar用作您的图像。

Copy the image to all of the nodes将图像复制到所有节点

The problem with this technique is that you need to ensure all of the nodes have this image.这种技术的问题是您需要确保所有节点都有这个图像。
Lack of image will result in failed pod creation.缺少镜像会导致 Pod 创建失败。

To copy it you can use scp .要复制它,您可以使用scp It's secure way to transer files between machines.这是在机器之间传输文件的安全方式。
Example command for scp: scp 的示例命令:

$ scp /path/to/your/file/nginx-local.tar user@ip_adddress:/where/you/want/it/nginx-local.tar

If image is already on the node, you will need to load it into local docker image repository with command:如果图像已经在节点上,您需要使用以下命令将其加载到本地 docker 图像存储库中:

$ docker load -i nginx-local.tar

To ensure that image is loaded invoke command确保加载图像调用命令

$ docker images | grep nginx-local

Link to documentation: docker load :文档链接: docker 负载

It should show something like that:它应该显示如下内容:

docker images | grep nginx
nginx-local                                           1.17.5              540a289bab6c        3 weeks ago         126MB

Creating deployment with local image使用本地映像创建部署

The last part is to create deployment with use of nginx-local image.最后一部分是使用 nginx-local 映像创建部署。

Please note that:请注意:

  • The image version is explicitly typed inside yaml file.映像版本在 yaml 文件中显式键入
  • ImagePullPolicy is set to Never . ImagePullPolicy 设置为Never ImagePullPolicy图像拉取策略

Without this options the pod creation will fail.如果没有这个选项,pod 创建将失败。

Below is example deployment which uses exactly that image:下面是使用该图像的示例部署:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx-local
  namespace: default
spec:
  selector:
    matchLabels:
      run: nginx-local
  replicas: 5 
  template:
    metadata:
      labels:
        run: nginx-local
    spec:
      containers:
      - image: nginx-local:1.17.5
        imagePullPolicy: Never
        name: nginx-local
        ports:
        - containerPort: 80

Create this deployment with command: $ kubectl create -f local-test.yaml使用以下命令创建此部署: $ kubectl create -f local-test.yaml

The result was that pods were created successfully as shown below:结果是成功创建了 pod,如下图所示:

NAME                           READY   STATUS    RESTARTS   AGE
nginx-local-84ddb99b55-7vpvd   1/1     Running   0          2m15s
nginx-local-84ddb99b55-fgb2n   1/1     Running   0          2m15s
nginx-local-84ddb99b55-jlpz8   1/1     Running   0          2m15s
nginx-local-84ddb99b55-kzgw5   1/1     Running   0          2m15s
nginx-local-84ddb99b55-mc7rw   1/1     Running   0          2m15s

This operation was successful but I would recommend you to use local docker repository.此操作成功,但我建议您使用本地 docker 存储库。 It will easier management process with images and will be inside your infrastructure.它将更容易使用图像进行管理,并将在您的基础架构中。 Link to documentation about it: Local Docker Registry链接到有关它的文档:本地 Docker 注册表

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

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