One of the benefits of OpenShift over a traditional PaaS supporting only 12-factor or cloud native applications is that you have access to persistent volumes. This means you can attach storage to your web applications, or run applications such as databases.
Databases deployed to OpenShift will typically be used to support the operations of a front end web application and therefore only need to be accessible by other applications running in the same OpenShift cluster.
To manage a database, it can though be convenient to use a database client running on your own local machine. This could be a command line client, or a GUI based application.
In this course you will learn how to access a database using an interactive shell, and also how to use port forwarding to temporarily expose a database outside of OpenShift, allowing you to access it from a database tool running on your own local machine.
You will be using just the
oc command line tool in this exercise.
In this course you learnt about
oc commands you would use for setting up a temporary connection between your local machine and a service running inside of OpenShift.
You can find a summary of the key commands covered below. To see more information on each
oc command, run it with the
oc rsh <pod-name>: Start an interactive shell in the specified pod.
oc port-forward <pod-name> <local-port>:<remote-port>: Forward connections between your local machine and an application running in OpenShift. The remote port is the port the application running in OpenShift accepts connections. The local port is the port on your local machine you wish to make it available as, and to which any client application running on your local machine would connect to.
oc port-forward <pod-name> :<remote-port>: Forward connections between your local machine and an application running in OpenShift. The remote port is the port the application running in OpenShift accepts connections. As a local port to use is not specified, a random local port is used, with the port number being displayed. Any client application running on your local machine would connect to the randomly assigned port.
Connecting to a Database Using Port Forwarding
Topic 1 - Creating an Initial Project
Before we get started, you need to login and create a project in OpenShift to work in.
To login to the OpenShift cluster used for this course from the Terminal, run:
oc login -u developer -p developer
This will log you in using the credentials:
You should see the output:
Login successful. You don't have any projects. You can try to create a new project, by running oc new-project <projectname>
To create a new project called
myproject run the command:
oc new-project myproject
You should see output similar to:
Now using project "myproject" on server "https://172.17.0.41:8443". You can add applications to this project with the 'new-app' command. For example, try: oc new-app centos/ruby-22-centos7~https://github.com/openshift/ruby-ex.git to build a new example application in Ruby.
We are not going to use the web console for this course, but if you want to check anything from the web console, switch to the Dashboard and use the same credentials to login as you used above to login from the command line.