k8s模版
apiVersion: v1
kind: Pod
metadata:
name: "MYAPP"
namespace: default
labels:
app: "MYAPP"
spec:
containers:
- name: MYAPP
image: "debian-slim:latest"
resources:
limits:
cpu: 200m
memory: 500Mi
requests:
cpu: 100m
memory: 200Mi
env:
- name: DB_HOST
value: '123456'
valueFrom:
configMapKeyRef:
name: MYAPP
key: DB_HOST
#镜像拉取策略(Always-默认值下载优先,Never-仅使用本地,IfNotPresent-本地优先,其次下载)
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
name: http
#端口协议 TCP和UDP 默认TCP
protocol: TCP
volumeMounts:
#和刚才 下面的volimes中定义的一样 名字
- name: localtime
#存放在容器中的路径
mountPath: /etc/localtime
#数据卷
volumes:
- name: localtime
hostPath:
path: /usr/share/zoneinfo/Asia/Shanghai
#目录不存在,就创建,存在就不创建
type: DirectoryOrCreate
#容器重启(A1ways-默认值始终重启,0nFailure-异常停止才重启,Nerver-始终不重启)
restartPolicy: Always
apiVersion: apps/v1
kind: Deployment
metadata:
name: MYAPP
namespace: default
labels:
app: MYAPP
spec:
selector:
matchLabels:
app: MYAPP
replicas: 1
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
labels:
app: MYAPP
spec:
# initContainers:
# Init containers are exactly like regular containers, except:
# - Init containers always run to completion.
# - Each init container must complete successfully before the next one starts.
containers:
- name: MYAPP
image: MYAPP:latest
resources:
requests:
cpu: 100m
memory: 100Mi
limits:
cpu: 100m
memory: 100Mi
livenessProbe:
tcpSocket:
port: 80
initialDelaySeconds: 5
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 3
periodSeconds: 10
readinessProbe:
httpGet:
path: /_status/healthz
port: 80
initialDelaySeconds: 5
timeoutSeconds: 2
successThreshold: 1
failureThreshold: 3
periodSeconds: 10
env:
- name: DB_HOST
valueFrom:
configMapKeyRef:
name: MYAPP
key: DB_HOST
ports:
- containerPort: 80
name: MYAPP
volumeMounts:
- name: localtime
mountPath: /etc/localtime
volumes:
- name: localtime
hostPath:
path: /usr/share/zoneinfo/Asia/Shanghai
restartPolicy: Always
apiVersion: batch/v1
kind: Job
metadata:
name: MYAPP
namespace: default
labels:
app: MYAPP
spec:
template:
metadata:
name: MYAPP
labels:
app: MYAPP
spec:
containers:
- name: MYAPP
image: python:3.7.6-alpine3.10
command: ['sh', '-c', 'python3 manage.py makemigrations && python3 manage.py migrate']
env:
- name: ENV_NAME
value: ENV_VALUE
volumeMounts:
- name: localtime
mountPath: /etc/localtime
volumes:
- name: localtime
hostPath:
path: /usr/share/zoneinfo/Asia/Shanghai
restartPolicy: OnFailure
dnsPolicy: ClusterFirst
apiVersion: v1
kind: Service
metadata:
name: MYAPP
namespace: default
spec:
selector:
app: MYAPP
type: ClusterIP
sessionAffinity: None
sessionAffinityConfig:
clientIP:
timeoutSeconds: 10800
ports:
- name: MYAPP
protocol: TCP
port:
targetPort:
# If you set the `spec.type` field to `NodePort` and you want a specific port number,
# you can specify a value in the `spec.ports[*].nodePort` field.
nodePort:
以下是逐行详细解释你提供的 Kubernetes Deployment 配置文件:
---
### **1. API 版本声明**
```yaml
apiVersion: apps/v1
```
- **作用**:定义使用的 Kubernetes API 版本。
- **说明**:`apps/v1` 是 Deployment 的标准 API 版本,确保兼容 Kubernetes 1.9+ 版本。
---
### **2. 资源类型定义**
```yaml
kind: Deployment
```
- **作用**:声明这是一个 `Deployment` 资源。
- **说明**:Deployment 用于管理 Pod 的副本集(ReplicaSet),支持滚动更新和回滚。
---
### **3. 元数据定义**
```yaml
metadata:
name: MYAPP
namespace: default
labels:
app: MYAPP
```
- **`name`**: Deployment 的名称(需唯一)。
- **`namespace`**: 部署的目标命名空间(这里是 `default`)。
- **`labels`**: 标签用于资源标识(标签选择器 `selector` 会匹配此标签)。
---
### **4. Deployment 规格**
```yaml
spec:
selector:
matchLabels:
app: MYAPP
```
- **`selector`**: 定义 Deployment 如何查找要管理的 Pod。
- **`matchLabels`**: 必须与 Pod 模板中的 `labels` 匹配(此处匹配 `app: MYAPP`)。
---
### **5. 副本策略**
```yaml
replicas: 1
```
- **作用**:指定 Pod 的副本数量(此处为 1 个 Pod)。
- **说明**:设置为 `1` 表示单副本运行,适合测试环境;生产环境通常需要更高副本数。
---
### **6. 更新策略**
```yaml
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
```
- **`rollingUpdate`**: 滚动更新策略。
- **`maxSurge`**: 更新期间允许超过副本数的最大 Pod 比例(此处允许最多增加 25% 的 Pod)。
- **`maxUnavailable`**: 更新期间允许不可用的 Pod 比例(此处最多 25% 的 Pod 不可用)。
- **`type`**: 更新类型(`RollingUpdate` 表示滚动更新,另一种是 `Recreate` 直接重建)。
---
### **7. Pod 模板**
```yaml
template:
metadata:
labels:
app: MYAPP
```
- **`template`**: 定义 Pod 的模板。
- **`metadata.labels`**: 给 Pod 打标签(必须与 `spec.selector.matchLabels` 匹配)。
---
### **8. Pod 规格**
```yaml
spec:
containers:
- name: MYAPP
image: MYAPP:latest
```
- **`containers`**: 定义 Pod 中的容器列表。
- **`name`**: 容器名称。
- **`image`**: 容器镜像(此处为 `MYAPP:latest`,需替换为实际镜像地址)。
---
### **9. 资源限制**
```yaml
resources:
requests:
cpu: 100m
memory: 100Mi
limits:
cpu: 100m
memory: 100Mi
```
- **`resources`**: 定义容器的资源请求和限制。
- **`requests`**: 容器启动所需的最小资源(`100m` 表示 0.1 核 CPU,`100Mi` 表示约 104MB 内存)。
- **`limits`**: 容器允许使用的最大资源(此处与 `requests` 相同,可能导致资源竞争时容器被限制)。
- **建议**:生产环境通常设置 `limits` 略高于 `requests`。
---
### **10. 存活探针 (Liveness Probe)**
```yaml
livenessProbe:
tcpSocket:
port: 80
initialDelaySeconds: 5
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 3
periodSeconds: 10
```
- **作用**:检查容器是否存活。
- **`tcpSocket`**: 通过 TCP 检查端口 `80` 是否开放。
- **参数**:
- `initialDelaySeconds`: 容器启动后等待 5 秒开始探测。
- `timeoutSeconds`: 探测超时时间为 5 秒。
- `failureThreshold`: 连续 3 次探测失败后重启容器。
- `periodSeconds`: 每 10 秒探测一次。
---
### **11. 就绪探针 (Readiness Probe)**
```yaml
readinessProbe:
httpGet:
path: /_status/healthz
port: 80
initialDelaySeconds: 5
timeoutSeconds: 2
successThreshold: 1
failureThreshold: 3
periodSeconds: 10
```
- **作用**:检查容器是否准备好接收流量。
- **`httpGet`**: 发送 HTTP GET 请求到 `/_status/healthz` 路径(需确保应用提供此端点)。
- **参数**:
- `timeoutSeconds`: 探测超时时间缩短为 2 秒(快速失败)。
- 其他参数与 `livenessProbe` 类似。
---
### **12. 环境变量**
```yaml
env:
- name: DB_HOST
valueFrom:
configMapKeyRef:
name: MYAPP
key: DB_HOST
```
- **`env`**: 定义容器环境变量。
- **`valueFrom.configMapKeyRef`**: 从名为 `MYAPP` 的 ConfigMap 中获取 `DB_HOST` 的值。
- **注意**:需提前创建名为 `MYAPP` 的 ConfigMap。
---
### **13. 容器端口**
```yaml
ports:
- containerPort: 80
name: MYAPP
```
- **`containerPort`**: 容器监听的端口(此处为 `80`)。
- **`name`**: 端口名称(用于 Service 中引用)。
---
### **14. 卷挂载**
```yaml
volumeMounts:
- name: localtime
mountPath: /etc/localtime
```
- **`volumeMounts`**: 将卷挂载到容器内。
- **`mountPath`**: 将宿主机时区文件挂载到容器的 `/etc/localtime`(同步时区)。
---
### **15. 卷定义**
```yaml
volumes:
- name: localtime
hostPath:
path: /usr/share/zoneinfo/Asia/Shanghai
```
- **`volumes`**: 定义 Pod 使用的存储卷。
- **`hostPath`**: 使用宿主机的文件路径(此处挂载时区文件)。
- **注意**:`hostPath` 存在安全风险,生产环境建议使用其他卷类型(如 `ConfigMap`)。
---
### **16. 重启策略**
```yaml
restartPolicy: Always
```
- **作用**:定义容器失败时的重启策略。
- **说明**:`Always` 表示总是重启(适用于 Pod 内容器崩溃),但此字段在 Pod 模板中应省略,因为 Deployment 默认使用 `Always`。
---
### **总结与改进建议**
1. **镜像名称**:`image: MYAPP:latest` 需替换为实际镜像地址(如 `registry.example.com/myapp:v1`)。
2. **资源限制**:建议 `limits` 适当高于 `requests`(如 `cpu: 200m`)。
3. **ConfigMap 依赖**:确保名为 `MYAPP` 的 ConfigMap 已创建。
4. **时区挂载**:如果容器基础镜像已配置时区,可省略 `hostPath` 挂载。
5. **存活/就绪探针**:确保 `/_status/healthz` 端点存在并返回正确的状态码。
6. **多副本部署**:生产环境建议 `replicas: 2` 或更高。
浙公网安备 33010602011771号