I have a Replication Controller which creates more than one PODs. Within each POD, there is a container running an application.
Now within the container, it can call kubernetes API as below.
KUBE_TOKEN=$(</var/run/secrets/kubernetes.io/serviceaccount/token)
curl --silent --insecure --header "Authorization: Bearer $KUBE_TOKEN" \
https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_PORT_443_TCP_PORT/api/v1/namespaces/$POD_NAMESPACE/pods/$POD_NAME
POD_NAME
is the name of the POD;
POD_NAMESPACE
is the namespace of the POD
The application instances in contains make up a cluster. And they elect one of the application instances as master, the others are slaves which replicates the data from master in real-time. When the master instance fails, another slave instance will be promoted to master.
When the application in the container is promoted to master, I want the master application instance to call the kubernetes API so that all the traffic goes to this POD/container.
{
"kind": "Service",
"apiVersion": "v1",
"metadata": {
"name": "my-cluster"
},
"spec": {
"ports": [{
"port": 33379,
"targetPort": 33379
}],
"type" : "None",
"selector": {
"name": "<POD_NAME>"
}
}
}
I specified the POD_NAME
as the selector, but the service can not find any of the POD.
How can I configure the service so that all the traffic goes to that POD?
You could have your service selector select a
"master"
label, e.g."role": "master"
- then you can add the master label to whichever pod is chosen as the master. Labels can be changed on running pods using PATCH method.The
name
label is treated the same as any other label in a selector. There is a common practice to add a name label to a pod that matches the metadata name, and then use that in a selector, but without the label on the pod the selector won't find it.