0

I am currently building a distributed consensus system such that it is important to know exactly when a Kubernetes pod can serve traffic (accept http requests). Any ideas as to how I can do that apart from setting a timer to keep making requests to itself?

Arghya Sadhu
  • 41,002
  • 9
  • 78
  • 107
Brian Shih
  • 301
  • 1
  • 8

1 Answers1

3

You can use readiness probe and kubernetes service. From the docs here

readinessProbe: Indicates whether the Container is ready to service requests. If the readiness probe fails, the endpoints controller removes the Pod's IP address from the endpoints of all Services that match the Pod. The default state of readiness before the initial delay is Failure. If a Container does not provide a readiness probe, the default state is Success

From the guide here

apiVersion: v1
kind: Pod
metadata:
  name: goproxy
  labels:
    app: goproxy
spec:
  containers:
  - name: goproxy
    image: k8s.gcr.io/goproxy:0.1
    ports:
    - containerPort: 8080
    readinessProbe:
      httpGet:
        path: /readiness
        port: 8888
      initialDelaySeconds: 300
      periodSeconds: 30

Your application need to have HTTP endpoint /readiness which should return 200 OK response.Kubelet will probe the endpoint that you define in the readiness probe. Once kubelet get a 200 OK response back from the endpoint then pod will be marked ready to accept traffic and Kubernetes service will start sending traffic to the backend pod.

Finally expose the pod via Kubernetes Service.

Arghya Sadhu
  • 41,002
  • 9
  • 78
  • 107