Importing Kubernetes applications into a workspace

This section describes how to import Kubernetes or OpenShift applications into a Che workspace.

For demonstration purposes, the section uses a sample Kubernetes or OpenShift application having the following two Pods:

To run the application on a Kubernetes or OpenShift cluster:

$ node=https://raw.githubusercontent.com/redhat-developer/devfile/master/samples/web-nodejs-with-db-sample/nodejs-app.yaml && \
mongo=https://raw.githubusercontent.com/redhat-developer/devfile/master/samples/web-nodejs-with-db-sample/mongo-db.yaml && \
kubectl  apply -f ${mongo} && \
kubectl  apply -f ${node}

To deploy a new instance of this application in a Che workspace, use one of the following three scenarios:

Including a Kubernetes or OpenShift application in a workspace devfile definition

This procedure describes how to define a Che workspace devfile to include a Kubernetes application.

The devfile format is used to define a Che workspace, and its format is described in the Configuring a workspace using a devfile section.

Prerequisites
Procedure
  1. Create the simplest devfile:

    apiVersion: 1.0.0
    metadata:
     name: minimal-workspace (1)
    1 Only the name minimal-workspace is specified. After the Che server processes this devfile, the devfile is converted to a minimal Che workspace that only has the default editor (Che-Theia) and the default editor plug-ins, including, for example, the terminal.
  2. To add Kubernetes applications to a workspace, modify the devfile and add the Kubernetes component type.

    For example, to embed the NodeJS-Mongo application in the minimal-workspace:

    apiVersion: 1.0.0
    metadata:
     name: minimal-workspace
    components:
     - type: kubernetes
       reference: https://raw.githubusercontent.com/.../mongo-db.yaml
     - alias: nodejs-app
       type: kubernetes
       reference: https://raw.githubusercontent.com/.../nodejs-app.yaml
       entrypoints:
         - command: ['sleep']  (1)
           args: ['infinity']
    1 The sleep infinity command is added as the entrypoint of the Node.js application. The command prevents the application from starting at the workspace start phase. This configuration allows the user to start the application when needed for testing or debugging purposes.
  3. Add the commands in the devfile to make it easier for a developer to test the application:

    apiVersion: 1.0.0
    metadata:
     name: minimal-workspace
    components:
     - type: kubernetes
       reference: https://raw.githubusercontent.com/.../mongo-db.yaml
     - alias: nodejs-app
       type: kubernetes
       reference: https://raw.githubusercontent.com/.../nodejs-app.yaml
       entrypoints:
         - command: ['sleep']
           args: ['infinity']
    commands:
     - name: run  (1)
       actions:
         - type: exec
           component: nodejs-app
           command: cd ${CHE_PROJECTS_ROOT}/nodejs-mongo-app/EmployeeDB/ && npm install && sed -i -- ''s/localhost/mongo/g'' app.js && node app.js
    1 The run command added to the devfile is available as a task in Che-Theia from the command palette. When executed, the command starts the Node.js application.
  4. Use the devfile to create and start a workspace:

    $ chectl workspace:start --devfile <devfile-path>

Adding a Kubernetes or OpenShift application to an existing workspace using the dashboard

This procedure demonstrates how to modify an existing workspace and import the Kubernetes or OpenShift application using the newly created devfile.

Prerequisites
  • A running instance of Che. To install an instance of Che, see Installing Che.

  • An existing workspace defined on this instance of Che.

Procedure
  1. After the creation of a workspace, use the Workspace menu and then click on the desired workspace.

  2. Modify the workspace devfile, use the Devfile tab.

  3. Add a Kubernetes or OpenShift component.

  4. For the changes to take effect, save the devfile and restart the Che workspace.

Generating a devfile from an existing Kubernetes or OpenShift application

This procedure demonstrates how to generate a devfile from an existing Kubernetes or OpenShift application using the chectl tool.

Prerequisites
Procedure
  1. To generate a devfile, use:

    $ chectl devfile:generate
    • It is also possible to generate a devfile from, for example, the NodeJS-MongoDB application that includes the NodeJS component, using the chectl devfile:generate command:

      Example:
      $ chectl devfile:generate --selector="app=nodejs"
      apiVersion: 1.0.0
      metadata:
        name: chectl-generated
      components:
        - type: kubernetes
          alias: app=nodejs
          referenceContent: |
            kind: List
            apiVersion: v1
            metadata:
              name: app=nodejs
            items:
              - apiVersion: apps/v1
                kind: Deployment
                metadata:
                  labels:
                    app: nodejs
                  name: web
      (...)

      The Node.js application YAML definition is available in the devfile, inline, using the referenceContent attribute.

    • To include support for a language, use the --language parameter:

      $ chectl devfile:generate --selector="app=nodejs" --language="typescript"
      apiVersion: 1.0.0
      metadata:
        name: chectl-generated
      components:
        - type: kubernetes
          alias: app=nodejs
          referenceContent: |
            kind: List
            apiVersion: v1
      (...)
        - type: chePlugin
          alias: typescript-ls
          id: che-incubator/typescript/latest
  2. Use the generated devfile to start a Che workspace with chectl.

    $ chectl workspace:start --devfile=devfile​.yaml