美文网首页程序员
使用Docker+Kubernetes运行python程序

使用Docker+Kubernetes运行python程序

作者: TEYmL | 来源:发表于2021-02-01 15:07 被阅读0次

1 Alpine

接下来的测试会以Alpine作为基础image

Alpine Linux 是一个社区开发的面向安全应用的轻量级Linux发行版。 Alpine 的意思是“高山的”,它采用了musl libc和busybox以减小系统的体积和运行时资源消耗,同时还提供了自己的包管理工具apk

eg1:使用alpine image制作python3 container

  • 制作DockerFile
root@k8smaster:~/micro# cat df3
FROM alpine
RUN echo "https://mirrors.aliyun.com/alpine/v3.6/main/" > /etc/apk/repositories
RUN apk update
RUN apk add python3
RUN mkdir -p /opt/
COPY example.txt /opt/example.txt
CMD python3 -c "print('hello container')"
  • docker build image
root@k8smaster:~/micro# docker build -f df3 --tag teym88/python3:v2 . 
Sending build context to Docker daemon  6.144kB
Step 1/7 : FROM alpine
 ---> e50c909a8df2
Step 2/7 : RUN echo "https://mirrors.aliyun.com/alpine/v3.6/main/" > /etc/apk/repositories
 ---> Running in fc27a81d35a8
Removing intermediate container fc27a81d35a8
 ---> 797e5a5dd8c0
Step 3/7 : RUN apk update
 ---> Running in 29523a576fe5
