k8s的配置文件总结

在 Kubernetes 中,配置文件 是定义集群资源的核心,通常以 YAML 或 JSON 格式编写。以下是 Kubernetes 中关键的配置文件类型及其作用:


1. 核心工作负载配置

(1) Deployment

用途 :定义无状态应用的 Pod 副本管理策略(滚动更新、回滚、扩缩容)。

核心字段

yaml 复制代码
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: app
        image: nginx:1.21
        ports:
        - containerPort: 80
(2) StatefulSet

用途 :管理有状态应用(如数据库),提供稳定的网络标识和持久化存储。

关键字段volumeClaimTemplates(自动创建 PVC)。

yaml 复制代码
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql
spec:
  serviceName: mysql
  replicas: 3
  volumeClaimTemplates:
  - metadata:
      name: data
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 10Gi
(3) DaemonSet

用途:在集群每个节点上运行一个 Pod(如日志收集、节点监控)。

yaml 复制代码
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: fluentd
spec:
  selector:
    matchLabels:
      name: fluentd
  template:
    metadata:
      labels:
        name: fluentd
    spec:
      containers:
      - name: fluentd
        image: fluentd:latest

2. 服务与网络配置

(1) Service

用途 :暴露 Pod 为网络服务,支持负载均衡。

类型ClusterIP(默认)、NodePortLoadBalancer

yaml 复制代码
apiVersion: v1
kind: Service
metadata:
  name: web-service
spec:
  type: LoadBalancer
  ports:
  - port: 80
    targetPort: 8080
  selector:
    app: web-app
(2) Ingress

用途:定义 HTTP/HTTPS 路由规则(如域名、SSL 证书)。

yaml 复制代码
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: example.com
    http:
      paths:
      - path: /app
        pathType: Prefix
        backend:
          service:
            name: app-service
            port:
              number: 80

3. 配置与存储

(1) ConfigMap

用途:存储非敏感配置(环境变量、配置文件)。

yaml 复制代码
apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  app.properties: |
    server.port=8080
    logging.level=INFO
(2) Secret

用途:存储敏感数据(密码、Token),需 Base64 编码。

yaml 复制代码
apiVersion: v1
kind: Secret
metadata:
  name: db-secret
type: Opaque
data:
  username: dXNlcm5hbWU=  # "username" 的 Base64
  password: cGFzc3dvcmQ=  # "password" 的 Base64
(3) PersistentVolume (PV) / PersistentVolumeClaim (PVC)

PV :定义集群存储资源(如 NFS、云存储)。

PVC:应用对存储资源的请求。

yaml 复制代码
# PV 示例(NFS)
apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs-pv
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany
  nfs:
    server: 10.0.0.1
    path: /data/nfs

# PVC 示例
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: data-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi

4. 权限与安全

(1) ServiceAccount

用途:为 Pod 分配身份,用于 API 访问权限控制。

yaml 复制代码
apiVersion: v1
kind: ServiceAccount
metadata:
  name: my-serviceaccount
(2) Role / ClusterRole

Role :定义命名空间内的权限。

ClusterRole:定义集群级别的权限。

yaml 复制代码
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: pod-reader
rules:
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["get", "list"]
(3) RoleBinding / ClusterRoleBinding

用途:将 Role/ClusterRole 绑定到 ServiceAccount 或用户。

yaml 复制代码
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: read-pods
subjects:
- kind: ServiceAccount
  name: my-serviceaccount
roleRef:
  kind: Role
  name: pod-reader
  apiGroup: rbac.authorization.k8s.io

5. 自动扩缩容与任务调度

(1) HorizontalPodAutoscaler (HPA)

用途:根据 CPU/内存使用率自动扩缩容 Pod。

yaml 复制代码
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: app-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: my-app
  minReplicas: 2
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 50
(2) CronJob

用途:定时执行任务(如备份、批量处理)。

yaml 复制代码
apiVersion: batch/v1
kind: CronJob
metadata:
  name: backup-job
spec:
  schedule: "0 0 * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: backup
            image: busybox
            command: ["/bin/sh", "-c", "tar czf /backup/data.tar.gz /data"]
          restartPolicy: OnFailure

6. 集群级配置

(1) Namespace

用途:逻辑隔离资源(如开发、测试、生产环境)。

yaml 复制代码
apiVersion: v1
kind: Namespace
metadata:
  name: production
(2) CustomResourceDefinition (CRD)

用途:扩展 Kubernetes API,定义自定义资源。

yaml 复制代码
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: myresources.example.com
spec:
  group: example.com
  versions:
    - name: v1
      served: true
      storage: true
      schema: {...}
  scope: Namespaced
  names:
    plural: myresources
    singular: myresource
    kind: MyResource

配置文件管理最佳实践

  1. 版本控制
    • 将 YAML 文件存储在 Git 仓库中,使用分支或目录区分环境(dev/prod)。

  2. 模板化
    • 使用 HelmKustomize 管理多环境配置,避免硬编码。
    • Helm 示例(values.yaml):

    yaml 复制代码
    replicaCount: 3
    image:
      repository: nginx
      tag: 1.21
  3. 安全
    • 使用 SealedSecretVault 加密敏感数据,避免直接存储明文。

  4. 验证配置

    bash 复制代码
    # 检查语法错误
    kubectl apply -f deployment.yaml --dry-run=client
    
    # 查看生成的配置(Kustomize)
    kustomize build overlays/prod

总结

核心配置文件 :Deployment、Service、ConfigMap、Secret、PersistentVolumeClaim。

高级配置 :Ingress、HPA、RBAC、CronJob。

集群管理 :Namespace、CRD。

工具推荐:Helm 用于应用打包,Kustomize 用于多环境配置,Prometheus 用于监控。

相关推荐
chuanauc2 小时前
Kubernets K8s 学习
java·学习·kubernetes
小张是铁粉2 小时前
docker学习二天之镜像操作与容器操作
学习·docker·容器
烟雨书信2 小时前
Docker文件操作、数据卷、挂载
运维·docker·容器
IT成长日记3 小时前
【Docker基础】Docker数据卷管理:docker volume prune及其参数详解
运维·docker·容器·volume·prune
这儿有一堆花3 小时前
Docker编译环境搭建与开发实战指南
运维·docker·容器
LuckyLay3 小时前
Compose 高级用法详解——AI教你学Docker
运维·docker·容器
Uluoyu3 小时前
redisSearch docker安装
运维·redis·docker·容器
IT成长日记7 小时前
【Docker基础】Docker数据持久化与卷(Volume)介绍
运维·docker·容器·数据持久化·volume·
疯子的模样11 小时前
Docker 安装 Neo4j 保姆级教程
docker·容器·neo4j
虚伪的空想家12 小时前
rook-ceph配置dashboard代理无法访问
ceph·云原生·k8s·存储·rook