Skip to content

Instantly share code, notes, and snippets.

@ll911
Created October 17, 2018 22:21
Show Gist options
  • Select an option

  • Save ll911/131dfdbb7020d11632431ec0b812a649 to your computer and use it in GitHub Desktop.

Select an option

Save ll911/131dfdbb7020d11632431ec0b812a649 to your computer and use it in GitHub Desktop.
mongodb-k8s-statefulset.yaml
kind: Template
apiVersion: v1
metadata:
name: mongodb-statefulset-replication
annotations:
description: "MongoDB Replication Example (based on StatefulSet). You must have persistent volumes available in your cluster to use this template."
iconClass: "icon-mongodb"
tags: "database,mongodb,replication"
parameters:
- name: MONGODB_USER
displayName: "MongoDB Connection Username"
description: "Username for MongoDB user that will be used for accessing the database."
generate: expression
from: "[a-zA-Z0-9]{3}"
required: true
- name: MONGODB_PASSWORD
displayName: "MongoDB Connection Password"
description: "Password for the MongoDB connection user."
generate: expression
from: "[a-zA-Z0-9]{16}"
required: true
- name: MONGODB_DATABASE
displayName: "MongoDB Database Name"
description: "Name of the MongoDB database accessed."
value: sampledb
required: true
- name: MONGODB_ADMIN_PASSWORD
displayName: "MongoDB Admin Password"
description: "Password for the database admin user."
generate: expression
from: "[a-zA-Z0-9]{16}"
required: true
- name: MONGODB_REPLICA_NAME
displayName: "Replica Set Name"
description: "The name of the replica set."
value: rs0
required: true
- name: MONGODB_KEYFILE_VALUE
displayName: "Keyfile Content"
description: "The value of the MongoDB keyfile (https://docs.mongodb.com/manual/core/security-internal-authentication/#internal-auth-keyfile)."
generate: expression
from: "[a-zA-Z0-9]{255}"
required: true
- name: MONGODB_IMAGE
displayName: "MongoDB Docker Image"
description: "A reference to a supported MongoDB Docker image."
value: "registry.access.redhat.com/rhscl/mongodb-32-rhel7:3.2"
required: true
- name: MONGODB_SERVICE_NAME
displayName: "OpenShift Service Name"
description: "The name of the OpenShift Service exposed for the database."
value: mongodb
required: true
- name: VOLUME_CAPACITY
displayName: "Volume Capacity"
description: "Volume space available for data, e.g. 512Mi, 2Gi."
value: "1Gi"
required: true
- name: MEMORY_LIMIT
displayName: "Memory Limit"
description: "Maximum amount of memory the container can use."
value: "512Mi"
objects:
- apiVersion: v1
kind: Secret
metadata:
name: ${MONGODB_SERVICE_NAME}
labels:
name: "${MONGODB_SERVICE_NAME}"
stringData:
username: "${MONGODB_USER}"
password: "${MONGODB_PASSWORD}"
admin-username: "admin"
admin-password: "${MONGODB_ADMIN_PASSWORD}"
database: "${MONGODB_DATABASE}"
replica-name: "${MONGODB_REPLICA_NAME}"
# A non-headless service which takes pod readiness into consideration
- kind: Service
apiVersion: v1
metadata:
name: "${MONGODB_SERVICE_NAME}"
labels:
name: "${MONGODB_SERVICE_NAME}"
spec:
# the list of ports that are exposed by this service
ports:
- name: mongodb
port: 27017
# will route traffic to pods having labels matching this selector
selector:
name: "${MONGODB_SERVICE_NAME}"
# A headless service to create DNS records
- kind: Service
apiVersion: v1
metadata:
name: "${MONGODB_SERVICE_NAME}-internal"
labels:
name: "${MONGODB_SERVICE_NAME}"
annotations:
service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
spec:
clusterIP: None
# the list of ports that are exposed by this service
ports:
- name: mongodb
port: 27017
# will route traffic to pods having labels matching this selector
selector:
name: "${MONGODB_SERVICE_NAME}"
- kind: StatefulSet
apiVersion: apps/v1beta1
metadata:
name: "${MONGODB_SERVICE_NAME}"
spec:
# pets get DNS/hostnames that follow the pattern: ${metadata.name}-NUM.${spec.serviceName}.default.svc.cluster.local
serviceName: "${MONGODB_SERVICE_NAME}-internal"
replicas: 3
# describes the pod that will be created if insufficient replicas are detected
template:
metadata:
# this label will be used for count running pods
labels:
name: "${MONGODB_SERVICE_NAME}"
spec:
containers:
- name: mongo-container
image: "${MONGODB_IMAGE}"
ports:
- containerPort: 27017
args:
- "run-mongod-pet"
volumeMounts:
- name: mongo-data
mountPath: "/var/lib/mongodb/data"
env:
- name: MONGODB_USER
valueFrom:
secretKeyRef:
key: username
name: "${MONGODB_SERVICE_NAME}"
- name: MONGODB_PASSWORD
valueFrom:
secretKeyRef:
key: password
name: "${MONGODB_SERVICE_NAME}"
- name: MONGODB_DATABASE
valueFrom:
secretKeyRef:
key: database
name: "${MONGODB_SERVICE_NAME}"
- name: MONGODB_ADMIN_PASSWORD
valueFrom:
secretKeyRef:
key: admin-password
name: "${MONGODB_SERVICE_NAME}"
- name: MONGODB_REPLICA_NAME
valueFrom:
secretKeyRef:
key: replica-name
name: "${MONGODB_SERVICE_NAME}"
- name: MONGODB_KEYFILE_VALUE
value: "${MONGODB_KEYFILE_VALUE}"
- name: MONGODB_SERVICE_NAME
value: "${MONGODB_SERVICE_NAME}-internal"
resources:
limits:
memory: "${MEMORY_LIMIT}"
readinessProbe:
exec:
command:
- stat
- /tmp/initialized
volumeClaimTemplates:
- metadata:
name: mongo-data
labels:
name: "${MONGODB_SERVICE_NAME}"
annotations:
# Uncomment this if using dynamic volume provisioning.
# https://docs.openshift.org/latest/install_config/persistent_storage/dynamically_provisioning_pvs.html
# volume.alpha.kubernetes.io/storage-class: anything
spec:
# the volume can be mounted as read-write by a single node
accessModes: [ ReadWriteOnce ]
resources:
requests:
storage: "${VOLUME_CAPACITY}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment