# MongoDB 分片集群最小示例（ConfigRS + 2 个 ShardRS + Mongos）
# 使用方式：
# 1) 先 apply 本文件
# 2) 按文档执行 rs.initiate / sh.addShard / sh.enableSharding
# 注意：本示例侧重结构演示，生产环境请补齐资源限制、反亲和、PDB、备份与监控。
---
apiVersion: v1
kind: Service
metadata:
  name: cfg-svc
  namespace: juwan
spec:
  clusterIP: None
  selector:
    app: mongo-cfg
  ports:
    - name: mongo
      port: 27019
      targetPort: 27019
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: cfg
  namespace: juwan
spec:
  serviceName: cfg-svc
  replicas: 3
  selector:
    matchLabels:
      app: mongo-cfg
  template:
    metadata:
      labels:
        app: mongo-cfg
    spec:
      containers:
        - name: mongod
          image: mongo:7.0
          args:
            [
              "--configsvr",
              "--replSet",
              "cfg-rs",
              "--port",
              "27019",
              "--bind_ip_all",
            ]
          ports:
            - containerPort: 27019
              name: mongo
          volumeMounts:
            - name: data
              mountPath: /data/db
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes: ["ReadWriteOnce"]
        resources:
          requests:
            storage: 20Gi
---
apiVersion: v1
kind: Service
metadata:
  name: shard1-svc
  namespace: juwan
spec:
  clusterIP: None
  selector:
    app: mongo-shard1
  ports:
    - name: mongo
      port: 27018
      targetPort: 27018
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: shard1
  namespace: juwan
spec:
  serviceName: shard1-svc
  replicas: 3
  selector:
    matchLabels:
      app: mongo-shard1
  template:
    metadata:
      labels:
        app: mongo-shard1
    spec:
      containers:
        - name: mongod
          image: mongo:7.0
          args:
            [
              "--shardsvr",
              "--replSet",
              "shard1-rs",
              "--port",
              "27018",
              "--bind_ip_all",
            ]
          ports:
            - containerPort: 27018
              name: mongo
          volumeMounts:
            - name: data
              mountPath: /data/db
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes: ["ReadWriteOnce"]
        resources:
          requests:
            storage: 20Gi
---
apiVersion: v1
kind: Service
metadata:
  name: shard2-svc
  namespace: juwan
spec:
  clusterIP: None
  selector:
    app: mongo-shard2
  ports:
    - name: mongo
      port: 27018
      targetPort: 27018
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: shard2
  namespace: juwan
spec:
  serviceName: shard2-svc
  replicas: 3
  selector:
    matchLabels:
      app: mongo-shard2
  template:
    metadata:
      labels:
        app: mongo-shard2
    spec:
      containers:
        - name: mongod
          image: mongo:7.0
          args:
            [
              "--shardsvr",
              "--replSet",
              "shard2-rs",
              "--port",
              "27018",
              "--bind_ip_all",
            ]
          ports:
            - containerPort: 27018
              name: mongo
          volumeMounts:
            - name: data
              mountPath: /data/db
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes: ["ReadWriteOnce"]
        resources:
          requests:
            storage: 20Gi
---
apiVersion: v1
kind: Service
metadata:
  name: mongos
  namespace: juwan
spec:
  selector:
    app: mongos
  ports:
    - name: mongo
      port: 27017
      targetPort: 27017
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mongos
  namespace: juwan
spec:
  replicas: 2
  selector:
    matchLabels:
      app: mongos
  template:
    metadata:
      labels:
        app: mongos
    spec:
      containers:
        - name: mongos
          image: mongo:7.0
          args:
            - "mongos"
            - "--configdb"
            - "cfg-rs/cfg-0.cfg-svc.juwan.svc.cluster.local:27019,cfg-1.cfg-svc.juwan.svc.cluster.local:27019,cfg-2.cfg-svc.juwan.svc.cluster.local:27019"
            - "--bind_ip_all"
            - "--port"
            - "27017"
          ports:
            - containerPort: 27017
              name: mongo
