码迷,mamicode.com
首页 > 其他好文 > 详细

k8s:py项目发布完整流程

时间:2020-06-18 21:39:50      阅读:71      评论:0      收藏:0      [点我收藏+]

标签:read   技术   python   ingress   end   图片   logs   add   stat   

k8s:py项目发布流程

1. 编写Dockerfile
# cat Dockerfile 
FROM python:3.6-slim
USER root
RUN apt-get update && apt-get install gcc -y &&     apt-get clean &&     rm -rf /var/lib/apt/lists/*
ADD . /app
WORKDIR /app
RUN pip install -r requirement.txt
EXPOSE 8000
CMD [ "uvicorn", "main:app","--host", "0.0.0.0", "--port", "8000" ]
2. 验证Docker镜像
3. 编写Pod资源清单

要学会借鉴官网!!!,演示一遍

首先要问清楚业务是有状态还是无状态,无状态就选deployment,有状态就选statefulset。我这里是个小程序,无状态应用。
技术图片
Kubernetes版本变化很快,搞k8s一定要学会看官网。

官方案例
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
改:是要标签对的上就OK了
##########################################################################
#Author:                     zisefeizhu
#QQ:                         2********0
#Date:                       2020-06-16
#FileName:                   delpoyment.yaml
#URL:                        https://www.cnblogs.com/zisefeizhu/
#Description:                The test script
#Copyright (C):              2020 All rights reserved
###########################################################################
apiVersion: apps/v1
kind: Deployment
metadata:
  name: businesscard-deployment
spec:
  selector:
    matchLabels:
      app: businesscard
  replicas: 2 # tells deployment to run 2 pods matching the template
  template:
    metadata:
      labels:
        app: businesscard
    spec:
      imagePullSecrets:
      - name: business-card 
      containers:
      - name: businesscard
        imagePullPolicy: "IfNotPresent"
        image: xxxxxx/business-card:v1  
        ports:
        - containerPort: 8000
4. 编写Service清单
官方案例
apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: MyApp
  ports:
    - protocol: TCP
      port: 80
      targetPort: 9376
改:标签对的上就OK!
cat service.yaml 
##########################################################################
#Author:                     zisefeizhu
#QQ:                         2********0
#Date:                       2020-06-16
#FileName:                   service.yaml
#URL:                        https://www.cnblogs.com/zisefeizhu/
#Description:                The test script
#Copyright (C):              2020 All rights reserved
###########################################################################
apiVersion: v1
kind: Service
metadata:
  name: businesscard
spec:
  #type: NodePort
  selector:
    app: businesscard
  ports:
    - protocol: TCP
      port: 8000

5. 编写ingress清单

这块就要根据不同的ingress-controller 编写不同的ingress,比如nginx、traefik 等

##########################################################################
#Author:                     zisefeizhu
#QQ:                         2********0
#Date:                       2020-06-17
#FileName:                   nginx-ingress.yaml
#URL:                        https://www.cnblogs.com/zisefeizhu/
#Description:                The test script
#Copyright (C):              2020 All rights reserved
###########################################################################
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: businesscard
spec:
 # tls:
 # - hosts:
 #   - businesscard.stage.realibox.com
 #   secretName: businesscard-ingress-secret
  rules:
  - host: card.linux.com
    http:
      paths:
      - path: /
        backend:
          serviceName: businesscard
          servicePort: 8000
6. 验证

资源pod、svc、ingress验证

# kubectl get pods,svc,ingress 
NAME                                          READY   STATUS    RESTARTS   AGE
pod/businesscard-deployment-f69768dd9-zc56p   1/1     Running   1          26h
pod/nginx-deployment-6b474476c4-4644v         1/1     Running   0          4h28m
pod/nginx-deployment-6b474476c4-b2dwh         1/1     Running   0          4h28m
pod/nginx-deployment-6b474476c4-hdsgv         1/1     Running   0          4h28m

NAME                   TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
service/businesscard   ClusterIP   10.68.32.93     <none>        8000/TCP   25h
service/kubernetes     ClusterIP   10.68.0.1       <none>        443/TCP    3d11h
service/nginx          ClusterIP   10.68.185.184   <none>        80/TCP     4h28m

NAME                              CLASS    HOSTS              ADDRESS   PORTS   AGE
ingress.extensions/businesscard   <none>   card.linux.com               80      6h11m
ingress.extensions/ingress-test   <none>   test.ingress.com             80      24h
ingress.extensions/nginx          <none>   nginx.linux.com              80      4h28m
客户端验证

技术图片
验证成功。

k8s:py项目发布完整流程

标签:read   技术   python   ingress   end   图片   logs   add   stat   

原文地址:https://www.cnblogs.com/zisefeizhu/p/13159625.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!