Red Hat OpenShift WorldTour
  • Red Hat OpenShift Workshop
  • Accessing IBM Cloud - the Free Lite Account
  • Lab 1 - Start with a Docker Image
  • Command Line Interface - Using the CLI in a Terminal
  • Claiming Your Red Hat OpenShift Cluster for This Workshop
  • Pre-Requesites Lab 2
  • Lab 2 - Using an existing image to create a project
  • Lab 3 - stretch goal
  • Lab 3 with LogDNA
  • Next Steps
    • IBM/minishift101 - step 1 - accessing cluster
    • IBM/minishift101 - step 2 - creating an app from source
    • IBM/minishift101 - step 3
    • IBM/minishift101 - step 4 networking
  • How Did You Like This Workshop?
  • FAQ
    • Lab 2 v 3.x - Using an existing image to create a project
    • Lab 3 - Deploying a Project to Red Hat OpenShift Kubernetes Cluster
      • Lab 3 - setting up
      • Lab 3 - instructions on creating an OpenShift project
Powered by GitBook
On this page
  • Deploying to OpenShift
  • Overview
  • Steps
  • Step 1 - creating and pushing image to the internal registry
  • 2. Creating deployment and a service
  • Apply the deployment.yaml
  • Apply the service.yaml

Was this helpful?

  1. FAQ
  2. Lab 3 - Deploying a Project to Red Hat OpenShift Kubernetes Cluster

Lab 3 - instructions on creating an OpenShift project

Please find detailed instructions after setting up the oc CLI in the previous step Lab 3 - setting up

PreviousLab 3 - setting up

Last updated 5 years ago

Was this helpful?

Deploying to OpenShift

Deploying to OpenShift via 'oc' CLI:

Overview

In this lab we will work in the OpenShift Web Console and with the OpenShift CLI. The following image is a simplified overview of the topics of that lab. Have in mind that is a platform.

This lab has two parts:

1. Build and save the container image to OpenShift internal Container Repository

  • We will create a OpenShift project

  • We will define a for OpenShift

  • We will build with the build Pod inside OpenShift and save container image to the internal

2. Deploy the application to the cluster and expose the service

  • You will define and apply a deployment configuration (yaml) to create a Pod with your microservice

  • You will define a service which routes requests to the Pod with your microservice

  • You will expose the service

The following gif is an animation of the simplified steps above in a sequence.

Steps

Step 1 - creating and pushing image to the internal registry

Create an Open Shift project

We need an OpenShift project, this is simply put equivalent to a Kubernetes namespace plus OpenShift security. Let us create one.

$ cd ${ROOT_FOLDER}/2-deploying-to-openshift
$ oc new-project cloud-native-starter

Build and save the container image in the Open Shift Container Registry

Now we want to build and save a container image in the OpenShift Container Registry. We use these commands to do that:

$ oc new-build --name authors --binary --strategy docker
$ oc start-build authors --from-dir=.

