Add databases

In this lab you will connect two types of databases to your service.

Connect to your cluster via the command line

  1. Utilize the IBM Cloud CLI to login to your account
    ibmcloud login
    
  2. Install the Kubernetes command-line plugin ibmcloud plugin install container-service

  3. List all available Kubernetes clusters
    ibmcloud cs clusters
    
  4. Export the Kubernetes config to your local environment
    ibmcloud cs cluster config --cluster <your-clusters-name>
    

Deploy a MongoDB database into your cluster

Mongo DB will be deployed as a local installation in your cluster.

  1. Clone the install script project
    >git clone https://github.com/thomassuedbroecker/mongodb-container-kubernetes.git
    >cd mongodb-container-kubernetes
    
  2. 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>

  3. Run the install script ./setup-mongo-db-and-ui.sh

  4. Note down the database endpoints returned by the script
    Link to the mongo db: http://<ip-address>:<port>
    Link to the mongo db WebUI: http://<ip-address>:<port>
    
  5. Check that both links contain a valid ip-address.

  6. Check that mogo db has started correctly
    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.

  7. Create a new yaml file called “mongodb-creds.yaml”
    apiVersion: v1
    kind: Secret
    metadata:
      name: mongodb-credentials
      namespace: default
    stringData:
      URL: "<'Link to the Mongo DB' from above without the trailing '/'>"
    type: Opaque
    
  8. Utilize kubectl to create the secret
kubectl apply -f mongodb-creds.yaml

secret/mongodb-credentials created

Deploy a Redis service instance

  1. Create a local file called “redis-conf.yaml” and put the following content into the file:
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: redis-config
    data:
      redis-config: |
         maxmemory 2mb
         maxmemory-policy allkeys-lru
    
  2. Create the ConfigMap in your cluster
    kubectl apply -f redis-conf.yaml
    
  3. Create a local file called “redis-deploy.yaml” and put the following content into the file
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
  1. Create the redis instance in your cluster
    kubectl apply -f redis-deploy.yaml
    
  2. Check whether the redis pod starts properly
    kubectl get pod
    
  3. Enter the created pod using “kubectl exec”
    kubectl exec -it redis -- redis-cli
    
  4. Use the PING command to check whether the redis instance works as expected. (If the server communicates with the console, it returns PONG as the answer)
    PING
    
  5. Obtain the IP address of your redis service
    kubectl get svc redis
    
  6. Obtain the port of your redis service
    kubectl get svc redis --output 'jsonpath={.spec.ports[*].nodePort}'
    
  7. Create a new yaml file called “redis-creds.yaml”
    apiVersion: v1
    kind: Secret
    metadata:
      name: redis-credentials
      namespace: default
    stringData:
      HOST: "<cluster-ip-address>"
      PORT: "<port>"
    type: Opaque
    
  8. Utilize kubectl to create the secret
kubectl apply -f redis-creds.yaml

secret/redis-credentials created

Reference the credentials in the deployment

  1. In the deploy stage of your CI/CD pipeline you’ll find the deploy script, which is executed on each run. In order to adjust the deployed kubernetes container to your need, you’ll need to adjust the script.
  2. Edit the 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
)

References


  • Reset App Name