最近,我将自己的Django后端部署到了他们的Linux2系统中(确切地说,平台名是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.对于互联网来说是独一无二的(似乎从来没有人遇到过这样的问题)。
另一件奇怪的事情是我的/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: []有没有人知道如何解决这个问题?最终的目标是拥有绿色健康。
编辑:,最后我切换到Basic医疗系统,问题突然消失了。然而,我仍然有兴趣解决最初的问题,因为Enhanced医疗系统提供了一些好处。
发布于 2021-04-10 07:53:27
我相信您所遇到的问题可能是由于ALLOWED_HOSTS设置位于您的文件settings.py中。
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重新部署它。
发布于 2022-02-24 00:10:04
有一个包django-ebhealthcheck用于解决这个问题,通过获取ec2实例的本地ip并将其添加到ALLOWED_HOSTS中,使用起来非常简单,您只需将'ebhealthcheck.apps.EBHealthCheckConfig'添加到INSTALLED_APPS
https://stackoverflow.com/questions/63011195
复制相似问题