mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-12 20:57:20 +00:00
Cloning docs for 0.19.0
This commit is contained in:
82
release-0.19.0/examples/liveness/README.md
Normal file
82
release-0.19.0/examples/liveness/README.md
Normal file
@@ -0,0 +1,82 @@
|
||||
## Overview
|
||||
This example shows two types of pod health checks: HTTP checks and container execution checks.
|
||||
|
||||
The [exec-liveness.yaml](./exec-liveness.yaml) demonstrates the container execution check.
|
||||
```
|
||||
livenessProbe:
|
||||
exec:
|
||||
command:
|
||||
- cat
|
||||
- /tmp/health
|
||||
initialDelaySeconds: 15
|
||||
timeoutSeconds: 1
|
||||
```
|
||||
Kubelet executes the command cat /tmp/health in the container and reports failure if the command returns a non-zero exit code.
|
||||
|
||||
Note that the container removes the /tmp/health file after 10 seconds,
|
||||
```
|
||||
echo ok > /tmp/health; sleep 10; rm -rf /tmp/health; sleep 600
|
||||
```
|
||||
so when Kubelet executes the health check 15 seconds (defined by initialDelaySeconds) after the container started, the check would fail.
|
||||
|
||||
|
||||
The [http-liveness.yaml](http-liveness.yaml) demonstrates the HTTP check.
|
||||
```
|
||||
livenessProbe:
|
||||
httpGet:
|
||||
path: /healthz
|
||||
port: 8080
|
||||
initialDelaySeconds: 15
|
||||
timeoutSeconds: 1
|
||||
```
|
||||
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.
|
||||
|
||||
This [guide](https://github.com/GoogleCloudPlatform/kubernetes/blob/master/examples/walkthrough/k8s201.md#health-checking) has more information on health checks.
|
||||
|
||||
## Get your hands dirty
|
||||
To show the health check is actually working, first create the pods:
|
||||
```
|
||||
# kubectl create -f exec-liveness.yaml
|
||||
# cluster/kbuectl.sh create -f http-liveness.yaml
|
||||
```
|
||||
|
||||
Check the status of the pods once they are created:
|
||||
```
|
||||
# kubectl get pods
|
||||
POD IP CONTAINER(S) IMAGE(S) HOST LABELS STATUS CREATED MESSAGE
|
||||
liveness-exec 10.244.3.7 kubernetes-minion-f08h/130.211.122.180 test=liveness Running 3 seconds
|
||||
liveness gcr.io/google_containers/busybox Running 2 seconds
|
||||
liveness-http 10.244.0.8 kubernetes-minion-0bks/104.197.10.10 test=liveness Running 3 seconds
|
||||
liveness gcr.io/google_containers/liveness Running 2 seconds
|
||||
```
|
||||
|
||||
Check the status half a minute later, you will see the termination messages:
|
||||
```
|
||||
# kubectl get pods
|
||||
POD IP CONTAINER(S) IMAGE(S) HOST LABELS STATUS CREATED MESSAGE
|
||||
liveness-exec 10.244.3.7 kubernetes-minion-f08h/130.211.122.180 test=liveness Running 34 seconds
|
||||
liveness gcr.io/google_containers/busybox Running 3 seconds last termination: exit code 137
|
||||
liveness-http 10.244.0.8 kubernetes-minion-0bks/104.197.10.10 test=liveness Running 34 seconds
|
||||
liveness gcr.io/google_containers/liveness Running 13 seconds last termination: exit code 2
|
||||
```
|
||||
The termination messages indicate that the liveness probes have failed, and the containers have been killed and recreated.
|
||||
|
||||
You can also see the container restart count being incremented by running `kubectl describe`.
|
||||
```
|
||||
# kubectl describe pods liveness-exec | grep "Restart Count"
|
||||
Restart Count: 8
|
||||
```
|
||||
|
||||
You would also see the killing and creating events at the bottom of the *kubectl describe* output:
|
||||
```
|
||||
Thu, 14 May 2015 15:23:25 -0700 Thu, 14 May 2015 15:23:25 -0700 1 {kubelet kubernetes-minion-0uzf} spec.containers{liveness} killing Killing 88c8b717d8b0940d52743c086b43c3fad0d725a36300b9b5f0ad3a1c8cef2d3e
|
||||
Thu, 14 May 2015 15:23:25 -0700 Thu, 14 May 2015 15:23:25 -0700 1 {kubelet kubernetes-minion-0uzf} spec.containers{liveness} created Created with docker id b254a9810073f9ee9075bb38ac29a4b063647176ad9eabd9184078ca98a60062
|
||||
Thu, 14 May 2015 15:23:25 -0700 Thu, 14 May 2015 15:23:25 -0700 1 {kubelet kubernetes-minion-0uzf} spec.containers{liveness} started Started with docker id b254a9810073f9ee9075bb38ac29a4b063647176ad9eabd9184078ca98a60062
|
||||
...
|
||||
```
|
||||
|
||||
|
||||
[]()
|
||||
|
||||
|
||||
[]()
|
21
release-0.19.0/examples/liveness/exec-liveness.yaml
Normal file
21
release-0.19.0/examples/liveness/exec-liveness.yaml
Normal file
@@ -0,0 +1,21 @@
|
||||
apiVersion: v1beta3
|
||||
kind: Pod
|
||||
metadata:
|
||||
labels:
|
||||
test: liveness
|
||||
name: liveness-exec
|
||||
spec:
|
||||
containers:
|
||||
- args:
|
||||
- /bin/sh
|
||||
- -c
|
||||
- echo ok > /tmp/health; sleep 10; rm -rf /tmp/health; sleep 600
|
||||
image: gcr.io/google_containers/busybox
|
||||
livenessProbe:
|
||||
exec:
|
||||
command:
|
||||
- cat
|
||||
- /tmp/health
|
||||
initialDelaySeconds: 15
|
||||
timeoutSeconds: 1
|
||||
name: liveness
|
18
release-0.19.0/examples/liveness/http-liveness.yaml
Normal file
18
release-0.19.0/examples/liveness/http-liveness.yaml
Normal file
@@ -0,0 +1,18 @@
|
||||
apiVersion: v1beta3
|
||||
kind: Pod
|
||||
metadata:
|
||||
labels:
|
||||
test: liveness
|
||||
name: liveness-http
|
||||
spec:
|
||||
containers:
|
||||
- args:
|
||||
- /server
|
||||
image: gcr.io/google_containers/liveness
|
||||
livenessProbe:
|
||||
httpGet:
|
||||
path: /healthz
|
||||
port: 8080
|
||||
initialDelaySeconds: 15
|
||||
timeoutSeconds: 1
|
||||
name: liveness
|
4
release-0.19.0/examples/liveness/image/Dockerfile
Normal file
4
release-0.19.0/examples/liveness/image/Dockerfile
Normal file
@@ -0,0 +1,4 @@
|
||||
FROM scratch
|
||||
|
||||
ADD server /server
|
||||
|
13
release-0.19.0/examples/liveness/image/Makefile
Normal file
13
release-0.19.0/examples/liveness/image/Makefile
Normal file
@@ -0,0 +1,13 @@
|
||||
all: push
|
||||
|
||||
server: server.go
|
||||
CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-w' ./server.go
|
||||
|
||||
container: server
|
||||
docker build -t gcr.io/google_containers/liveness .
|
||||
|
||||
push: container
|
||||
gcloud preview docker push gcr.io/google_containers/liveness
|
||||
|
||||
clean:
|
||||
rm -f server
|
46
release-0.19.0/examples/liveness/image/server.go
Normal file
46
release-0.19.0/examples/liveness/image/server.go
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
Copyright 2014 The Kubernetes Authors All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
// A simple server that is alive for 10 seconds, then reports unhealthy for
|
||||
// the rest of its (hopefully) short existence.
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
started := time.Now()
|
||||
http.HandleFunc("/started", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(200)
|
||||
data := (time.Now().Sub(started)).String()
|
||||
w.Write([]byte(data))
|
||||
})
|
||||
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
|
||||
duration := time.Now().Sub(started)
|
||||
if duration.Seconds() > 10 {
|
||||
w.WriteHeader(500)
|
||||
w.Write([]byte(fmt.Sprintf("error: %v", duration.Seconds())))
|
||||
} else {
|
||||
w.WriteHeader(200)
|
||||
w.Write([]byte("ok"))
|
||||
}
|
||||
})
|
||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||
}
|
Reference in New Issue
Block a user