简体   繁体   English

如何在Kubernetes卷中使用Skaffold?

[英]How to use Skaffold with kubernetes volumes?

I have an python application whose docker build takes about 15-20 minutes. 我有一个python应用程序,其docker build大约需要15-20分钟。 Here is how my Dockerfile looks like more or less 这是我的Dockerfile看起来或多或少的样子

FROM ubuntu:18.04
...
COPY . /usr/local/app
RUN pip install -r /usr/local/app/requirements.txt
...
CMD ...

Now if I use skaffold, any code change triggers a rebuild and it is going to do a reinstall of all requirements(as from the COPY step everything else is going to be rebuild) regardless of whether they were already installed. 现在,如果我使用skaffold,则任何代码更改都会触发重建,并且它将重新安装所有需求(从COPY步骤开始,所有其他需求都将被重建),无论它们是否已经安装。 iIn docker-compose this issue would be solved using volumes. 在泊坞窗中,使用卷即可解决此问题。 In kubernetes, if we use volumes in the following way: 在kubernetes中,如果我们以以下方式使用卷:

apiVersion: v1
kind: Pod
metadata:
name: test
spec:
containers:
- image: test:test
name: test-container
volumeMounts:
- mountPath: /usr/local/venv # this is the directory of the 
# virtualenv of python
    name: test-volume
volumes:
- name: test-volume
  awsElasticBlockStore:
    volumeID: <volume-id>
    fsType: ext4

will this extra requirements build be resolved with skaffold? skaffold是否可以解决此额外要求?

I can't speak for skaffold specifically but the container image build can be improved. 我不能专门为skaffold说话,但是可以改善容器映像的构建。 If there is layer caching available then only reinstall the dependencies when your requirements.txt changes. 如果有可用的层缓存,则仅当您的requirements.txt更改时才重新安装依赖项。 This is documented in the "ADD or COPY" Best Practices . 这在“添加或复制” 最佳做法中有所记录。

FROM ubuntu:18.04
...
COPY requirements.txt /usr/local/app/
RUN pip install -r /usr/local/app/requirements.txt
COPY . /usr/local/app
...
CMD ...

You may need to trigger updates some times if the module versions are loosely defined and say you want a new patch version. 如果模块版本定义松散,并说您想要一个新的补丁程序版本,则可能需要触发一些更新。 I've found requirements should be specific so versions don't slide underneath your application without your knowledge/testing. 我发现要求应该是特定的,因此在没有您的知识/测试的情况下,版本不会滑入您的应用程序下方。

Kaniko in-cluster builds Kaniko集群内构建

For kaniko builds to make use of a cache in a cluster where there is no persistent storage by default, kaniko needs either a persistent volume mounted ( --cache-dir ) or a container image repo ( --cache-repo ) with the layers available. 为了使kaniko构建在默认情况下没有持久性存储的集群中利用缓存,kaniko需要安装具有层的持久性卷( --cache-dir )或容器映像存储库( --cache-repo )可用。

@Matt's answer is a great best practice (+1) - skaffold in and of itself won't solve the underlying layer cache invalidation issues which results in having to re-install the requirements during every build. skaffold答案是一个很好的最佳实践(+1) skaffold本身并不能解决底层的层缓存失效问题,这导致必须在每次构建过程中重新安装需求。

For additional performance, you can cache all the python packages in a volume mounted in your pod for example: 为了提高性能,您可以将所有python软件包缓存在您的pod中安装的volume中,例如:

apiVersion: v1
kind: Pod
metadata:
name: test
spec:
containers:
- image: test:test
  name: test-container
volumeMounts:
- mountPath: /usr/local/venv
    name: test-volume
- mountPath: /root/.cache/pip
    name: pip-cache
volumes:
- name: test-volume
  awsElasticBlockStore:
    volumeID: <volume-id>
    fsType: ext4
- name: pip-cache
  awsElasticBlockStore:
    volumeID: <volume-id>
    fsType: ext4

That way if the build cache is ever invalidated and you have to re-install your requirements.txt you'd be saving some time by fetching them from cache. 这样,如果构建缓存曾经失效,而您必须重新安装您的requirements.txt ,则可以通过从缓存中获取它们来节省一些时间。

If you're building with kaniko you can also cache base images to a persistent disk using the kaniko-warmer , for example: 如果使用kaniko构建, kaniko还可以使用kaniko-warmer将基本映像缓存到永久磁盘上,例如:

...
volumeMounts:
...
- mountPath: /cache
    name: kaniko-warmer
volumes:
...
- name: kaniko-warmer
  awsElasticBlockStore:
    volumeID: <volume-id>
    fsType: ext4

Running the kaniko-warmer inside the pod: docker run --rm -it -v /cache:/cache --entrypoint /kaniko/warmer gcr.io/kaniko-project/warmer --cache-dir=/cache --image=python:3.7-slim --image=nginx:1.17.3 . 在pod中运行kaniko-warmerdocker run --rm -it -v /cache:/cache --entrypoint /kaniko/warmer gcr.io/kaniko-project/warmer --cache-dir=/cache --image=python:3.7-slim --image=nginx:1.17.3 Your skaffold.yaml might look something like: 您的skaffold.yaml可能类似于:

apiVersion: skaffold/v1beta13
kind: Config
build:
  artifacts:
  - image: test:test
    kaniko:
      buildContext:
        localDir: {}
      cache:
        hostPath: /cache
  cluster:
    namespace: jx
    dockerConfig:
      secretName: jenkins-docker-cfg
  tagPolicy:
    envTemplate:
      template: '{{.DOCKER_REGISTRY}}/{{.IMAGE_NAME}}'
deploy:
  kubectl: {}

If your goal is to speed up the dev process: Instead of triggering an entirely new deployment process every time you change a line of code, you can switch to a sync-based dev process to deploy once and then update the files within the running containers when editing code. 如果您的目标是加快开发过程:您不必每次更改一行代码就触发一个全新的部署过程,而是可以切换到基于同步的开发过程进行一次部署,然后更新正在运行的容器中的文件编辑代码时。

Skaffold supports file sync to directly update files inside the deployed containers if you change them on your local machine. 如果在本地计算机上更改文件, Skaffold支持文件同步以直接更新已部署容器内的文件 However, the docs state "File sync is alpha" ( https://skaffold.dev/docs/how-tos/filesync/ ) and I can completely agree from working with it a while ago: The sync mechanism is only one-directional (no sync from container back to local) and pretty buggy, ie it crashes frequently when switching git branches, installing dependencies etc. which can be pretty annoying. 但是,文档状态为“文件同步为Alpha”( https://skaffold.dev/docs/how-tos/filesync/ ),我完全同意在一段时间前使用它:同步机制只是单向的(没有从容器同步回本地)和相当多的错误,即,当切换git分支,安装依赖项等时,它经常崩溃,这可能很烦人。

If you want a more stable alternative for sync-based Kubernetes development which is very easy to get started with, take a look at DevSpace: https://github.com/devspace-cloud/devspace 如果您想轻松地开始基于同步的Kubernetes开发的替代方案 ,请查看DevSpace: https : //github.com/devspace-cloud/devspace

I am one of the maintainers of DevSpace and started the project because Skaffold was much too slow for our team and it did not have a file sync back then. 我是DevSpace的维护者之一,因此开始了这个项目,因为Skaffold对我们的团队来说太慢了,那时还没有文件同步。

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

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