fix: MongoDB Replica Set when Titl is restarted (#43)

This commit is contained in:
ftocal 2022-12-15 12:51:15 -03:00 committed by GitHub
parent af94eac721
commit 9f035b40bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 42 additions and 97 deletions

View File

@ -41,10 +41,10 @@ if mongo:
]
)
k8s_yaml("devnet/mongo-configure-job.yaml")
k8s_yaml("devnet/mongo-rs-config.yaml")
k8s_resource(
"mongo-configure-job",
"mongo-rs-config",
resource_deps = ["mongo"]
)
@ -56,7 +56,7 @@ if mongoExpress:
port_forwards = [
port_forward(8081, name = "Mongo Express [:8081]", host = webHost),
],
resource_deps = ["mongo"]
resource_deps = ["mongo-rs-config"]
)
if fly:
@ -73,7 +73,7 @@ if fly:
port_forwards = [
port_forward(8001, name = "Server [:8001]", host = webHost),
],
resource_deps = ["mongo"]
resource_deps = ["mongo-rs-config"]
)
if server:
@ -90,7 +90,7 @@ if server:
port_forwards = [
port_forward(4000, name = "Server [:4000]", host = webHost),
],
resource_deps = ["mongo"]
resource_deps = ["mongo-rs-config"]
)
if api:
@ -107,7 +107,7 @@ if api:
port_forwards = [
port_forward(8000, name = "Server [:8000]", host = webHost),
],
resource_deps = ["mongo"]
resource_deps = ["mongo-rs-config"]
)
if web:
@ -149,7 +149,7 @@ if onchain_data:
k8s_resource(
"onchain-data",
resource_deps = ["mongo"],
resource_deps = ["mongo-rs-config"],
)
if spy:
@ -166,5 +166,5 @@ if spy:
port_forwards = [
port_forward(7777, name = "Spy [:7777]", host = webHost),
],
resource_deps = ["mongo"]
resource_deps = ["mongo-rs-config"]
)

View File

@ -34,7 +34,7 @@ spec:
image: indexer-api:latest
env:
- name: WORMSCAN_DB_URL
value: mongodb://mongo-0.mongo,mongo-1.mongo,mongo-2.mongo/?replicaSet=rs0
value: mongodb://mongo-0.mongo/?replicaSet=rs0
- name: WORMSCAN_PORT
value: "8000"
readinessProbe:

View File

@ -36,7 +36,7 @@ spec:
args: ["-local"]
env:
- name: MONGODB_URI
value: mongodb://mongo-0.mongo,mongo-1.mongo,mongo-2.mongo/?replicaSet=rs0
value: mongodb://mongo-0.mongo/?replicaSet=rs0
- name: API_PORT
value: "8001"
readinessProbe:

View File

@ -1,82 +0,0 @@
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: mongodb-account
namespace: 'default'
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: mongodb-role
rules:
- apiGroups: ["*"]
resources: ["services"]
verbs: ["*"]
- apiGroups: ["*"]
resources: ["pods"]
verbs: ["get","list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: mongodb-role-binding
subjects:
- kind: ServiceAccount
name: mongodb-account
namespace: 'default'
roleRef:
kind: ClusterRole
name: mongodb-role
apiGroup: rbac.authorization.k8s.io
---
apiVersion: batch/v1
kind: Job
metadata:
name: mongo-configure-job
spec:
template:
metadata:
name: mongo-configure-job
spec:
serviceAccountName: mongodb-account
initContainers:
- name: mongo-configure-dependency
image: ylonkar/pod-dependency-init-container:1.0.2
env:
- name: POD_LABELS
value: "app=mongo,statefulset.kubernetes.io/pod-name=mongo-2"
- name: MAX_RETRY
value: "10"
- name: RETRY_TIME_OUT
value: "30000"
containers:
- name: mongo
image: mongo:latest
command:
- mongosh
- mongo-0.mongo
- "--eval"
- |
rs.initiate({
_id : "rs0",
members: [
{
"_id": 0,
"host": "mongo-0.mongo",
"priority": 4
},
{
"_id": 1,
"host": "mongo-1.mongo",
"priority": 2
},
{
"_id": 2,
"host": "mongo-2.mongo",
"priority": 1
}
]
})
restartPolicy: Never
backoffLimit: 4

View File

@ -39,7 +39,7 @@ spec:
- name: ME_CONFIG_MONGODB_ADMINPASSWORD
value: example
- name: ME_CONFIG_MONGODB_URL
value: mongodb://mongo-0.mongo,mongo-1.mongo,mongo-2.mongo/?replicaSet=rs0
value: mongodb://mongo-0.mongo/?replicaSet=rs0
readinessProbe:
tcpSocket:
port: 8081

View File

@ -0,0 +1,27 @@
---
apiVersion: batch/v1
kind: Job
metadata:
name: mongo-rs-config
spec:
template:
metadata:
name: mongo-rs-config
spec:
containers:
- name: mongo
image: mongo:latest
command:
- /bin/bash
- -c
- |-
config='{_id : "rs0", members: [{"_id": 0,"host": "mongo-0.mongo","priority": 3 }]}'
if [ ! $(mongosh mongo-0.mongo --eval "rs.status().ok" --quiet) ]; then
echo "**** Cluster initiate ..."
if [ ! $(mongosh mongo-0.mongo --eval "rs.initiate($config).ok" --quiet) ]; then
echo "**** Cluster reconfig ..."
mongosh mongo-0.mongo --eval "cfg = rs.conf(); cfg.members[0].priority = cfg.members[0].priority + 1; rs.reconfig(cfg, {force: true})"
fi;
fi;
restartPolicy: Never
backoffLimit: 4

View File

@ -23,7 +23,7 @@ spec:
matchLabels:
app: mongo
serviceName: mongo
replicas: 3
replicas: 1
template:
metadata:
labels:

View File

@ -31,7 +31,7 @@ spec:
image: onchain-data:latest
env:
- name: MONGODB_URI
value: mongodb://root:example@mongo:27017/
value: mongodb://mongo-0.mongo/?replicaSet=rs0
- name: allowlist
value: "true"
readinessProbe:

View File

@ -35,7 +35,7 @@ spec:
image: server:latest
env:
- name: MONGODB_URI
value: mongodb://mongo-0.mongo,mongo-1.mongo,mongo-2.mongo/?replicaSet=rs0
value: mongodb://mongo-0.mongo/?replicaSet=rs0
readinessProbe:
tcpSocket:
port: 4000

View File

@ -35,7 +35,7 @@ spec:
image: spy:latest
env:
- name: MONGODB_URI
value: mongodb://mongo-0.mongo,mongo-1.mongo,mongo-2.mongo/?replicaSet=rs0
value: mongodb://mongo-0.mongo/?replicaSet=rs0
- name: MONGODB_DATABASE
value: wormhole
- name: GRPC_ADDRESS