Skip to Content

Deploy a Node.js Application in the Kyma Runtime

Build a basic Node.js application into OCI image and push it into Docker registry. Besides, describe the corresponding Kubernetes objects for the application. Based on the above, deploy the application into the Kyma runtime.
You will learn
  • How to build Application to OCI Image
  • How to push OCI image to Docker Hub
  • How to describe Kubernetes objects for web application
  • How to deploy application into Kyma runtime
TiaXu1122Tia XuJuly 18, 2022
Created by
TiaXu1122
April 1, 2022
Contributors
TiaXu1122

Prerequisites

  • Step 1

    Open your subaccount in the Cockpit. In the overview page, find the subdomain for your deployment.

    For example:

    image-20211214133316133
  • Step 2

    Find the full Kyma cluster domain in the downloaded kubeconfig.yml file. For example: e6803e4.kyma.shoot.live.k8s-hana.ondemand.com.

  • Step 3

    In order to run your code on the Kyma Runtime (or on any Kubernetes-based platform), you need to provide an OCI image (aka Docker image) for your application. While you are in principle free to choose your image building tool, we recommend using Cloud Native Buildpacks (CNB).

    The command-line tool pack supports providing a buildpack and your local source code and creating an OCI image from it. We are working on a process to provide recommended and supported buildpacks. In the meantime, you can use the community-supported Paketo Buildpacks.

    1. Install command-line tool pack: Install Pack

    For example (macOS):

    Shell / Bash
    Copy
    brew install buildpacks/tap/pack
    

    When we speak about repository name, we mean the combination of account and repo name that is usual with Docker Hub: <docker-hub-account>/<repo-name>. An example would be tiaxu/multitenant-kyma-backend.

    As you can only create one private repository in a free Docker hub account, Docker images stored in Docker hub will have different tag names so that they can be stored under one repository. Thus, addressing an image will include the tag name:<docker-hub-account>/<repo-name>:<tag-name>. An example would be tiaxu/multitenant-kyma-backend:v1.

    2. In the directory kyma-multitenant-node, build the image for the approuter app from source, for example:

    Shell / Bash
    Copy
    pack build multitenant-kyma-backend --builder paketobuildpacks/builder:full
    docker tag multitenant-kyma-backend <docker-hub-account>/multitenant-kyma-backend:v1
    
  • Step 4

    1. Log in to Docker using this command:

    Shell / Bash
    Copy
    docker login -u <docker-id> -p <password>
    

    2. Push the local image into the Docker Hub:

    Shell / Bash
    Copy
    docker push <docker-hub-account>/multitenant-kyma-backend:v1
    

    For more details, see the Kubernetes documentation.

  • Step 5

    Then you are ready to deploy it into the Kubernetes cluster with Kyma runtime.

    1. Select the Link to dashboard to open the Kyma dashboard.

    image-20220112154735200

    2. Create a new namespace through the Kyma dashboard or kubectl CLI, for example, called multitenancy-ns:

    image-20220214150615225
  • Step 6

    Since the OCI image is stored in your Docker hub, you need to provide the access information to your Kyma cluster that you can pull the images from those repositories, replace the placeholder values according to your account:

    Shell / Bash
    Copy
    kubectl -n multitenancy-ns create secret docker-registry registry-secret --docker-server=https://index.docker.io/v1/  --docker-username=<docker-id> --docker-password=<password> --docker-email=<email>
    

    Therefore, all deployment files contain an imagePullSecret entry, which should be set to registry-secret.

    yaml
    Copy
    imagePullSecrets:
            - name: registry-secret # replace with your own registry secret
    
  • Step 7

    Based on the previous preparation steps, you can define the description file for deployment.

    In the root directory multitenancy-kyma-tutorial, create a new YAML file called k8s-deployment-backend.yaml with the following content:

    yaml
    Copy

    --- apiVersion: gateway.kyma-project.io/v1alpha1 kind: APIRule metadata: creationTimestamp: null labels: app: kyma-multitenant-node-multitenancy release: multitenancy name: kyma-multitenant-node-multitenancy spec: gateway: kyma-gateway.kyma-system.svc.cluster.local rules: - accessStrategies: - handler: allow methods: - GET - POST - PUT - PATCH - DELETE - HEAD path: /.* service: host: <subaccount-subadomain>-node.<cluster-domain> # replace with the values of your account name: kyma-multitenant-node-multitenancy port: 8080 status: {} --- apiVersion: apps/v1 kind: Deployment metadata: creationTimestamp: null labels: app: kyma-multitenant-node-multitenancy release: multitenancy name: kyma-multitenant-node-multitenancy spec: replicas: 1 selector: matchLabels: app: kyma-multitenant-node-multitenancy release: multitenancy strategy: {} template: metadata: creationTimestamp: null labels: app: kyma-multitenant-node-multitenancy release: multitenancy spec: automountServiceAccountToken: true imagePullSecrets: - name: registry-secret # replace with your own registry secret containers: - env: - name: PORT value: "8080" - name: TMPDIR value: /tmp image: <docker-hub-account>/multitenant-kyma-backend:v1 # replace with your Docker Hub account name livenessProbe: exec: command: - nc - -z - localhost - "8080" failureThreshold: 1 initialDelaySeconds: 60 periodSeconds: 30 successThreshold: 1 timeoutSeconds: 60 name: kyma-multitenant-node-multitenancy ports: - containerPort: 8080 readinessProbe: exec: command: - nc - -z - localhost - "8080" failureThreshold: 1 initialDelaySeconds: 60 periodSeconds: 30 successThreshold: 1 timeoutSeconds: 60 resources: limits: ephemeral-storage: 256M memory: 256M requests: cpu: 100m ephemeral-storage: 256M memory: 256M securityContext: allowPrivilegeEscalation: false capabilities: drop: - ALL privileged: false readOnlyRootFilesystem: false volumeMounts: - mountPath: /tmp name: tmp securityContext: runAsNonRoot: true volumes: - emptyDir: {} name: tmp status: {} --- apiVersion: v1 kind: Service metadata: creationTimestamp: null labels: app: kyma-multitenant-node-multitenancy release: multitenancy name: kyma-multitenant-node-multitenancy spec: ports: - port: 8080 protocol: TCP targetPort: 8080 selector: app: kyma-multitenant-node-multitenancy release: multitenancy status: loadBalancer: {} --- apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: creationTimestamp: null labels: release: multitenancy name: multitenancy spec: ingress: - from: - namespaceSelector: matchLabels: name: istio-system podSelector: matchLabels: app: istio-ingressgateway podSelector: matchLabels: release: multitenancy policyTypes: - Ingress

    Replace the placeholders with the values that you got in the previous steps.

  • Step 8

    Deploy the Node.js application by executing this command under the root directory multitenancy-kyma-tutorial:

    Shell / Bash
    Copy
    kubectl -n multitenancy-ns apply -f k8s-deployment-backend.yaml
    
  • Step 9

    1. Find the URL address of your application in the Kyma dashboard:

    image-20220214153901104

    2. Access it in the browser, then the application will return the message that you defined before.

    Can you access the application you deployed into the Kyma runtime?

  • Step 10

    You can find the final Node.js project from repository: here.

Back to top