简体   繁体   English

将带有初始化的 docker 运行命令翻译到多容器 k8s pod 或 compose

[英]Translate docker run commands with initialization to a multi-container k8s pod or compose

I have a container that I need to configure for k8s yaml.我有一个需要为 k8s yaml 配置的容器。 The workflow on docker run using the terminal looks like this.:使用终端docker run的工作流程如下所示:

docker run -v $(pwd):/projects \
             -w /projects \
             gcr.io/base-project/myoh:v1 init *myproject*

This command creates a directory called myproject .此命令创建一个名为myproject的目录。 To complete the workflow, I need to cd into this myproject folder and run:要完成工作流程,我需要 cd 进入这个myproject文件夹并运行:

docker run -v $(pwd):/project \
             -w /project \
             -p 8081:8081 \
             gcr.io/base-project/myoh:v1

Any idea how to convert this to either a docker-compose or a k8s pods/deployment yaml?知道如何将其转换为 docker-compose 或 k8s pod/部署 yaml 吗? I have tried all that come to mind with no success.我已经尝试了所有想到的,但没有成功。

The bind mount of the current directory can't be translated to Kubernetes at all.当前目录的bind mount根本无法翻译成Kubernetes。 There's no way to connect a pod's filesystem back to your local workstation.无法将 pod 的文件系统连接回本地工作站。 A standard Kubernetes setup has a multi-node installation, and if it's possible to directly connect to a node (it may not be) you can't predict which node a pod will run on, and copying code to every node is cumbersome and hard to maintain.一个标准的 Kubernetes 设置有一个多节点安装,如果可以直接连接到一个节点(它可能不是)你无法预测一个 pod 将在哪个节点上运行,并且将代码复制到每个节点既麻烦又困难维持。 If you're using a hosted Kubernetes installation like GKE, it's even possible that the cluster autoscaler will create and delete nodes automatically, and you won't have an opportunity to manually copy things in.如果您使用像 GKE 这样的托管 Kubernetes 安装,集群自动缩放器甚至可能会自动创建和删除节点,您将没有机会手动复制内容。

You need to build your application code into a custom image.您需要将应用程序代码构建到自定义映像中。 That can set the desired WORKDIR , COPY the code in, and RUN any setup commands that are required.这可以设置所需的WORKDIRCOPY代码并RUN所需的任何设置命令。 Then you need to push that to an image repository, like GCR然后您需要将其推送到图像存储库,例如 GCR

docker build -t gcr.io/base-project/my-project:v1 .
docker push gcr.io/base-project/my-project:v1

Once you have that, you can create a minimal Kubernetes Deployment to run it.一旦你有了它,你可以创建一个最小的 Kubernetes 部署来运行它。 Set the GCR name of the image you built and pushed as its image: .将您构建并推送的镜像的 GCR 名称设置为其image: . You will also need a Service to make it accessible, even from other Pods in the same cluster.您还需要一个服务来使其可访问,即使是从同一集群中的其他 Pod 访问。

Try this (untested yaml, but you will get the idea)试试这个(未经测试的 yaml,但你会明白的)

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myoh-deployment
  labels:
    app: myoh
spec:
  replicas: 1
  selector:
    matchLabels:
      app: myoh
  template:
    metadata:
      labels:
        app: myoh
    spec:
      initContainers:
      - name: init-myoh
        image: gcr.io/base-project/myoh:v1
        command: ['sh', '-c', "mkdir -p myproject"]
      containers:
      - name: myoh
        image: gcr.io/base-project/myoh:v1
        ports:
        - containerPort: 8081
        volumeMounts:
        - mountPath: /projects
          name: project-volume
    volumes:
    - name: test-volume
      hostPath:
        # directory location on host
        path: /data
        # this field is optional
        type: Directory

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

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