Python服务Dokcer化并k8s部署实例
这篇文章记录了我试验将一个基于python的服务docker化并k8s部署的过程。
服务介绍
Docker化
设计
业务代码改造
创建docker镜像
K8S部署
设计
yaml文件
运行
服务介绍
这是一个用 python 实现的程序,tornado 作为 web 服务器,mongodb 作为存储,redis 作为缓存。
Docker化
设计
为了方便,使用docker hub作为镜像仓库,而不是自己搭建私有库。
业务代码直接打包进docker image,如果修改业务代码,需要重新生成docker image。
业务代码改造
将配置文件中之前涉及到的 mongo 和 redis 的配置改为服务名称。
改造前:
{
"mongodb": {
"host":"127.0.0.1",
"port":"27017"
},
"redis": {
"host":"127.0.0.1",
"port":"6379"
}
}
1
2
3
4
5
6
7
8
9
10
改造后:
{
"mongodb": {
"host":"nebulae-redis-mongo", # 服务名称
"port":"27017"
},
"redis": {
"host":"nebulae-redis-mongo",
"port":"6379"
}
}
1
2
3
4
5
6
7
8
9
10
nebulae-redis-mongo 为一个服务的名称,下面讲 k8s 设计时会说到。
创建docker镜像
一、目录结构
nebulae
|
--- Dockerfile
|
--- code # 业务逻辑代码
|
--- nebulae
1
2
3
4
5
6
7
二、Dockerfile
FROM python:3.6 # 基础镜像
COPY ./code/nebulae /code/nebulae # 将代码copy进容器
WORKDIR /code/nebulae/services
RUN pip install -r requirements.txt # 安装程序依赖
EXPOSE 10001 # 容器对外暴露的端口
CMD ["python","nebulae_server.py"] # 容器执行的命令,起动python程序
1
2
3
4
5
6
7
8
9
10
11
创建镜像命令
在 nebulae 目录下运行下面命令,创建镜像
docker build -t test/nebulae:tag
1
将镜像推至 docker hub
docker push test/nebulae:tag
1
K8S部署
设计
本例中,mongo 和 redis 放入一个 pod 里,并在创建服务时命名为nebulae-redis-mongo,也是上面代码改造中替换的名字。
nebulae-redis-mongo 只有一个Pod
python 应用程序启动三个Pod,将服务命名为 nebulae-python
使用volume的hostPath 将 python 应用程序的日志挂截到 Node 上
k8s yaml 文件
nebulae-redis-mongo的Pod的定义:
piVersion: v1
kind: ReplicationController # 声名资源类型,为k8s的类型
metadata:
name: nebulae-redis-mongo
labels:
name: nebulae-redis-mongo
spec:
replicas: 1 # 维持的Pod个数
selector:
app: nebulae-redis-mongo # 对应下方template里的labels
template: # 具体的 Pod 定义
metadata:
labels:
app: nebulae-redis-mongo
spec:
containers: # 这个Pod里包含了mongo和redis里
- name: nebulae-redis
image: redis # redis官方镜像
ports:
- containerPort: 6379
- name: nebulae-mongo
image: mongo # mongo官方镜像
ports:
- containerPort: 27017
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
nebulae-redis-mongo服务的定义
apiVersion: v1
kind: Service # k8s 服务资源
metadata:
name: nebulae-redis-mongo
labels:
name: nebulae-redis-mongo
spec:
type: NodePort
ports:
- port: 6379
name: nebulae-redis
nodePort: 30011 # 在Node上监听此端口
- port: 27017
name: nebulae-mongo
nodePort: 30012 # 在Node上监听此端口
selector:
app: nebulae-redis-mongo # 对应 Pod 的标签
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
nebulae-pyton 的Pod定义
apiVersion: v1
kind: ReplicationController
metadata:
name: nebulae-python
spec:
replicas: 3 # Pod副本数
selector:
app: nebulae-python
template:
metadata:
labels:
app: nebulae-python
spec:
containers:
- name: nebulae-python
image: test/nebulae:0.0.3 # 在docker hub中的镜像地址
volumeMounts:
- mountPath: /code/nebulae/services/log # 容器中的日志目录
name: nebulae-python-log # 卷名
ports:
- containerPort: 10001
volumes:
- name: nebulae-python-log # 卷名
hostPath:
path: "/tmp/logs/nebulae" # node上的目录地址
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
nebulae-pyton 的服务定义
apiVersion: v1
kind: Service
metadata:
name: nebulae-python
spec:
type: NodePort
ports:
- port: 10001
nodePort: 30013
selector:
app: nebulae-python
1
2
3
4
5
6
7
8
9
10
11
运行:
最终的 nebulae 的创建文件,名为 nebulae.yaml
apiVersion: v1
kind: ReplicationController
metadata:
name: nebulae-redis-mongo
labels:
name: nebulae-redis-mongo
spec:
replicas: 1
selector:
app: nebulae-redis-mongo
template:
metadata:
labels:
app: nebulae-redis-mongo
spec:
containers:
- name: nebulae-redis
image: redis
ports:
- containerPort: 6379
- name: nebulae-mongo
image: mongo
ports:
- containerPort: 27017
---
apiVersion: v1
kind: Service
metadata:
name: nebulae-redis-mongo
labels:
name: nebulae-redis-mongo
spec:
type: NodePort
ports:
- port: 6379
name: nebulae-redis
nodePort: 30011
- port: 27017
name: nebulae-mongo
nodePort: 30012
selector:
app: nebulae-redis-mongo
---
apiVersion: v1
kind: ReplicationController
metadata:
name: nebulae-python
spec:
replicas: 3
selector:
app: nebulae-python
template:
metadata:
labels:
app: nebulae-python
spec:
containers:
- name: nebulae-python
image: yf8155674/nebulae:0.0.3
volumeMounts:
- mountPath: /code/nebulae/services/log
name: nebulae-python-log
ports:
- containerPort: 10001
volumes:
- name: nebulae-python-log
hostPath:
path: "/tmp/logs/nebulae"
---
apiVersion: v1
kind: Service
metadata:
name: nebulae-python
spec:
type: NodePort
ports:
- port: 10001
nodePort: 30013
selector:
app: nebulae-python
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
最后布署到k8s,一行命令解决:
$kubectl create -f nebulae.yaml
.....
replicationcontroller "nebulae-redis-mongo" created
service "nebulae-redis-mongo" created
replicationcontroller "nebulae-python" created
service "nebulae-python" created
1
2
3
4
5
6
查看服务
$kubectl get svc # 查看当前的服务
.....
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nebulae-python NodePort 10.8.255.117 <none> 10001:30013/TCP 1m
nebulae-redis-mongo NodePort 10.8.255.202 <none> 6379:30011/TCP,27017:30012/TCP 1m
1
2
3
4
5
查看Pod状态
$kubectl get pods
....
NAME READY STATUS RESTARTS AGE
nebulae-python-9phxq 1/1 Running 0 3m
nebulae-python-nqkq4 1/1 Running 0 3m
nebulae-python-p9zvs 1/1 Running 0 3m
nebulae-redis-mongo-p7625 2/2 Running 0 3m
相关文章
- Python 命令行参数的3种传入方式
- pycharm配置python运行环境_C中调用Python
- python shutil删除_python删除文件
- 如何使用python删除一个文件?
- python格式化转换_Python进制转换format格式化[通俗易懂]
- Python入门系列(五)一篇搞懂python语句
- Python测试框架之pytest详解
- python中dtype什么意思_NumPy Python中的数据类型对象(dtype)
- python中dtype的使用规范_Python numpy.dtype() 使用实例
- python截图识别文字_Python文字截图识别OCR工具实例解析
- python海龟作图红绿灯_海龟作图—用Python绘图
- python监控网页内容变化_使用Python监控文件内容变化代码实例
- k8s系列(1)-腾讯云CVM手动部署K8S_Dashboard安装1
- python表情代码_Python实现表情包的代码实例[通俗易懂]
- 【测试开发】python系列教程:如何注释和注意规范
- python读取pkl_Python读取文件的一段内容
- lambda python表达式_Python的条件表达式和lambda表达式实例
- python中if判断语句的用法_Python if判断语句的用法详细介绍[通俗易懂]
- python编程中,各种随机种子seed设置总结
- 使用Python进行ETL数据处理
- 从Python连接Oracle数据库介绍(python连接oracle)
- 从 Python 连接到 MySQL:实现更多强大的数据库应用(python和mysql)
- Python分析Nginx访问日志并保存到MySQL数据库实例
- python使用BerkeleyDB数据库实例