Skip to content

dsf deployment dsf4openshift

jodacalc edited this page Jun 20, 2019 · 10 revisions

dsf4openshift deployment environment

In this section you will see how you can create a new environment instance in OpenShift and the things that you must add to the Jenkinsfiles of your repository to deploy a branch in this new environment. To conclude you are going to see how to add config files for environment in the source code of the applications.

Prerrequisite

OpenShift Cluster

To have your deployment environment with OpenShift you need to have an OpenShift Cluster.

Download OpenShift Client Tools

First of all you need to download the OpenShift client, you can find it here.

Remember that what you need to download oc Client Tools and not OKD Server.

Note
This tutorial has been made with the version 3.10.0 of the client, it is recommended to use the most current client, but if it does not work, it is possible that the instructions have become obsolete or that the OpenShift used needs another older/newer version of the client. To download a specific version of the client you can find here the older versions and the version 3.10.0.

Add oc client to path

Once you have downloaded the client you have to add it to the PATH environment variable.

Log into OpenShift with admin account

You can log using a terminal and executing the next instructions:

oc login $OpenShiftUrl
Note
You need a valid user to log in.

Select the project where you are going to create the environment

oc project $projectName

Add all the secrets that you need

For example, to create a secret for a nexus repository you should execute the next commands:

oc create secret docker-registry $nameForSecret --docker-server=${dockerRegistry} --docker-username=${user} --docker-password=${pass} --docker-email=no-reply@email.com

Configure OpenShift

Configure builds to create docker image using OpenShift

If you need to create docker images of your projects you could use OpenShift to do it (Off course only if you have enough rights).

To do it, follow the next steps.

Create new builds configs

The first thing you need to do for create a new environment is prepare the buildconfigs for the front and for the middleware and rise default memory limits for the middleware. You can do it using a terminal and executing the next instructions:

These are a summary about the parameters used in our commands:

  • ${dockerRegistry}: The url of the docker repository.

  • ${props.name}: The name of the project (for example could be find on package.json)

  • ${dockerTag}: The tag of the image

Note
From now on you will refer to the name that you are going to give to the environment as $enviroment. Remember to modify it for the correct value in all instructions.
devon4ng build config

You need to create nginx build config with docker.

oc new-build --strategy docker --binary --docker-image nginx:alpine-perl --name=${props.name}-$environment --to=${dockerRegistry}/${props.name}:${dockerTag} --to-docker=true
Note
You need nginx:alpine-perl to read the environment config file in openshift, if you are not going to use it, you could use nginx:latest instead.
devon4node build config
oc new-build --strategy docker --binary --docker-image node:lts --name=${props.name}-$environment --to=${dockerRegistry}/${props.name}:${dockerTag} --to-docker=true
devon4j build config
oc new-build --strategy docker --binary --docker-image openjdk:<version> --name=${props.name}-$environment --to=${dockerRegistry}/${props.name}:${dockerTag} --to-docker=true
Note
You need to specify the <version> of java used for your project. Also you can use the -alpine image. This image is based on the popular Alpine Linux project. Alpine Linux is much smaller than most distribution base images (~5MB), and thus leads to much slimmer images in general. More information on docker hub.
How to use the build

In this step is where you will build a docker image from a compiled application.

Prerequisite

To build the source in OpenShift, first of all you need to compile your source and obtain the artifacts "dist folder" or download it from a repository. Normally the artifacts have been built on Jenkins and have been stored in Nexus.

To download it, you can access to your registry, select the last version and download the ".tar". The next image show an example of where is the link to download it, marked in yellow:

nexus stored artifacts
Build in OpenShift

When you have the artifacts, you can send them to your openshift and build them using your buildconfig that you created on the previous step. This is going to create a new docker image and push it to your registry.

If your docker registry need credentials you shold use a secret. You could add it to your buildconfig using the next command:

oc set build-secret --push bc/${props.name}-$environment ${nameForSecret}

Now you can use your build config and push the docker image to your tegistry. To do it you need to use a terminal and execute the following:

