[英]Error in executing a command when starting a container using docker-compose
[英]error starting container in Kubernetes but not with docker-compose
我的容器使用 docker-compose 运行良好。但是一旦我在 Kube.netes 上应用我的部署,它每次都会因相同的错误而失败。 不管我怎么努力。 我被卡住了,希望得到一些输入/帮助。
我不知道如何调试这个。 Kube.netes 有点新。 所以如果有人可以指导我,我可以尝试调试它。
错误堆栈跟踪如下:
Running migration with alembic
INFO [src.core.config] value for BACKEND_CORS_ORIGINS= ['http://localhost:3000', 'http://localhost:8001']
Traceback (most recent call last):
File "pydantic/env_settings.py", line 197, in pydantic.env_settings.EnvSettingsSource.__call__
File "pydantic/env_settings.py", line 131, in pydantic.env_settings.Config.parse_env_var
File "/usr/local/lib/python3.9/json/__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "/usr/local/lib/python3.9/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/local/lib/python3.9/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/local/bin/alembic", line 8, in <module>
sys.exit(main())
File "/usr/local/lib/python3.9/site-packages/alembic/config.py", line 590, in main
CommandLine(prog=prog).main(argv=argv)
File "/usr/local/lib/python3.9/site-packages/alembic/config.py", line 584, in main
self.run_cmd(cfg, options)
File "/usr/local/lib/python3.9/site-packages/alembic/config.py", line 561, in run_cmd
fn(
File "/usr/local/lib/python3.9/site-packages/alembic/command.py", line 322, in upgrade
script.run_env()
File "/usr/local/lib/python3.9/site-packages/alembic/script/base.py", line 569, in run_env
util.load_python_file(self.dir, "env.py")
File "/usr/local/lib/python3.9/site-packages/alembic/util/pyfiles.py", line 94, in load_python_file
module = load_module_py(module_id, path)
File "/usr/local/lib/python3.9/site-packages/alembic/util/pyfiles.py", line 110, in load_module_py
spec.loader.exec_module(module) # type: ignore
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "alembic/env.py", line 22, in <module>
from src.core.config import settings
File "/app/src/core/config.py", line 72, in <module>
settings = Settings()
File "pydantic/env_settings.py", line 40, in pydantic.env_settings.BaseSettings.__init__
File "pydantic/env_settings.py", line 75, in pydantic.env_settings.BaseSettings._build_values
File "pydantic/env_settings.py", line 200, in pydantic.env_settings.EnvSettingsSource.__call__
pydantic.env_settings.SettingsError: error parsing env var "BACKEND_CORS_ORIGINS"
Alembic migration done
[2022-11-15 21:25:36 +0000] [1] [INFO] Starting gunicorn 20.1.0
[2022-11-15 21:25:36 +0000] [1] [INFO] Listening at: http://0.0.0.0:8001 (1)
[2022-11-15 21:25:36 +0000] [1] [INFO] Using worker: uvicorn.workers.UvicornWorker
[2022-11-15 21:25:36 +0000] [23] [INFO] Booting worker with pid: 23
[2022-11-15 21:25:37 +0000] [24] [INFO] Booting worker with pid: 24
[2022-11-15 21:25:37 +0000] [25] [INFO] Booting worker with pid: 25
[2022-11-15 21:25:40 +0000] [23] [ERROR] Exception in worker process
现在它是如何设置的。
我的 docker-compose 文件使用 Dockerfile 构建。Dockerfile 的结尾如下。 CMD指令前有一个ENTRYPOINT
ENTRYPOINT ["./docker-entrypoint.sh"]
CMD ["./run.sh"]
docker-entrypoint.sh
if [ -n "$DB_SERVER" ]; then
./wait-for-it.sh "$DB_SERVER:${DB_PORT:-3306}"
fi
# Run the main container command.
exec "$@"
这是有效的,因为我可以从“等待”脚本中看到日志
run.sh (错误来自哪里)
export APP_MODULE=${APP_MODULE-src.main:app}
export HOST=${HOST:-0.0.0.0}
export PORT=${PORT:-8001}
export BACKEND_CORS_ORIGINS=${BACKEND_CORS_ORIGINS:-'http://localhost:8001'}
echo "Running migration with alembic"
# Run migrations
alembic upgrade head
echo "Alembic migration done"
exec gunicorn -b $HOST:$PORT "$APP_MODULE" --reload --workers=3 --timeout 0 -k uvicorn.workers.UvicornWorker
我已将默认值设置为 BACKEND_CORS_ORIGINS 以防万一。
在上面的堆栈跟踪中,您可以在第 2 行看到一个日志。 这是在 BACKEND_CORS_ORIGINS 的 pydantic 验证器中。 这是我的config.py文件的一部分:
class Settings(BaseSettings):
BACKEND_CORS_ORIGINS: List[AnyHttpUrl] = ["http://localhost:3000", "http://localhost:8001"]
@validator("BACKEND_CORS_ORIGINS", pre=True, allow_reuse=True)
def assemble_cors_origins(cls, value: Union[str, List[str]]) -> Union[List[str], str]:
logger.info(f"value for BACKEND_CORS_ORIGINS= {value}, type of BACKEND_CORS_ORIGINS= {type(value)}")
backend_cors_origins = None
if isinstance(value, str) and not value.startswith("["):
backend_cors_origins = [i.strip() for i in value.split(",")]
elif isinstance(value, (list, str)):
backend_cors_origins = value
logger.info(f"value for BACKEND_CORS_ORIGINS= {backend_cors_origins}, type of BACKEND_CORS_ORIGINS= {type(backend_cors_origins)}")
if backend_cors_origins:
return backend_cors_origins
raise ValueError(value)
由于某些原因,我没有在日志的最后一部分看到我想要查看类型的地方。
最后,这是我的 Kube.netes 的Deployment.yml文件
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: dev
name: api-name
spec:
replicas: 1
selector:
matchLabels:
app: api-name
template:
metadata:
labels:
app: api-name
spec:
imagePullSecrets:
- name: secret_name
containers:
- name: api-name
image: [private_registry]/api
env:
- name: MARIADB_USER
value: 'db_user'
- name: MARIADB_PASSWORD
value: 'password'
- name: MARIADB_SERVER
value: 'IP_DB_SERVER'
- name: MARIADB_DATABASE
value: 'db_name'
- name: MARIADB_PORT
value: '1234'
- name: BACKEND_CORS_ORIGINS
value: 'http://localhost:8001'
resources:
limits:
memory: "512Mi"
cpu: "500m"
ports:
- containerPort: 8001
---
apiVersion: v1
kind: Service
metadata:
namespace: dev
name: svc-api
spec:
type: NodePort
selector:
app: api-name
ports:
- port: 8001
targetPort: 8001
有人可以帮助或指出我正确的方向吗? 一直在尝试我所知道的一切来解决这个问题,但没有任何运气。
也许我需要更多 Kube.netes 的配置文件?
如果我删除run.sh脚本中的 alembic 命令,我仍然会收到相同的错误,但 @validator 中没有日志
这个问题似乎来自这条出口线
BACKEND_CORS_ORIGINS=${BACKEND_CORS_ORIGINS:-'http://localhost:8001'}
似乎需要一个 URL 列表,所以也许你可以更改代码
- name: BACKEND_CORS_ORIGINS
value:
- 'localhost:8001'
在 Deployment.yml
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.