Exercise - Enhance the application with configmaps

Completed

You deployed the application back end, and now you need to deploy the application front end using a ConfigMap.

Before we start

Note

This exercise is optional. If you want to complete this exercise, you'll need to create an Azure subscription before you begin. If you don't have an Azure account or you don't want to create one at this time, you can read through the instructions so you understand the information that's being presented.

Create a ConfigMap

  1. Create a new YAML file named configmap.yaml and paste in the following code to create the ConfigMap spec:

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: ship-manager-config
      namespace: default
    data:
      config.js: |
        const config = (() => {
          return {
            'VUE_APP_BACKEND_BASE_URL': 'http://ship-manager-backend.{your-dns-zone}.aksapp.io',
          }
        })()
    
  2. Replace {your-dns-zone} with the value of the ZONE_NAME variable you created earlier.

  3. Save and close the file.

  4. Apply the changes to your cluster using the kubectl apply command.

    kubectl apply -f configmap.yaml
    
  5. Check the result by querying for the ConfigMap using the kubectl get configmap command.

    kubectl get configmap ship-manager-config
    

Create the application

  1. Create a new YAML file named frontend.yaml and paste in the following code to create the Deployment spec:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: contoso-ship-manager-frontend
      namespace: default
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: contoso-ship-manager-frontend
      template:
        metadata:
          labels:
            app: contoso-ship-manager-frontend
        spec:
          containers:
            - image: mcr.microsoft.com/mslearn/samples/contoso-ship-manager:frontend
              name: contoso-ship-manager-frontend
              ports:
                - containerPort: 80
                  name: http
              volumeMounts:
                - name: config
                  mountPath: /usr/src/app/dist/config.js
                  subPath: config.js
          volumes:
            - name: config
              configMap:
                name: ship-manager-config
    ---
    

    Notice how the ConfigMap is mounted in the Deployment object. We don't specify any keys, which means we need to specify a subPath key. The subpath is the filename inside the container.

  2. Below the three dashes, paste in the following code to create the Service and Ingress specs:

    apiVersion: v1
    kind: Service
    metadata:
      name: contoso-ship-manager-frontend
      namespace: default
    spec:
      selector:
        app: contoso-ship-manager-frontend
      ports:
        - name: http
          port: 80
          targetPort: 80
    ---
    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: contoso-ship-manager-frontend
      namespace: default
      annotations:
        spec.ingressClassName: webapprouting.kubernetes.azure.com
    spec:
      rules:
        - host: contoso-ship-manager.{your-dns-zone}.aksapp.io
          http:
            paths:
              - path: /
                pathType: Prefix
                backend:
                  service:
                    name: contoso-ship-manager-frontend
                    port: 
                      name: http
    
  3. Replace {your-dns-zone} in the Ingress with the value of the ZONE_NAME variable you created earlier.

  4. Save and close the file.

  5. Deploy the application using the kubectl apply command.

    kubectl apply -f frontend.yaml
    
  6. Check the result by querying the Kubernetes API using the kubectl get deployment command.

    kubectl get deployment contoso-ship-manager-frontend
    

    When the API is available, you should get an output similar to the following example:

    NAME                           READY   UP-TO-DATE   AVAILABLE   AGE
    contoso-ship-manager-frontend  1/1     1            1           18s