This documentation is for a release that is no longer maintained
See documentation for the latest supported version 3 or the latest supported version 4.Questo contenuto non è disponibile nella lingua selezionata.
Chapter 4. Creating and building an application using the CLI
4.1. Before you begin Copia collegamentoCollegamento copiato negli appunti!
- Review About the OpenShift CLI.
- You must be able to access a running instance of OpenShift Container Platform. If you do not have access, contact your cluster administrator.
-
You must have the OpenShift CLI (
oc
) downloaded and installed.
4.2. Logging in to the CLI Copia collegamentoCollegamento copiato negli appunti!
You can log in to the OpenShift CLI (oc
) to access and manage your cluster.
Prerequisites
- You must have access to an OpenShift Container Platform cluster.
-
You must have installed the OpenShift CLI (
oc
).
Procedure
Log into OpenShift Container Platform from the CLI using your username and password or with an OAuth token:
With username and password:
oc login -u=<username> -p=<password> --server=<your-openshift-server> --insecure-skip-tls-verify
$ oc login -u=<username> -p=<password> --server=<your-openshift-server> --insecure-skip-tls-verify
Copy to Clipboard Copied! Toggle word wrap Toggle overflow With an OAuth token:
oc login <https://api.your-openshift-server.com> --token=<tokenID>
$ oc login <https://api.your-openshift-server.com> --token=<tokenID>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
You can now create a project or issue other commands for managing your cluster.
4.3. Creating a new project Copia collegamentoCollegamento copiato negli appunti!
A project enables a community of users to organize and manage their content in isolation. Projects are OpenShift Container Platform extensions to Kubernetes namespaces. Projects have additional features that enable user self-provisioning.
Users must receive access to projects from administrators. Cluster administrators can allow developers to create their own projects. In most cases, users automatically have access to their own projects.
Each project has its own set of objects, policies, constraints, and service accounts.
Prerequisites
- You must have access to an OpenShift Container Platform cluster.
-
You must have installed the OpenShift CLI (
oc
).
Procedure
To create a new project, enter the following command:
oc new-project user-getting-started --display-name="Getting Started with OpenShift"
$ oc new-project user-getting-started --display-name="Getting Started with OpenShift"
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
Now using project "user-getting-started" on server "https://openshift.example.com:6443".
Now using project "user-getting-started" on server "https://openshift.example.com:6443".
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
4.4. Granting view permissions Copia collegamentoCollegamento copiato negli appunti!
OpenShift Container Platform automatically creates a few special service accounts in every project. The default service account takes responsibility for running the pods. OpenShift Container Platform uses and injects this service account into every pod that launches.
The following procedure creates a RoleBinding
object for the default ServiceAccount
object. The service account communicates with the OpenShift Container Platform API to learn about pods, services, and resources within the project.
Prerequisites
- You must have access to an OpenShift Container Platform cluster.
-
You must have installed the OpenShift CLI (
oc
). - You have a deployed image.
-
You must have
cluster-admin
orproject-admin
privileges.
Procedure
To add the view role to the default service account in the
user-getting-started project
, enter the following command:oc adm policy add-role-to-user view -z default -n user-getting-started
$ oc adm policy add-role-to-user view -z default -n user-getting-started
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
4.5. Deploying your first image Copia collegamentoCollegamento copiato negli appunti!
The simplest way to deploy an application in OpenShift Container Platform is to run an existing container image. The following procedure deploys a front-end component of an application called national-parks-app
. The web application displays an interactive map. The map displays the location of major national parks across the world.
Prerequisites
- You must have access to an OpenShift Container Platform cluster.
-
Install the OpenShift CLI (
oc
).
Procedure
To deploy an application, enter the following command:
oc new-app quay.io/openshiftroadshow/parksmap:latest --name=parksmap -l 'app=national-parks-app,component=parksmap,role=frontend,app.kubernetes.io/part-of=national-parks-app'
$ oc new-app quay.io/openshiftroadshow/parksmap:latest --name=parksmap -l 'app=national-parks-app,component=parksmap,role=frontend,app.kubernetes.io/part-of=national-parks-app'
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
4.5.1. Creating a route Copia collegamentoCollegamento copiato negli appunti!
External clients can access applications running on OpenShift Container Platform through the routing layer and the data object behind that is a route. The default OpenShift Container Platform router (HAProxy) uses the HTTP header of the incoming request to determine where to proxy the connection.
Optionally, you can define security, such as TLS, for the route.
Prerequisites
- You must have access to an OpenShift Container Platform cluster.
-
You must have installed the OpenShift CLI (
oc
). - You have a deployed image.
-
You must have
cluster-admin
orproject-admin
privileges.
Procedure
To retrieve the created application service, enter the following command:
oc get service
$ oc get service
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE parksmap ClusterIP <your-cluster-IP> <123.456.789> 8080/TCP 8m29s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE parksmap ClusterIP <your-cluster-IP> <123.456.789> 8080/TCP 8m29s
Copy to Clipboard Copied! Toggle word wrap Toggle overflow To create a route, enter the following command:
oc create route edge parksmap --service=parksmap
$ oc create route edge parksmap --service=parksmap
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
route.route.openshift.io/parksmap created
route.route.openshift.io/parksmap created
Copy to Clipboard Copied! Toggle word wrap Toggle overflow To retrieve the created application route, enter the following command:
oc get route
$ oc get route
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
NAME HOST/PORT PATH SERVICES PORT TERMINATION WILDCARD parksmap parksmap-user-getting-started.apps.cluster.example.com parksmap 8080-tcp edge None
NAME HOST/PORT PATH SERVICES PORT TERMINATION WILDCARD parksmap parksmap-user-getting-started.apps.cluster.example.com parksmap 8080-tcp edge None
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
4.5.2. Examining the pod Copia collegamentoCollegamento copiato negli appunti!
OpenShift Container Platform leverages the Kubernetes concept of a pod, which is one or more containers deployed together on one host, and the smallest compute unit that can be defined, deployed, and managed. Pods are the rough equivalent of a machine instance, physical or virtual, to a container.
You can view the pods in your cluster and to determine the health of those pods and the cluster as a whole.
Prerequisites
- You must have access to an OpenShift Container Platform cluster.
-
You must have installed the OpenShift CLI (
oc
). - You have a deployed image.
Procedure
To list all pods with node names, enter the following command:
oc get pods
$ oc get pods
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
NAME READY STATUS RESTARTS AGE parksmap-5f9579955-6sng8 1/1 Running 0 77s
NAME READY STATUS RESTARTS AGE parksmap-5f9579955-6sng8 1/1 Running 0 77s
Copy to Clipboard Copied! Toggle word wrap Toggle overflow To list all pod details, enter the following command:
oc describe pods
$ oc describe pods
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
4.5.3. Scaling the application Copia collegamentoCollegamento copiato negli appunti!
In Kubernetes, a Deployment
object defines how an application deploys. In most cases, users use Pod
, Service
, ReplicaSets
, and Deployment
resources together. In most cases, OpenShift Container Platform creates the resources for you.
When you deploy the national-parks-app
image, a deployment resource is created. In this example, only one Pod
is deployed.
The following procedure scales the national-parks-image
to use two instances.
Prerequisites
- You must have access to an OpenShift Container Platform cluster.
-
You must have installed the OpenShift CLI (
oc
). - You have a deployed image.
Procedure
To scale your application from one pod instance to two pod instances, enter the following command:
oc scale --current-replicas=1 --replicas=2 deployment/parksmap
$ oc scale --current-replicas=1 --replicas=2 deployment/parksmap
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
deployment.apps/parksmap scaled
deployment.apps/parksmap scaled
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
To ensure that your application scaled properly, enter the following command:
oc get pods
$ oc get pods
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
NAME READY STATUS RESTARTS AGE parksmap-5f9579955-6sng8 1/1 Running 0 7m39s parksmap-5f9579955-8tgft 1/1 Running 0 24s
NAME READY STATUS RESTARTS AGE parksmap-5f9579955-6sng8 1/1 Running 0 7m39s parksmap-5f9579955-8tgft 1/1 Running 0 24s
Copy to Clipboard Copied! Toggle word wrap Toggle overflow To scale your application back down to one pod instance, enter the following command:
oc scale --current-replicas=2 --replicas=1 deployment/parksmap
$ oc scale --current-replicas=2 --replicas=1 deployment/parksmap
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
4.6. Deploying a Python application Copia collegamentoCollegamento copiato negli appunti!
The following procedure deploys a back-end service for the parksmap
application. The Python application performs 2D geo-spatial queries against a MongoDB database to locate and return map coordinates of all national parks in the world.
The deployed back-end service is nationalparks
.
Prerequisites
- You must have access to an OpenShift Container Platform cluster.
-
You must have installed the OpenShift CLI (
oc
). - You have a deployed image.
Procedure
To create a new Python application, enter the following command:
oc new-app python~https://github.com/openshift-roadshow/nationalparks-py.git --name nationalparks -l 'app=national-parks-app,component=nationalparks,role=backend,app.kubernetes.io/part-of=national-parks-app,app.kubernetes.io/name=python' --allow-missing-images=true
$ oc new-app python~https://github.com/openshift-roadshow/nationalparks-py.git --name nationalparks -l 'app=national-parks-app,component=nationalparks,role=backend,app.kubernetes.io/part-of=national-parks-app,app.kubernetes.io/name=python' --allow-missing-images=true
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
Copy to Clipboard Copied! Toggle word wrap Toggle overflow To create a route to expose your application,
nationalparks
, enter the following command:oc create route edge nationalparks --service=nationalparks
$ oc create route edge nationalparks --service=nationalparks
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
route.route.openshift.io/parksmap created
route.route.openshift.io/parksmap created
Copy to Clipboard Copied! Toggle word wrap Toggle overflow To retrieve the created application route, enter the following command:
oc get route
$ oc get route
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
NAME HOST/PORT PATH SERVICES PORT TERMINATION WILDCARD nationalparks nationalparks-user-getting-started.apps.cluster.example.com nationalparks 8080-tcp edge None parksmap parksmap-user-getting-started.apps.cluster.example.com parksmap 8080-tcp edge None
NAME HOST/PORT PATH SERVICES PORT TERMINATION WILDCARD nationalparks nationalparks-user-getting-started.apps.cluster.example.com nationalparks 8080-tcp edge None parksmap parksmap-user-getting-started.apps.cluster.example.com parksmap 8080-tcp edge None
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
4.7. Connecting to a database Copia collegamentoCollegamento copiato negli appunti!
Deploy and connect a MongoDB database where the national-parks-app
application stores location information. Once you mark the national-parks-app
application as a backend for the map visualization tool, parksmap
deployment uses the OpenShift Container Platform discover mechanism to display the map automatically.
Prerequisites
- You must have access to an OpenShift Container Platform cluster.
-
You must have installed the OpenShift CLI (
oc
). - You have a deployed image.
Procedure
To connect to a database, enter the following command:
oc new-app quay.io/centos7/mongodb-36-centos7 --name mongodb-nationalparks -e MONGODB_USER=mongodb -e MONGODB_PASSWORD=mongodb -e MONGODB_DATABASE=mongodb -e MONGODB_ADMIN_PASSWORD=mongodb -l 'app.kubernetes.io/part-of=national-parks-app,app.kubernetes.io/name=mongodb'
$ oc new-app quay.io/centos7/mongodb-36-centos7 --name mongodb-nationalparks -e MONGODB_USER=mongodb -e MONGODB_PASSWORD=mongodb -e MONGODB_DATABASE=mongodb -e MONGODB_ADMIN_PASSWORD=mongodb -l 'app.kubernetes.io/part-of=national-parks-app,app.kubernetes.io/name=mongodb'
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
4.7.1. Creating a secret Copia collegamentoCollegamento copiato negli appunti!
The Secret
object provides a mechanism to hold sensitive information such as passwords, OpenShift Container Platform client configuration files, private source repository credentials, and so on. Secrets decouple sensitive content from the pods. You can mount secrets into containers using a volume plugin or the system can use secrets to perform actions on behalf of a pod. The following procedure adds the secret nationalparks-mongodb-parameters
and mounts it to the nationalparks
workload.
Prerequisites
- You must have access to an OpenShift Container Platform cluster.
-
You must have installed the OpenShift CLI (
oc
). - You have a deployed image.
Procedure
To create a secret, enter the following command:
oc create secret generic nationalparks-mongodb-parameters --from-literal=DATABASE_SERVICE_NAME=mongodb-nationalparks --from-literal=MONGODB_USER=mongodb --from-literal=MONGODB_PASSWORD=mongodb --from-literal=MONGODB_DATABASE=mongodb --from-literal=MONGODB_ADMIN_PASSWORD=mongodb
$ oc create secret generic nationalparks-mongodb-parameters --from-literal=DATABASE_SERVICE_NAME=mongodb-nationalparks --from-literal=MONGODB_USER=mongodb --from-literal=MONGODB_PASSWORD=mongodb --from-literal=MONGODB_DATABASE=mongodb --from-literal=MONGODB_ADMIN_PASSWORD=mongodb
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
secret/nationalparks-mongodb-parameters created
secret/nationalparks-mongodb-parameters created
Copy to Clipboard Copied! Toggle word wrap Toggle overflow To update the environment variable to attach the mongodb secret to the
nationalpartks
workload, enter the following command:oc set env --from=secret/nationalparks-mongodb-parameters deploy/nationalparks
$ oc set env --from=secret/nationalparks-mongodb-parameters deploy/nationalparks
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
deployment.apps/nationalparks updated
deployment.apps/nationalparks updated
Copy to Clipboard Copied! Toggle word wrap Toggle overflow To show the status of the
nationalparks
deployment, enter the following command:oc rollout status deployment nationalparks
$ oc rollout status deployment nationalparks
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
deployment "nationalparks" successfully rolled out
deployment "nationalparks" successfully rolled out
Copy to Clipboard Copied! Toggle word wrap Toggle overflow To show the status of the
mongodb-nationalparks
deployment, enter the following command:oc rollout status deployment mongodb-nationalparks
$ oc rollout status deployment mongodb-nationalparks
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
deployment "nationalparks" successfully rolled out deployment "mongodb-nationalparks" successfully rolled out
deployment "nationalparks" successfully rolled out deployment "mongodb-nationalparks" successfully rolled out
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
4.7.2. Loading data and displaying the national parks map Copia collegamentoCollegamento copiato negli appunti!
You deployed the parksmap
and nationalparks
applications and then deployed the mongodb-nationalparks
database. However, no data has been loaded into the database.
Prerequisites
- You must have access to an OpenShift Container Platform cluster.
-
You must have installed the OpenShift CLI (
oc
). - You have a deployed image.
Procedure
To load national parks data, enter the following command:
oc exec $(oc get pods -l component=nationalparks | tail -n 1 | awk '{print $1;}') -- curl -s http://localhost:8080/ws/data/load
$ oc exec $(oc get pods -l component=nationalparks | tail -n 1 | awk '{print $1;}') -- curl -s http://localhost:8080/ws/data/load
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
"Items inserted in database: 2893"
"Items inserted in database: 2893"
Copy to Clipboard Copied! Toggle word wrap Toggle overflow To verify that your data is loaded properly, enter the following command:
oc exec $(oc get pods -l component=nationalparks | tail -n 1 | awk '{print $1;}') -- curl -s http://localhost:8080/ws/data/all
$ oc exec $(oc get pods -l component=nationalparks | tail -n 1 | awk '{print $1;}') -- curl -s http://localhost:8080/ws/data/all
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output (trimmed)
, {"id": "Great Zimbabwe", "latitude": "-20.2674635", "longitude": "30.9337986", "name": "Great Zimbabwe"}]
, {"id": "Great Zimbabwe", "latitude": "-20.2674635", "longitude": "30.9337986", "name": "Great Zimbabwe"}]
Copy to Clipboard Copied! Toggle word wrap Toggle overflow To add labels to the route, enter the following command:
oc label route nationalparks type=parksmap-backend
$ oc label route nationalparks type=parksmap-backend
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
route.route.openshift.io/nationalparks labeled
route.route.openshift.io/nationalparks labeled
Copy to Clipboard Copied! Toggle word wrap Toggle overflow To retrieve your routes to view your map, enter the following command:
oc get routes
$ oc get routes
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
NAME HOST/PORT PATH SERVICES PORT TERMINATION WILDCARD nationalparks nationalparks-user-getting-started.apps.cluster.example.com nationalparks 8080-tcp edge None parksmap parksmap-user-getting-started.apps.cluster.example.com parksmap 8080-tcp edge None
NAME HOST/PORT PATH SERVICES PORT TERMINATION WILDCARD nationalparks nationalparks-user-getting-started.apps.cluster.example.com nationalparks 8080-tcp edge None parksmap parksmap-user-getting-started.apps.cluster.example.com parksmap 8080-tcp edge None
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Copy and paste the
HOST/PORT
path you retrieved above into your web browser. Your browser should display a map of the national parks across the world.Figure 4.1. National parks across the world