kubernetes 使用不同的命令为同一个镜像创建多个 pods/deployments

标签 kubernetes google-kubernetes-engine

我在单个 pod 中部署了 2 个容器( container-test2 cloudsql-proxy )。

容器测试 2 运行一个 docker 镜像,它将 ["my_app", "arg1", "arg2"] 作为 CMD 传递。我想用不同的参数组合运行这个容器的几个实例。我还想在单独的 pod 中运行它们,以便我可以跨节点分发它们。我不完全确定如何做到这一点。

我可以成功运行这两个容器,但我不知道如何使用不同的参数复制 container-test2 并使每个容器在单个 pod 内启动。

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: test
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: test
    spec:
      containers:
        - image: gcr.io/testing-11111/testology:latest
          name: container-test2
          command: ["my_app", "arg1", "arg2"]
          env:
            - name: DB_HOST
              # Connect to the SQL proxy over the local network on a fixed port.
              # Change the [PORT] to the port number used by your database
              # (e.g. 3306).
              value: 127.0.0.1:5432
            # These secrets are required to start the pod.
            # [START cloudsql_secrets]
            - name: DB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: cloudsql-db-credentials
                  key: password
            - name: DB_USER
              valueFrom:
                secretKeyRef:
                  name: cloudsql-db-credentials
                  key: username
            # [END cloudsql_secrets]
          resources:
            requests:
              #memory: "64Mi"
              cpu: 0.1
            limits:
              memory: "375Mi"
              cpu: 0.15


        # Change [INSTANCE_CONNECTION_NAME] here to include your GCP
        # project, the region of your Cloud SQL instance and the name
        # of your Cloud SQL instance. The format is
        # $PROJECT:$REGION:$INSTANCE
        # Insert the port number used by your database.
        # [START proxy_container]
        - image: gcr.io/cloudsql-docker/gce-proxy:1.09
          name: cloudsql-proxy
          command: ["/cloud_sql_proxy", "--dir=/cloudsql",
                    "-instances=testing-11111:europe-west2:testology=tcp:5432",
                    "-credential_file=/secrets/cloudsql/credentials.json"]
          volumeMounts:
            - name: cloudsql-instance-credentials
              mountPath: /secrets/cloudsql
              readOnly: true
            - name: ssl-certs
              mountPath: /etc/ssl/certs
            - name: cloudsql
              mountPath: /cloudsql
        # [END proxy_container]
          resources:
            requests:
              #memory: "64Mi"
              cpu: 0.1
            limits:
              memory: "375Mi"
              cpu: 0.15

      # [START volumes]
      volumes:
        - name: cloudsql-instance-credentials
          secret:
            secretName: cloudsql-instance-credentials
        - name: ssl-certs
          hostPath:
            path: /etc/ssl/certs
        - name: cloudsql
          emptyDir:
      # [END volumes]

编辑:解决方案

我通过在目录“deployments”中创建部署配置的多个副本,修改名称和命令然后运行来解决它:

kubectl create -f deployments/

最佳答案

  • 您不能让单个副本使用不同的参数运行,它们不会像“精确副本”中那样是副本。如果要使用不同的参数多次运行应用程序,则需要使用多个部署。
  • 复制的容器在它们自己的 Pod 中运行,例如扩展为三个复制的部署应该有三个 Pod
  • 关于kubernetes 使用不同的命令为同一个镜像创建多个 pods/deployments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46425997/

    相关文章:

    kubernetes - 如何使用 client-go 从 K8s API 获取 Kubernetes 集群名称

    Kubernetes Prometheus CrashLoopBackOff/OOMKilled Puzzle

    vagrant - Kubernetes Guestbook Vagrant 前端服务EXTERNAL_IP问题

    kubernetes - Kubernetes上NodePort与ClusterIP与 headless 服务的性能注意事项

    kubernetes - 具有多个securityPolicy的BackendConfig不起作用

    使用 Kubernetes Helm install values.yaml 的 Nginx-ingress 设置注释

    Azure AKS - 我禁用了 addon-http-application-routing 但 pod、部署、服务和其他内容仍在集群中

    go - 在 GCP Container-Optimized OS 上构建 go 可执行文件的 GOOS 和 GOARCH 值是多少

    node.js - Kubernetes 通过基于时间的触发器扩展 pod

    kubernetes - 通过 daemonset 运行的 Fluentd pod 因谷歌容器引擎上的警告而终止