介绍
在 Kubernetes 中,探针(probe)是一种诊断工具,用于确定容器是否存活并准备好接收流量。探针有两种类型:存活探针(liveness probe)和就绪探针(readiness probe)。在本实验中,我们将重点介绍 ContainerProbe,这是一种用于检测容器何时准备好接收流量的工具。
在 Kubernetes 中,探针(probe)是一种诊断工具,用于确定容器是否存活并准备好接收流量。探针有两种类型:存活探针(liveness probe)和就绪探针(readiness probe)。在本实验中,我们将重点介绍 ContainerProbe,这是一种用于检测容器何时准备好接收流量的工具。
在创建资源之前,你需要一个正在运行的 Kubernetes 集群。Minikube 是一个轻量级的 Kubernetes 环境,可以在你的本地机器上运行。
导航到工作目录:
打开终端并导航到默认的项目文件夹:
cd /home/labex/project
启动 Minikube:
启动 Minikube 以初始化一个 Kubernetes 集群:
minikube start
验证 Minikube 是否正在运行:
检查 Minikube 集群的状态:
minikube status
kubelet
和 apiserver
等组件是否显示为 Running
。minikube start
。如果启动 Minikube 时遇到问题,可以使用 minikube delete
来重置环境(如有需要)。
第一步是在 Kubernetes 中创建一个 Deployment。我们将使用这个 Deployment 来测试 ContainerProbe。
/home/labex/project
目录下创建一个名为 deployment.yaml
的新文件。apiVersion: apps/v1
kind: Deployment
metadata:
name: containerprobe-deployment
spec:
replicas: 1
selector:
matchLabels:
app: containerprobe
template:
metadata:
labels:
app: containerprobe
spec:
containers:
- name: containerprobe
image: nginx
ports:
- containerPort: 80
此代码创建了一个具有一个副本的 Deployment,一个带有标签 app: containerprobe
的选择器,以及一个运行 nginx 镜像的容器。
kubectl apply -f deployment.yaml
下一步是为 nginx 容器添加一个存活探针(liveness probe)。存活探针用于确定容器是否存活。如果探针失败,Kubernetes 将重启容器。
/home/labex/project
目录中的 deployment.yaml
文件:apiVersion: apps/v1
kind: Deployment
metadata:
name: containerprobe-deployment
spec:
replicas: 1
selector:
matchLabels:
app: containerprobe
template:
metadata:
labels:
app: containerprobe
spec:
containers:
- name: containerprobe
image: nginx
ports:
- containerPort: 80
livenessProbe:
httpGet:
path: /
port: 80
此代码指定存活探针应向端口 80 的根路径发送 HTTP GET 请求。
kubectl apply -f deployment.yaml
现在我们已经添加了存活探针,可以测试它是否正常工作。
kubectl get pods -l app=containerprobe -o jsonpath='{.items[0].metadata.name}'
此命令获取由 Deployment 创建的 Pod 的名称。
kubectl describe pod <pod-name>
将 <pod-name>
替换为上一步中获取的 Pod 名称。
你应该会看到包含以下内容的输出:
Liveness: http-get http://:80/ delay=0s timeout=1s period=10s #success=1 #failure=3
这表明存活探针已正确配置。
下一步是为 nginx 容器添加一个就绪探针(readiness probe)。就绪探针用于确定容器是否准备好接收流量。如果探针失败,Kubernetes 将不会向容器发送流量。
deployment.yaml
文件的容器定义中添加以下代码:apiVersion: apps/v1
kind: Deployment
metadata:
name: containerprobe-deployment
spec:
replicas: 1
selector:
matchLabels:
app: containerprobe
template:
metadata:
labels:
app: containerprobe
spec:
containers:
- name: containerprobe
image: nginx
ports:
- containerPort: 80
livenessProbe:
httpGet:
path: /
port: 80
readinessProbe:
httpGet:
path: /
port: 80
此代码指定就绪探针应向端口 80 的根路径发送 HTTP GET 请求。
kubectl apply -f deployment.yaml
现在我们已经添加了就绪探针,可以测试它是否正常工作。
kubectl get pods -l app=containerprobe -o jsonpath='{.items[0].metadata.name}'
此命令获取由 Deployment 创建的 Pod 的名称。
kubectl describe pod <pod-name>
将 <pod-name>
替换为上一步中获取的 Pod 名称。
你应该会看到包含以下内容的输出:
Readiness: http-get http://:80/ delay=0s timeout=1s period=10s #success=1 #failure=3
这表明就绪探针已正确配置。
在本实验中,我们学习了如何在 Kubernetes 中使用 ContainerProbe。我们创建了一个 Deployment,添加了存活探针(liveness probe),测试了存活探针,添加了就绪探针(readiness probe),并测试了就绪探针。通过使用 ContainerProbe,我们可以确保容器已准备好接收流量并正常运行。