mongodb-template.yaml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. apiVersion: v1
  2. kind: Template
  3. labels:
  4. template: mongodb-openshift-dev-preview-template
  5. message: |-
  6. This is the MongoDB Enterprise Openshift Developer Preview.
  7. Sweet!
  8. metadata:
  9. annotations:
  10. description: |-
  11. Provisions MongoDB replica set and agent-only pods which are managed by MongoDB Ops Manager.
  12. NOTE: Sharded clusters are not supported yet. Security is not automatically enabled.
  13. iconClass: icon-mongodb
  14. openshift.io/display-name: MongoDB Enterprise
  15. openshift.io/documentation-url: https://docs.openshift.org/latest/using_images/db_images/mongodb.html
  16. openshift.io/long-description: This template provisions a MongoDB replica set
  17. which is managed by an instance of MongoDB Ops Manager.
  18. openshift.io/provider-display-name: MongoDB, Inc.
  19. openshift.io/support-url: https://access.redhat.com
  20. tags: database,mongodb
  21. name: mongodb-openshift-dev-preview
  22. uid: d50820c0-32ef-11e8-944b-12f4b1c41cea
  23. objects:
  24. - apiVersion: v1
  25. kind: ServiceAccount
  26. metadata:
  27. name: mongodb-${MMS_PROJECT_NAME}-${CLUSTER_NAME}-secret-access-sa
  28. namespace: ${NAMESPACE}
  29. labels:
  30. app: mongodb-${MMS_PROJECT_NAME}-${CLUSTER_NAME}
  31. - apiVersion: rbac.authorization.k8s.io/v1beta1
  32. kind: ClusterRole
  33. metadata:
  34. name: ${CLUSTER_NAME}-mongodb-secret-access-cr
  35. labels:
  36. app: mongodb-${MMS_PROJECT_NAME}-${CLUSTER_NAME}
  37. rules:
  38. - apiGroups: [""] # "" indicates the core API group
  39. resources: ["pods", "secrets"]
  40. verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
  41. - apiVersion: rbac.authorization.k8s.io/v1beta1
  42. kind: ClusterRoleBinding
  43. metadata:
  44. name: ${CLUSTER_NAME}-mongodb-secret-access-rb
  45. labels:
  46. app: mongodb-${MMS_PROJECT_NAME}-${CLUSTER_NAME}
  47. subjects:
  48. - kind: ServiceAccount
  49. name: mongodb-${MMS_PROJECT_NAME}-${CLUSTER_NAME}-secret-access-sa
  50. namespace: ${NAMESPACE}
  51. roleRef:
  52. kind: ClusterRole
  53. name: ${CLUSTER_NAME}-mongodb-secret-access-cr
  54. apiGroup: rbac.authorization.k8s.io
  55. - apiVersion: v1
  56. kind: Secret
  57. metadata:
  58. annotations:
  59. template.openshift.io/expose-base-url: '{.data[''MMS_BASE_URL'']}'
  60. template.openshift.io/expose-cluster-name: '{.data[''CLUSTER_NAME'']}'
  61. template.openshift.io/expose-project-name: '{.data[''MMS_PROJECT_NAME'']}'
  62. template.openshift.io/expose-user: '{.data[''MMS_USER'']}'
  63. template.openshift.io/expose-user-apikey: '{.data[''MMS_USER_APIKEY'']}'
  64. name: ${CLUSTER_NAME}-secret
  65. labels:
  66. app: mongodb-${MMS_PROJECT_NAME}-${CLUSTER_NAME}
  67. namespace: ${NAMESPACE}
  68. state: present
  69. stringData:
  70. base-url: ${MMS_BASE_URL}
  71. cluster-name: ${CLUSTER_NAME}
  72. project-name: ${MMS_PROJECT_NAME}
  73. mms-user: ${MMS_USER}
  74. mms-user-apikey: ${MMS_USER_APIKEY}
  75. agent-rpm: ${AGENT_RPM}
  76. type: Opaque
  77. - apiVersion: apps/v1beta1
  78. kind: StatefulSet
  79. metadata:
  80. name: mongodb-${MMS_PROJECT_NAME}-${CLUSTER_NAME}
  81. labels:
  82. app: mongodb-${MMS_PROJECT_NAME}-${CLUSTER_NAME}
  83. spec:
  84. replicas: ${NUMBER_PODS_RS}
  85. serviceName: mongodb-${MMS_PROJECT_NAME}-${CLUSTER_NAME}
  86. template:
  87. metadata:
  88. labels:
  89. app: mongodb-${MMS_PROJECT_NAME}-${CLUSTER_NAME}
  90. spec:
  91. serviceAccountName: mongodb-${MMS_PROJECT_NAME}-${CLUSTER_NAME}-secret-access-sa
  92. initContainers:
  93. - name: mms-node-init
  94. image: centos
  95. command:
  96. - /bin/sh
  97. - -c
  98. - |
  99. set -ex
  100. echo "Attempt to configure MongoDB Ops Manager Project & apikeys"
  101. echo "Check if we have mms project and apikey info in existing secret"
  102. TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)
  103. K8S="https://${KUBERNETES_SERVICE_HOST}:${KUBERNETES_SERVICE_PORT}"
  104. CACERT="/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"
  105. NAMESPACE=$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace)
  106. SECRET_NAME=$(echo "mms-project-${MMS_PROJECT_NAME}-secret" | tr '[:upper:]' '[:lower:]')
  107. echo "Contacting ${K8S} in namespace '${NAMESPACE}' for the keys 'mmsGroupId' & 'mmsAgentApikey' in secret '${SECRET_NAME}'"
  108. SECRET_STUFF=$(curl -vvvsSk --cacert $CACERT -H "Authorization: Bearer ${TOKEN}" ${K8S}/api/v1/namespaces/${NAMESPACE}/secrets/${SECRET_NAME})
  109. KIND=$(echo ${SECRET_STUFF} | python -c 'import sys,json,base64,os;r=json.load(sys.stdin); print r["kind"]')
  110. echo "KIND=${KIND}"
  111. if [ "${KIND}" == "Secret" ];
  112. then
  113. MMS_GROUP_ID=$(echo ${SECRET_STUFF} | python -c 'import sys,json,base64;r=json.load(sys.stdin);print base64.b64decode(r["data"]["mmsGroupId"])')
  114. MMS_AGENT_APIKEY=$(echo ${SECRET_STUFF} | python -c 'import sys,json,base64;r=json.load(sys.stdin);print base64.b64decode(r["data"]["mmsAgentApikey"])')
  115. fi
  116. if [ "${KIND}" == "Status" ];
  117. then
  118. echo "${SECRET_STUFF}"
  119. echo "If not, then we need to create the group"
  120. echo "Fetch MongoDB Ops Manager project (group) information"
  121. echo "(next command does a POST with the project name, this will either create a project or return project information)"
  122. curl --header "Content-Type: application/json" --header "Accept: application/json" -u "${MMS_USER}:${MMS_USER_APIKEY}" --digest "${MMS_BASE_URL}api/public/v1.0/groups" -d '{"name":"${MMS_PROJECT_NAME}"}' > /mms-config/group.${MMS_PROJECT_NAME}.json
  123. cat /mms-config/group.${MMS_PROJECT_NAME}.json
  124. MMS_GROUP_ID=$(cat /mms-config/group.${MMS_PROJECT_NAME}.json | python -c 'import sys, json; r = json.load(sys.stdin);print r["id"]')
  125. MMS_AGENT_APIKEY=$(cat /mms-config/group.${MMS_PROJECT_NAME}.json | python -c 'import sys, json; r = json.load(sys.stdin);print r["agentApiKey"]')
  126. temp=$(mktemp)
  127. echo { \"kind\": \"Secret\", \"apiVersion\": \"v1\", \"metadata\": { \"name\": \"${SECRET_NAME}\" }, \"stringData\": { \"mmsGroupId\": \"${MMS_GROUP_ID}\", \"mmsAgentApikey\" : \"${MMS_AGENT_APIKEY}\" } } > ${temp}
  128. SR=$(curl -k -X POST -H "Authorization: Bearer $TOKEN" -H 'Accept: application/json' -H 'Content-Type: application/json' ${K8S}/api/v1/namespaces/$NAMESPACE/secrets --data "@${temp}")
  129. K_SR=$(echo ${SR} | python -c 'import sys,json,base64,os;r=json.load(sys.stdin); print r["kind"]')
  130. if [ "${KIND}" == "Status" ]
  131. then
  132. STATUS=$(echo ${SR} | python -c 'import sys,json,base64,os;r=json.load(sys.stdin); print r["status"]')
  133. echo "Attempting to create '${SECRET_NAME}' got Status.status='${STATUS}'"
  134. exit 1
  135. fi
  136. echo "Created Secret '${SECRET_NAME}' Response: '${SR}'"
  137. fi
  138. if [ -z ${MMS_GROUP_ID+x} ]
  139. then
  140. echo "[ERROR] Unable to set MMS_GROUP_ID, cannot continue."
  141. exit 1
  142. fi
  143. if [ -z ${MMS_AGENT_APIKEY+x} ]
  144. then
  145. echo "[ERROR] Unable to set MMS_AGENT_APIKEY, cannot continue."
  146. exit 1
  147. fi
  148. echo "Found MMS_GROUP_ID=${MMS_GROUP_ID} for Ops Manager Project '${MMS_PROJECT_NAME}'"
  149. echo "Found MMS_AGENT_APIKEY=${MMS_AGENT_APIKEY} for Ops Manager Project '${MMS_PROJECT_NAME}'"
  150. echo "Storing MongoDB Ops Manager configuration in shared mount '/mms-config/mms-env.sh'"
  151. echo "#!/bin/sh" > /mms-config/mms-env.sh
  152. echo "export MMS_PROJECT_NAME=${MMS_PROJECT_NAME}" >> /mms-config/mms-env.sh
  153. echo "export MMS_GROUP_ID=${MMS_GROUP_ID}" >> /mms-config/mms-env.sh
  154. echo "export MMS_AGENT_APIKEY=${MMS_AGENT_APIKEY}" >> /mms-config/mms-env.sh
  155. volumeMounts:
  156. - name: mms-config
  157. mountPath: /mms-config
  158. env:
  159. - name: MMS_BASE_URL
  160. valueFrom:
  161. secretKeyRef:
  162. key: base-url
  163. name: ${CLUSTER_NAME}-secret
  164. - name: MMS_USER
  165. valueFrom:
  166. secretKeyRef:
  167. key: mms-user
  168. name: ${CLUSTER_NAME}-secret
  169. - name: MMS_USER_APIKEY
  170. valueFrom:
  171. secretKeyRef:
  172. key: mms-user-apikey
  173. name: ${CLUSTER_NAME}-secret
  174. - name: MMS_PROJECT_NAME
  175. valueFrom:
  176. secretKeyRef:
  177. key: project-name
  178. name: ${CLUSTER_NAME}-secret
  179. containers:
  180. - args:
  181. command:
  182. - /bin/sh
  183. - -c
  184. - |
  185. set -ex
  186. if [ ! -f /mms-config/mms-env.sh ]; then
  187. echo "[ERROR] MongoDB Ops Manager configration not found."
  188. echo "[ERROR] File '/mms-config/mms-env.sh' does not exist."
  189. echo "[ERROR] Check Init Container 'mms-node-init' logs. (oc logs <pod_name> -c mms-node-init)."
  190. exit 1
  191. fi
  192. source /mms-config/mms-env.sh
  193. # /download/agent/automation/mongodb-mms-automation-agent-latest.linux_x86_64.tar.gz
  194. AGENT_URL=${MMS_BASE_URL}download/agent/automation/
  195. echo "MongoDB Ops Manager - Openshift Developer Preview"
  196. echo "MMS_PROJECT_NAME='${MMS_PROJECT_NAME}'"
  197. echo "MMS_GROUP_ID='${MMS_GROUP_ID}'"
  198. echo "MMS_AGENT_APIKEY='${MMS_AGENT_APIKEY}'"
  199. echo "Downloading ${AGENT_URL}${AGENT_RPM}"
  200. curl -OL "${AGENT_URL}${AGENT_RPM}"
  201. echo "Installing ${AGENT_RPM}"
  202. rpm -U "${AGENT_RPM}"
  203. echo "Updating /etc/mongodb-mms/automation-agent.config with:"
  204. echo "mmsGroupId=${MMS_GROUP_ID}"
  205. echo "mmsApiKey=${MMS_AGENT_APIKEY}"
  206. echo "mmsBaseUrl=${MMS_BASE_URL}"
  207. export MMS_BASE_URL_TRIMMED="${MMS_BASE_URL%/}"
  208. echo "mmsBaseUrl (trimmed of trailing slash for automation-agent.config)=${MMS_BASE_URL_TRIMMED}"
  209. echo "mmsGroupId=${MMS_GROUP_ID}" > /etc/mongodb-mms/automation-agent.config
  210. echo "mmsApiKey=${MMS_AGENT_APIKEY}" >> /etc/mongodb-mms/automation-agent.config
  211. echo "mmsBaseUrl=${MMS_BASE_URL_TRIMMED}" >> /etc/mongodb-mms/automation-agent.config
  212. chown mongod:mongod /data
  213. echo "Creating /var/run/mongodb-mms-automation"
  214. /usr/bin/mkdir -p /var/run/mongodb-mms-automation
  215. /usr/bin/chown -R mongod:mongod /var/run/mongodb-mms-automation
  216. echo "Starting automation agent..."
  217. MMS_LOG_DIR=/var/log/mongodb-mms-automation
  218. /opt/mongodb-mms-automation/bin/mongodb-mms-automation-agent \
  219. -f /etc/mongodb-mms/automation-agent.config \
  220. -pidfilepath /var/run/mongodb-mms-automation/mongodb-mms-automation-agent.pid \
  221. -logLevel DEBUG \
  222. -logFile ${MMS_LOG_DIR}/automation-agent.log \
  223. >> ${MMS_LOG_DIR}/automation-agent-fatal.log 2>&1 &
  224. echo "MongoDB Ops Manager automation agent assigned to project '${MMS_PROJECT_NAME}' started `date`."
  225. echo "-- Reading automation agent log file forever"
  226. VERBOSE_LOG="${MMS_LOG_DIR}/automation-agent-verbose.log"
  227. if [ ! -f ${VERBOSE_LOG} ]; then
  228. echo "${VERBOSE_LOG} not found, checking again in 2 seconds."
  229. sleep 2
  230. fi
  231. echo "${VERBOSE_LOG} was found."
  232. tail -F ${VERBOSE_LOG}
  233. env:
  234. - name: MMS_BASE_URL
  235. valueFrom:
  236. secretKeyRef:
  237. key: base-url
  238. name: ${CLUSTER_NAME}-secret
  239. - name: AGENT_RPM
  240. valueFrom:
  241. secretKeyRef:
  242. key: agent-rpm
  243. name: ${CLUSTER_NAME}-secret
  244. image: centos
  245. name: mongodb-${MMS_PROJECT_NAME}-${CLUSTER_NAME}
  246. ports: null
  247. volumeMounts:
  248. - mountPath: /data
  249. name: pvc
  250. - mountPath: /mms-config
  251. name: mms-config
  252. terminationGracePeriodSeconds: "10"
  253. volumes:
  254. - name: mms-config
  255. emptyDir: {}
  256. volumeClaimTemplates:
  257. - name: "pvc"
  258. metadata:
  259. name: "pvc"
  260. labels:
  261. app: mongodb-${MMS_PROJECT_NAME}-${CLUSTER_NAME}
  262. spec:
  263. accessModes: [ "ReadWriteOnce" ]
  264. resources:
  265. requests:
  266. storage: ${DISK_SIZE_GB}Gi
  267. - apiVersion: v1
  268. kind: Service
  269. metadata:
  270. labels:
  271. name: mongodb-${MMS_PROJECT_NAME}-${CLUSTER_NAME}
  272. replSet: ${CLUSTER_NAME}
  273. app: mongodb-${MMS_PROJECT_NAME}-${CLUSTER_NAME}
  274. name: mongodb-${MMS_PROJECT_NAME}-${CLUSTER_NAME}
  275. spec:
  276. clusterIp: None
  277. ports:
  278. - name: mongodb-${MMS_PROJECT_NAME}-${CLUSTER_NAME}
  279. port: ${MONGODB_PORT}
  280. selector:
  281. app: mongodb-${MMS_PROJECT_NAME}-${CLUSTER_NAME}
  282. parameters:
  283. - description: The namespace to create a cluster in.
  284. displayName: Namespace
  285. name: NAMESPACE
  286. required: true
  287. - description: Maximum amount of memory the container can use.
  288. displayName: Memory Limit
  289. name: MEMORY_LIMIT
  290. required: true
  291. value: 512Mi
  292. - description: URL for MongoDB Ops Manager
  293. displayName: Ops Manager URL
  294. name: MMS_BASE_URL
  295. required: true
  296. value: http://mongodb-opsmgr:8080/
  297. - description: MongoDB Ops Manager User
  298. displayName: Ops Manager user
  299. name: MMS_USER
  300. required: true
  301. value: mongodude@mongorocks.com
  302. - description: MongoDB Ops Manager API key for user
  303. displayName: User"s Ops Manager API key
  304. name: MMS_USER_APIKEY
  305. required: true
  306. value: 45fbcc0d-2667-41e0-996e-0555ff62baca
  307. - description: Name for Ops Manager Project
  308. displayName: Project to create replica set in
  309. from: openshift-[a-z0-9]{3}
  310. generate: expression
  311. name: MMS_PROJECT_NAME
  312. required: true
  313. - description: Automation Agent RPM
  314. displayName: Full automation agent rpm filename, this may vary depending on your version of Ops Manager
  315. name: AGENT_RPM
  316. required: true
  317. value: "mongodb-mms-automation-agent-manager-5.4.0.5405-1.x86_64.rhel7.rpm"
  318. - description: API Timeout
  319. displayName: MongoDB Ops Manager API Timeout in seconds
  320. name: MMS_API_TIMEOUT
  321. required: true
  322. value: "30"
  323. - description: Cluster Name
  324. displayName: MongoDB Cluster Name
  325. from: cluster-[a-z0-9]{5}
  326. generate: expression
  327. name: CLUSTER_NAME
  328. required: true
  329. - description: Path for MongoDB data files in container
  330. displayName: Path for data files
  331. name: MONGODB_DBPATH
  332. required: true
  333. value: /data
  334. - description: Log file destination
  335. displayName: Full path and name of database logfile
  336. name: MONGODB_LOGPATH
  337. required: true
  338. value: /data/mongodb.log
  339. - description: MongoDB port
  340. displayName: Port for MongoDB to listen on
  341. name: MONGODB_PORT
  342. required: true
  343. value: "27000"
  344. - description: Size of disk
  345. displayName: Size in Gb for persistent storage claim on data node
  346. name: DISK_SIZE_GB
  347. required: true
  348. value: "5"
  349. - description: Number of members in replica set
  350. displayName: Number of nodes in Replica Set
  351. name: NUMBER_PODS_RS
  352. required: true
  353. value: "3"