zoukankan      html  css  js  c++  java
  • 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
    ————————————————
    版权声明:本文为CSDN博主「何止七八」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/qq_24095941/article/details/85761609

  • 相关阅读:
    VS2008编写MFC程序--使用opencv2.4()
    November 02nd, 2017 Week 44th Thursday
    November 01st, 2017 Week 44th Wednesday
    October 31st, 2017 Week 44th Tuesday
    October 30th, 2017 Week 44th Monday
    October 29th, 2017 Week 44th Sunday
    October 28th, 2017 Week 43rd Saturday
    October 27th, 2017 Week 43rd Friday
    October 26th, 2017 Week 43rd Thursday
    October 25th, 2017 Week 43rd Wednesday
  • 原文地址:https://www.cnblogs.com/ExMan/p/11681161.html
Copyright © 2011-2022 走看看