![](/img/trans.png)
[英]AWS Elastic Beanstalk Django application health check problem
[英]How to resolve AWS Elastic Beanstalk Django health check problems
我最近将我的 Django API 后端部署到 AWS EB 到他们的 Linux 2 系统(确切的平台名称是Python 3.7 running on 64bit Amazon Linux 2
)。
几乎一切都按预期工作,但我的应用程序健康状态是Severe
的,经过数小时的调试我不知道为什么。
正在使用以下端点( django-health-check
模块)处理应用程序的健康检查。
url(r'^ht/', include('health_check.urls'))
100% 的请求的状态代码为200
,但我的整体健康状况如下:
|--------------------|----------------|---------------------------------------------------|
| instance-id | status | cause |
|--------------------|----------------|---------------------------------------------------|
| Overall | Degraded | Impaired services on all instances. |
| i-0eb89f... | Severe | Following services are not running: release. |
|--------------------|----------------|---------------------------------------------------|
最奇怪的是消息Following services are not running: release.
是 inte.net 独有的(似乎以前没有人遇到过这样的问题)。
另一个奇怪的事情是我的/var/log/healthd/daemon.log
文件的内容,这些行类似于
W, [2020-07-21T09:00:01.209091 #3467] WARN -- : log file "/var/log/nginx/healthd/application.log.2020-07-21-09" does not exist
时间改变的地方。
最后可能相关的是.ebextensions
目录中我的单个文件的内容:
option_settings:
"aws:elasticbeanstalk:application:environment":
DJANGO_SETTINGS_MODULE: "app.settings"
"PYTHONPATH": "/var/app/current:$PYTHONPATH"
"aws:elasticbeanstalk:container:python":
WSGIPath: app.wsgi:application
NumProcesses: 3
NumThreads: 20
aws:elasticbeanstalk:environment:proxy:staticfiles:
/static: static
/static_files: static_files
container_commands:
01_migrate:
command: "source /var/app/venv/staging-LQM1lest/bin/activate && python manage.py migrate --noinput"
leader_only: true
packages:
yum:
git: []
postgresql-devel: []
有谁知道如何解决这个问题? 最终目标是拥有绿色OK健康。
编辑:最后我切换到Basic
健康系统,问题突然消失了。 然而,我仍然对解决原始问题感兴趣,因为Enhanced
健康系统提供了一些好处
我相信您遇到的问题可能是由于文件 settings.py 中的 ALLOWED_HOSTS 设置造成的。
EB 向您的应用程序发送 HTTP 请求以查看其是否正常工作,但 Django 会阻止任何不来自设置变量中指定主机的通信。 但是这里有一个问题,EB将请求发送到ec2实例的私有ip。
解决这个问题的最简单方法是在你的settings.py
文件中允许所有这样的主机:
ALLOWED_HOSTS=['*']
这可能会导致安全问题,但这是最快的方法。 现在,为了使其动态工作,因为 ec2 实例可以随时启动,私有 ip 从一个实例更改为另一个实例。
要解决此问题,您必须在部署过程开始时获取私有 IP。
在settings.py
的顶部放置以下函数:
import os
import requests
# Other imports ...
def is_ec2_linux():
"""Detect if we are running on an EC2 Linux Instance
See http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/identify_ec2_instances.html
"""
if os.path.isfile("/sys/hypervisor/uuid"):
with open("/sys/hypervisor/uuid") as f:
uuid = f.read()
return uuid.startswith("ec2")
return False
def get_token():
"""Set the autorization token to live for 6 hours (maximum)"""
headers = {
'X-aws-ec2-metadata-token-ttl-seconds': '21600',
}
response = requests.put('http://169.254.169.254/latest/api/token', headers=headers)
return response.text
def get_linux_ec2_private_ip():
"""Get the private IP Address of the machine if running on an EC2 linux server.
See https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html"""
if not is_ec2_linux():
return None
try:
token = get_token()
headers = {
'X-aws-ec2-metadata-token': f"{token}",
}
response = requests.get('http://169.254.169.254/latest/meta-data/local-ipv4', headers=headers)
return response.text
except:
return None
finally:
if response:
response.close()
# Other settings
最重要的函数是get_token()
和get_linux_ec2_private_ip()
,第一个设置访问令牌并检索它以供第二个使用它并获取当前的 ec2 实例 IP。
检索到它后,将其添加到您的 ALLOWED_HOSTS
ALLOWED_HOSTS = ['127.0.0.1', 'mywebsite.com']
private_ip = get_linux_ec2_private_ip()
if private_ip:
ALLOWED_HOSTS.append(private_ip)
之后,如果您已设置 EB CLI,只需提交您的更改并使用eb deploy
重新部署它。
有一个 package django-ebhealthcheck
旨在通过获取您的 ec2 实例的本地 ip 并将其添加到ALLOWED_HOSTS
来解决此问题,它使用起来非常简单,您只需将'ebhealthcheck.apps.EBHealthCheckConfig'
添加到INSTALLED_APPS
Package github 页 – https://github.com/sjkingo/django-ebhealthcheck
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.