Make external API hit from kubernetes

9/1/2018

I have a Kubernetes service running and we have an external API's dependent on this service.

We would like to be notified if there is any service restart. Is there any possibility to hit an API endpoint on every service restart?

-- umesh dc
kubernetes

2 Answers

9/2/2018

Expanding on enzian's comment about using initContainers. Here is an example using a Curl based initContainer and mounting the metadata to pass as an environment variable in the call:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: some-service
  namespace: the-project
  labels:
    app: some-service
spec:
  replicas: 1
  selector:
    matchLabels:
      app: some-service
  template:
    metadata:
      labels:
        app: some-service
    spec:
      initContainers:
      - name: service-name-init
        image: txn2/curl:v3.0.0
        - name: SOME_NAME
          valueFrom:
            fieldRef:
              fieldPath: spec.nodeName
        command: [
          "/bin/sh",
          "-c",
          "/usr/bin/curl -sX GET example.com/notify/$(SOME_NAME)"
        ]
      containers:
      - name: ok
        image: txn2/ok
        imagePullPolicy: Always
        env:
        - name: NODE_NAME
          valueFrom:
            fieldRef:
              fieldPath: spec.nodeName
        - name: POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        - name: POD_IP
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        - name: SERVICE_ACCOUNT
          valueFrom:
            fieldRef:
              fieldPath: spec.serviceAccountName
        ports:
        - name: ok-port
          containerPort: 8080
-- cjimti
Source: StackOverflow

9/1/2018

Hi and welcome to the community!

There are multiple ways of achieve this. A really simple one (as pointed out by Thomas) is an Init Container. Refer to the Kubernetes docs for more on how to get those running! This init container would do nothing more than send out an HTTP request to your external API once the pod is started and terminate immediately afterwards.

The other way is much more complex and will require you to write some code yourself. What you'd have to do is write your own controller that watches the entities through the Kubernetes API and notify your external service when a pod is rescheduled, killed, died etc. (You could however have your external service to exactly that why accessing the kube-api directly...)

-- enzian
Source: StackOverflow