I have copied install_replicaSet.sh inside the mongo docker image. this script needs to run when all the replica sets have come up.
FROM mongo:4.2.6
COPY install_replicaSet.sh /install_replicaSet.sh
RUN chmod +x /install_replicaSet.sh
ENTRYPOINT ["/bin/bash/","/install_replicaSet.sh"]kind: StatefulSet
metadata:
name: mongo
spec:
selector:
matchLabels:
app: mongo
serviceName: "mongo"
replicas: 3
template:
metadata:
labels:
app: mongo
spec:
terminationGracePeriodSeconds: 10
containers:
- name: mongo
# image: mongo
image: xxxxxxx/mongo:v4.2.6
command:
- mongod
- "--bind_ip_all"
- "--replSet"
- rs0
- ["/bin/bash/", "-c", "/install_replicaSet.sh"]
ports:
- containerPort: 27107
volumeMounts:
- name: mongo-volume
mountPath: /data/db
volumeClaimTemplates:
- metadata:
name: mongo-volume
spec:
storageClassName: ebs
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 1Giinstall_replicaSet.sh
#!/bin/bash
cfg="{
_id: 'rs0',
members: [
{_id: 1, host: 'mongo-0.mongo:27017'},
{_id: 2, host: 'mongo-1.mongo:27017'},
{_id: 3, host: 'mongo-2.mongo:27017'}
]
}"
mongo mongo-0.mongo:27017 --eval "JSON.stringify(db.adminCommand({'replSetInitiate' : $cfg}))"