In this lab you will connect two types of databases to your service.
ibmcloud login
Install the Kubernetes command-line plugin
ibmcloud plugin install container-service
ibmcloud cs clusters
ibmcloud cs cluster config --cluster <your-clusters-name>
Mongo DB will be deployed as a local installation in your cluster.
>git clone https://github.com/thomassuedbroecker/mongodb-container-kubernetes.git
>cd mongodb-container-kubernetes
Edit the file setup-mongo-db-and-ui.sh
and change the CLUSTER_NAME variable in line 2 to the name of your cluster
CLUSTER_NAME=<your clustername>
Run the install script
./setup-mongo-db-and-ui.sh
Link to the mongo db: http://<ip-address>:<port>
Link to the mongo db WebUI: http://<ip-address>:<port>
Check that both links contain a valid ip-address.
kubectl -n mongo-db get pods
NAME READY STATUS RESTARTS AGE
mongo-database-5bb5b56864-hsbg2 1/1 Running 0 9m19s
mongo-express-768bbf7b5d-x8csd 1/1 Running 2 (7m54s ago) 9m13s
Both pods should be in Running
status.
apiVersion: v1
kind: Secret
metadata:
name: mongodb-credentials
namespace: default
stringData:
URL: "<'Link to the Mongo DB' from above without the trailing '/'>"
type: Opaque
kubectl apply -f mongodb-creds.yaml
secret/mongodb-credentials created
apiVersion: v1
kind: ConfigMap
metadata:
name: redis-config
data:
redis-config: |
maxmemory 2mb
maxmemory-policy allkeys-lru
kubectl apply -f redis-conf.yaml
apiVersion: v1
kind: Service
metadata:
name: redis
spec:
ports:
- port: 6379
selector:
app: redis
type: NodePort
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis
spec:
selector:
matchLabels:
app: redis
strategy:
type: Recreate
template:
metadata:
labels:
app: redis
spec:
containers:
- name: redis
image: redis:5.0.4
command:
- redis-server
- "/redis-master/redis.conf"
env:
- name: MASTER
value: "true"
ports:
- containerPort: 6379
resources:
limits:
cpu: "0.1"
volumeMounts:
- mountPath: /redis-master-data
name: data
- mountPath: /redis-master
name: config
volumes:
- name: data
emptyDir: {}
- name: config
configMap:
name: redis-config
items:
- key: redis-config
path: redis.conf
kubectl apply -f redis-deploy.yaml
kubectl get pod
kubectl exec -it redis -- redis-cli
PING
kubectl get svc redis
kubectl get svc redis --output 'jsonpath={.spec.ports[*].nodePort}'
apiVersion: v1
kind: Secret
metadata:
name: redis-credentials
namespace: default
stringData:
HOST: "<cluster-ip-address>"
PORT: "<port>"
type: Opaque
kubectl apply -f redis-creds.yaml
secret/redis-credentials created
spec.template.spec.containers
section of the deployment yaml definition and add three environment variables in a new env
section as shown below. The three environment variables reference the values from the secrets created above.DEPLOYMENT=$(cat <<EOF''
apiVersion: apps/v1
kind: Deployment
metadata:
name: $NAME
spec:
replicas: 1
selector:
matchLabels:
app: $NAME
template:
metadata:
labels:
app: $NAME
spec:
containers:
- name: $NAME
image: $IMAGE
imagePullPolicy: IfNotPresent
ports:
- containerPort: $PORT
env:
- name: REDIS_HOST
valueFrom:
secretKeyRef:
name: redis-credentials
key: HOST
optional: false
- name: REDIS_PORT
valueFrom:
secretKeyRef:
name: redis-credentials
key: PORT
optional: false
- name: MONGODB_URL
valueFrom:
secretKeyRef:
name: mongodb-credentials
key: URL
optional: false
---
apiVersion: v1
kind: Service
metadata:
name: $NAME
labels:
app: $NAME
spec:
type: NodePort
ports:
- port: $PORT
selector:
app: $NAME
EOF
)