Kubernetes 部署python flask应用
Dockerfile
# Use a base image with Python 3.9 installed
FROM python:3.11
# Set the working directory inside the container
WORKDIR /app
# Copy the requirements file to the working directory
COPY requirements.txt .
# Install the required dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Copy the application code to the working directory
COPY . .
# Expose the port that the Flask application will run on
EXPOSE 5000
# Set the environment variable for Flask
ENV FLASK_APP=app.py
# Run the Flask application (0.0.0.0意味着向所有IP开放)
CMD [ "flask", "run","--host","0.0.0.0","--port","5000"]
Kubernetes yaml file
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: gptapi-ingress
namespace: chatgpt
annotations:
kubernetes.io/ingress.class: "nginx"
cert-manager.io/issuer: "namespace/letsencrypt-dns01"
nginx.ingress.kubernetes.io/ssl-redirect: "true"
spec:
tls:
- hosts:
- test.com
secretName: gptapi-selfsigned-cert-tls
rules:
- host: test.com
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: chatgpt-api-service
port:
number: 80
---
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
name: gptapi-selfsigned-cert-tls
namespace: chatgpt
spec:
dnsNames:
- test.com
secretName: gptapi-selfsigned-cert-tls
issuerRef:
name: letsencrypt-dns01
---
apiVersion: v1
kind: Service
metadata:
name: chatgpt-api-service
namespace: chatgpt
spec:
selector:
app: chatgpt_api_app
ports:
- name: "8080www"
protocol: TCP
port: 80
targetPort: 5000
clusterIP: None
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: chatgpt-api-deployment
namespace: chatgpt
spec:
replicas: 1
selector:
matchLabels:
app: chatgpt_api_app
template:
metadata:
labels:
app: chatgpt_api_app
spec:
containers:
- name: chatgpt-container
image: registry-intl-vpc.ap-southeast-1.aliyuncs.com/namespace/project:latest
imagePullPolicy: Always
ports:
- containerPort: 5000
name: port80test
imagePullSecrets:
- name: alicloudwwwkey
Facebook评论