Migrate and deploy Cloud Foundry applications to Kubernetes

Transform a Cloud Foundry application to Kubernetes with the Move2Kube command-line tool. You’ll learn how to create deployment artifacts using Move2Kube’s three-step process: collect, plan, and transform.

Prerequisites:


Deploy the application to Kubernetes

Now it’s time to deploy the application. Let's get inside the ./myproject directory:

$ cd myproject/
$ ls
Readme.md deploy    scripts   source

Run the builddockerimages.sh script inside the ./myproject/scripts directory. (This step might take some time to complete.)

$ cd scripts
$ ./builddockerimages.sh
[+] Building 1.9s (8/8) FINISHED     
=> [internal] load build definition from Dockerfile                                                                                              0.1s
=> => transferring dockerfile: 747B                                                                                                              0.0s
=> [internal] load .dockerignore                                                                                                                 0.0s
=> => transferring context: 2B                                                                                                                   0.0s
=> [internal] load metadata for registry.access.redhat.com/ubi8/nodejs-12:latest                                                                 1.7s
=> [internal] load build context                                                                                                                 0.0s
=> => transferring context: 868B                                                                                                                 0.0s
=> [1/3] FROM registry.access.redhat.com/ubi8/nodejs-12@sha256:1208ace959a40906e0e0e753b5ed5621c052a5a115e333d70ca8fa5e5c0dc0ca                  0.0s
=> CACHED [2/3] COPY . .                                                                                                                         0.0s
=> CACHED [3/3] RUN npm install                                                                                                                  0.0s
=> exporting to image                                                                                                                            0.0s
=> => exporting layers                                                                                                                           0.0s
=> => writing image sha256:2d560a9a7b40f2d29447b57b619d55a5dd103ae5dd626cd517791f4ae8e61cbb                                                      0.0s
=> => naming to docker.io/library/cfnodejsapp                                                                                                    0.0s
/Users/username/myproject
done

Now, using the pushimages.sh script, we can push our application’s images to the registry we specified during the transform phase. For this step, you need to log in to your Docker registry. To log in to quay.io, run docker login quay.io. To log in to the IBM Cloud us.icr.io registry, refer to the documentation.

$ ./pushimages.sh
Using default tag: latest
The push refers to repository [quay.io/danieloh30/cfnodejsapp]
44682b6a7ca8: Layer already exists 
4d994360499e: Layer already exists 
31226b9a7823: Layer already exists 
718375ad3f65: Layer already exists 
e0b9a99c51af: Layer already exists 
ef5d9ad2a541: Layer already exists 
93749af418e7: Layer already exists 
latest: digest: sha256:197c820069a3691c727db3ccf0d544601035fdca6dba7b96bae16463068a5307 size: 1789

Note:  If you pushed the image repository to quay.io, then in the Repository Visibility in the quay.io cfnodejsapp repository settings, select whether you want the repository to be public so that the Kubernetes cluster can properly access it.

Finally, we will deploy the application with the oc apply command using the YAML files that Move2Kube created for us in the ./myproject/deploy/yamls directory.

We'll use Developer Sandbox for Red Hat OpenShift, a shared multitenant OpenShift cluster that is preconfigured with a set of developer tools. To access the sandbox, visit Get started in the Sandbox and sign up for free.

We will create Kubernetes resources in the OpenShift cluster using the oc command line tool instead of kubectl. If you haven't already installed the oc CLI, follow the instructions in this article: Where can I download the OpenShift command line tool?

Before you deploy the application, you need to log in to the OpenShift cluster on the sandbox. You can copy the oc login command with a valid token (Figure 1).

 
Log into the OpenShift cluster before you deploy. Copy the oc login command by selecting Display Token.
Figure 1: Copy the login command to log in to the OpenShift cluster on the Developer Sandbox for Red Hat OpenShift.

When you paste the oc login command into your local terminal, the output should look like this:

$ oc login --token=sha256~_4yyHH7hAANsx7BchfB4xkclvdmWUAdt-efqZt0MVOQ --server=https://api.sandbox.x8i5.p1.openshiftapps.com:6443
Logged into "https://api.sandbox.x8i5.p1.openshiftapps.com:6443" as "USERNAME" using the token provided.
You have access to the following projects and can switch between them with 'oc project <projectname>':
  * USERNAME-dev
    USERNAME-stage
Using project "doh-dev".

Replace USERNAME with your account name. Be sure to log in to the USERNAME-dev project.

$ cd ..
$ oc apply -f deploy/yamls
deployment.apps/cfnodejsapp created
imagestream.image.openshift.io/cfnodejsapp-latest created
service/cfnodejsapp created
secret/cfnodejsapp-vcapasenv configured

Now our application is accessible on the cluster. You can check the status of pods by running the following command:

$ oc get pods
NAME                           READY   STATUS    RESTARTS   AGE
cfnodejsapp-7f9ccb98d7-b97xn   1/1     Running   0          7s
cfnodejsapp-7f9ccb98d7-zhdnh   1/1     Running   0          7s

Create a Route to access the application via the FQDN URL:

$ oc expose svc/cfnodejsapp
route.route.openshift.io/cfnodejsapp exposed

You can get the route URL using the following oc command:

$ oc get route
NAME       HOST/PORT                                                    PATH   SERVICES      PORT        TERMINATION   WILDCARD
cfnodejsapp  cfnodejsapp-doh-dev.apps.sandbox.x8i5.p1.openshiftapps.com          cfnodejsapp   port-8080                 None

Access the application using the curl command-line tool:

$ curl cfnodejsapp-doh-dev.apps.sandbox.x8i5.p1.openshiftapps.com
<h1>This is a node server</h1>

You can also make sure that the application deployed successfully via the Topology view on the OpenShift cluster. Let's add a Node.js label to the application pod using the following oc command:

$ oc label deployment/cfnodejsapp app.openshift.io/runtime=nodejs --overwrite 
deployment.apps/cfnodejsapp labeled

In the OpenShift web console, navigate to the Topology view in the Developer perspective. You will see the Node.js icon (Figure 2).

 
The Node.js icon has a small Open icon in the upper right area. Click it to check the application.
Figure 2: Checking the application in the Topology view in the OpenShift web console.

Click on the Open URL icon. This will take you to the application page (Figure 3).

 
You've found the web application page.
Figure 3: Web application page.

Conclusion

This tutorial demonstrated a simple way to perform a holistic transformation of your Cloud Foundry application to Kubernetes using the Developer Sandbox for Red Hat OpenShift. Visit the Developer Sandbox page to learn more.

 
 
Previous resource
Plan and transform your application