Configure a service to establish a connection between two VMs, one with a MariaDB server and the other with a database client. You then define a network policy to limit access to the database server to pods within the network-services project.
Outcomes
Create a service of the ClusterIP type.
Link the service to a virtual machine.
Create a network policy to protect a virtual machine.
Add a label to a namespace.
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.
This command also creates the network-services namespace and starts two virtual machines in that namespace: the mariadb-server VM, which hosts a MariaDB database, and the mariadb-client VM, which provides the client mysql command-line tool.
[student@workstation ~]$ lab start network-services
Instructions
As the admin user, navigate to the Red Hat OpenShift web console, and confirm that the mariadb-server VM and the mariadb-client VM are running.
From a command line, log in to your Red Hat 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
Login Successful
...output omitted...Identify the URL for the OpenShift web console.
[student@workstation ~]$ oc whoami --show-console
https://console-openshift-console.apps.ocp4.example.comSet the network-services project as the active project.
[student@workstation ~]$ oc project network-services
Now using project "network-services" on server "https://api.ocp4.example.com:6443".Open a web browser and navigate to https://console-openshift-console.apps.ocp4.example.com.
Select and log in as the admin user with redhatocp as the password.
Navigate to → and then select the network-services project.
Confirm that the mariadb-client and mariadb-server VMs are running.
![]() |
Add the app: mariadb-server label to the mariadb-server VM.
You use this label later in the exercise.
From the web console, navigate to → , select the mariadb-server VM, and then navigate to the tab to access the YAML editor.
![]() |
Add the app: mariadb-server label in the .spec.template.metadata.labels path.
...output omitted...
spec:
dataVolumeTemplates:
...output omitted...
template:
metadata:
creationTimestamp: null
labels:
app: mariadb-server
flavor.template.kubevirt.io/small: "true"
kubevirt.io/domain: mariadb-server
kubevirt.io/size: small
...output omitted...Click to save the changes.
![]() |
Restart the VM so that Kubernetes re-creates an instance that includes the new label. From the web console, click → .
![]() |
From the command line, use oc get vmi,pods command to confirm that Kubernetes re-creates the VMI resource and the virt-launcher pod.
You might have to run the command several times because it takes time for the pod to start.
[student@workstation ~]$oc get vmi,podsNAME AGE PHASE IP ... virtualmachineinstance.kubevirt.io/mariadb-client 24m Running 10.8.2.33 ...virtualmachineinstance.kubevirt.io/mariadb-server94s Running 10.11.0.41 ... NAME READY STATUS RESTARTS AGE pod/virt-launcher-mariadb-client-dcjtc 1/1 Running 0 24mpod/virt-launcher-mariadb-server-274ts1/1 Running 0 94s
Create a service named mariadb with the ClusterIP type.
Use the app: mariadb-server label for the selector.
The service must listen on TCP port 3306 and forward the traffic to the VM on port 3306.
From the web console, navigate to → , select the network-services project, and then click .
Complete the YAML file with the following content and then click .
By using this configuration, the DNS operator creates the mariadb.network-services.svc.cluster.local record.
apiVersion: v1 kind: Service metadata:name: mariadbnamespace: network-services spec:type: ClusterIPselector:app: mariadb-serverports: - protocol: TCPport: 3306targetPort: 3306
The lab command prepared the ~/DO316/labs/network-services/solutions/mariadb.yaml file so that you can compare it with your version.
![]() |
From the command line, confirm that the mariadb service has an active endpoint.
[student@workstation ~]$ oc get endpoints
NAME ENDPOINTS AGE
mariadb 10.11.0.41:3306 110sLog in to the mariadb-client VM, and then verify database connectivity.
From the web console, navigate to → , select the mariadb-client VM, and then access the tab.
![]() |
Log in as the developer user with developer as the password.
Connect to the sakila database that is running on the mariadb-server VM.
Use the DNS service name to access the database and log in as the devuser user with developer as the password.
[developer@mariadb-client ~]$ mysql -u devuser -p'developer' \
-h mariadb.network-services.svc.cluster.local sakila
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.28-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(sakila)]>List the tables of the sakila database, and log out.
MariaDB [sakila]>SHOW TABLES;\+------------------+ | Tables_in_sakila | \+------------------+ | actors | | actor_info | | address | | category | ...output omitted... MariaDB [sakila]>quitBye [developer@mariadb-client ~]$
Inspect and confirm that the DHCP server that is running inside the virt-launcher pod configures the VM system to use the short service name.
Review the search line in the /etc/resolv.conf file.
With that configuration, you can refer to the mariadb service as mariadb, mariadb.network-services, or mariadb.network-services.svc.
[developer@mariadb-client ~]$cat /etc/resolv.conf# Generated by NetworkManager searchnetwork-services.svc.cluster.local svc.cluster.local cluster.localocp4.example.com srv.example.com nameserver 172.30.0.10 [developer@mariadb-client ~]$
Access the sakila database by using the mariadb.network-services host.
Log out of the database after confirming that you can use the short name.
[developer@mariadb-client ~]$mysql -u devuser -p'developer' \ -h mariadb.network-services sakilaWelcome to the MariaDB monitor. Commands end with ; or \g. ...output omitted... MariaDB [sakila]>quitBye [developer@mariadb-client ~]$
Verify that you can access the database that is running on the mariadb-server VM from any namespace.
Start an interactive pod by using the quay.io/redhattraining/mariadb:10.5 image, which provides the mysql client command.
From the command line, run a temporary interactive pod in the default namespace.
The pod uses the quay.io/redhattraining/mariadb:10.5 image, which provides the mysql command.
[student@workstation ~]$ oc run testdb -it --rm -n default \
--image=quay.io/redhattraining/mariadb:10.5 --command \
-- mysql --connect-timeout=5 -u devuser -p'developer' \
-h mariadb.network-services.svc.cluster.local sakila
If you don't see a command prompt, try pressing enter.
MariaDB [(sakila)]>You can copy and paste the command from the ~/DO316/labs/network-services/solutions/commands.txt file that the lab command prepared for you.
Execute an SQL query to verify connectivity. When done, log out of the database.
The test confirms that you can access the service from any namespace.
MariaDB [sakila]>SHOW TABLES;\+----------------------------+ | Tables_in_sakila | \+----------------------------+ | actor | | actor_info | | address | | category | ...output omitted... MariaDB [sakila]>quitBye Session ended, resume using 'oc attach testdb -c testdb -i -t' command when the pod is running pod "testdb" deleted [student@workstation ~]$
Protect the database server so that only clients running in the network-services namespace can access it.
Add the allowing=db-clients label to the network-services namespace.
You use that label later when you create the network policy.
From the web console, navigate to → , select the network-services namespace, and then access the tab.
Click in the section.
![]() |
Add the allowing=db-clients label and then click .
Do not remove any labels that might exist.
![]() |
Prepare the network policy resource.
From the web console, navigate to → , select the network-services project, click , and then click .
Complete the file with the following content and then click .
kind: NetworkPolicy apiVersion: networking.k8s.io/v1 metadata: name: allow-db-clients namespace: network-services spec: podSelector:matchLabels: app: mariadb-server ingress:
- from: - namespaceSelector: matchLabels: allowing: db-clients ports: - port: 3306 protocol: TCP
The | |
The |
The ~/DO316/labs/network-services/solutions/policy.yaml file contains the correct configuration, and you can use it for comparison.
Confirm that the mariadb-client VM can still access the database.
That VM is running in the network-services namespace.
Confirm that pods that are running in a different namespace can no longer access the database.
Access the serial console of the mariadb-client VM, and connect to the database server to use the sakila database to list the available tables.
[developer@mariadb-client ~]$mysql -u devuser -p'developer' \ -h mariadb.network-services sakila...output omitted... MariaDB [sakila]>SHOW TABLES;\+------------------+ | Tables_in_sakila | \+------------------+ | actors | | actor_info | | address | | category | ...output omitted...
From the command line, run the test pod again in the default namespace.
The command does not display the MariaDB prompt, and times out after five seconds.
The timeout confirms that you can access the database only from namespaces with the allowing: db-clients label.
[student@workstation ~]$oc run testdb -it --rm -n default \ --image=quay.io/redhattraining/mariadb:10.5 --command -- \ mysql --connect-timeout=5 -u devuser -p'developer' \ -h mariadb.network-services.svc.cluster.local sakilaIf you don't see a command prompt, try pressing enter.EnterERROR 2002 (HY000): Can't connect to MySQL server on 'mariadb.network-services.svc.cluster.local' (115) ...output omitted...