Django应用程序的Kubernetes健康检查总是失败

4urapxun  于 2023-08-03  发布在  Kubernetes
关注(0)|答案(1)|浏览(95)

我是kubernetes的新手,我正在尝试如何配置我的健康检查。当我配置我的livenessProbe时,它总是返回400,但是当我移除探针,exec到pod,并运行curl 127.0.0.1/health时,我得到了{"status": "ok"}
(我在minikube主机上运行这个)
这是我的dockerfile

FROM python:3.11
# setup env variables
ENV PYTHONBUFFERED=1
ENV DockerHOME=/app/django-app
# Expose port
EXPOSE 8000
# create work dir
RUN mkdir -p $DockerHOME
# set work dir
WORKDIR $DockerHOME
# copy code to work dir
COPY . $DockerHOME
# install dependencies
RUN pip install -r requirements.txt
# move working dir to where manage.py is
WORKDIR $DockerHOME/flag_games
# set default command (I thinkk)
ENTRYPOINT ["python"]
# run commands for app to run
CMD ["manage.py", "collectstatic", "--noinput"]
CMD ["manage.py", "runserver", "localhost:8000"]

字符串
这是我的deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: flag-game-deployment
  labels:
    app: flag-game-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: flag-game-deployment
  template:
    metadata:
      labels:
        app: flag-game-deployment
    spec:
      containers:
      - image: docker-django
        imagePullPolicy: Never
        name: docker-django
        livenessProbe:
          httpGet:
            path: /health
            port: 8000
            scheme: HTTP
          initialDelaySeconds: 5
          periodSeconds: 5


以下是我的makebuild步骤

minikube-deploy:
    make docker-build
    minikube start
    minikube image load $(IMAGE_NAME)
    kubectl apply -f "$(PWD)\manifests\deployment.yaml"
    kubectl expose deployment $(KUBE_DEPLOYMENT_NAME) --type=NodePort --port=8000 --dry-run=client -o yaml | kubectl apply -f -


这是我的views.py和url.py

def health_check(request):
    # Perform any checks to determine the health of your application
    is_healthy = True

    # Return a JSON response with the health status
    if is_healthy:
        return JsonResponse({'status': 'ok'}, status=200)
    else:
        return JsonResponse({'status': 'error'}, status=503)

urlpatterns = [
    path('', views.index, name='index'),
    path('world_flags/', include('world_flags.urls')),
    path('health', views.health_check),
]


任何和所有的帮助是赞赏!

hgqdbh6s

hgqdbh6s1#

找到问题了
deployments.yamllivenessProbe的路径应该是/health/而不是/health

相关问题