0

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?

Ace McCloud
  • 798
  • 9
  • 27

1 Answers1

1

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).

mozello
  • 1,083
  • 3
  • 8
  • Great point about changing the backend service name. How can I do that dynamically? We cannot parametrize service name in ingresses from what it looks like ? https://stackoverflow.com/questions/70993999/kubernetes-ingress-with-backend-servicename-regex-wildcard-or-selector – Ace McCloud Feb 11 '22 at 03:40
  • You can modify ingress yaml file (putting the new backend service name there) and run `kubectl apply -f youringress.yaml` command for example. It will update your ingress configuration. – mozello Feb 11 '22 at 09:26
  • I maybe missing something here, but this modifying of the backend service name will be a code change right and not something I can automate as part of deloyment? – Ace McCloud Feb 11 '22 at 22:50
  • 1
    @Ace McCloud Yes, that's right. You cannot automate it as a part of k8s deployment. – mozello Feb 14 '22 at 10:00