Verify the build in the OpenShift web console

  1. Select the 'cloud-native-starter' project in 'My Projects'

  2. Open 'Builds' in the menu and then click 'Builds'

  3. Select 'Last Build' (#1)

  4. Open 'Logs'

  5. Inspect the logs

Verify the container image in the Open Shift Container Registry UI

  1. Select the 'default' project

  2. Expand DEPLOYMENT 'registry-console' in 'Overview' and click on the URL in 'Routes - External Traffic'

  3. In the container registry you will find the 'authors' image and you can click on the latest label.

2. Creating deployment and a service

Apply the deployment.yaml

A Pod is the basic building block of Kubernetes–the smallest and simplest unit in the Kubernetes object model that you create or deploy. A Pod represents processes running on your Cluster .

Here is a simplified image for that topic. The deployment.yaml file points to the container image that needs to be instantiated in the pod.

Definition of kind defines this as a Deployment configuration.

kind: Deployment
apiVersion: apps/v1beta1
metadata:
  name: authors

Inside the spec section we specify an app name and version label.

spec:
  ...
  template:
    metadata:
      labels:
        app: authors
        version: v1

Then we define a name for the container and we provide the container image location, e.g. where the container can be found in the Container Registry.

The containerPort depends on the port definition inside our Dockerfile and in our server.xml.

We have previously talked about the usage of the HealthEndpoint class for our Authors service and here we see it the livenessProbe definition.

spec:
      containers:
      - name: authors
        image: authors:1
        ports:
        - containerPort: 3000
        livenessProbe:
kind: Deployment
apiVersion: apps/v1beta1
metadata:
  name: authors
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: authors
        version: v1
    spec:
      containers:
      - name: authors
        image: docker-registry.default.svc:5000/cloud-native-starter/authors:latest
        ports:
        - containerPort: 3000
        livenessProbe:
          exec:
            command: ["sh", "-c", "curl -s http://localhost:3000/"]
          initialDelaySeconds: 20
        readinessProbe:
          exec:
            command: ["sh", "-c", "curl -s http://localhost:3000/health | grep -q authors"]
          initialDelaySeconds: 40
      restartPolicy: Always

Step 1: Apply the deployment

1. Ensure you are in the {ROOT_FOLDER}/2-deploying-to-openshift/deployment

$ cd ${ROOT_FOLDER}/2-deploying-to-openshift/deployment

2. Apply the deployment to OpenShift

$ oc apply -f deployment.yaml

Step 2: Verify the deployment in OpenShift

  1. Open your OpenShift Web Console

  2. Select the Cloud-Native-Starter project and examine the deployment

  3. Click on #1 to open the details of the deployment

  4. In the details you find the 'health check' we defined before

Apply the service.yaml

A Kubernetes Service is an abstraction which defines a logical set of Pods and a policy by which to access them - sometimes called a micro-service. The set of Pods targeted by a Service is (usually) determined by a Label Selector.

In the service we map the NodePort of the cluster to the port 3000 of the Authors microservice running in the authors Pod, as we can see in the following picture.

kind: Service
apiVersion: v1
metadata:
  name: authors
  labels:
    app: authors
spec:
  selector:
    app: authors
  ports:
    - port: 3000
      name: http
  type: NodePort
---

Step 1: Apply the service deployment

  1. Apply the service to OpenShift

$ oc apply -f service.yaml
$ oc expose svc/authors

Step 2: Test the microservice

  1. Execute this command, copy the URL to open the Swagger UI in browser

$ echo http://$(oc get route authors -o jsonpath={.spec.host})/openapi/ui/
$ http://authors-cloud-native-starter.openshift-devadv-eu-wor-160678-0001.us-south.containers.appdomain.cloud/openapi/ui/

This is the Swagger UI in your browser:

2. Execute this command to verify the output:

$ curl -X GET "http://$(oc get route authors -o jsonpath={.spec.host})/api/v1/getauthor?name=Niklas%20Heidloff" -H "accept: application/json"

3. Output

$ {"name":"Niklas Heidloff","twitter":"https://twitter.com/nheidloff","blog":"http://heidloff.net"}

Step 3: Inspect the service in OpenShift

  1. Open your OpenShift Web Console

  2. Select the Cloud-Native-Starter project

  3. Chose 'Applications' and then 'Services'

  4. Click on 'authors'

  5. Examine the traffic and remember the simplified overview picture.

Congratulations! You deployed from a source code, thru container definition, your first project.

Note: A a community of users to organize and manage their content in isolation from other communities.

Make sure you are logged on to your OpenShift cluster.

Defining a new build using 'binary build' and the Docker strategy ( and )

Starting the build process on OpenShift with our defined build configuration. ()

This deployment will deploy a container to a Pod in Kubernetes. For more details we use the for Pods.

Let's start with the deployment yaml. For more details see the for deployments.

This is the full file.

After the definition of the Pod we need to define how to access the Pod. For this we use a service in Kubernetes. For more details see the for service.

In the we see a selector of the pod using the label 'app: authors'.

Using oc we create a Route to our service in the OpenShift cluster. ()

project allows
See here.
more details
oc new-build documentation
oc start-build documentation
Kubernetes documentation
Kubernetes documentation
deployment.yaml
Kubernetes documentation
service.yaml
expose
oc expose documentation
video (14:32 mins)
OpenShift
Kubernetes
build config
OpenShift container registry
Building the project in two steps
cloud native starter
select Builds / Builds
Select the latest build #1
Open Logs
Inspect logs
Check the internal container registry in the Default project
You should find the project you just pushed
The pod is created from the internal container repository
Open your project
Choose the latest version
Verify the health check
Exposing the pod with the service
Swagger in a browser
choose your project again
Select Applications, then Services
See the simplified overview picture