Outcomes
In this exercise, you deploy two MySQL database server pods to compare deployment methods and the resources that each creates.
Deploy a database from a template.
Deploy a database from a container image.
As the student user on the workstation machine, use the lab command to prepare your system for this exercise.
This command ensures that resources are available for the exercise.
[student@workstation ~]$ lab start deploy-newapp
Instructions
As the
developeruser, create a project and verify that it is not empty after creation.Log in as the
developeruser with thedeveloperpassword.[student@workstation ~]$
oc login -u developer -p developer \ https://api.ocp4.example.com:6443...output omitted...Create a project named
deploy-newapp.[student@workstation ~]$
oc new-project deploy-newappNow using project "deploy-newapp" on server ...output omitted...The new project is automatically selected.
Observe that resources for the new project are not returned with the
oc get allcommand.[student@workstation ~]$
oc get allNo resources found in deploy-newapp namespace.Important
Commands that use
allfor the resource type do not include every available resource type. Instead,allis a shorthand form for a predefined subset of types. When you use this command argument, ensure thatallincludes any types that you intend to address.Observe that the new project contains other types of resources.
[student@workstation ~]$
oc get serviceaccountsNAME SECRETS AGE builder 1 20s default 1 20s deployer 1 20s[student@workstation ~]$
oc get secretsNAME TYPE DATA AGE builder-dockercfg-sczxg kubernetes.io/dockercfg 1 36m builder-token-gsnqj kubernetes.io/service-account-token 4 36m default-dockercfg-6f8nm kubernetes.io/dockercfg 1 36m ...output omitted...
Create two MySQL instances by using the
oc new-appcommand with different options.View the
mysql-persistenttemplate definition to see the resources that it creates. Specify the project that houses the template by using the-n openshiftoption.[student@workstation ~]$
oc describe template mysql-persistent -n openshiftName: mysql-persistent Namespace: openshift ...output omitted... Objects: Secret ${DATABASE_SERVICE_NAME} Service ${DATABASE_SERVICE_NAME} PersistentVolumeClaim ${DATABASE_SERVICE_NAME} DeploymentConfig.apps.openshift.io ${DATABASE_SERVICE_NAME}The
objectsattribute specifies several resource definitions that are applied on using the template. These resources include one of each of the following types: secret, service (svc), persistent volume claim (pvc), and deployment configuration (dc).Create an instance by using the
mysql-persistenttemplate. Specify anameoption and attach a customteam=redlabel to the created resources.[student@workstation ~]$
oc new-app -l team=red --template mysql-persistent \ -p MYSQL_USER=developer \ -p MYSQL_PASSWORD=developer...output omitted... --> Creating resources with label team=red ...secret"mysql" createdservice"mysql" createdpersistentvolumeclaim"mysql" createddeploymentconfig.apps.openshift.io"mysql" created --> Success ...output omitted...The template creates resources of the types from the preceding step.
View and wait for the pod to start, which takes a few minutes to complete. You might need to run the command several times before the status changes to
Running.[student@workstation ~]$
oc get podsNAME READY STATUS RESTARTS AGE mysql-1-deploy0/1Completed 0 93s mysql-1-qmxbf 1/1 Running 1 60sYour pod names might differ from the previous output.
Create an instance by using a container image. Specify a
nameoption and attach a customteam=bluelabel to the created resources.[student@workstation ~]$
oc new-app --name db-image -l team=blue \ --image registry.ocp4.example.com:8443/rhel9/mysql-80:1 \ -e MYSQL_USER=developer \ -e MYSQL_PASSWORD=developer \ -e MYSQL_ROOT_PASSWORD=redhat...output omitted... --> Creating resources with label team=blue ... imagestream.image.openshift.io "db-image" created deployment.apps "db-image" created service "db-image" created --> Success ...output omitted...The command creates predefined resources that are needed to deploy an image. These resource types are image stream (
is), deployment, and service (svc). Image streams and services are discussed in more detail elsewhere in the course.Note
It is safe to ignore pod security warnings for exercises in this course. OpenShift uses the Security Context Constraints controller to provide safe defaults for pod security.
Wait for the pod to start. After a few moments, list all pods and the value of the
teamlabel for each pod.[student@workstation ~]$
oc get pods -L teamNAME READY STATUS RESTARTS AGE TEAM db-image-8d4b97594-6jb85 1/1 Running 0 55s blue mysql-1-deploy 0/1 Completed 0 2m mysql-1-hn64v 1/1 Running 0 1m30sYour pod name might differ from the previous output. Without a
readinessProbe, this pod shows as ready before the MySQL service is ready for requests. Readiness probes are discussed in more detail elsewhere in the course.Notice that only the
db-imagepod has a label that contains the wordteam. Pods that themysql-persistenttemplate creates do not have theteam=redlabel, because the template does not define this label in its pod specification template.
Compare the resources that each image and template method creates.
View the
template-createdpod and observe that it contains a readiness probe.[student@workstation ~]$
oc get pods -l deploymentconfig=mysql \ -o jsonpath='{.items[0].spec.containers[0].readinessProbe}' | jq{ "exec": { "command": [ "/bin/sh", "-i", "-c", "MYSQL_PWD=\"$MYSQL_PASSWORD\" mysqladmin -u $MYSQL_USER ping" ] }, "failureThreshold": 3, "initialDelaySeconds": 5, "periodSeconds": 10, "successThreshold": 1, "timeoutSeconds": 1 }Note
The results of the preceding
occommand are passed to thejqcommand, which formats the JSON output.Observe that the image-based pod does not contain a readiness probe.
[student@workstation ~]$
oc get pods -l deployment=db-image \ -o jsonpath='{.items[0].spec.containers[0].readinessProbe}' | jqno output expectedObserve that the template-based pod has a memory resource limit, which restricts allocated memory to the resulting pods. Resource limits are discussed in more detail elsewhere in the course.
[student@workstation ~]$
oc get pods -l deploymentconfig=mysql \ -o jsonpath='{.items[0].spec.containers[0].resources.limits}' | jq{ "memory": "512Mi" }Observe that the image-based pod has no resource limits.
[student@workstation ~]$
oc get pods -l deployment=db-image \ -o jsonpath='{.items[0].spec.containers[0].resources}' | jq{}Retrieve secrets in the project. Notice that the template produced a secret, whereas the pod that was created with only an image did not.
[student@workstation ~]$
oc get secretsNAME TYPE DATA AGE ...output omitted... mysql Opaque 4 3m
Explore filtering resources via labels.
Observe that not supplying a label shows all services.
[student@workstation ~]$
oc get servicesNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE db-image ClusterIP 172.30.38.113 <none> 3306/TCP 1m30s mysql ClusterIP 172.30.95.52 <none> 3306/TCP 2m30sObserve that supplying a label shows only the services with the label.
[student@workstation ~]$
oc get services -l team=redNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE mysql ClusterIP 172.30.95.52 <none> 3306/TCP 2m43sObserve that not all resources include the label, such as the pods that are created with the template.
[student@workstation ~]$
oc get pods -l team=redNo resources found in deploy-newapp namespace.
Use labels to delete only the resources that are associated with the template-based deployment.
Delete only the resources that use the
team=redlabel by using it with theoc deletecommand. List the resource types from the template to ensure that all relevant resources are deleted.[student@workstation ~]$
oc delete all -l team=redreplicationcontroller "mysql-1" deleted service "mysql" deleted ...output omitted... deploymentconfig.apps.openshift.io "mysql" deleted[studen@workstation ~]$
oc delete secret,pvc -l team=redsecret "mysql" deleted persistentvolumeclaim "mysql" deletedNote
By using the
oc delete all -l team=redcommand, some resources are deleted, but the persistent volume claim and secret remain.Observe that the resources that the template created are deleted.
[student@workstation ~]$
oc get secret,svc,pvc,dc -l team=red...output omitted... No resources found in deploy-newapp namespace.Observe that the image-based resources remain unchanged.
[student@workstation ~]$
oc get is,deployment,svcNAME IMAGE REPOSITORY ...output omitted... imagestream.image.openshift.io/db-imageimage-registry.openshift...output omitted... NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/db-image1/1 1 1 46m NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/db-imageClusterIP 172.30.71.0 <none> 3306/TCP 46m