Deploying Docker Images to OpenShift
We take a look at how to deploy a Docker image from DockerHub into RedHat's OpenShift environment, bringing added functionality along the way.
Join the DZone community and get the full member experience.
Join For FreeOpenShift is RedHat's cloud development Platform-as-a-Service (PaaS). It uses Kubernetes for container orchestration (so you can use OpenShift as your Kubernetes implementation) while providing some features missed in Kubernetes, such as automation of the build process of the containers, health management, dynamic provision storage, or multi-tenancy, to cite a few.
In this post, I am going to explain how you can deploy a Docker image from DockerHub into an OpenShift instance.
It is important to note that OpenShift offers other ways to create and deploy a container into its infrastructure. You can read more here, but as read in the previous paragraph, in this case, I am going to show you how to deploy already-created Docker images from DockerHub.
The first thing to do is create an account in OpenShift Online. It is free and, for the sake of this post, is enough. Of course, you can use any other OpenShift approach, like OpenShift Origin.
After that, you need to log into OpenShift cluster. In the case of OpenShift Online, use the token provided:
oc login https://api.starter-us-east-1.openshift.com --token=xxxxxxx
Then you need to create a new project inside OpenShift.
oc new-project villains
You can understand a project as a Kubernetes namespace with additional features.
Then, let's create a new application within the previous project based on a Docker image published on DockerHub. This example is a VertX application where you can get crimes from several fictional villains — from Lex Luthor to Gru.
oc new-app lordofthejars/crimes:1.0 --name crimes
In this case, a new app called crimes is created based on the lordofthejars/crimes:1.0 image. After running the previous command, a new pod running the previous image + a service + a replication controller is created.
After that, we need to create a route so the service is available to the public Internet.
oc expose svc crimes --name=crimeswelcome
The last step is just to get the version of the service from the browser. In my case, it was: http://crimeswelcome-villains.1d35.starter-us-east-1.openshiftapps.com/version
Notice that you need to change the public host with the one generated by your router and then append the version. You can find the public URL by going to the OpenShift dashboard, at the top of the pods definition.
Ok, now you'll get a 1.0, which is the version we have deployed. Now suppose you want to update to next version of the service, to version 1.1, so you need to run next commands to deploy next version of crimes service container, which is pushed at Docker Hub.
oc import-image crimes:1.1 --from=lordofthejars/crimes:1.1
With the previous command, you are configuring internal OpenShift Docker Registry with next Docker image to release.
Then let's prepare the application so when next rollout command is applied, the new image is deployed:
oc patch dc/crimes -p '{"spec": { "triggers":[ {"type": "ConfigChange", "type": "ImageChange" , "imageChangeParams": {"automatic": true, "containerNames":["crimes"],"from": {"name":"crimes:1.1"}}}]}}'
And finally you can do the rollout of the application by using:
oc rollout latest dc/crimes
After a few seconds, you can again go to http://crimeswelcome-villains.1d35.starter-us-east-1.openshiftapps.com/version (of course, change the host with your host), and the version you'll get is 1.1.
Finally, what happens if this new version contains a bug and you want to do a rollback of the deployment to previous version? Easy. Just run next command:
oc rollback crimes-1
And the previous version is going to be deployed again, so after a few seconds, you can go again to /version and you'll see 1.0 version again.
Finally, if you want to delete the application to have a clean cluster, run:
oc delete all --all
So as you can see, it is really easy to deploy container images from DockerHub to OpenShift. Notice that there are other ways to deploy our application into OpenShift, in this post I have just shown you one.
Commands can be found here.
Published at DZone with permission of Alex Soto, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments