k8s-statefulset创建mongo集群

首先创建headless

#创建对应的headless service
apiVersion: v1
kind: Service
metadata:
 name: mongo
 labels:
  name: mongo
spec:
 ports:
 - port: 27017
   targetPort: 27017
 clusterIP: None
 selector:
  role: mongo     

创建statefulset

#创建StatusFulSet mongoDB
apiVersion: apps/v1
kind: StatefulSet
metadata:
 name: mongo
spec:
 selector:
   matchLabels:
    role: mongo #必须匹配 .spec.template.metadata.labels
 serviceName: "mongo"
 replicas: 3
 template:
  metadata:
   labels:
    role: mongo
    environment: test
  spec:
   terminationGracePeriodSeconds: 10
   containers:
   - name: mongo
     image: mongo
     ports:
     - containerPort: 27017
     volumeMounts:
     - name: mongo-persistent-storage
       mountPath: /data/db
   - name: mongo-sidecar
     image: cvallance/mongo-k8s-sidecar
     env:
     - name: MONGO_SIDECAR_POD_LABELS #设置为mongo容器的标签,用于sidecar查询它所要管理的mongoDB集群实例
       value: "role=mongo,environment=test"
     - name: KUBERNETES_MONGO_SERVICE_NAME #设置为mongo,表示sidecar将使用mongo这个服务名来完成MongoDB集群的设置
       value: "mongo"
 volumeClaimTemplates:
 - metadata:
    name: mongo-persistent-storage
   spec:
    accessModes: [ "ReadWriteOnce" ] #读写权限
    storageClassName: standard  #存储类storageClass名 这里使用k8s自带的StorageClass,它使用的是本地存储 localPath
    resources:
     requests:
       storage: 5Gi

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