K8S 部署服务 -- 编排应用
Johann Li | October 06, 2022
直接上原始文件和注释,这里是基于 K3S 的,存储直接使用 local storage
,然后使用默认配置的 traefik 作为 ingress。
这个文件直接使用 kubectl apply -f -
将文件作为标准输入即可配置。
# 创建一个 Kubernetes 的 Namespace
apiVersion: v1
kind: Namespace
metadata:
name: detection
---
# 创建一个 PV
apiVersion: v1
kind: PersistentVolume
metadata:
name: infer-worker
labels:
type: local
app: infer-worker
namespace: detection
spec:
storageClassName: local-path
capacity:
storage: 10Gi
volumeMode: Filesystem
accessModes:
- ReadWriteMany
hostPath:
# 指定了本机对应的存储位置
path: /raid/model-weights
---
# 创建了一个 PVC
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: infer-worker
labels:
app: infer-worker
namespace: detection
spec:
storageClassName: local-path
accessModes:
- ReadWriteMany
resources:
requests:
storage: 10Gi
---
# 创建了一个 ConfigMap(http-server)
apiVersion: v1
kind: ConfigMap
metadata:
name: http-server
labels:
app: http-server
namespace: detection
data:
# 指定的是环境变量
# infer worker 对应的 URL
WORKER_URL: tcp://infer-worker:5001 # 这里指定的域名是按照 kubernetes 的规则来的
---
# 创建了一个 ConfigMap (infer-worker)
apiVersion: v1
kind: ConfigMap
metadata:
name: infer-worker
labels:
app: infer-worker
namespace: detection
data:
# 指定环境变量
# 指定 FTP 连接 IP(域名)
FTP_CONNECT_IP: ftp.server
# 指定 权重文件位置(容器中的路径)
WEIGHT_FILE: /model/best.pt
---
# 创建了一个 http-server 的 部署(deployment)
apiVersion: apps/v1
kind: Deployment
metadata:
name: http-server
namespace: detection
spec:
selector:
matchLabels:
app: http-server
replicas: 1 # POD 实例个数
strategy: # 创建策略
type: Recreate
template: # POD 模板
metadata:
labels:
app: http-server
spec:
containers: # 容器参数
- name: http-server # 容器名称
image: http-server # 镜像名称
args: # 命令行参数
- '--bind=0.0.0.0:8080' # 指定绑定 HTTP 的位置
- '--timeout=1000' # 防止超时
imagePullPolicy: "Never" # 组织拉取镜像(离线部署,如果是在线部署,可以选择 Always)
envFrom: # 设置环境变量
- configMapRef: # 并从 config-map http-server 中获取,上面 data 中的内容就会变成环境变量和相关的值。
name: http-server
ports: # 容器导出的端口
- containerPort: 8080
---
# 创建了一个 http-server 的 部署(deployment) (同上)
apiVersion: apps/v1
kind: Deployment
metadata:
name: infer-worker
namespace: detection
spec:
selector:
matchLabels:
app: infer-worker
replicas: 1
strategy:
type: Recreate
template:
metadata:
labels:
app: infer-worker
spec:
hostAliases:
- hostnames: [ "ftp.server" ]
ip: "192.168.1.100"
containers:
- name: infer-worker
image: infer-worker
imagePullPolicy: "Never"
resources: # 配置环境资源限制
limits:
nvidia.com/gpu: 1 # 分配一个 GPU 来使用
ports:
- containerPort: 5001
volumeMounts: # 将 上面创建的 PVC 绑定并挂载到容器的 /model 文件夹中
- mountPath: /model
name: model
volumes: # 声明的挂载的卷,采用上面创建的 PVC
- name: model
persistentVolumeClaim:
claimName: infer-worker
---
# 配置 http-server 的 Service
apiVersion: v1
kind: Service
metadata:
name: http-server
labels:
app: http-server
namespace: detection
spec:
selector:
app: http-server
ports: # 指定端口, 从 8080 绑定到 8080
- port: 8080
targetPort: 8080
---
# 配置 infer-worker 的 Service
apiVersion: v1
kind: Service
metadata:
name: infer-worker
labels:
app: infer-worker
namespace: detection
spec:
selector:
app: infer-worker
ports:
- port: 5001 # 指定端口,从 5001 绑定到 5001
targetPort: 5001
---
# 配置 ingress 暴漏 http 服务
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: proxy
namespace: detection
spec:
rules:
- host: detection # http 服务的域名
http:
paths:
- path: /detect # http 服务的路径
pathType: Exact # http url 的绑定方式
backend:
service: # 指定转发到的 server 和对应的端口
name: http-server
port:
number: 8080