Fork me on GitHub

容器化案例二:Python服务容器化

公司的AI项目和运维项目基本都是Python项目,这里记录下Python项目的容器化知识。

Dockerfile文件编写

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
FROM python:3.6.8-slim

LABEL Maintainer="[email protected]" \
Description="Python Project based on Alpine Linux."

# Enable unbuffered logging
ENV PYTHONUNBUFFERED=1

# Enable Profiler
ENV ENABLE_PROFILER=1


RUN set -x \
&& echo "deb http://mirrors.aliyun.com/debian/ stretch main non-free contrib" > /etc/apt/sources.list \
&& echo "deb http://mirrors.aliyun.com/debian/ stretch-proposed-updates main non-free contrib" >> /etc/apt/sources.list \
&& echo "deb-src http://mirrors.aliyun.com/debian/ stretch main non-free contrib" >> /etc/apt/sources.list \
&& echo "deb-src http://mirrors.aliyun.com/debian/ stretch-proposed-updates main non-free contrib" >> /etc/apt/sources.list \
&& apt-get update \
&& apt-get install -y --no-install-recommends --no-install-recommends curl telnet net-tools g++ \
&& rm -rf /var/lib/apt/lists/*

# get packages
COPY requirements.txt .

RUN pip install --upgrade pip -i https://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

RUN pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

WORKDIR /data/source/

COPY . .

RUN \cp ${project_name}/_settings_prod.py ${project_name}/settings.py

RUN sed -i '/if query is not None:/d' /usr/local/lib/python3.6/site-packages/django/db/backends/mysql/operations.py && \
sed -i '/query = query.decode/d' /usr/local/lib/python3.6/site-packages/django/db/backends/mysql/operations.py

EXPOSE 9999

ENTRYPOINT [ "python", "manage.py", "runserver","0.0.0.0:9999" ]

Deployment及Service文件编写

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
{{$MODULE := "serve" }}
{{$TRUE_APP_NAME := (printf "%v-%v" .Env.FLOW_STANDARD_NAME $MODULE)}}


---
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{$TRUE_APP_NAME}}-deploy
labels:
rd: {{.Env.CMP_RD}}
app: {{.Env.FLOW_STANDARD_NAME}}
project-name: {{.Env.FLOW_PROJECT_NAME}}
module: {{$MODULE}}
spec:
replicas: {{.Env.D_STATUS_REPLICAS}}
selector:
matchLabels:
app: {{.Env.FLOW_STANDARD_NAME}}
project-name: {{.Env.FLOW_PROJECT_NAME}}
module: {{$MODULE}}
template:
metadata:
annotations:
alibabacloud.com/burst-resource: eci
labels:
rd: {{.Env.CMP_RD}}
app: {{.Env.FLOW_STANDARD_NAME}}
project-name: {{.Env.FLOW_PROJECT_NAME}}
module: {{$MODULE}}
spec:
dnsConfig:
options:
- name: ndots
value: "3"
imagePullSecrets:
- name: harbor-registry
containers:
- name: serve
image: {{.Env.IMAGE_NAME}}
imagePullPolicy: Always
readinessProbe:
tcpSocket:
port: {{.Env.CMP_PORT}}
initialDelaySeconds: 15
periodSeconds: 10
timeoutSeconds: 1
successThreshold: 1
failureThreshold: 30
env:
- name: JAVA_APP_OPTS
value: {{if eq .Env.FLOW_DIST_ENV "pre"}}"-Denv=PRO -Didc=pre"{{else}}"-Denv=PRO"{{end}}
- name: ENABLE_PP_AGENT
value: {{if eq .Env.FLOW_DIST_ENV "pre"}}"0"{{else}}"1"{{end}}
resources:
limits:
cpu: 3000m
memory: 4096Mi
requests:
cpu: 2000m
memory: 2048Mi
ports:
- containerPort: {{.Env.CMP_PORT}}
name: web
protocol: TCP
volumeMounts:
- name: logs
mountPath: /tmp/log
- name: xxx
mountPath: /data/xxx
volumeMounts:
- name: logs
mountPath: /tmp/log
volumes:
- name: xxx
persistentVolumeClaim:
claimName: xxx
- name: logs
emptyDir: {}
...

---
apiVersion: v1
kind: Service
metadata:
name: {{$TRUE_APP_NAME}}-svc
labels:
rd: {{.Env.CMP_RD}}
app: {{.Env.FLOW_STANDARD_NAME}}
module: {{$MODULE}}
spec:
type: ClusterIP
selector:
app: {{.Env.FLOW_STANDARD_NAME}}
module: {{$MODULE}}
ports:
- name: web
protocol: TCP
port: {{.Env.CMP_PORT}}
targetPort: {{.Env.CMP_PORT}}

======================================================
希望各位朋友支持一下

本文作者:dongsheng
本文地址https://mds1455975151.github.io/archives/acd906c.html
版权声明:转载请注明出处!

坚持技术分享,您的支持将鼓励我继续创作!