K8s 配置与网络
应用上 K8s 后,配置管理和网络策略是两个绕不开的话题。ConfigMap / Secret 解决"配置怎么注入",Ingress / NetworkPolicy 解决"流量怎么进来、怎么隔离"。
一、ConfigMap:非敏感配置
ConfigMap 把配置从镜像里剥离出来,做到同一个镜像跑不同环境。
1. 创建
yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
APP_PORT: "8080"
LOG_LEVEL: "info"
application.yml: |
spring:
datasource:
url: jdbc:mysql://mysql-svc:3306/demobash
# 或者命令行快速创建
kubectl create configmap app-config \
--from-literal=APP_PORT=8080 \
--from-file=application.yml2. 注入方式一:环境变量
yaml
env:
- name: APP_PORT
valueFrom:
configMapKeyRef:
name: app-config
key: APP_PORT
# 或者一次性全部注入
envFrom:
- configMapRef:
name: app-config3. 注入方式二:挂载为文件
yaml
volumes:
- name: config-vol
configMap:
name: app-config
items:
- key: application.yml
path: application.yml
containers:
- volumeMounts:
- name: config-vol
mountPath: /app/configConfigMap 更新后,环境变量注入需要重启 Pod 才生效;Volume 挂载的文件会在 ~1 分钟内自动热更新。
二、Secret:敏感配置
和 ConfigMap 几乎一样,区别:值是 base64 编码(注意:这不是加密,只是混淆,生产应结合外部 Secret 管理系统)。
创建
bash
kubectl create secret generic db-secret \
--from-literal=DB_PASSWORD=mysecretpwd \
--from-literal=JWT_SECRET=supersecretyaml
apiVersion: v1
kind: Secret
metadata:
name: db-secret
type: Opaque
data:
DB_PASSWORD: bXlzZWNyZXRwd2Q= # base64
JWT_SECRET: c3VwZXJzZWNyZXQ=注入
yaml
env:
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: db-secret
key: DB_PASSWORDSecret 类型
| 类型 | 用途 |
|---|---|
Opaque | 通用键值(最常用) |
kubernetes.io/dockerconfigjson | 私有镜像仓库凭证 |
kubernetes.io/tls | TLS 证书 + 私钥 |
kubernetes.io/service-account-token | ServiceAccount Token |
生产建议:Secret 不要
git commit。用 Sealed Secrets、External Secrets Operator(对接 AWS SM / Vault)管理。
三、Ingress:HTTP 流量入口
Service 只做四层(TCP/UDP)负载均衡,Ingress 做七层(HTTP/HTTPS)路由:根据 Host 和 Path 分发到不同 Service。
前提:安装 Ingress Controller
bash
# 最常用 NGINX Ingress Controller
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.10.1/deploy/static/provider/cloud/deploy.yaml基本路由
yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: app-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
ingressClassName: nginx
rules:
- host: api.example.com
http:
paths:
- path: /v1
pathType: Prefix
backend:
service:
name: springboot-svc
port:
number: 8080
- path: /v2
pathType: Prefix
backend:
service:
name: elysia-svc
port:
number: 3000HTTPS(TLS 终止)
yaml
spec:
tls:
- hosts:
- api.example.com
secretName: tls-secret # kubectl create secret tls tls-secret --cert=... --key=...
rules:
- host: api.example.com
...常用 Ingress 注解(NGINX):
yaml
annotations:
nginx.ingress.kubernetes.io/proxy-body-size: "10m" # 上传大小
nginx.ingress.kubernetes.io/proxy-connect-timeout: "30"
nginx.ingress.kubernetes.io/rate-limit: "100" # 限流 RPS
nginx.ingress.kubernetes.io/ssl-redirect: "true"
cert-manager.io/cluster-issuer: letsencrypt-prod # 自动申请证书四、NetworkPolicy:Pod 间流量隔离
默认 K8s 里所有 Pod 互通。NetworkPolicy 让你做微隔离:只允许特定 Pod 或命名空间通信。
示例:只允许 app Pod 访问数据库
yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: db-allow-app-only
spec:
podSelector:
matchLabels:
app: mysql # 保护目标:mysql Pod
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
app: springboot # 只允许 springboot Pod 来访
ports:
- protocol: TCP
port: 3306拒绝所有 Ingress(白名单模式起点)
yaml
spec:
podSelector: {} # 匹配所有 Pod
policyTypes:
- Ingress
ingress: [] # 没有 from → 拒绝所有入站NetworkPolicy 需要 CNI 插件支持(Calico、Cilium、Weave)。flannel 不支持。
五、资源限制与 QoS
不设 CPU/内存限制,一个应用 OOM 会把整个节点拖垮。
yaml
resources:
requests: # 调度依据:节点至少要有这么多
cpu: "250m" # 0.25 核
memory: "256Mi"
limits: # 硬上限:超出会被 OOM Kill(内存)或 throttle(CPU)
cpu: "1"
memory: "512Mi"| QoS 级别 | 条件 | OOM 优先级 |
|---|---|---|
| Guaranteed | requests == limits | 最后被杀 |
| Burstable | 有 requests,limits > requests | 中等 |
| BestEffort | 没有 requests / limits | 最先被杀 |
六、HPA:自动水平扩缩容
根据 CPU / 内存 / 自定义指标自动调整 Deployment 副本数。
yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: springboot-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: springboot
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70 # CPU 平均超 70% 就扩容bash
kubectl get hpa
kubectl describe hpa springboot-hpa七、常用 kubectl 配置命令
bash
# 查看 ConfigMap / Secret
kubectl get cm
kubectl describe cm app-config
kubectl get secret db-secret -o yaml
# 查看 Ingress
kubectl get ingress
kubectl describe ingress app-ingress
# 查看 NetworkPolicy
kubectl get networkpolicy
# 查看资源用量(需要 metrics-server)
kubectl top nodes
kubectl top pods总结
| 对象 | 解决什么 |
|---|---|
| ConfigMap | 非敏感配置注入(env / file) |
| Secret | 敏感配置(密码、证书) |
| Ingress | HTTP/HTTPS 七层路由入口 |
| NetworkPolicy | Pod 间网络隔离 |
| Resources | CPU/内存限制,防止资源争抢 |
| HPA | 基于指标自动扩缩容 |
下一章:把 Spring Boot + MySQL + Redis 完整部署到 K8s,把前面所有知识串起来。