如何使用 Kubernetes 在 Cloud Containers Engine 中的 Container 开头 运行 脚本

How to Run a script at the start of Container in Cloud Containers Engine with Kubernetes

我正在尝试 运行 shell 脚本在 docker 容器 运行 上使用 Kubernetes 的 Google 云容器开始。我的应用程序目录的结构是这样的。我想在容器的开头添加 运行 prod_start.sh 脚本(不过我不想将它作为 Dockerfile 的一部分)。当前设置无法使用 Command not found file ./prod_start.sh does not exist 启动容器。知道如何解决这个问题吗?

app/
  ...
  Dockerfile
  prod_start.sh
  web-controller.yaml
  Gemfile
  ...

Docker 文件

FROM ruby
RUN mkdir /backend
WORKDIR /backend
ADD Gemfile /backend/Gemfile
ADD Gemfile.lock /backend/Gemfile.lock
RUN bundle install

网络-controller.yaml

apiVersion: v1
kind: ReplicationController
metadata:
  name: backend
  labels:
    app: myapp
    tier: backend
spec:
  replicas: 1
  selector:
    app: myapp
    tier: backend
  template:
    metadata:
      labels:
        app: myapp
        tier: backend
    spec:
      volumes:
      - name: secrets
        secret:
          secretName: secrets
      containers:
      - name: my-backend
        command: ['./prod_start.sh']
        image: gcr.io/myapp-id/myapp-backend:v1
        volumeMounts:
        - name: secrets
          mountPath: /etc/secrets
          readOnly: true
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        ports:
        - containerPort: 80
          name: http-server

经过大量实验,我相信将脚本添加到 Dockerfile:

ADD prod_start.sh /backend/prod_start.sh

然后在 yaml 控制器文件中调用如下命令:

command: ['/bin/sh', './prod_start.sh']

已修复。

您可以将配置映射添加到您的 yaml 而不是添加到您的 dockerfile。

apiVersion: v1
kind: ReplicationController
metadata:
  name: backend
  labels:
    app: myapp
    tier: backend
spec:
  replicas: 1
  selector:
    app: myapp
    tier: backend
  template:
    metadata:
      labels:
        app: myapp
        tier: backend
    spec:
      volumes:
      - name: secrets
        secret:
          secretName: secrets
      - name: prod-start-config
        configMap:
          name: prod-start-config-script
          defaultMode: 0744
      containers:
      - name: my-backend
        command: ['./prod_start.sh']
        image: gcr.io/myapp-id/myapp-backend:v1
        volumeMounts:
        - name: secrets
          mountPath: /etc/secrets
          readOnly: true
        - name: prod-start-config
          mountpath: /backend/
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        ports:
        - containerPort: 80
          name: http-server

然后为您的脚本创建另一个 yaml 文件: script.yaml

apiVersion: v1
kind: ConfigMap
metadata: 
  name: prod-start-config-script
data:
  prod_start.sh: |
    apt-get update 

部署后,脚本将位于脚本目录中