首页 > 解决方案 > 'OSError: [Errno 98] Address already in use' 在 kubernetes 上部署

问题描述

我正在尝试使用 Python 的 Flask API 在 Google Kubernetes Engine 上部署 Python 应用程序。部署应用程序后,我遇到了“OSError:[Errno 98] 地址已在使用中”。

我认为错误可能是由于端口号,我尝试更改 gunicorn 配置文件上的端口号,但仍然遇到同样的问题。

这些是我正在使用 gunicorn_config.py 的文件


import multiprocessing

bind = "0.0.0.0:8000"
workers = multiprocessing.cpu_count() * 2 + 1
accesslog = "-" # STDOUT
access_log_format = '%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"'
loglevel = "debug"
capture_output = True
enable_stdio_inheritance = True

以下是我用来部署应用程序的 Dockerfile:

# python runtime
FROM ubuntu as intermediate

# install git
RUN apt-get update
RUN apt-get install -y git

# add credentials on build
ARG ACCESS_TOKEN
RUN git config --global url."https://${ACCESS_TOKEN}:@github.com/".insteadOf "https://github.com/"

ARG BRANCH
RUN git clone https://github.com/org_name/rest_api.git -b "${BRANCH}"


FROM python:3.6-stretch




# copy the repository form the previous image
COPY --from=intermediate rest_api rest_api


# working directory
WORKDIR /rest_api/id_generation


# install requirements
RUN pip3 install -r ./src/requirements.txt

# make port 8000 available to the world outside
EXPOSE 8000

CMD ["gunicorn", "--config", "./conf/gunicorn_config.py", "src.main:app"]

这是我用来在 Kuberenetes Engine 上部署的 Kubernetes 命令

kubectl run deployment_name --image-pull-policy=Always --overrides='
{
    "apiVersion": "apps/v1beta1",
    "kind": "Deployment",
    "metadata": {
        "name": "deployment_name",
        "labels": {
            "app": "deployment_name"
        }
    },
    "spec": {
        "selector": {
            "matchLabels": {
                "app": "deployment_name"
            }
        },
        "template": {
            "metadata": {
                "labels": {
                    "app": "deployment_name"
                }
            },
            "spec": {
                "containers": [
                    {
                        "name": "deployment_name",
                        "image": "gcr.io/project_name/id_generator:development",
                        "imagePullPolicy": "Always",
                        "ports": [
                            {
                                "containerPort": 8000
                            }
                        ],
                        "volumeMounts": [
                            {
                                "name": "credentials",
                                "readOnly": true,
                                "mountPath":"/rest_api/keys"
                            }
                        ],
                        "env":[
                            {
                                "name":"GOOGLE_APPLICATION_CREDENTIALS",
                                "value":"/rest_api/keys/key.json"
                            },
                            {
                                "name":"ENVIRONMENT",
                                "value":"development"
                            }
                        ]
                    }
                ],
                "volumes": [
                    {
                        "name": "credentials",
                        "secret": {
                            "secretName": "credentials"
                        }
                    }
                ]
            }
        }
    }
}
'  --image=gcr.io/project_name/deployment_name:development --port 8000

我该如何解决这个错误?

标签: pythondockerflaskgoogle-kubernetes-engine

解决方案


推荐阅读