Liveness Probe
The complete details written @Kubernetes for Developers #12: Effective way of using K8 Liveness Probe
The readiness probe determines when a container is ready to start accepting traffic. It invokes the probe periodically and If a pod reports that it is not ready, it is removed from the service. If the Pod then becomes ready again, it will be re-added automatically.
Liveness probes keep pods healthy by restarting unhealthy containers. whereas readiness probes make sure that only pods that are ready to serve requests receive them.
1. HTTP GET probe
It performs an HTTP GET request on the container’s REST API resource. If the probe receives a response code between 2xx to 3xx is considered successful. If Http server returns different response code (i.e. other than 2xx to 3xx) or if it doesn’t respond at all, the probe is considered a failure and pod will be detached from the service endpoint.apiVersion: v1
kind: Pod
metadata:
name: readiness-http
spec:
containers:
- name: readiness
image: luksa/kubia-unhealthy
ports:
- containerPort: 8080
readinessProbe:
httpGet:
path: /
port: 8080
initialDelaySeconds: 3
periodSeconds: 3
failureThreshold: 3
// run the pod yaml file
> kubectl apply -f ./liveness-http-test.yaml
// view running pod details
> kubectl get po liveness-http-test
// view Pod complete details
> kubectl describe po liveness-http-test
// view previous terminated container logs
> kubectl logs liveness-http-test --previous
2. TCP Socket probe
apiVersion: v1
kind: Pod
metadata:
name: readiness-tcp
spec:
containers:
- name: goproxy
image: k8s.gcr.io/goproxy:0.1
ports:
- containerPort: 8080
readinessProbe:
tcpSocket:
port: 8080
initialDelaySeconds: 5
periodSeconds: 3
3. Exec probe
apiVersion: v1
kind: Pod
metadata:
name: readiness-exec
spec:
containers:
- name: readiness
image: k8s.gcr.io/busybox
args:
- /bin/sh
- -c
- touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600
readinessProbe:
exec:
command:
- cat
- /tmp/healthy
initialDelaySeconds: 5
periodSeconds: 5
When the container starts, it executes this command:
For the first 30 seconds of the container's life, there is a /tmp/healthy file. So, during the first 30 seconds, the command cat /tmp/healthy returns a success code. After 30 seconds, cat /tmp/healthy returns a failure code.
/bin/sh -c "touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600"
you should always define a Readiness probe. If your application takes too long to start listening for incoming connections, all the client requests hitting to this Pod will fail until an application is ready to accept.
- Configure the Readiness probe to perform request on a specific URL path (i.e. /ready)
- Make sure HTTP GET /ready does not require validation or authentication
- Liveness probes do not wait for readiness probes to succeed. Both probes run parallel. If you want to wait before executing a liveness probe you should use initialDelaySeconds.
- It is highly useful if your application needs to load lot of configuration/data during startup.
- Readiness and liveness probes can be used in parallel for the same container. Using both can ensure that traffic does not reach a container that is not ready for it, and that containers are restarted when they fail.
- liveness Prob failureThreshold default value is 3. So, it is not mandatory to specify again in pod yaml file.
- Do not set the same specification for Liveness and Readiness Probe
- Try to avoid "exec" probes as there are known problems with them
Kubernetes for Developers Journey.
- Kubernetes for Developers #25: PersistentVolume and PersistentVolumeClaim in-detail
- Kubernetes for Developers #24: Kubernetes Volume hostPath in-detail
- Kubernetes for Developers #23: Kubernetes Volume emptyDir in-detail
- Kubernetes for Developers #22: Access to Multiple Clusters or Namespaces using kubectl and kubeconfig
- Kubernetes for Developers #21: Kubernetes Namespace in-detail
- Kubernetes for Developers #20: Create Automated Tasks using Jobs and CronJobs
- Kubernetes for Developers #19: Manage app credentials using Kubernetes Secrets
- Kubernetes for Developers #18: Manage app settings using Kubernetes ConfigMap
- Kubernetes for Developers #17: Expose service using Kubernetes Ingress
- Kubernetes for Developers #16: Kubernetes Service Types - ClusterIP, NodePort, LoadBalancer and ExternalName
- Kubernetes for Developers #15: Kubernetes Service YAML manifest in-detail
- Kubernetes for Developers #14: Kubernetes Deployment YAML manifest in-detail
- Kubernetes for Developers #13: Effective way of using K8 Readiness Probe
- Kubernetes for Developers #12: Effective way of using K8 Liveness Probe
- Kubernetes for Developers #11: Pod Organization using Labels
- Kubernetes for Developers #10: Kubernetes Pod YAML manifest in-detail
- Kubernetes for Developers #9: Kubernetes Pod Lifecycle
- Kubernetes for Developers #8: Kubernetes Object Name, Labels, Selectors and Namespace
- Kubernetes for Developers #7: Imperative vs. Declarative Kubernetes Objects
- Kubernetes for Developers #6: Kubernetes Objects
- Kubernetes for Developers #5: Kubernetes Web UI Dashboard
- Kubernetes for Developers #4: Enable kubectl bash autocompletion
- Kubernetes for Developers #3: kubectl CLI
- Kubernetes for Developers #2: Kubernetes for Local Development
- Kubernetes for Developers #1: Kubernetes Architecture and Features
Happy Coding :)
No comments:
Post a Comment