Kubernetes NGINX找不到uWSGI应用程序服务器,但使用docker-compose

时间:2019-09-13 12:30:08

标签: flask kubernetes docker-compose uwsgi

我有一个包含两个图像的应用程序,一个运行NGINX,另一个运行Flask / uWSGI。使用docker-compose可以正常工作。现在,我尝试将应用程序部署在Kubernetes集群上,但无法在NGINX和uWSGI应用程序服务器之间建立连接。

日志是我的nginx部署说的:

2019/09/13 11:29:53 [错误] 6#6:* 21在连接到上游时,上游超时(110:连接超时),客户端:10.244.0.1,服务器:,请求:“ GET / HTTP / 1.1”,上游:“ uwsgi://10.0.232.218:8080”,主机:“ 52.166.xxxxxx”

但是,我的烧瓶服务似乎运行正常。

NAMESPACE              NAME                        TYPE           CLUSTER-IP     EXTERNAL-IP     PORT(S)          AGE
default                flask                       NodePort       10.0.232.218   <none>          8080:32043/TCP   23m   
default                kubernetes                  ClusterIP      10.0.0.1       <none>          443/TCP          19h
default                nginx                       LoadBalancer   10.0.162.165   52.166.xxxxx   80:31669/TCP     23m  

我尝试更改服务中的设置,但未成功。我使用kompose将docker-compose.yaml转换为* -deployment,*-service.yaml文件。值得注意的是,在此过程中,kompose没有提供flask-service.yaml。我必须手动创建它。

这是我的代码: docker-compose.yaml

version: "3"

services:

flask:
  image: registry.azurecr.io/model_flask
  build: ./flask
  container_name: flask
  restart: always
  environment:
    - APP_NAME=fundamentalmodel
  expose:
    - 8080

nginx:
  image: registry.azurecr.io/model_nginx
  build: ./nginx
  container_name: nginx
  restart: always
  ports:
    - "80:80"

用于Flask的Dockerfile

# Dockerfile to build glpk container images
# Based on Ubuntu

# Set the base image to Ubuntu
FROM ubuntu:latest

# Switch to root for install
USER root

# Install wget
RUN apt-get update -y && apt-get install -y \
wget \
build-essential \
python3 \
python3-pip \
python3.6-dev \
--no-install-recommends \
&& rm -rf /var/lib/apt/lists/*

RUN pip3 install setuptools Cython numpy wheel 
RUN pip3 install uwsgi


# Create a user
ENV HOME /home/user
RUN useradd --create-home --home-dir $HOME user \
&& chmod -R u+rwx $HOME \
&& chown -R user:user $HOME


# switch back to user
WORKDIR $HOME
USER user

COPY . /usr/src/app
WORKDIR /usr/src/app
RUN pip3 install -r requirements.txt
RUN pip3 install .

RUN export LC_ALL=C.UTF-8
RUN export LANG=C.UTF-8
RUN export FLASK_APP=server
CMD ["uwsgi", "uwsgi.ini"]

我的nginx Dockerfile

# Use the Nginx image
FROM nginx

# Remove the default nginx.conf
RUN rm /etc/nginx/conf.d/default.conf

# Replace with our own nginx.conf
COPY nginx.conf /etc/nginx/conf.d/

现在我的Kubernetes配置文件:

flask-deployment.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: kompose convert
    kompose.version: 1.17.0 (a74acad)
  creationTimestamp: null
  labels:
    io.kompose.service: flask
  name: flask
spec:
  replicas: 1
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        io.kompose.service: flask
    spec:
      containers:
      - env:
        - name: APP_NAME
          value: fundamentalmodel
        image: registry.azurecr.io/fundamentalmodel_flask
        name: flask
        resources: {}
        ports:
          - containerPort: 8080
      restartPolicy: Always
status: {}

flask-service.yaml

apiVersion: v1
kind: Service
metadata:
  labels:
    name: flask
  name: flask
spec:
type: NodePort
  selector:
    app: flask
  ports:
  - name: http
    port: 8080
    targetPort: 8080

nginx-deployment.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: kompose.exe convert
    kompose.version: 1.17.0 (a74acad)
  creationTimestamp: null
  labels:
    io.kompose.service: nginx
  name: nginx
spec:
  replicas: 1
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        io.kompose.service: nginx
    spec:
      containers:
      - image: registry.azurecr.io/fundamentalmodel_nginx
        name: nginx
        ports:
        - containerPort: 80
        resources: {}
      restartPolicy: Always
status: {}

nginx-service.yaml

apiVersion: v1
kind: Service
metadata:
  annotations:
    kompose.cmd: kompose.exe convert
    kompose.version: 1.17.0 (a74acad)
  creationTimestamp: null
  labels:
    io.kompose.service: nginx
  name: nginx
spec:
  type: LoadBalancer
  ports:
  - name: "80"
    port: 80
    targetPort: 80
  selector:
    io.kompose.service: nginx
status:
  loadBalancer: {}

nginx.conf

server {

    listen 80;

    location / {
        include uwsgi_params;
        uwsgi_pass flask:8080;
    }

}

uswgi.ini

[uwsgi]
wsgi-file = run.py
callable = app
socket = :8080
processes = 4
threads = 2
master = true
chmod-socket = 660
vacuum = true
die-on-term = true

1 个答案:

答案 0 :(得分:0)

我认为这里的解决方案是您更改几个设置,即您的服务应使用ClusterIP,因为NodePort的用途不同(请在此处https://kubernetes.io/docs/concepts/services-networking/service/#nodeport了解更多):

apiVersion: v1
kind: Service
metadata:
  labels:
    name: flask
  name: flask
spec:
type: ClusterIP
  selector:
    app: flask
  ports:
  - name: http
    port: 8080
    targetPort: 8080

您的UWSGI配置也应指定0.0.0.0:8080

[uwsgi]
wsgi-file = run.py
callable = app
socket = 0.0.0.0:8080
processes = 4
threads = 2
master = true
chmod-socket = 660
vacuum = true
die-on-term = true