Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kubernetes create multiple pods/deployments of the same image with different command

I have a deployment with 2 containers inside a single pod (container-test2 and cloudsql-proxy).

container-test2 runs a docker image which passes ["my_app", "arg1", "arg2"] as CMD. I would like to run several instances of this container with different argument combinations. I would also like to run them in separate pods so that I can distribute them across nodes. I am not entirely sure how to do this.

I can successfully run the two containers but I don't know how to make container-test2 replicate with different arguments and make each container start inside an individual 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]

EDIT: Solution

I solved it by creating multiple copies of the deployment config into directory "deployments", amending the names and command and then running:

kubectl create -f deployments/

like image 782
s5s Avatar asked Sep 26 '17 12:09

s5s


People also ask

Can a Kubernetes deployment have multiple pods?

Kubernetes Multiple Containers in a POD ^ This is where a Multi Container Pod comes in. Thus, when an application needs several containers running on the same host, the best option is to make a Multi Container (Pod) with everything you need for the deployment of the application.

How do you make multiple pods in Kubernetes?

Creating a pod. Multi-container pods must be created with the create command. Properties are passed to the command as a YAML- or JSON-formatted configuration file. The create command can be used to create a pod directly, or it can create a pod or pods through a Deployment .

Can we run containers of same pod on different Nodes?

In a pre-container world, they would have run on the same physical or virtual machine. Pods are tied to the Node where they are deployed and remain there until termination (according to restart policy) or deletion. In case of a Node failure, new identical Pods will be deployed on other available Nodes.

Can you briefly explain how a single pod in Kubernetes communicates with another pod on another machine?

A Pod can communicate with another Pod by directly addressing its IP address, but the recommended way is to use Services. A Service is a set of Pods, which can be reached by a single, fixed DNS name or IP address. In reality, most applications on Kubernetes use Services as a way to communicate with each other.


1 Answers

  1. You can not make individual replicas run with different arguments, they would not be replicas as in "exact copy". If you want to run your application multiple times with different arguments, you need to use multiple deployments.

  2. The containers of a replication run in their own Pod, e.g. there should be three Pods existing for a Deployment scaled to three replications

like image 143
Simon Tesar Avatar answered Oct 14 '22 10:10

Simon Tesar