2015-07-14 00:13:09 +00:00
<!-- BEGIN MUNGE: UNVERSIONED_WARNING -->
<!-- BEGIN STRIP_FOR_RELEASE -->
2015-07-16 17:02:26 +00:00
< img src = "http://kubernetes.io/img/warning.png" alt = "WARNING"
width="25" height="25">
< img src = "http://kubernetes.io/img/warning.png" alt = "WARNING"
width="25" height="25">
< img src = "http://kubernetes.io/img/warning.png" alt = "WARNING"
width="25" height="25">
< img src = "http://kubernetes.io/img/warning.png" alt = "WARNING"
width="25" height="25">
< img src = "http://kubernetes.io/img/warning.png" alt = "WARNING"
width="25" height="25">
< h2 > PLEASE NOTE: This document applies to the HEAD of the source tree< / h2 >
If you are using a released version of Kubernetes, you should
refer to the docs that go with that version.
< strong >
The latest 1.0.x release of this document can be found
[here ](http://releases.k8s.io/release-1.0/docs/user-guide/liveness/README.md ).
Documentation for other releases can be found at
[releases.k8s.io ](http://releases.k8s.io ).
< / strong >
--
2015-07-13 22:15:35 +00:00
2015-07-14 00:13:09 +00:00
<!-- END STRIP_FOR_RELEASE -->
<!-- END MUNGE: UNVERSIONED_WARNING -->
2015-07-17 22:35:41 +00:00
2015-05-13 20:03:28 +00:00
## Overview
2015-07-17 22:35:41 +00:00
2015-07-16 00:28:59 +00:00
This example shows two types of pod [health checks ](../production-pods.md#liveness-and-readiness-probes-aka-health-checks ): HTTP checks and container execution checks.
2015-05-13 20:03:28 +00:00
2015-07-14 00:13:09 +00:00
The [exec-liveness.yaml ](exec-liveness.yaml ) demonstrates the container execution check.
2015-07-17 02:01:02 +00:00
2015-07-19 01:43:15 +00:00
```yaml
2015-05-13 20:03:28 +00:00
livenessProbe:
exec:
command:
- cat
- /tmp/health
initialDelaySeconds: 15
timeoutSeconds: 1
```
2015-07-17 02:01:02 +00:00
2015-07-16 00:28:59 +00:00
Kubelet executes the command `cat /tmp/health` in the container and reports failure if the command returns a non-zero exit code.
2015-05-13 20:03:28 +00:00
2015-07-16 00:28:59 +00:00
Note that the container removes the `/tmp/health` file after 10 seconds,
2015-07-17 02:01:02 +00:00
2015-07-19 01:43:15 +00:00
```sh
2015-05-13 20:03:28 +00:00
echo ok > /tmp/health; sleep 10; rm -rf /tmp/health; sleep 600
```
2015-07-17 02:01:02 +00:00
2015-05-13 20:03:28 +00:00
so when Kubelet executes the health check 15 seconds (defined by initialDelaySeconds) after the container started, the check would fail.
2015-05-24 07:15:58 +00:00
The [http-liveness.yaml ](http-liveness.yaml ) demonstrates the HTTP check.
2015-07-17 02:01:02 +00:00
2015-07-19 01:43:15 +00:00
```yaml
2015-05-13 20:03:28 +00:00
livenessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 15
timeoutSeconds: 1
```
2015-07-17 02:01:02 +00:00
2015-05-13 20:03:28 +00:00
The Kubelet sends a HTTP request to the specified path and port to perform the health check. If you take a look at image/server.go, you will see the server starts to respond with an error code 500 after 10 seconds, so the check fails.
2015-07-07 04:34:41 +00:00
This [guide ](../walkthrough/k8s201.md#health-checking ) has more information on health checks.
2015-05-13 20:03:28 +00:00
## Get your hands dirty
2015-07-17 22:35:41 +00:00
2015-05-13 20:03:28 +00:00
To show the health check is actually working, first create the pods:
2015-07-17 02:01:02 +00:00
2015-07-19 01:43:15 +00:00
```console
$ kubectl create -f docs/user-guide/liveness/exec-liveness.yaml
$ kubectl create -f docs/user-guide/liveness/http-liveness.yaml
2015-05-13 20:03:28 +00:00
```
Check the status of the pods once they are created:
2015-07-17 02:01:02 +00:00
2015-07-19 01:43:15 +00:00
```console
$ kubectl get pods
2015-07-07 20:42:05 +00:00
NAME READY STATUS RESTARTS AGE
2015-06-28 11:59:08 +00:00
[...]
liveness-exec 1/1 Running 0 13s
liveness-http 1/1 Running 0 13s
2015-05-13 20:03:28 +00:00
```
2015-07-17 02:01:02 +00:00
2015-06-28 11:59:08 +00:00
Check the status half a minute later, you will see the container restart count being incremented:
2015-07-17 02:01:02 +00:00
2015-07-19 01:43:15 +00:00
```console
$ kubectl get pods
2015-06-28 11:59:08 +00:00
mwielgus@mwielgusd:~/test/k2/kubernetes/examples/liveness$ kubectl get pods
2015-07-07 20:42:05 +00:00
NAME READY STATUS RESTARTS AGE
2015-06-28 11:59:08 +00:00
[...]
liveness-exec 1/1 Running 1 36s
liveness-http 1/1 Running 1 36s
2015-05-13 20:03:28 +00:00
```
2015-07-17 02:01:02 +00:00
2015-06-28 11:59:08 +00:00
At the bottom of the *kubectl describe* output there are messages indicating that the liveness probes have failed, and the containers have been killed and recreated.
2015-05-13 20:03:28 +00:00
2015-07-19 01:43:15 +00:00
```console
$ kubectl describe pods liveness-exec
2015-06-28 11:59:08 +00:00
[...]
Sat, 27 Jun 2015 13:43:03 +0200 Sat, 27 Jun 2015 13:44:34 +0200 4 {kubelet kubernetes-minion-6fbi} spec.containers{liveness} unhealthy Liveness probe failed: cat: can't open '/tmp/health': No such file or directory
Sat, 27 Jun 2015 13:44:44 +0200 Sat, 27 Jun 2015 13:44:44 +0200 1 {kubelet kubernetes-minion-6fbi} spec.containers{liveness} killing Killing with docker id 65b52d62c635
Sat, 27 Jun 2015 13:44:44 +0200 Sat, 27 Jun 2015 13:44:44 +0200 1 {kubelet kubernetes-minion-6fbi} spec.containers{liveness} created Created with docker id ed6bb004ee10
Sat, 27 Jun 2015 13:44:44 +0200 Sat, 27 Jun 2015 13:44:44 +0200 1 {kubelet kubernetes-minion-6fbi} spec.containers{liveness} started Started with docker id ed6bb004ee10
2015-05-13 20:03:28 +00:00
```
2015-07-14 00:13:09 +00:00
<!-- BEGIN MUNGE: GENERATED_ANALYTICS -->
2015-07-14 16:37:37 +00:00
[![Analytics ](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/docs/user-guide/liveness/README.md?pixel )]()
2015-07-14 00:13:09 +00:00
<!-- END MUNGE: GENERATED_ANALYTICS -->