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
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
Change to the ~/DO280/labs/declarative-kustomize directory.
[student@workstation ~]$ cd DO280/labs/declarative-kustomize
[student@workstation declarative-kustomize]$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...Change to the repository directory.
[student@workstation declarative-kustomize]$ cd declarative-kustomizeExamine the first version of the application.
Use the tree command to review the structure of the repository.
[student@workstation declarative-kustomize]$tree. ├── base │ ├── database│ │ ├── configmap.yaml │ │ ├── deployment.yaml │ │ ├── kustomization.yaml │ │ └── service.yaml │ ├── exoplanets
│ │ ├── deployment.yaml │ │ ├── kustomization.yaml │ │ ├── route.yaml │ │ └── service.yaml │ └── kustomization.yaml
└── README.md 3 directories, 10 files
Examine the base/kustomization.yaml file.
[student@workstation declarative-kustomize]$cat base/kustomization.yamlkind: Kustomization resources:- database - exoplanets secretGenerator:
- name: db-secrets literals: - DB_ADMIN_PASSWORD=postgres - DB_NAME=database - DB_PASSWORD=password - DB_USER=user configMapGenerator:
- name: db-config literals: - DB_HOST=database - DB_PORT=5432
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.
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...Create the declarative-kustomize project.
[student@workstation declarative-kustomize]$ oc new-project declarative-kustomize
...output omitted...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 createdUse 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.
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.
Change to the v1.1.1 version of the application and examine the changes.
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'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.1imagePullPolicy: Always livenessProbe: httpGet:
The v1.1.1 version updates the application to the v1.1.1 image.
Deploy the updated application and verify that the URL now displays the v1.1.1 version.
Use the oc apply -k command to execute the changes.
[student@workstation declarative-kustomize]$ oc apply -k base
...output omitted...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.
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.
Change to the v1.1.2 version of the application and examine the changes.
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'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.
List the secrets in the namespace.
[student@workstation declarative-kustomize]$oc get secretNAME TYPE DATA AGE builder-dockercfg-qwn4v kubernetes.io/dockercfg 1 4m31s builder-token-z754n kubernetes.io/service-account-token 4 4m31sdb-secrets-55cbgc8c6m Opaque 4 4m28sdefault-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.
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
databaseDeploy the updated application.
Use the oc apply -k command to execute the changes.
[student@workstation declarative-kustomize]$oc apply -k baseconfigmap/database unchanged configmap/db-config-2d7thbcgkc unchangedsecret/db-secrets-6h668tk789 createdservice/database unchanged service/exoplanets unchangeddeployment.apps/database configureddeployment.apps/exoplanets configuredroute.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.
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.
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.
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.
Examine the secret. Use the name of the secret from a previous step.
[student@workstation declarative-kustomize]$oc extract \ secret/db-secrets-# DB_ADMIN_PASSWORD postgres # DB_NAME database # DB_PASSWORD6h668tk789--to=-newpassword# DB_USER user
The deployment uses the changed password.
Change to the v1.1.3 version of the application and examine the changes.
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'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: 2The v1.1.3 version adds a production overlay that increases the number of replicas.
Deploy the updated application and verify the number of replicas.
Use the oc apply -k command to execute the changes.
[student@workstation declarative-kustomize]$ oc apply -k overlays/production
...output omitted...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.
Delete the application.
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" deletedChange to the home directory.
[student@workstation declarative-kustomize]$ cd
[student@workstation ~]$