$ oc login https://api.cluster.chp5-test.npocloud.nl:6443
Authentication required for https://api.cluster.chp5-test.npocloud.nl:6443 (openshift)
Username: UserName
Password:
Login successful.
You don't have any projects. You can try to create a new project, by running
oc new-project <projectname>
Welcome! See 'oc help' to get started.
5 Deploy an App using the OpenShift CLI
Prerequisites
-
You need API URL to the cluster.
-
You need credentials (UserName and Password) to login to the cluster
-
Login to the cluster running
If you have 2FA enabled on your account, you will first need to login tot the OpenShift webconsole. On the top-right corner, select your username and then select "Copy Login Command".
Now select "Display Token". Here you can copy the login command and paste in your terminal.
Introduction
In this lab you will learn to use openshift CLI to deploy an application.
Deploy using OC
Step 1: Add a new project from command line
Note: Please replace UserName with the username assigned to you in the commands below. Project names in OpenShift must be unique across the cluster. In order not to have an overlap between all the users, we are using YourName to enforce uniqueness in the name.
$ oc new-project workshop-mycliproject-YourName --description="My CLI Project" --display-name="CLI Project"
Note: If you get an error message The ProjectRequest "mycliproject-YourName" is invalid
, it means you have not substituted your YourName!!
Upon project creation, OpenShift will automatically switch to the newly created project/namespace. If you wish to view the list of projects, run the following command:
$ oc get projects
If you have more than one project, you can switch to a different one by issuing oc project <project name>
. Although you don’t want to do it now.
You can also check the status of the project by running the following command. It says that the project is currently not running anything.
$ oc status In project CLI Project (workshop-mycliproject-YourName) You have no services, deployment configs, or build configs. Run 'oc new-app' to create an application.
Step 2: Create an application from a Docker Image
Next we will create an application inside the above project using an existing docker image. We will be using a very simple docker image on dockerhub.
First create a new application using the docker image using the
oc new-app
command as shown below:
$ oc new-app redhatworkshops/welcome-php --name=welcome --> Found container image 3e73433 (7 years old) from Docker Hub for "redhatworkshops/welcome-php" chx/welcome-php-1:b68a2d86 -------------------------- Platform for building and running PHP 5.6 applications Tags: builder, php, php56, rh-php56 * An image stream tag will be created as "welcome:latest" that will track this image --> Creating resources ... imagestream.image.openshift.io "welcome" created deployment.apps "welcome" created service "welcome" created --> Success Application is not exposed. You can expose services to the outside world by executing one or more of the commands below: 'oc expose svc/welcome' Run 'oc status' to view your app.
After running the command you can see the pod that was created by the build.
$ oc get pods NAME READY STATUS RESTARTS AGE welcome-1-dkyyq 0/1 Pending 0 0s
Step 3: Add a Route for your application
OpenShift also spins up a service for this application. Run the following command to view the list of services in the project (you can also use oc get svc
shorthand).
$ oc get services NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE welcome 172.30.77.93 <none> 8080/TCP 2m
You will notice the welcome
service was created for this project.
However, there is no route for this application yet. So you cannot access this application from outside.
Now add a route to the service with the following command. oc expose
command will allow you to expose your service to the world so that you can access it from the browser.
$ oc expose service welcome --name=welcome route.route.openshift.io/welcome exposed
Check the route created for your application now
$ oc get route
NAME HOST/PORT PATH SERVICES PORT TERMINATION WILDCARD
welcome welcome-workshop-mycliproject-YourName.apps.first.40.ocpcloud.com welcome 8080-tcp None
Note the URL listed under HOST/PORT
.
Step 4: Try your application
Access the application: Now access the application using curl (looking for 200 status code) or from the browser and see the result
$ curl -Is <route>
Voila!! you created an application on OpenShift using an existing docker image on OpenShift.
Step 4: Clean up
Run the oc get all
command to view all the components that were
created in your project.
$ oc get all NAME READY STATUS RESTARTS AGE pod/welcome-85c8dc949-5q2r6 1/1 Running 0 110s NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/welcome ClusterIP 172.30.81.171 <none> 8080/TCP 112s NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/welcome 1/1 1 1 112s NAME DESIRED CURRENT READY AGE replicaset.apps/welcome-54f9b49fc4 0 0 0 112s replicaset.apps/welcome-85c8dc949 1 1 1 110s NAME IMAGE REPOSITORY TAGS UPDATED imagestream.image.openshift.io/welcome openshift-image-registry.apps.cluster.chp5-test.npocloud.nl/hens-tim-test/welcome latest About a minute ago NAME HOST/PORT PATH SERVICES PORT TERMINATION WILDCARD route.route.openshift.io/welcome welcome-hens-tim-test.apps.cluster.chp5-test.npocloud.nl welcome 8080-tcp None
Now you can delete all these components by running one command.
$ oc delete all --all pod "welcome-86777fcbb5-mjrvr" deleted service "welcome" deleted deployment.apps "welcome" deleted Warning: apps.openshift.io/v1 DeploymentConfig is deprecated in v4.14+, unavailable in v4.10000+ imagestream.image.openshift.io "welcome" deleted route.route.openshift.io "welcome" deleted
You will notice that it has deleted the imagestream for the application, the deploymentconfig, the service and the route.
You can run oc get all
again to make sure the project is empty.
You can now delete the project by running. Substitute your YourName in the command below.
oc delete project workshop-mycliproject-YourName
Congratulations!! You now know how to create a project, an application using an external docker image and navigate around. Get ready for more fun stuff!