K8S安装mongo:4.0.28单实例版

选择这个版本、单实例是项目需求,我根据docker-compose改编而来。

apiVersion: v1
kind: Service
metadata:
  name: mongo-service
spec:
  selector:
    app: mongo
  ports:
    - protocol: TCP
      name: defaltport
      #集群内部:Service IP +Port就可以访问到服务
      port: 27017
      #Pod内的应用暴露的服务端口
      targetPort: 27017
      #集群外部:宿主机IP+nodePort就可以访问到服务
      nodePort: 32322
  type: NodePort
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mongo-statefulset
spec:
  serviceName: mongo-service
  replicas: 1
  selector:
    matchLabels:
      app: mongo
  template:
    metadata:
      labels:
        app: mongo
    spec:
      containers:
        - name: mongo-container
          imagePullPolicy: Always
          image: mongo:4.0.28
#          securityContext:
#            runAsUser: 1000 # 指定用户 ID
#            runAsGroup: 1000 # 指定用户组 ID
          env:
            - name: MONGO_INITDB_ROOT_USERNAME
              value: "admin"
            - name: MONGO_INITDB_ROOT_PASSWORD
              value: "FCMS)n@I"

          #容器内路径
          volumeMounts:
            - name: mongo-data-pvc
              mountPath: /data/db
      #容器外目录
      volumes:
        - name: mongo-data-pvc
          persistentVolumeClaim:
            claimName: mongo-data-pvc

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mongo-data-pvc
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: yoda-lvm-default
  volumeMode: Filesystem
  resources:
    requests:
      storage: 100Gi
  selector:
    matchLabels:
      app: mongo-data-pv
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: mongo-data-pv
  labels:
    app: mongo-data-pv
spec:
  capacity:
    storage: 100Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: yoda-lvm-default
  nfs:
    path: /mnt/nfs/mongo/data
    server: 192.168.1.61

你可能感兴趣的:(云原生技术,kubernetes,容器,云原生)