224 字
1 分钟
Kubernetes 网络与存储
一、Kubernetes 网络模型
1.1 网络原则
graph LR
A["Pod 1"] -->|同一 Node| B["Pod 2"]
A -->|跨 Node| C["Pod 3"]
style A fill:#87CEEB
style B fill:#87CEEB
style C fill:#90EE90
| 原则 | 说明 |
|---|---|
| Pod 间通信 | 所有 Pod 可以直接通信,无需 NAT |
| Node 与 Pod | Node 可以与所有 Pod 通信,无需 NAT |
| Pod 自身 IP | 每个 Pod 有独立的 IP,不依赖端口映射 |
1.2 CNI 网络插件
# CNI: Container Network Interface# 常见 CNI 插件
# 1. Calico - 网络策略优先# 2. Flannel - 简单 overlay 网络# 3. Cilium - eBPF 驱动# 4. Weave Net - 自动加密二、Service
2.1 Service 类型
# ClusterIP - 内部访问(默认)apiVersion: v1kind: Servicemetadata: name: nginx-clusteripspec: type: ClusterIP selector: app: nginx ports: - port: 80 # Service 端口 targetPort: 80 # Pod 端口 protocol: TCP
---
apiVersion: v1kind: Servicemetadata: name: nginx-nodeportspec: type: NodePort selector: app: nginx ports: - port: 80 targetPort: 80 nodePort: 30080 # 30000-32767
---
apiVersion: v1kind: Servicemetadata: name: nginx-lbspec: type: LoadBalancer selector: app: nginx ports: - port: 80 targetPort: 802.2 Headless Service
# Headless Service - 无负载均衡apiVersion: v1kind: Servicemetadata: name: nginx-headlessspec: clusterIP: None # 关键设置 selector: app: nginx ports: - port: 80
# DNS 行为# 普通 Service: my-svc.namespace.svc.cluster.local -> ClusterIP# Headless: pod-name.my-svc.namespace.svc.cluster.local -> Pod IP2.3 Service 发现
# 环境变量方式(自动注入)# 格式: {SVCNAME}_SERVICE_HOST, {SVCNAME}_SERVICE_PORTNGINX_SERVICE_HOST=10.0.0.100NGINX_SERVICE_PORT=80
# DNS 方式# 格式: <service-name>.<namespace>.svc.cluster.localnginx.default.svc.cluster.local2.4 kube-proxy 模式
# iptables 模式(默认)# 规则匹配,性能随规则增加下降
# IPVS 模式(大规模推荐)# 支持负载均衡算法:rr, wrr, lc, wlc, sh, dh
# 查看模式kubectl get configmap kube-proxy -n kube-system -o yaml三、Ingress
3.1 Ingress 配置
apiVersion: networking.k8s.io/v1kind: Ingressmetadata: name: app-ingress annotations: nginx.ingress.kubernetes.io/rewrite-target: / nginx.ingress.kubernetes.io/ssl-redirect: "true"spec: ingressClassName: nginx tls: - hosts: - app.example.com secretName: app-tls rules: - host: app.example.com http: paths: - path: /api pathType: Prefix backend: service: name: api-service port: number: 8080 - path: /static pathType: Prefix backend: service: name: static-service port: number: 803.2 IngressClass
apiVersion: networking.k8s.io/v1kind: IngressClassmetadata: name: nginx annotations: ingressclass.kubernetes.io/is-default-class: "true"spec: controller: k8s.io/ingress-nginx四、NetworkPolicy
4.1 默认拒绝
# 默认拒绝所有入站流量apiVersion: networking.k8s.io/v1kind: NetworkPolicymetadata: name: default-deny-ingressspec: podSelector: {} policyTypes: - Ingress4.2 允许特定流量
# 允许前端访问后端apiVersion: networking.k8s.io/v1kind: NetworkPolicymetadata: name: allow-frontend-to-backendspec: podSelector: matchLabels: app: backend policyTypes: - Ingress ingress: - from: - podSelector: matchLabels: app: frontend ports: - protocol: TCP port: 8080
---
apiVersion: networking.k8s.io/v1kind: NetworkPolicymetadata: name: allow-dnsspec: podSelector: {} policyTypes: - Egress egress: - to: - namespaceSelector: {} ports: - protocol: UDP port: 53五、存储卷
5.1 卷类型对比
| 类型 | 说明 | 生命周期 |
|---|---|---|
| emptyDir | 临时存储 | 与 Pod 相同 |
| hostPath | 节点文件系统 | 持久 |
| persistentVolumeClaim | 持久存储 | 独立于 Pod |
| configMap | 配置数据 | 可独立更新 |
| secret | 敏感数据 | 安全存储 |
| nfs | 网络文件系统 | 持久 |
5.2 emptyDir
apiVersion: v1kind: Podmetadata: name: testspec: containers: - name: test image: alpine volumeMounts: - name: cache mountPath: /tmp volumes: - name: cache emptyDir: sizeLimit: 100Mi medium: Memory # 内存存储5.3 hostPath
apiVersion: v1kind: Podmetadata: name: testspec: containers: - name: test image: alpine volumeMounts: - name: data mountPath: /data volumes: - name: data hostPath: path: /var/local/data type: DirectoryOrCreate # 不存在则创建5.4 NFS
apiVersion: v1kind: Podmetadata: name: nfs-podspec: containers: - name: app image: alpine volumeMounts: - name: nfs-volume mountPath: /mnt/nfs volumes: - name: nfs-volume nfs: server: nfs-server.example.com path: /share六、持久化存储
6.1 PV 与 PVC
# PersistentVolume (PV) - 管理员创建apiVersion: v1kind: PersistentVolumemetadata: name: pv-1spec: capacity: storage: 10Gi accessModes: - ReadWriteOnce # 单节点读写 # - ReadOnlyMany # 多节点只读 # - ReadWriteMany # 多节点读写 storageClassName: standard persistentVolumeReclaimPolicy: Retain # Retain/Delete/Recycle nfs: server: nfs-server path: /data/pv-1
---
apiVersion: v1kind: PersistentVolumeClaimmetadata: name: pvc-1spec: accessModes: - ReadWriteOnce resources: requests: storage: 5Gi storageClassName: standard6.2 StorageClass
apiVersion: storage.k8s.io/v1kind: StorageClassmetadata: name: fast-ssdprovisioner: pd.csi.storage.gke.io # GCPparameters: type: pd-ssd filesystem: xfsreclaimPolicy: RetainvolumeBindingMode: WaitForFirstConsumer # 延迟绑定allowVolumeExpansion: true6.3 动态存储
# 使用 StorageClass 自动创建 PVapiVersion: v1kind: PersistentVolumeClaimmetadata: name: dynamic-pvcspec: storageClassName: fast-ssd accessModes: - ReadWriteOnce resources: requests: storage: 20Gi七、ConfigMap 与 Secret
7.1 ConfigMap
apiVersion: v1kind: ConfigMapmetadata: name: app-configdata: config.json: | { "database": { "host": "db-service", "port": 5432 } } log-level: "info"
---
apiVersion: v1kind: Podmetadata: name: appspec: containers: - name: app image: app:latest envFrom: - configMapRef: name: app-config env: - name: DB_PASSWORD valueFrom: configMapKeyRef: name: app-config key: log-level7.2 Secret
apiVersion: v1kind: Secretmetadata: name: app-secretstype: Opaquedata: # echo -n "password" | base64 db-password: cGFzc3dvcmQ=stringData: # 自动 Base64 编码 api-key: "my-secret-key"
---
apiVersion: v1kind: Podmetadata: name: appspec: containers: - name: app image: app:latest env: - name: DB_PASSWORD valueFrom: secretKeyRef: name: app-secrets key: db-password volumeMounts: - name: secrets mountPath: /etc/secrets volumes: - name: secrets secret: secretName: app-secrets支持与分享
如果这篇文章对你有帮助,欢迎支持作者或分享给更多人
部分信息可能已经过时
相关文章 智能推荐
1
Kubernetes 调度与资源管理
面试 Kubernetes 调度机制——节点选择、亲和性、反亲和性、污点与容忍、资源配额、LimitRange。
2
Kubernetes 核心架构与组件
面试 Kubernetes 核心架构——Control Plane 组件、Node 组件、Pod 生命周期、Deployment 管理。
3
容器化面试题
面试 面试中常见的容器化技术题目——Docker 命名空间、cgroup 隔离、Kubernetes 调度机制等知识点整理。
4
Kubernetes 安全与 RBAC
面试 Kubernetes 安全——RBAC 权限模型、Security Context、Pod Security Standards、网络策略、Secret 管理。
5
计算机基础面试题
面试 面试中常见的计算机基础题目——网络七层模型、进程与线程、内存管理、系统调用等核心知识点整理。






