93

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?
1
  • From my understanding, you cannot have different Pods in the same deployment. I was wondering how I could add 2 different Pods (1 container each) to the same Deployment. I don't think this is possible. Commented Aug 5, 2022 at 16:04

3 Answers 3

82

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).
Sign up to request clarification or add additional context in comments.

11 Comments

Thanks Oswin. Your syntax example combining Deployment and Service in one is extremely helpful!
i'm confused about the use of port: 80 and also nodePort: 32756 in the same service. Can you please explain why they are both needed?
The port: 80 says that if you address the service as an entity, e.g. through the DNS entry for its name or the service IP, the port 80 will forward to the PODS supplying the actual service. nodePort: 32xxx says that if you address the cluster nodes, e.g. from the outside via a loadbalancer or node IP, the port 32xxx will forward to the PODS supplying the actual service.
I thought the point of a deployment was to monitor a group of pods? I don't understand why I need to create a deployment for each pod.
@fionbio: Yes, you can influence what constitutes success with defining probes and there is no boundary on what you can come up with. You are free to create indirect dependencies between deployments and so on. So you should carefully design a solution for your domain that makes sense and is easy to use/upgrade. Complicated designs are easy - Simple is hard.
|
22

You can use the Kubernetes API reference for the Deployment and you'll find that the spec->template field is of type PodTemplateSpec along with the related comment (Template describes the pods that will be created.) it answers you questions. A longer description can of course be found in the Deployment user guide.

To answer your questions...

1) The Pods are managed by the Deployment and defining them separately doesn't make sense as they are created on demand by the Deployment. Keep in mind that there might be more replicas of the same pod type.

2) For each of the applications in your list, you'd have to define one Deployment - which also makes sense when it comes to difference replica counts and application rollouts.

3) you haven't asked that but it's related - along with separate Deployments each of your applications will also need a dedicated Service so the others can access it.

6 Comments

Thanks @pagid. To be clear, while spec -> template is indeed a PodTemplateSpec, it is still the spec for a single pod. Is that right?
Also, regarding #1, I am sensing there are two options: 1) inline pod spec or 2) external reference via labels. For my initial project, for the sake of my sanity I'll stick to inline pod spec.
We'll a deployment can have a replica definition - therefore the PodTemplateSpec accounts for a group of Pods and the replica config, defines how big that group is. The external definition is something which I'm not able to "see" as an option when using the API definition along.
"it is still the spec for a single pod. Is that right?" YES, a single pod template spec that might cause multiple pods to be instantiated.
do you recommend having seperate service for seperate deployments? I guess using same service and having different ports would be easy to maintain as there are small number of services right? This is just my assumption, please let me know if you have different thoughts on this.
|
1

additional information:
API server use deployment
UI server use deployment
Redis cache use statefulset
Timer/Scheduled task server maybe use a statefulset (If your service has some state in)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.