Bookmark this page

Guided Exercise: Deploying Applications by Using the oc and odo CLIs

Create and deploy applications by using the odo and oc CLI tools.

Outcomes

  • Deploy an application by using oc.

  • Deploy an application by using odo.

As the student user on the workstation machine, use the lab command to prepare your system for this exercise.

The lab start command performs the following tasks:

  • Creates the OpenShift project for the lab.

  • Copies the source code that contains the devfile.yaml to deploy an application.

[student@workstation ~]$ lab start deploy-cli

Instructions

  1. Use the oc command to log in to your OpenShift cluster and change to the deploy-cli project.

    1. Run the oc login command with the developer user and the developer password.

      [student@workstation ~]$ oc login -u developer -p developer \
          https://api.ocp4.example.com:6443
      Login successful.
      
      ...output omitted...
    2. Change to the deploy-cli project.

      [student@workstation ~]$ oc project deploy-cli
      ...output omitted...
  2. Deploy the application by using the oc new-app command and activate external access.

    1. Run the oc new-app command with the container image URL parameter.

      [student@workstation ~]$ oc new-app \
      registry.ocp4.example.com:8443/redhattraining/openshift-dev-deploy-cli-weather:1.0
      --> Found container image 6ef27a6 ...
      ...output omitted...
      --> Success
          Application is not exposed. You can expose services to the outside world by executing one or more of the commands below:
           'oc expose service/openshift-dev-deploy-cli-weather'
          Run 'oc status' to view your app.

      Note

      You can safely ignore pod security warnings for exercises in this course. OpenShift uses the Security Context Constraints controller to provide safe defaults for pod security.

    2. Verify that the oc new-app command creates the following resources:

      • Pod

      • ReplicaSet

      • Deployment

      • Service

      • Image stream

        [student@workstation ~]$ oc get all
        NAME ...
        pod/openshift-dev-deploy-cli-weather-7f78cd4969-cfgts ...
        
        NAME ...
        service/openshift-dev-deploy-cli-weather ...
        
        NAME ...
        deployment.apps/openshift-dev-deploy-cli-weather ...
        
        NAME ...
        replicaset.apps/openshift-dev-deploy-cli-weather-74b5454bc4
        replicaset.apps/openshift-dev-deploy-cli-weather-7f78cd4969 ...
        
        NAME ...
        imagestream.image.openshift.io/openshift-dev-deploy ...
  3. Expose the application outside of the cluster.

    1. Create an external route by using the oc expose command with the --name=weather option to name the route.

      [student@workstation ~]$ oc expose --name=weather \
      service/openshift-dev-deploy-cli-weather
      route.route.openshift.io/weather exposed
    2. Use the oc get command to get the URL for the weather route.

      [student@workstation ~]$ oc get route weather
      NAME          HOST/PORT ...
      weather   weather-deploy-cli.apps.ocp4.example.com ...
    3. Verify that you can access the application from the workstation machine by running the curl command with the previous URL and the /tomorrow path.

      [student@workstation ~]$ curl \
        weather-deploy-cli.apps.ocp4.example.com/tomorrow
      {"rain_chance":"5%","weather":"sunny"}
  4. Log out by using the oc logout command.

    [student@workstation ~]$ oc logout
    Logged "developer" out on "https://api.ocp4.example.com:6443"
  5. Run the odo login command with the developer user and the developer password. If the command prompts you to collect usage data, then you can press Enter to accept.

    [student@workstation ~]$ odo login -u developer -p developer \
        https://api.ocp4.example.com:6443
    Connecting to the OpenShift cluster
    
    Login successful.
    
    ...output omitted...
  6. Use odo to create a project with the odo-deploy-cli name.

    [student@workstation ~]$ odo create project odo-deploy-cli
     ✓  Creating the project "odo-deploy-cli" ...
     ✓  Project "odo-deploy-cli" is ready for use
     ✓  New project created and now using project: odo-deploy-cli
    
    ...output omitted...

    Note

    In some occasions the odo create project command might not change to the new project. You might want to run the following command to ensure that you use the odo-deploy-cli project:

    [student@workstation ~]$ oc project odo-deploy-cli
  7. Examine the contents of the devfile.yaml for the weather application.

    1. Change to the weather application directory.

      [student@workstation ~]$ cd ~/DO288/labs/deploy-cli/weather
    2. Run the cat command to examine the devfile.yaml contents. Verify that the devfile defines the following commands:

      CommandDescription
      build-image Builds and pushes the application image from the build component.
      deployk8s Deploys the Kubernetes resources from the deploy component.

      Note that the current devfile.yaml does not bind to any of the odo commands.

      [student@workstation weather]$ cat devfile.yaml
      commands:
      - apply:
          component: build
        id: build-image
      - apply:
          component: deploy
           <CHANGE_ME>
        id: deployk8s
      components:
      - image:
          dockerfile:
            buildContext: .
            rootRequired: false
            uri: docker/Dockerfile
          imageName: .../openshift-dev-deploy-cli-weather:1.0
        name: build
      - kubernetes:
          endpoints:
          - name: http-8081
            targetPort: 8081
          uri: kubernetes/deploy.yaml
        name: deploy
      ...output omitted...
  8. Deploy the weather application by using odo.

    1. Update the devfile.yaml by using the editor of your choice, such as Vim or VSCodium. Make command with the deployk8s identifier the default deploy command. Replace the CHANGE_ME label to look like the following code:

      commands:
      - apply:
          component: build
        id: build-image
      - apply:
          component: deploy
          group:
            isDefault: true
            kind: deploy
        id: deployk8s
      components:
      ...output omitted...
    2. Run odo deploy to run the application in the cluster.

      [student@workstation weather]$ odo deploy
        __
       /  \__     Running the application in Deploy mode using weather Devfile
       \__/  \    Namespace: odo-deploy-cli
       /  \__/    odo version: v3.11.0
       \__/
      
      ↪ Deploying Kubernetes Component: weather
       ✓  Creating resource Service/weather
      
      ↪ Deploying Kubernetes Component: weather
       ✓  Creating resource Deployment/weather
      
      ↪ Deploying Kubernetes Component: weather
       ✓  Creating resource Route/weather
      
      Your Devfile has been successfully deployed
      
      ...output omitted...
    3. Run the oc get all command to verify that odo creates the following resources:

      • Pod

      • ReplicaSet

      • Deployment

      • Service

      • Route

        [student@workstation weather]$ oc get all
        NAME ...
        pod/weather-7bbf9fdbdb-ltpn7 ...
        
        NAME ...
        service/weather ...
        
        NAME ...
        deployment.apps/weather ...
        
        NAME ...
        replicaset.apps/weather-7bbf9fdbdb ...
        
        NAME ...
        route.route.openshift.io/weather ...

        Note that this command includes a route with the external weather-odo-deploy-cli.apps.ocp4.example.com URL.

    4. Verify that you can access the application from the workstation machine by running the curl with the previous URL and the /tomorrow path.

      [student@workstation ~]$ curl \
      weather-odo-deploy-cli.apps.ocp4.example.com/tomorrow
      {"rain_chance":"5%","weather":"sunny"}
  9. Log out by using odo.

    [student@workstation weather]$ odo logout
    Logged "developer" out on "https://api.ocp4.example.com:6443"
    
    ...output omitted...

Finish

On the workstation machine, use the lab command to complete this exercise. This step is important to ensure that resources from previous exercises do not impact upcoming exercises.

[student@workstation ~]$ lab finish deploy-cli

Revision: do288-4.12-0d49506