oc start-build ${props.name}-$environment --from-dir=${artifactsPath} --follow
Note
${artifactsPath} is the path where you have the artifacts of the prerequisite (On jenkins is the dist folder generated by the build).
Note
Maybe you need to raise your memory or CPU limits.

Configure new environment

Now it is time to configure the environment.

Prerequisite

You need a docker image of your application. You could created it using OpenShift as you see in the last step.

Create new app on OpenShift

To create new app you need to use the next command.

oc new-app --docker-image=${artifactsPath} --name=${props.name}-$environment --source-secret=${nameForSecret}
Note
You could add environment variables using -e $name=$value
Note
If you do not need to use a secret remove the end part of the command --source-secret=${nameForSecret}

Create routes

Finally, you need add a route to access the service.

Add http route

If tou want to create an http reoute execute the following command in a terminal:

oc expose svc/${props.name}-$environment

Add https route

If you want to create an https route you can do it executing the following command:

oc create route edge --service=${props.name}-$environment

If you want to change the default route path you can use the command --hostname=$url. For example:

oc expose svc/${props.name}-$environment --hostname=$url

oc create route edge --service=${props.name}-$environment --hostname=$url

Import new images from registry

When you have new images in the registry you must import them to OpenShift. You could do it executing the next commands:

oc import-image ${props.name}-$environment --from=${dockerRegistry}/${props.name}:${dockerTag} --confirm
Note
Maybe you need to raise your memory or CPU limits. It is explained below.

Raise/decrease memory or CPU limits

If you need to raise (or decrease) the memory or CPU limits that you need you could do it for your deployments and bulders configurations following the next steps.

For deployments

You could do it in OpenShift using the user interface. To do it you should enter in OpenShift and go to deployments.

openshift deployments menu

At the right top, you could see a drop down actions, click on it and you could edit the resource limits of the container.

openshift deployments actions
openshift deployments resource limits

Maybe you should modify the resource limits of the pod too. To do it you should click on drop down actions and go to edit YAML. Then you could see something like the next image.

openshift deployments yaml resources

In the image, you could see that appear resources two times. One at the bottom of the image, this are the container resources that you modified on the previous paragraph and another one at the top of the image. The resources of the top are for the pod, you should give to it at least the same of the sum for all containers that the pod use.

Also you could do it using command line interface and executing the next command:

To modify pod limits

oc patch dc/boat-frontend-test --patch '{"spec":{"strategy":{"resources":{"limits":{"cpu": "100m", "memory": "100Mi"}, "requests":{"cpu": "100m", "memory": "100Mi"}}}}}'

To modify container limits

When this guide was wrotten Openshift have a bug and you cannot do it from command line interface.

Note
If that command did not work and you received an error like this error: unable to parse "'{spec:…​": yaml: found unexpected end of stream, try to use the patch using "" instead of ''. It looks like this: --patch "{\"spec\":…​\"}}}}"

For builders

You could do it using command line interface and executing the next command:

oc patch bc/${props.name}${APP_NAME_SUFFIX} --patch '{"spec":{"resources":{"limits":{"cpu": "125m", "memory": "400Mi"},"requests":{"cpu": "125m", "memory": "400Mi"}}}}'
Note
If that command did not work and you received an error like this error: unable to parse "'{spec:…​": yaml: found unexpected end of stream, try to use the patch using "" instead of ''. It looks like this: --patch "{\"spec\":…​\"}}}}"

Service integration with jenkins

Prerrequisite

To integrate it, you need to have installed the plugin OpenShift Client. To install it go to Manage Jenkins clicking on left menu and enter in Manage Plugins. Go to Available tab and search it using the filter textbox in the top right corner and install it.

Configuration

Second, you need to configure the OC Client. Go to Manage Jenkins clicking on left menu and enter in Global Tool Configuration.

Go to OpenShift Client Tools section and add a new one like this.

openshift jenkins plugin

Upgrade your Jenkinsfile

Now it is time to add/upgrade the next stages in to your Jenkinsfile:

Add create docker image stage.

Add deploy docker image stage.

Add check status stage.

Upgrade Setup Jenkins tools stage.

Note
Remember to upgrade your parameters to diference which environment is used per branch.
Clone this wiki locally