Define the fundamentals of GitOps and its use with Kubernetes clusters and applications.
Deploy a GitOps instance for cluster administration.
Deploy another GitOps instance for application developers and administrators.
Outcomes
Use GitOps practices to create a link in the application menu of the OpenShift console.
Manage cron jobs with GitOps practices.
As the student user on the workstation machine, use the lab command to prepare your environment for this exercise, and to ensure that all required resources are available.
[student@workstation ~]$ lab start gitops-review
Instructions
Some teams in your organization execute periodic tasks manually. Your organization wants to evaluate automating the tasks with OpenShift and GitOps practices.
In this exercise, you install the Red Hat OpenShift GitOps operator for this purpose. You create a custom Argo CD instance, a Git repository with a sample periodic task, and an Argo CD application. For convenience, you use the default Argo CD instance to create a link to GitLab in the OpenShift web console.
Users can create cron jobs only by pushing them to the Git repository. The organization can evaluate later how to enable users to manage and troubleshoot the cron jobs.
Install the OpenShift GitOps operator from OperatorHub.
Use the terminal to log in to the OpenShift cluster as the admin user with redhatocp as the password.
[student@workstation ~]$ oc login -u admin -p redhatocp \
https://api.ocp4.example.com:6443
...output omitted...Identify the URL for the OpenShift web console.
[student@workstation ~]$ oc whoami --show-console
https://console-openshift-console.apps.ocp4.example.comOpen a web browser and navigate to https://console-openshift-console.apps.ocp4.example.com. Either type the URL in a web browser, or right-click and select from the terminal.
Click and log in as the admin user with redhatocp as the password.
Navigate to → .
Click , and then click .
Review the default configuration and click . The Operator Lifecycle Manager can take a few minutes to install the operator. Click to navigate to the operator details.
Log in to the default Argo CD instance as a user with administrator privileges.
You can use the local admin user with the credentials from the openshift-gitops-cluster secret in the openshift-gitops namespace.
The operator adds a link to the default instance in the application menu of the OpenShift console.
Open a separate tab and open the default Argo CD instance. You can use the application menu, which is the grid icon on the top navigation bar, by clicking . You can also use the https://openshift-gitops-server-openshift-gitops.apps.ocp4.example.com URL.
The browser displays a warning because Argo CD uses a self-signed certificate. Trust the certificate. Argo CD might take a few minutes before showing the login page.
Extract the password for the local admin user.
In the web console, navigate to → , search for the openshift-gitops-cluster secret in all namespaces, and then click in the detail page to view the password.
Alternatively, you can execute the oc extract -n openshift-gitops secret/openshift-gitops-cluster --to=- command in the terminal to view the password.
Log in to Argo CD by using the admin user and the password from the previous step, and click instead of .
Configure the default instance to trust GitLab to access repositories.
Argo CD accesses only trusted repositories.
You can use the config.openshift.io/inject-trusted-cabundle label to inject the bundle with the cluster certificate authority into a configuration map, and then configure Argo CD to trust the bundle.
The bundle contains the signing certificate for the classroom GitLab instance.
The bundle is in the ca-bundle.crt file in the configuration map, and Argo CD uses the /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem path for trusted certificates in the repository server container.
Change to the terminal window, and create a cluster-root-ca-bundle configuration map in the openshift-gitops namespace.
[student@workstation ~]$ oc create configmap -n openshift-gitops \
cluster-root-ca-bundleAdd the config.openshift.io/inject-trusted-cabundle label to the configuration map with the true value.
OpenShift injects the bundle with the cluster certificate authority into a configuration map with this label.
This bundle contains the signing certificate for the classroom GitLab instance.
[student@workstation ~]$ oc label configmap -n openshift-gitops \
cluster-root-ca-bundle config.openshift.io/inject-trusted-cabundle=true
configmap/cluster-root-ca-bundle labeledEdit the Argo CD default instance to inject the bundle.
You can use the following command to edit the resource:
[student@workstation ~]$ oc edit argocd -n openshift-gitops openshift-gitopsEdit the resource to mount the ca-bundle.crt file from the configuration map in the /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem path of the repository server container.
...output omitted... spec: ...output omitted... repo: resources: limits: cpu: "1" memory: 1Gi requests: cpu: 250m memory: 256MivolumeMounts:- mountPath: /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pemname: cluster-root-ca-bundlesubPath: ca-bundle.crtvolumes:- configMap:name: cluster-root-ca-bundlename: cluster-root-ca-bundleresourceExclusions: | ...output omitted...
Create a gitops-review public repository for the applications in the classroom GitLab at https://git.ocp4.example.com.
Use the developer GitLab user with d3v3lop3r as the password.
Populate the repository with a console link to GitLab.
The following text is an example definition of the console link:
apiVersion: console.openshift.io/v1 kind: ConsoleLink metadata: name: git spec: href: 'https://git.ocp4.example.com' text: Git location: ApplicationMenu
Click , and then copy the https://git.ocp4.example.com/developer/gitops-review.git HTTPS URL.
Change to the ~/DO380/labs/gitops-admin/ directory.
[student@workstation ~]$ cd ~/DO380/labs/gitops-reviewIn a terminal, run the following command to clone the new repository.
[student@workstation gitops-review]$ git clone \
https://git.ocp4.example.com/developer/gitops-review.git
Cloning into 'gitops-review'...
...output omitted...Change to the cloned repository directory.
[student@workstation gitops-review]$ cd gitops-reviewThe default configuration for new repositories adds a README.md initial file.
Create the console directory.
[student@workstation gitops-review]$ mkdir consoleCreate a git_link.yaml file in the console directory with the following contents:
apiVersion: console.openshift.io/v1 kind: ConsoleLink metadata: name: git spec: href: 'https://git.ocp4.example.com' text: Git location: ApplicationMenu
Add the git_link.yaml file to the repository.
[student@workstation gitops-review]$ git add console/git_link.yamlCommit the changes.
[student@workstation gitops-review]$ git commit -m "add git link"
...output omitted...Push the changes.
[student@workstation gitops-review]$ git push
...output omitted...Create an application in Argo CD with the repository and observe the results.
Navigate to Argo CD, and then click .
Create an application with the information in the following table:
| Field | Value |
|---|---|
| Application Name |
gitops-review
|
| Project Name |
default
|
| Repository URL |
https://git.ocp4.example.com/developer/gitops-review.git
|
| Path |
console
|
| Cluster URL |
https://kubernetes.default.svc
|
Then, click .
Click to view the application.
Click to display the synchronization panel, and then click .
Argo CD starts synchronizing the application.
Verify that the Git link appears in the application menu of the OpenShift web console.
As the developer OpenShift user, create a gitops-review project.
Create a configuration map that contains the bundle with the cluster certificate authority by using the same procedure as in a preceding step.
In the terminal, log in as the developer user with developer as the password.
[student@workstation gitops-review]$ oc login -u developer -p developer
Login successful.
...output omitted...Create the gitops-review project.
[student@workstation gitops-review]$ oc new-project gitops-review
Now using project "gitops-review" on server "https://api.ocp4.example.com:6443".
...output omitted...Create a cluster-root-ca-bundle configuration map.
[student@workstation gitops-review]$ oc create configmap cluster-root-ca-bundleAdd the config.openshift.io/inject-trusted-cabundle label to the configuration map with the true value.
[student@workstation gitops-review]$ oc label configmap cluster-root-ca-bundle \
config.openshift.io/inject-trusted-cabundle=true
configmap/cluster-root-ca-bundle labeledAs the admin OpenShift user, create a gitops Argo CD instance in the gitops-review namespace.
You can use the web console to create the Argo CD instance.
When you create the instance, set the server route as enabled, choose the reencrypt termination for the server route, and add the bundle with the cluster certificate authority.
You can use the same procedure to add the bundle from a previous step.
You can also use the credentials for the local admin user in the gitops-cluster secret.
In the web console, navigate to → .
Select the gitops-review project in the project list.
Click in the column, and then click .
Use the form view to create an Argo CD instance.
Use gitops as the resource name.
Expand , and then expand .
Expand and select .
Expand .
In the field, enter reencrypt.
Then, switch to the YAML view and complete the definition with the following excerpt:
apiVersion: argoproj.io/v1beta1 kind: ArgoCD metadata: name: gitops namespace: gitops-review spec:repo:volumeMounts:- mountPath: /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pemname: cluster-root-ca-bundlesubPath: ca-bundle.crtvolumes:- configMap:name: cluster-root-ca-bundlename: cluster-root-ca-bundleserver: route: enabled: true tls: termination: reencrypt
Click .
Navigate to → , click , click on the detail page, and copy the password.
Navigate to → , and then click the URL. Argo CD might take a few minutes before handling requests.
Log in as the admin user, with the password from a previous step.
Click .
Create a periodic-process cron job in the Git repository.
Put the manifest in a jobs directory, which you create.
The cron job runs the echo hello command every minute with the registry.ocp4.example.com:8443/ubi9/ubi image.
You can use the following command to generate a template for the cron job:
[student@workstation gitops-review]$ oc create cronjob periodic-process \
-n gitops-review --schedule "* * * * *" \
--image registry.ocp4.example.com:8443/ubi9/ubi \
--dry-run=client -o yaml -- echo helloCreate the jobs directory.
[student@workstation gitops-review]$ mkdir jobsCreate a jobs/periodic-process.yaml file with the following content:
apiVersion: batch/v1
kind: CronJob
metadata:
name: periodic-process
namespace: gitops-review
spec:
jobTemplate:
metadata:
name: periodic-process
spec:
template:
spec:
containers:
- command:
- echo
- hello
image: registry.ocp4.example.com:8443/ubi9/ubi
name: periodic-process
restartPolicy: OnFailure
schedule: '* * * * *'You can create the file with the output of the previous command, and removing the parts that are not present in the previous content.
Add the periodic-process.yaml file to the repository.
[student@workstation gitops-review]$ git add jobs/periodic-process.yamlCommit the changes.
[student@workstation gitops-review]$ git commit -m "add job"
...output omitted...Push the changes.
[student@workstation gitops-review]$ git push
...output omitted...Create and synchronize an application, and then observe the cron job execution.
Use the second Argo CD instance in the gitops-review namespace, with the https://gitops-server-gitops-review.apps.ocp4.example.com URL.
Navigate to Argo CD, and then click .
Create an application with the information in the following table:
| Field | Value |
|---|---|
| Application Name |
jobs
|
| Project Name |
default
|
| Repository URL |
https://git.ocp4.example.com/developer/gitops-review.git
|
| Path |
jobs
|
| Cluster URL |
https://kubernetes.default.svc
|
Then, click .
Click to view the application.
Click to display the synchronization panel, and then click .
Argo CD starts synchronizing the application. After synchronization completes, the cron job executes within one minute. You can observe the resulting jobs and pods from the Argo CD console. Click the pod, and then select the tab to view the output from the cron job execution.
Change to the /home/student directory.
[student@workstation gitops-admin]$ cd