Chapter 5. Using OpenShift Enterprise
When you initialize the CDK Vagrant box using the rhel-ose Vagrantfile, which is provided as a part of Red Hat Container Tools (located in cdk/components/rhel/rhel-ose/Vagrantfile in the ZIP file), the launched virtual machine automatically provisions an instance of OpenShift Enterprise. (See the CDK Installation Guide for additional information on how to launch CDK with a specific Vagrantfile.)
OpenShift Enterprise is a Platform as a Service (PaaS) offering by Red Hat that extends the functionality of the Docker service and the Kubernetes container orchestration tool to provide a powerful and easy-to-use platform for building, deploying, and orchestrating multi-container applications and services.
Command-line usage of the OpenShift service provided by the CDK box is possible both from within the virtual machine and from the host machine. This section provides examples of basic usage of the oc command. Note that you can also administer a subset of OpenShift features using its web user interface, which is accesible at https://10.1.2.2:8443/console.
See also the OpenShift Enterprise Developer Guide for detailed instructions on how to set up and configure a workstation to develop and deploy applications in an OpenShift cloud environment with a command-line interface (CLI) and the web console.
5.1. Preparing Your Host System for Using OpenShift リンクのコピーリンクがクリップボードにコピーされました!
To use the oc command on your host system to interact with the OpenShift service running inside the CDK virtual machine, you need to obtain and install the oc executable.
If you intend to use OpenShift (and the oc command) only from within the CDK virtual machine, no preparation is required — the oc command is installed in the CDK box by default.
5.1.1. Obtaining the oc Executable リンクのコピーリンクがクリップボードにコピーされました!
Download an archive with the executable for your operating system (Linux, Mac OS X, or Windows) from the Red Hat Customer Portal: Red Hat OpenShift Enterprise (you need to log in to the Customer Portal and have an active OpenShift Enterprise subscription to be able to access the download page).
Note that downloading this archive is not necessary if you use Red Hat Enterprise Linux as your host system because the oc executable can be installed from system repositories using yum. See Installing the oc Executable on Red Hat Enterprise Linux for details.
5.1.2. Installing the oc Executable on Red Hat Enterprise Linux リンクのコピーリンクがクリップボードにコピーされました!
Install the atomic-openshift-clients package, which provides the oc executable. The package is available from the rhel-7-server-ose-3.1-rpms (Red Hat OpenShift Enterprise 3.1) repository. Use the following command as root to enable the repository (you need to have an active OpenShift Enterprise subscription to be able to access the repository):
subscription-manager repos --enable=rhel-7-server-ose-3.1-rpms
~]# subscription-manager repos --enable=rhel-7-server-ose-3.1-rpms
Install the atomic-openshift-clients package using the following command as root:
yum install -y atomic-openshift-clients
~]# yum install -y atomic-openshift-clients
5.1.3. Installing the oc Executable on Fedora or Other Linux Distribution リンクのコピーリンクがクリップボードにコピーされました!
Unpack the oc executable from the archive obtained using instructions in Obtaining the oc Executable to a directory of your choice and add that directory to your PATH, so that the oc command can be used without specifying its location.
For example, to unpack the executable to the ~/openshift directory and configure the PATH variable accordingly, run the following commands from the directory to which you downloaded the archive:
5.1.4. Installing the oc Executable on Mac OS X リンクのコピーリンクがクリップボードにコピーされました!
Unpack the oc executable from the archive obtained using instructions in Obtaining the oc Executable to a directory of your choice and add that directory to your PATH, so that the oc command can be used without specifying its location.
For example, to unpack the executable to the ~/openshift directory and configure the PATH variable accordingly, run the following commands from the directory to which you downloaded the archive:
5.1.5. Installing the oc.exe Executable on Windows リンクのコピーリンクがクリップボードにコピーされました!
Unpack the oc.exe executable from the archive obtained using instructions in Obtaining the oc Executable to a folder of your choice and add that folder to your PATH, so that the oc command can be used without specifying its location.
For example, to unpack the executable to the C:\Program Files (x86)\openshift folder and configure the PATH variable accordingly, run the following commands (using the Command Prompt or other terminal emulator) from the folder to which you downloaded the archive:
5.2. Verifying the OpenShift Installation リンクのコピーリンクがクリップボードにコピーされました!
Upon initializing the CDK Vagrant box using the rhel-ose Vagrantfile, the following information is output about the OpenShift service that has been provisioned for you:
The automatically provisioned instance of OpenShift Enterprise in CDK is provided in the form of a container. You can check that the OpenShift container is installed and running by examining the output of the docker ps command:
docker ps
~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
...
ddb2d13604ef openshift "/usr/bin/openshift s" 2 hours ago Up 2 hours openshift
You can also run a health check to determine whether OpenShift is running properly by querying its network interface using the curl command:
curl -k https://10.1.2.2:8443/healthz
~]$ curl -k https://10.1.2.2:8443/healthz
ok
5.3. Displaying Information about the OpenShift Service リンクのコピーリンクがクリップボードにコピーされました!
The command-line interface of OpenShift is accessed using the oc command. This command has a number of subcommands that help to interact with various features of the OpenShift service. This section describes how to use the oc command to gather basic information about the current OpenShift instance.
5.3.1. Checking the Version of OpenShift and Kubernetes リンクのコピーリンクがクリップボードにコピーされました!
Use the oc version command to display the version of OpenShift Enterprise and the Kubernetes service currently in use:
oc version
~]$ oc version
oc v3.1.0.4-16-g112fcc4
kubernetes v1.1.0-origin-1107-g4c8e6f4
5.4. Logging in and out of the OpenShift Server リンクのコピーリンクがクリップボードにコピーされました!
Use the oc login command to log into specific user profiles. Upon start up, the CDK box logs you in automatically to the openshift-dev account.
5.4.1. Logging in to the OpenShift Server リンクのコピーリンクがクリップボードにコピーされました!
Use the oc login command to log into the OpenShift server. If you want top log in from your hosty system, you to specify a host address (10.1.2.2:8443). The command asks for the username and password interactively, but you can also supply this information on the command line using the -u (--username) and -p (--password) options respectively. For example, to log into the default OpenShift instance in CDK from your host system, use the following command:
Use the oc whoami command to check the user account to which you are currently logged:
oc whoami
~]$ oc whoami
openshift-dev
5.4.2. Viewing Current OpenShift CLI Configuration リンクのコピーリンクがクリップボードにコピーされました!
To display the configuration values for the command-line environment, use the oc config view. For example, to show the part of the configuration pertaining to the default user name (openshift-dev), issue the following command:
oc config view | grep -A2 "name: openshift-dev"
~]$ oc config view | grep -A2 "name: openshift-dev"
- name: openshift-dev/10-1-2-2:8443
user:
token: THdHS4sw1qgzs95CnCf5ic6D8eOrZss1aT5jyGOmd2w
5.4.3. Logging out of the OpenShift Server リンクのコピーリンクがクリップボードにコピーされました!
To log out, use the oc logout command:
oc logout
~]$ oc logout
Logged "openshift-dev" out on "https://10.1.2.2:8443"
5.5. Working with OpenShift Projects リンクのコピーリンクがクリップボードにコピーされました!
In OpenShift, projects are used to organize and manage applications by groups of users. Individual projects keep their content separated from each other, and individual users need to be granted access to projects. Upon start up, the CDK box automatically creates the sample-project project.
5.5.1. Creating a New OpenShift Project リンクのコピーリンクがクリップボードにコピーされました!
Use the oc new-project command to create a new project on the OpenShift server you are currently logged in to. The following example uses the optional --display-name option to set a user-friendly name for the project:
5.5.2. Switching to a Different OpenShift Project リンクのコピーリンクがクリップボードにコピーされました!
Use the oc project <project-name> command to switch to another project on the OpenShift server you are currently logged in to. The following switches to the default sample-project project:
oc project sample-project
~]$ oc project sample-project
Now using project "sample-project" on server "https://10.1.2.2:8443".
5.5.3. Listing Available Projects リンクのコピーリンクがクリップボードにコピーされました!
To list projects available on the server and their status, use the oc get projects command. Note that the oc get command can be used to display information about a number of other resources besides projects (pods, builds, services, etc.). In the following example, only the project created in the preceeding example is shown:
oc get project
~]$ oc get project
NAME DISPLAY NAME STATUS
testing Test Project Active
5.5.4. Displaying Status Information about the Currently Used Project リンクのコピーリンクがクリップボードにコピーされました!
Run the oc status command to display information about the currently used project and about activities (services, builds, deployments, etc.) within that project. When executed in a new (empty) project, the output looks like the following:
oc status
~]$ oc status
In project Test Project (testing) on server https://10.1.2.2:8443
You have no services, deployment configs, or build configs.
Run 'oc new-app' to create an application.
Alternatively, you can also use the oc project command to display project information:
oc project
~]$ oc project
Using project "testing" from context named "testing/10-1-2-2:8443/openshift-dev" on server "https://10.1.2.2:8443".
5.5.5. Deleting a Project リンクのコピーリンクがクリップボードにコピーされました!
To remove a project from the server, use the oc delete project command and specify the name of the project you want deleted. To remove all projects, include the --all option. Note that the oc delete command can be used to delete other resources (pods, services, etc.) as well. For example, to delete the project created in this section, run the following command:
oc delete project testing
~]$ oc delete project testing
project "testing" deleted
5.6. Working with OpenShift Templates リンクのコピーリンクがクリップボードにコピーされました!
A template in OpenShift is a text file (in the JSON or YAML format) that defines a set of objects. The object definitions, such as services or build configurations, can be parametrized. Templates can be processed to create the objects they describe and thus populate your current project.
To get you quickly started with application development, OpenShift offers a number of basic templates, which are included in the global openshift project. In order to use these templates as the foundation block of a new application, they need to be downloaded from the openshift project and subsequently uploaded to your current project. You can also create a new application (using the oc new-app command) directly from a template.
5.6.1. Listing Available Templates リンクのコピーリンクがクリップボードにコピーされました!
To display the templates available in a project, issue the oc get templates command and use the -n option to specify the project from which you wish to list the templates. For example, to list the templates available in the global openshift project, run:
5.6.2. Downloading an InstantApp Template リンクのコピーリンクがクリップボードにコピーされました!
To obtain a template definition file, which can be used for uploading to your project, use the oc get templates command and specify the project in which the template is located (using the -n or --namespace option), choose the format in which you wish to store the template (typically JSON or YAML — use the -o or --output option), and supply the name of the requested template. Note that by default, the oc get command outputs the requested data to the standard output. To store the template in a file, redirect the output of the command to a file.
For example, to download the nodejs-mongodb-example template from the global openshift project and save it in the JSON format on your local syastem, use the following command:
oc get templates -o json -n openshift nodejs-mongodb-example \ > nodejs-mongodb-example.json ls
~]$ oc get templates -o json -n openshift nodejs-mongodb-example \ > nodejs-mongodb-example.json
~]$ ls
nodejs-mongodb-example.json
5.6.3. Modifying the Namespace (Project Name) リンクのコピーリンクがクリップボードにコピーされました!
When you download an InstantApp template from the default openshift project, the namespace (project name) in the template is set to openshift. To be able to upload the template to your project, you need to modify the template accordingly. If you do not do that, the upload fails with the following error message:
oc create -f nodejs-mongodb-example.json -n testing
~]$ oc create -f nodejs-mongodb-example.json -n testing
the namespace from the provided object "openshift" does not match the namespace "testing". You must pass '--namespace=openshift' to perform this operation.
To modify the namespace, edit the file using a text editor, such as vi, and in the metadata section, replace the value of the namespace parameter with the name of your project. For example, to use the name testing, the beginning of the nodejs-mongodb-example.json template file would look like this:
5.6.4. Uploading a Template to the Current Project リンクのコピーリンクがクリップボードにコピーされました!
For a template to be available for use in your project, it must be first uploaded to it. Use the oc create command to do this and specify the template file using the -f or --filename option. For example, to upload the nodejs-mongodb-example InstantApp template obtained from the openshift project, run the following command:
oc create -f nodejs-mongodb-example.json
~]$ oc create -f nodejs-mongodb-example.json
template "nodejs-mongodb-example" created
To verify that the template has been successfully uploaded, use the oc get templates command. Optionally, you can specify the namespace from which you wish to list by using the -n or --namespace option. By default, templates from the currently used project are listed:
oc get templates
~]$ oc get templates
NAME DESCRIPTION PARAMETERS OBJECTS
nodejs-mongodb-example An example Node.js application with a MongoDB database 11 (3 blank) 7
5.6.5. Listing Template Parameters リンクのコピーリンクがクリップボードにコピーされました!
To list the parameters that a template specifies along with short descriptions and predefined values, use the oc process command with the --parameters option. For example, to list the parameters used by the nodejs-mongodb-example template, use the following command:
5.6.6. Creating Objects from a Template リンクのコピーリンクがクリップボードにコピーされました!
To process a template and use it to create the objects it defines in your project, use a combination of the oc process and oc create commands. The template processed by the oc process command can be piped to the oc create command.
Note that in most cases, the template parameters need to be modified, so that the resulting application can be useful in a real-world scenario. To modify the template parameters when creating the objects, use the -v or --value option.
For example, to create objects using the nodejs-mongodb-example template and to set a different source repository for the application (the original repository would need to be forked to the repository specified on the command line), use the following combination of commands:
In the above example, substitute username for the name of the GitHub account into which the original repository was forked.
5.7. Additional Resources リンクのコピーリンクがクリップボードにコピーされました!
-
See the output of the
oc helpcommand for an overview of allocsubcommands available. More detailed help for individual subcommands can be accessed by runningoc help [subcommand]. - See the OpenShift Enterprise CLI Reference for detailed descriptions of the command-line user interface used to interact with OpenShift Enterprise.
- See the OpenShift Enterprise Developer Guide for detailed instructions and examples to help developers configure a workstation to develop and deploy applications in an OpenShift Enterprise cloud environment with a command-line interface.