-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathmongodb.yaml
162 lines (162 loc) · 5.29 KB
/
mongodb.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
apiVersion: v1
kind: Service
metadata:
name: mongodb-service
labels:
name: mongo
spec:
ports:
- port: 27017
targetPort: 27017
clusterIP: None
selector:
role: mongo
---
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
name: mongod
spec:
serviceName: mongodb-service
replicas: 3
template:
metadata:
labels:
role: mongo
environment: test
replicaset: MainRepSet
spec:
affinity:
# This is to prevent mongodb replicas from ending up on the same
# host machine
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchExpressions:
- key: replicaset
operator: In
values:
- MainRepSet
topologyKey: kubernetes.io/hostname
terminationGracePeriodSeconds: 10
containers:
- name: mongod-container
#image: pkdone/mongo-ent:3.4
image: mongo
imagePullPolicy: Always
env:
- name: STATEFULSET_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: MACHINE_MEMORY
value: &machineMemory "500Mi"
# We can change this to a secret
- name: MONGO_USER
value: "admin"
- name: MONGO_PASSWORD
value: "abc123changeme"
command:
- "bash"
- "-c"
#the default cache size guidance is: "50% of RAM minus 1 GB, or 256 MB"
# Which is why the wired tiger cache size is this way. This assumes 2gb of memory
# on the host machine. The plan is to change 0.25 to a calculated value
# by passing an environment variable that contains the host machines memory
# and automatically "personalizing" mongo to the machine its running on
- |
numactl --interleave=all mongod --wiredTigerCacheSizeGB 0.25 --bind_ip 0.0.0.0 \
--replSet MainRepSet --auth --clusterAuthMode keyFile \
--keyFile /etc/secrets-volume/mongosecret \
--setParameter authenticationMechanisms=SCRAM-SHA-1 \
--fork --logpath /var/log/mongod.log \
&&
if [ $STATEFULSET_NAME == "mongod-0" ];
then
initiated=0
replica_exists=false
while [ "$replica_exists" != true ]
do
# Need to check to make sure that the other pods are currently up
initiated=`mongo --quiet --eval 'rs.initiate({_id: "MainRepSet", version: 1, members: [
{ _id: 0, host : "mongod-0.mongodb-service.default.svc.cluster.local:27017" },
{ _id: 1, host : "mongod-1.mongodb-service.default.svc.cluster.local:27017" },
{ _id: 2, host : "mongod-2.mongodb-service.default.svc.cluster.local:27017" }
]})["ok"]'`;
replica_exists=`mongo --quiet --eval 'db.isMaster()["ismaster"]'`;
# Shit we need to check rs status i think instead
echo "Replica exists: " $replica_exists;
sleep 2
done
# If everything else is workign then we need to create an admin user
mongo --eval "db.getSiblingDB('admin').createUser({
user : \"$MONGO_USER\",
pwd : \"$MONGO_PASSWORD\",
roles: [ { role: 'root', db: 'admin' } ]
});"
echo "STATEFULSET_NAME: $STATEFULSET_NAME \n MACHINE_MEMORY: $MACHINE_MEMORY";
fi && tailf /var/log/mongod.log
# resources:
# requests:
# cpu: 1
# memory: 300Mi
ports:
- containerPort: 27017
volumeMounts:
- name: secrets-volume
# readOnly: true
mountPath: /etc/secrets-volume/mongosecret
subPath: mongosecret
- name: mongodb-persistent-storage-claim
mountPath: /data/db
volumes:
- name: secrets-volume
secret:
secretName: mongosecret
# https://coderstoolbox.net/number/
# Convert to octal to decimal because kube only accepts
# decimal as input
defaultMode: 256
volumeClaimTemplates:
- metadata:
name: mongodb-persistent-storage-claim
annotations:
volume.beta.kubernetes.io/storage-class: "fast"
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 500Mi
# ---
# Google cloud
# kind: StorageClass
# apiVersion: storage.k8s.io/v1beta1
# metadata:
# name: fast
# provisioner: kubernetes.io/gce-pd
# parameters:
# type: pd-ssd
# apiVersion: "v1"
# ---
# kind: "PersistentVolume"
# metadata:
# name: data-volume-INST
# spec:
# capacity:
# storage: 10Gi
# accessModes:
# - ReadWriteOnce
# persistentVolumeReclaimPolicy: Retain
# storageClassName: fast
# gcePersistentDisk:
# fsType: xfs
# pdName: pd-ssd-disk-INST
---
# Local storage
kind: StorageClass
apiVersion: storage.k8s.io/v1beta1
metadata:
name: fast
provisioner: k8s.io/minikube-hostpath