Search code examples
kuberneteskubernetes-podkubernetes-service

Kubernetes service select pods based on start time


I have a K8s service defined as:

apiVersion: v1
kind: Service
metadata:
  labels:
    name: myapp
  name: myapp
  namespace: myapps

spec:
  ports:
    - name: ui
      port: 8081
      protocol: TCP
      targetPort: 8081
  selector:
    my-app: myapp
    my-deployment-type: jobmanager
  type: ClusterIP

This service serves as backend for an ingress.

Now during blue green deployment there are two apps running i.e two sets of pods that match the selctors specified above: my-app: myapp my-deployment-type: jobmanager

And I observe that it selects both pods i.e both app versions at random till the older app pods gets killed.

Is there a way to ensure that the service will just choose the new app pods? i.e Have a selector to also depend on the "Start Time" of the pods apart from the selector?


Solution

  • Is there a way to ensure that the service will just choose the new app pods? i.e Have a selector to also depend on the "Start Time" of the pods apart from the selector?

    There is no such feature in Kubernetes to filter a pod start time in service selector block.

    The official k8s documentation says ¨Labels selectors for service objects are defined in json or yaml files using maps, and only equality-based requirement selectors are supported ¨.

    So, in your case the best option is to:

    • create a new k8s deployment with new labels for green deployment (the new application version)
    • create a new ClusterIP service for the green deployment
    • then switch your Ingress to this green deployment by changing the backend service name.

    If something goes wrong, you can quickly switch back to your blue deployment (previous application version).