Kubernetes NGINX 找不到 uWSGI 应用程序服务器但正在使用 docker-compose
Kubernetes NGINX can't find uWSGI application server but working with docker-compose
我有一个包含两个图像的应用程序,一个 运行 NGINX 和另一个 运行 Flask/uWSGI。它使用 docker-compose 按预期工作。现在我尝试在 Kubernetes 集群上部署我的应用程序,但我无法在 NGINX 和我的 uWSGI 应用程序服务器之间建立连接。
日志是我的 nginx 部署说:
2019/09/13 11:29:53 [error]6#6: *21 upstream timed out (110: Connection timed out) while connecting to upstream, client: 10.244.0.1, server: , 请求: "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 配置文件:
烧瓶-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
我认为这里的解决方案是更改几个设置,即您的服务应该使用 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
我有一个包含两个图像的应用程序,一个 运行 NGINX 和另一个 运行 Flask/uWSGI。它使用 docker-compose 按预期工作。现在我尝试在 Kubernetes 集群上部署我的应用程序,但我无法在 NGINX 和我的 uWSGI 应用程序服务器之间建立连接。
日志是我的 nginx 部署说:
2019/09/13 11:29:53 [error]6#6: *21 upstream timed out (110: Connection timed out) while connecting to upstream, client: 10.244.0.1, server: , 请求: "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 配置文件:
烧瓶-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
我认为这里的解决方案是更改几个设置,即您的服务应该使用 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