之前在k8s上搭建的mongo集群,总是需要在pod新建好之后,再连上去执行命令在多个pod间建立联系。本想着要不要写一个东西一起放进pod里面去代替最后那步手动操作,但上github上一搜发现已经有老哥完成了这个工具了。

以下是用到的两个k8s配置:

statefulset.yaml

apiVersion: apps/v1beta1
kind: StatefulSet
metadata: 
  namespace: service
  name: mongo
spec: 
  selector: 
    matchLabels: 
      app: ump-mongo
  serviceName: ump-mongo-service
  replicas: 3
  podManagementPolicy: Parallel
  template: 
    metadata: 
      labels: 
        app: ump-mongo
        applicationinstance.tess.io/name: zk-staging-statefulsets
    spec: 
      terminationGracePeriodSeconds: 10
      affinity: 
         podAntiAffinity: 
           requiredDuringSchedulingIgnoredDuringExecution: 
           - labelSelector: 
               matchExpressions: 
               - key: app
                 operator: In
                 values:
                 - ump-mongo
             topologyKey: "kubernetes.io/hostname"
      containers: 
      - name: mongo-master
        image: mongo:4.0.13
        command:  
        - mongod
        - "--bind_ip"
        - 0.0.0.0
        - "--replSet"
        - rs0
        ports: 
        - containerPort: 27017
        volumeMounts:
        - name: mongo-persistent-storage
          mountPath: /data/db
      - name: mongo-sidecar
        image: mongo-k8s-sidecar:2.0
        env:
        - name: KUBERNETES_POD_LABELS
          value: "app=ump-mongo"
        - name: KUBERNETES_SERVICE_NAME
          value: "ump-mongo-service"
        - name: KUBERNETES_CLUSTER_DOMAIN
          value: 33.tess.io
  volumeClaimTemplates:
   - metadata:
       name: mongo-persistent-storage
     spec:
       storageClassName: cinder-standard
       accessModes: [ "ReadWriteOnce" ]
       resources:
         requests:
           storage: 70Gi

service.json

apiVersion: v1
kind: Service
metadata:
  name: ump-mongo-cluster
  namespace: service
  labels:
    app: ump-mongo
spec:
  ports:
  - name: mongo
    port: 27017
    targetPort: 27017
  selector:
    app: ump-mongo
  clusterIP: None

mongo-k8s-sidecar

github地址: https://github.com/morphy2k/k8s-mongo-sidecar

当前的这个sidecar对mongo 4.4以上不太好使,如果用的4.4以上的images需要把代码fork,去改一下

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