fetch https://mirrors.aliyun.com/alpine/v3.6/main/x86_64/APKINDEX.tar.gz
v3.6.5-44-gda55e27396 [https://mirrors.aliyun.com/alpine/v3.6/main/]
OK: 5572 distinct packages available
Removing intermediate container 29523a576fe5
 ---> e1d216887e57
Step 4/7 : RUN apk add python3
 ---> Running in cddbfd87e8e5
(1/13) Installing libbz2 (1.0.6-r5)
(2/13) Installing libressl2.5-libcrypto (2.5.5-r2)
(3/13) Installing expat (2.2.0-r1)
(4/13) Installing libffi (3.2.1-r3)
(5/13) Installing gdbm (1.12-r0)
(6/13) Installing xz-libs (5.2.3-r0)
(7/13) Installing ncurses-terminfo-base (6.0_p20171125-r1)
(8/13) Installing ncurses-terminfo (6.0_p20171125-r1)
(9/13) Installing ncurses-libs (6.0_p20171125-r1)
(10/13) Installing readline (6.3.008-r5)
(11/13) Installing sqlite-libs (3.25.3-r0)
(12/13) Installing libressl2.5-libssl (2.5.5-r2)
(13/13) Installing python3 (3.6.8-r0)
Executing busybox-1.32.1-r2.trigger
OK: 76 MiB in 27 packages
Removing intermediate container cddbfd87e8e5
 ---> fd213d512afc
Step 5/7 : RUN mkdir -p /opt/
 ---> Running in c1aa4b4aa539
Removing intermediate container c1aa4b4aa539
 ---> 109eff5fb159
Step 6/7 : COPY example.txt /opt/example.txt
 ---> 75a3075270e4
Step 7/7 : CMD python3 -c "print('hello container')"
 ---> Running in 1b240fb49f12
Removing intermediate container 1b240fb49f12
 ---> 4b6ed4cdb37d
Successfully built 4b6ed4cdb37d
Successfully tagged teym88/python3:v2
  • docker run container
root@k8smaster:~/micro# docker run teym88/python3
hello container

2 构建微服务

2.1 服务概述

编写一个获取时间的python程序,使用alpine作为基础image,将python程序copy到image里面运行,再将python程序获取到的信息写在index.html文件,使用nginx容器暴露出来

2.2 步骤

2.2.1 编写python程序

代码如下

root@k8smaster:~/micro# cat getDate.py
#!/usr/bin/python
# coding=utf-8

import subprocess 
import time

def getDate():
    while 1 > 0:
        #print (222)
        cmd = "date > /date/index.html" 
        weather_html = subprocess.Popen(cmd,stderr=subprocess.PIPE, stdout=subprocess.PIPE, stdin=subprocess.PIPE,shell=True)
        time.sleep(5)

getDate()

实现每隔5秒在/date路径下更新一次index.html文件

2.2.2 编写DockerFile

如下

root@k8smaster:~/micro# cat Dockerfile.date
FROM alpine
RUN echo "https://mirrors.aliyun.com/alpine/v3.6/main/" > /etc/apk/repositories
RUN apk update
#install python3 
RUN apk add python3
RUN mkdir date
COPY getDate.py /getDate.py
CMD ["sh", "-c", "python3 /getDate.py"]

2.2.3 Build docker image

image tag 为 teym88/getdate:v1.0

root@k8smaster:~/micro# docker build -f Dockerfile.date --tag teym88/getdate:v1.0 .
Sending build context to Docker daemon  3.876MB
Step 1/7 : FROM alpine
 ---> e50c909a8df2
Step 2/7 : RUN echo "https://mirrors.aliyun.com/alpine/v3.6/main/" > /etc/apk/repositories
 ---> Using cache
 ---> 797e5a5dd8c0
Step 3/7 : RUN apk update
 ---> Using cache
 ---> e1d216887e57
Step 4/7 : RUN apk add python3
 ---> Using cache
 ---> fd213d512afc
Step 5/7 : RUN mkdir date
 ---> Running in 404d72b06c01
Removing intermediate container 404d72b06c01
 ---> f775f5ef1464
Step 6/7 : COPY getDate.py /getDate.py
 ---> b728d37af576
Step 7/7 : CMD ["sh", "-c", "python3 /getDate.py"]
 ---> Running in db3282301316
Removing intermediate container db3282301316
 ---> 7d31bfea0567
Successfully built 7d31bfea0567
Successfully tagged teym88/getdate:v1.0

2.2.4 创建local persistent volume claim

2.2.4.1 创建pv

编写一个yaml文件

root@k8smaster:~# cat localpv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-local
spec:
  storageClassName: local-pv
  capacity:
    storage: 50Mi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /mnt/data

执行

root@k8smaster:~# kubectl apply -f localpv.yaml
persistentvolume/pv-local created

2.2.4.2 创建pvc

编写一个yaml文件

root@k8smaster:~# cat localpvc.yaml 
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-local
spec:
  storageClassName: local-pv
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Mi

执行

root@k8smaster:~# kubectl apply -f localpvc.yaml
persistentvolumeclaim/pvc-local created

2.2.4.3 检查pvc

root@k8smaster:~# kubectl get pvc
NAME        STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
pvc-local   Bound    pv-local                                   50Mi       RWO            local-pv       4s

2.2.5 编写yaml文件

如下,使用刚刚创建的persistent volume claim pvc-local,挂载给两个容器,便于共享数据,第26行定义的就是使用teym88/getdate:v1.0 image的 container,运行之后会生成一个index.html文件在persistent volume挂载的/date路径下,同时第18行运行的是nginx容器,将persistent volume挂载到/usr/share/nginx/html路径下,所以/usr/share/nginx/html路径也存在index.html文件

root@k8smaster:~# cat getDateDep.yaml -n
     1  apiVersion: apps/v1
     2  kind: Deployment
     3  metadata:
     4    name: ng1
     5  spec:
     6    replicas: 1
     7    strategy:
     8      type: Recreate
     9    selector:
    10      matchLabels:
    11        run: ng1
    12    template:
    13      metadata:
    14        labels:
    15          run: ng1
    16      spec:
    17        nodeName: k8smaster
    18        containers:
    19        - name: ng1
    20          image: nginx
    21          ports:
    22          - containerPort: 80
    23          volumeMounts:
    24          - mountPath: /usr/share/nginx/html      
    25            name: local-pv
    26        - name: ubt
    27          image: teym88/getdate:v1.0
    28          #command: ["/bin/bash", "-ce", 'while true; do echo | date > /mnt/data/index.html; sleep 5; done']
    29      #command: ["/bin/bash", "-ce", "tail -f /dev/null", 'while true; do echo | date > /mnt/data/index.html && sleep 5; done']
    30          volumeMounts:
    31          - name: local-pv
    32            mountPath: /date
    33        volumes:
    34        - name: local-pv
    35          persistentVolumeClaim:
    36            claimName: pvc-local

2.2.6 创建Deployment并expose service

root@k8smaster:~# kubectl apply -f getDateDep.yaml
deployment.apps/ng1 created
root@k8smaster:~# kubectl expose deployment ng1 --port=80 --type=NodePort     
service/ng1 exposed
root@k8smaster:~# kubectl get all                                        
NAME                       READY   STATUS    RESTARTS   AGE
pod/ng1-566b48b9b8-ml6n6   2/2     Running   0          39s
pod/ngxpodwithhp           1/1     Running   0          4d22h

NAME                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
service/kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        12d
service/ng1          NodePort    10.103.114.205   <none>        80:32217/TCP   7s

NAME                  READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/ng1   1/1     1            1           39s

NAME                             DESIRED   CURRENT   READY   AGE
replicaset.apps/ng1-566b48b9b8   1         1         1       39s

2.3 验证服务

root@k8smaster:~# curl 10.203.1.90:32217
Mon Feb  1 07:54:31 UTC 2021
root@k8smaster:~# curl 10.203.1.90:32217
Mon Feb  1 07:54:31 UTC 2021
root@k8smaster:~# curl 10.203.1.90:32217
Mon Feb  1 07:54:31 UTC 2021
root@k8smaster:~# curl 10.203.1.90:32217
Mon Feb  1 07:54:31 UTC 2021
root@k8smaster:~# curl 10.203.1.90:32217
Mon Feb  1 07:54:36 UTC 2021
root@k8smaster:~# curl 10.203.1.90:32217
Mon Feb  1 07:54:36 UTC 2021
root@k8smaster:~# curl 10.203.1.90:32217
Mon Feb  1 07:54:36 UTC 2021

相关文章

网友评论

    本文标题:使用Docker+Kubernetes运行python程序

    本文链接:https://www.haomeiwen.com/subject/dvnjtltx.html