Bookmark this page

Guided Exercise: Kustomize Overlays

Deploy and update an application by applying different Kustomize overlays that are stored in a Git server.

Outcomes

  • Deploy an application by using Kustomize from provided files.

  • Apply an application update that changes a deployment.

  • Deploy an overlay of the application that increases the number of replicas.

As the student user on the workstation machine, use the lab command to prepare your system for this exercise.

This command ensures that the cluster API is reachable.

[student@workstation ~]$ lab start declarative-kustomize

Instructions

  1. Clone the v1.1.0 version of the application. Because this repository uses Git branches to represent application versions, you must use the v1.1.0 branch.

    Clone the repository from the following URL:

    https://git.ocp4.example.com/developer/declarative-kustomize.git

    1. Change to the ~/DO280/labs/declarative-kustomize directory.

      [student@workstation ~]$ cd DO280/labs/declarative-kustomize
      [student@workstation declarative-kustomize]$
    2. Clone the initial version of the application.

      [student@workstation declarative-kustomize]$ git clone \
        https://git.ocp4.example.com/developer/declarative-kustomize.git --branch v1.1.0
      Cloning into 'declarative-kustomize'...
      ...output omitted...
    3. Change to the repository directory.

      [student@workstation declarative-kustomize]$ cd declarative-kustomize
  2. Examine the first version of the application.

    1. Use the tree command to review the structure of the repository.

      [student@workstation declarative-kustomize]$ tree
      .
      ├── base
      │   ├── database  1
      │   │   ├── configmap.yaml
      │   │   ├── deployment.yaml
      │   │   ├── kustomization.yaml
      │   │   └── service.yaml
      │   ├── exoplanets  2
      │   │   ├── deployment.yaml
      │   │   ├── kustomization.yaml
      │   │   ├── route.yaml
      │   │   └── service.yaml
      │   └── kustomization.yaml  3
      └── README.md
      
      3 directories, 10 files

      1

      The database base defines resources to deploy a database.

      2

      The exoplanets base defines resources to deploy an application that uses the database.

      3

      The repository has a kustomization.yaml file at the root, which uses two other bases.

    2. Examine the base/kustomization.yaml file.

      [student@workstation declarative-kustomize]$ cat base/kustomization.yaml
      kind: Kustomization
      resources:  1
      - database
      - exoplanets
      secretGenerator:  2
      - name: db-secrets
        literals:
          - DB_ADMIN_PASSWORD=postgres
          - DB_NAME=database
          - DB_PASSWORD=password
          - DB_USER=user
      configMapGenerator:  3
      - name: db-config
        literals:
          - DB_HOST=database
          - DB_PORT=5432

      1

      The base/kustomization.yaml file uses the other two bases.

      2 3

      The base also uses generators to provide configuration for the two deployments in the application.

  3. Deploy the base directory of the repository to a new declarative-kustomize project. Verify that the v1.1.0 version of the application is available at http://exoplanets-declarative-kustomize.apps.ocp4.example.com.

    1. Log in to the OpenShift cluster as the developer user with the developer password.

      [student@workstation declarative-kustomize]$ oc login -u developer -p developer \
        https://api.ocp4.example.com:6443
      Login successful.
      
      ...output omitted...
    2. Create the declarative-kustomize project.

      [student@workstation declarative-kustomize]$ oc new-project declarative-kustomize
      ...output omitted...
    3. Use the oc apply -k command to deploy the application with Kustomize.

      [student@workstation declarative-kustomize]$ oc apply -k base
      configmap/database created
      configmap/db-config-2d7thbcgkc created
      secret/db-secrets-55cbgc8c6m created
      service/database created
      service/exoplanets created
      deployment.apps/database created
      deployment.apps/exoplanets created
      route.route.openshift.io/exoplanets created
    4. Use the watch command to wait until the workloads are running.

      [student@workstation declarative-kustomize]$ watch oc get all
      NAME                             READY   STATUS    RESTARTS      AGE
      pod/database-55d6c77787-47649    1/1     Running   0             57s
      pod/exoplanets-d6f57869d-jhkhc   1/1     Running   2 (54s ago)   57s
      
      NAME                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
      service/database     ClusterIP   172.30.236.123   <none>        5432/TCP   57s
      service/exoplanets   ClusterIP   172.30.248.130   <none>        8080/TCP   57s
      
      NAME                         READY   UP-TO-DATE   AVAILABLE   AGE
      deployment.apps/database     1/1     1            1           57s
      deployment.apps/exoplanets   1/1     1            1           57s
      
      NAME                                   DESIRED   CURRENT   READY   AGE
      replicaset.apps/database-55d6c77787    1         1         1       57s
      replicaset.apps/exoplanets-d6f57869d   1         1         1       57s
      
      NAME
        HOST/PORT                                                ...
      route.route.openshift.io/exoplanets
        exoplanets-declarative-kustomize.apps.ocp4.example.com   ...

      Press Ctrl+C to exit the watch command.

    5. Open a web browser and navigate to http://exoplanets-declarative-kustomize.apps.ocp4.example.com.

      The browser displays the v1.1.0 version of the application.

  4. Change to the v1.1.1 version of the application and examine the changes.

    1. Change to the v1.1.1 branch.

      [student@workstation declarative-kustomize]$ git checkout v1.1.1
      branch 'v1.1.1' set up to track 'origin/v1.1.1'.
      Switched to a new branch 'v1.1.1'
    2. Use the git show command to display the last commit.

      [student@workstation declarative-kustomize]$ git show
      ...output omitted...
      diff --git a/base/exoplanets/deployment.yaml b/base/exoplanets/deployment.yaml
      index 8bc4cf9..8389b69 100644
      --- a/base/exoplanets/deployment.yaml
      +++ b/base/exoplanets/deployment.yaml
      @@ -23,7 +23,7 @@ spec:
                   name: exoplanets
               - secretRef:
                   name: exoplanets
      -        image: registry.ocp4.example.com:8443/redhattraining/exoplanets:v1.1.0
      +        image: registry.ocp4.example.com:8443/redhattraining/exoplanets:v1.1.1
               imagePullPolicy: Always
               livenessProbe:
                 httpGet:

      The v1.1.1 version updates the application to the v1.1.1 image.

  5. Deploy the updated application and verify that the URL now displays the v1.1.1 version.

    1. Use the oc apply -k command to execute the changes.

      [student@workstation declarative-kustomize]$ oc apply -k base
      ...output omitted...
    2. Use the watch command to wait until the application redeploys.

      [student@workstation declarative-kustomize]$ watch oc get all
      NAME                             READY   STATUS    RESTARTS      AGE
      pod/database-55d6c77787-47649    1/1     Running   0             57s
      pod/exoplanets-d6f57869d-jhkhc   1/1     Running   2 (54s ago)   57s
      
      NAME                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
      service/database     ClusterIP   172.30.236.123   <none>        5432/TCP   57s
      service/exoplanets   ClusterIP   172.30.248.130   <none>        8080/TCP   57s
      
      NAME                         READY   UP-TO-DATE   AVAILABLE   AGE
      deployment.apps/database     1/1     1            1           57s
      deployment.apps/exoplanets   1/1     1            1           57s
      
      NAME                                   DESIRED   CURRENT   READY   AGE
      replicaset.apps/database-55d6c77787    1         1         1       57s
      replicaset.apps/exoplanets-d6f57869d   1         1         1       57s
      
      NAME
        HOST/PORT                                                ...
      route.route.openshift.io/exoplanets
        exoplanets-declarative-kustomize.apps.ocp4.example.com   ...

      Press Ctrl+C to exit the watch command.

    3. Open a web browser and navigate to http://exoplanets-declarative-kustomize.apps.ocp4.example.com.

      The browser displays the v1.1.1 version of the application.

  6. Change to the v1.1.2 version of the application and examine the changes.

    1. Change to the v1.1.2 branch.

      [student@workstation declarative-kustomize]$ git checkout v1.1.2
      branch 'v1.1.2' set up to track 'origin/v1.1.2'.
      Switched to a new branch 'v1.1.2'
    2. Use the git show command to display the last commit.

      [student@workstation declarative-kustomize]$ git show
      ...output omitted...
      diff --git a/base/kustomization.yaml b/base/kustomization.yaml
      index fdf129a..8de16e8 100644
      --- a/base/kustomization.yaml
      +++ b/base/kustomization.yaml
      @@ -7,7 +7,7 @@ secretGenerator:
         literals:
           - DB_ADMIN_PASSWORD=postgres
           - DB_NAME=database
      -    - DB_PASSWORD=password
      +    - DB_PASSWORD=newpassword
           - DB_USER=user
       configMapGenerator:
       - name: db-config

      The v1.1.2 version updates the base kustomization. This update changes the password that the database uses. This change is possible because the sample application re-creates the database on startup.

    3. List the secrets in the namespace.

      [student@workstation declarative-kustomize]$ oc get secret
      NAME                       TYPE                                  DATA   AGE
      builder-dockercfg-qwn4v    kubernetes.io/dockercfg               1      4m31s
      builder-token-z754n        kubernetes.io/service-account-token   4      4m31s
      db-secrets-55cbgc8c6m      Opaque                                4      4m28s
      default-dockercfg-w4v89    kubernetes.io/dockercfg               1      4m31s
      default-token-zw89c        kubernetes.io/service-account-token   4      4m31s
      deployer-dockercfg-l8sct   kubernetes.io/dockercfg               1      4m31s
      deployer-token-knvhb       kubernetes.io/service-account-token   4      4m31s

      When creating a secret, Kustomize appends a hash to the secret name.

    4. Extract the contents of the secret. The name of the secret can change in your environment. Use the output from a previous step to learn the name of the secret.

      [student@workstation declarative-kustomize]$ oc extract \
        secret/db-secrets-55cbgc8c6m --to=-
      # DB_PASSWORD
      password
      # DB_USER
      user
      # DB_ADMIN_PASSWORD
      postgres
      # DB_NAME
      database
  7. Deploy the updated application.

    1. Use the oc apply -k command to execute the changes.

      [student@workstation declarative-kustomize]$ oc apply -k base
      configmap/database unchanged
      configmap/db-config-2d7thbcgkc unchanged
      secret/db-secrets-6h668tk789 created
      service/database unchanged
      service/exoplanets unchanged
      deployment.apps/database configured
      deployment.apps/exoplanets configured
      route.route.openshift.io/exoplanets configured

      Because the password is different, Kustomize creates another secret. Kustomize also updates the two deployments that use the secret to use the new secret.

    2. Use the watch command to wait until the application redeploys.

      [student@workstation declarative-kustomize]$ watch oc get all
      NAME                             READY   STATUS    RESTARTS      AGE
      pod/database-55d6c77787-47649    1/1     Running   0             57s
      pod/exoplanets-d6f57869d-jhkhc   1/1     Running   2 (54s ago)   57s
      
      NAME                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
      service/database     ClusterIP   172.30.236.123   <none>        5432/TCP   57s
      service/exoplanets   ClusterIP   172.30.248.130   <none>        8080/TCP   57s
      
      NAME                         READY   UP-TO-DATE   AVAILABLE   AGE
      deployment.apps/database     1/1     1            1           57s
      deployment.apps/exoplanets   1/1     1            1           57s
      
      NAME                                   DESIRED   CURRENT   READY   AGE
      replicaset.apps/database-55d6c77787    1         1         1       57s
      replicaset.apps/exoplanets-d6f57869d   1         1         1       57s
      
      NAME
        HOST/PORT                                                ...
      route.route.openshift.io/exoplanets
        exoplanets-declarative-kustomize.apps.ocp4.example.com   ...

      Press Ctrl+C to exit the watch command.

    3. Open a web browser and navigate to http://exoplanets-declarative-kustomize.apps.ocp4.example.com.

      The browser continues showing the v1.1.1 version of the application.

    4. Examine the deployment.

      [student@workstation declarative-kustomize]$ oc get deployment exoplanets \
        -o jsonpath='{.spec.template.spec.containers[0].envFrom}{"\n"}'
      [{"configMapRef":{"name":"db-config-2d7thbcgkc"}},{"secretRef":{"name":"db-secrets-6h668tk789"}}]

      The deployment uses the new secret.

    5. Examine the secret. Use the name of the secret from a previous step.

      [student@workstation declarative-kustomize]$ oc extract \
        secret/db-secrets-6h668tk789 --to=-
      # DB_ADMIN_PASSWORD
      postgres
      # DB_NAME
      database
      # DB_PASSWORD
      newpassword
      # DB_USER
      user

      The deployment uses the changed password.

  8. Change to the v1.1.3 version of the application and examine the changes.

    1. Change to the v1.1.3 branch.

      [student@workstation declarative-kustomize]$ git checkout v1.1.3
      branch 'v1.1.3' set up to track 'origin/v1.1.3'.
      Switched to a new branch 'v1.1.3'
    2. Use the git show command to display the last commit.

      [student@workstation declarative-kustomize]$ git show
      ...output omitted...
      diff --git a/overlays/production/kustomization.yaml b/overlays/production/kustomization.yaml
      new file mode 100644
      index 0000000..73bb7fe
      --- /dev/null
      +++ b/overlays/production/kustomization.yaml
      @@ -0,0 +1,8 @@
      +kind: Kustomization
      +resources:
      +- ../../base/
      +patches:
      +- path: patch-replicas.yaml
      +  target:
      +    kind: Deployment
      +    name: exoplanets
      diff --git a/overlays/production/patch-replicas.yaml b/overlays/production/patch-replicas.yaml
      new file mode 100644
      index 0000000..a025aa0
      --- /dev/null
      +++ b/overlays/production/patch-replicas.yaml
      @@ -0,0 +1,6 @@
      +apiVersion: apps/v1
      +kind: Deployment
      +metadata:
      +  name: exoplanets
      +spec:
      +  replicas: 2

      The v1.1.3 version adds a production overlay that increases the number of replicas.

  9. Deploy the updated application and verify the number of replicas.

    1. Use the oc apply -k command to execute the changes.

      [student@workstation declarative-kustomize]$ oc apply -k overlays/production
      ...output omitted...
    2. Use the watch command to wait until the application redeploys.

      [student@workstation declarative-kustomize]$ watch oc get all
      NAME                             READY   STATUS    RESTARTS      AGE
      pod/database-7dfb559cf7-rvxhx    1/1     Running   0             11m
      pod/exoplanets-957bb5b48-5xl2d   1/1     Running   2 (11m ago)   11m
      pod/exoplanets-957bb5b48-mgbrx   1/1     Running   0             19s
      
      NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
      service/database     ClusterIP   172.30.87.214   <none>        5432/TCP   19m
      service/exoplanets   ClusterIP   172.30.25.65    <none>        8080/TCP   19m
      
      NAME                         READY   UP-TO-DATE   AVAILABLE   AGE
      deployment.apps/database     1/1     1            1           19m
      deployment.apps/exoplanets   2/2     2            2           19m
      
      NAME                                    DESIRED   CURRENT   READY   AGE
      replicaset.apps/database-7dfb559cf7     1         1         1       11m
      replicaset.apps/database-d4cd8dcc       0         0         0       19m
      replicaset.apps/exoplanets-6c7b4bb44c   0         0         0       19m
      replicaset.apps/exoplanets-7ccb754c8b   0         0         0       18m
      replicaset.apps/exoplanets-957bb5b48    2         2         2       11m
      
      NAME
        HOST/PORT                                                ...
      route.route.openshift.io/exoplanets
        exoplanets-declarative-kustomize.apps.ocp4.example.com   ...

      Press Ctrl+C to exit the watch command. After you run the command, the application has two replicas.

  10. Delete the application.

    1. Use the oc delete -k command to delete the resources that Kustomize manages.

      [student@workstation declarative-kustomize]$ oc delete -k base
      # Warning: 'bases' is deprecated. Please use 'resources' instead. Run 'kustomize edit fix' to update your Kustomization automatically.
      configmap "database" deleted
      configmap "db-config-2d7thbcgkc" deleted
      secret "db-secrets-h9hdmt2g79" deleted
      service "database" deleted
      service "exoplanets" deleted
      deployment.apps "database" deleted
      deployment.apps "exoplanets" deleted
      route.route.openshift.io "exoplanets" deleted
    2. Change to the home directory.

      [student@workstation declarative-kustomize]$ cd
      [student@workstation ~]$

Finish

On the workstation machine, use the lab command to complete this exercise. This step is important to ensure that resources from previous exercises do not impact upcoming exercises.

[student@workstation ~]$ lab finish declarative-kustomize

Revision: do280-4.14-08d11e1