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 - ...
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
客戶端驗證
驗證成功。