Search code examples
kubernetesdeploymentcontainersgoogle-kubernetes-engine

How to configure a Kubernetes Multi-Pod Deployment


I would like to deploy an application cluster by managing my deployment via Kubernetes Deployment object. The documentation has me extremely confused. My basic layout has the following components that scale independently:

  1. API server
  2. UI server
  3. Redis cache
  4. Timer/Scheduled task server

Technically, all 4 above belong in separate pods that are scaled independently.

My questions are:

  1. Do I need to create pod.yml files and then somehow reference them in the deployment.yml file or can a deployment file also embed pod definitions?
  2. Kubernetes documentation seems to imply that the spec portion of Deployment is equivalent to defining one pod. Is that correct? What if I want to declaratively describe multi-pod deployments? Do I need multiple deployment.yml files?

Solution

  • Pagid's answer has most of the basics. You should create 4 Deployments for your scenario. Each deployment will create a ReplicaSet that schedules and supervises the collection of Pods for the Deployment.

    Each Deployment will most likely also require a Service in front of it for access. I usually create a single yaml file that has a Deployment and the corresponding Service in it. Here is an example for an nginx.yaml that I use:

    apiVersion: v1
    kind: Service
    metadata:
      annotations:
        service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
      name: nginx
      labels:
        app: nginx
    spec:
      type: NodePort
      ports:
      - port: 80
        name: nginx
        targetPort: 80
        nodePort: 32756
      selector:
        app: nginx
    ---
    apiVersion: extensions/v1beta1
    kind: Deployment
    metadata:
      name: nginxdeployment
    spec:
      replicas: 3
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
          - name: nginxcontainer
            image: nginx:latest
            imagePullPolicy: Always
            ports:
            - containerPort: 80
    

    Here some additional information for clarification:

    • A Pod is not a scalable unit. A Deployment that schedules pods is.
    • A Deployment is meant to represent a single group of pods fulfilling a single purpose together.
    • You can have many Deployments work together in the virtual network of the cluster.
    • For accessing a Deployment that may consist of many Pods running on different nodes you have to create a Service.
    • Deployments are meant to contain stateless services. If you need to store a state you need to create StatefulSet instead (e.g. for a database service).