64
loading...
This website collects cookies to deliver better user experience
PodScheduled
or Initialized
. Those conditions can then be used to evaluate Pod readiness.Ready
, then whole is Ready
too. If readiness gate is added to a Pod, then readiness of a Pod gets determined by readiness of all containers and status of all readiness gate conditions.kind: Pod
...
spec:
readinessGates:
- conditionType: "www.example.com/some-gate-1"
...
status:
conditions:
- type: Ready
status: "False"
lastProbeTime: null
lastTransitionTime: 2021-11-01T00:00:00Z
- type: ContainersReady
status: "True"
lastProbeTime: null
lastTransitionTime: 2021-11-01T00:00:00Z
- type: "www.example.com/some-gate-1"
status: "False"
lastProbeTime: null
lastTransitionTime: 2021-11-01T00:00:00Z
www.example.com/some-gate-1
. Looking at the conditions in status stanza, we can see that the ContainersReady
condition is True
, meaning that all containers are ready, but the custom readiness gate condition is False
and therefore also Pod's Ready
condition must be False
.kubectl describe pod ...
on such a pod you would also see the following in the Conditions section:...
Conditions:
Type Status
www.example.com/gate-1 False
Initialized True
Ready False
ContainersReady True
PodScheduled True
readinessGates
stanza in Pod spec
with the name of our desired condition:# kubectl run nginx \
# --image=nginx \
# --overrides='{"spec": {"readinessGates": [{"conditionType": "www.example.com/gate-1"}]}}'
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
readinessGates:
- conditionType: www.example.com/gate-1
containers:
- name: nginx
image: nginx:latest
kubectl
subcommands don't support patching of object status, therefore we cannot use kubectl patch
set the condition to True
/False
. Instead, we have to use PATCH
HTTP request sent directly to API server.kubectl proxy
, which allows us to reach the server on localhost
:kubectl proxy --port 12345 &
curl -s http://localhost:12345/
curl -k -H 'Accept: application/json' http://localhost:12345/api/v1/namespaces/default/pods/nginx/status
curl
to check if the server is reachable and queried the server for manifest/status of the pod we will be updating.False
, but let's start by explicitly setting it:# Explicitly set status to "False"
curl -k \
-H "Content-Type: application/json-patch+json" \
-X PATCH http://localhost:12345/api/v1/namespaces/default/pods/nginx/status \
--data '[ { "op": "add", "path": "/status/conditions/-", "value": { "lastProbeTime": null, "lastTransitionTime": "2020-03-05T15:50:51Z", "status": "False", "type": "www.example.com/gate-1" }}]'
kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx 1/1 Running 0 25s 10.244.0.6 kind-control-plane <none> 0/1
kubectl describe pod nginx
...
Readiness Gates:
Type Status
www.example.com/gate-1 False
Conditions:
Type Status
www.example.com/gate-1 False
Initialized True
Ready False
ContainersReady True
PodScheduled True
PATCH
request against API proxy server to apply JSON patch to status.condition
fields of the Pod. In this case we used add
operation because the status was not set yet. Additionally, you can also see that when we list the pods with -o wide
, the READINESS GATES
column shows 0/1
indicating that the gate is set to False
. Same can be also seen in output of kubectl describe
.True
:curl -k \
-H "Content-Type: application/json-patch+json" \
-X PATCH http://localhost:12345/api/v1/namespaces/default/pods/nginx/status \
--data '[{ "op": "replace", "path": "/status/conditions/0", "value": { "type": "www.example.com/gate-1", "status": "True" }}]'
kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx 1/1 Running 0 58s 10.244.0.6 kind-control-plane <none> 1/1
PATCH
request to update the condition, this time however we used replace
operation, specifically on the first condition in the list as specified by /status/conditions/0
. Be aware though, that the custom condition doesn't necessarily have to be first in the list so if you will be using some script to update conditions, then you should first check which condition you should be updating.curl
like we saw above works for simple scripts or quick manual updates, but generally you will probably need more robust solution. Considering that kubectl
is not an option here, your best bet will be one of the Kubernetes client libraries. For demonstration purposes let's see how it can be done in Python:# pip install kubernetes
import time
from kubernetes import client, config
config.load_kube_config()
pod_manifest = {
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"name": "nginx"
},
"spec": {
"readinessGates": [
{"conditionType": "www.example.com/gate-1"}
],
"containers": [{
"image": "nginx",
"name": "nginx",
}]
}
}
v1 = client.CoreV1Api()
response = v1.create_namespaced_pod(body=pod_manifest, namespace="default")
while True:
response = v1.read_namespaced_pod(name="nginx", namespace="default")
if response.status.phase != "Pending":
break
time.sleep(1)
print("Pod is 'Running'...")
config.load_kube_config()
which loads your credentials from ~/.kube/config
, in general though it's better to use service accounts and tokens to authenticate to the cluster, sample for that can be found in docs.Pending
.False
value:response = v1.patch_namespaced_pod_status(name="nginx", namespace="default", body=[{
"op": "add", "path": "/status/conditions/-",
"value": {
"lastProbeTime": None,
"lastTransitionTime": "2020-03-05T15:50:51Z",
"status": "False",
"type": "www.example.com/gate-1"
}}])
pod = v1.read_namespaced_pod_status(name="nginx", namespace="default")
for i, condition in enumerate(pod.status.conditions):
if condition.type == "Ready":
gate, index = condition, i
print(f"ReadinessGate '{gate.type}' has readiness status: {gate.status}, Reason: {gate.message}.")
# ReadinessGate 'Ready' has readiness status: False, Reason: the status of pod readiness gate "www.example.com/gate-1" is not "True", but False.
Ready
condition and printed its status.True
with the following code:pod = v1.read_namespaced_pod_status(name="nginx", namespace="default")
for i, condition in enumerate(pod.status.conditions):
if condition.type == "www.example.com/gate-1":
gate, index = condition, i
print(f"ReadinessGate '{gate.type}' has readiness status: {gate.status}, Reason: {gate.message}.")
# ReadinessGate 'www.example.com/gate-1' has readiness status: False, Reason: None.
response = v1.patch_namespaced_pod_status(name="nginx", namespace="default", body=[{
"op": "replace", "path": f"/status/conditions/{index}",
"value": {
"lastProbeTime": None,
"lastTransitionTime": "2020-03-05T15:50:51Z",
"status": "True",
"type": "www.example.com/gate-1"
}}])
www.example.com/gate-1
, we verify its current state with print
and then apply the change using replace
operation to the condition listed at index
.readinessGate
stanza set to relevant conditionType
s. The controller would be then able to update the condition(s) based on the observed state of the pod, whether it's based on custom pod metrics, external network state or whatever else.kube-conditioner
.