[V5.0.7 or later]

Build and deploy a sample application to a Kubernetes container

About this task

Once you have set up a Kubernetes container runtime environment, build and deploy a small LoopBack application to confirm that it works properly.

Procedure

  1. Get a simple LoopBack project.

    If you don't have a simple "Hello World" LoopBack project handy, get the LoopBack "Getting Started" app. On one of the master nodes, enter the following commands:

    git clone https://github.com/strongloop/loopback-getting-started
    cd loopback-getting-started

    Alternatively, you can create a new LoopBack project from scratch. For more information, see Tutorial: Creating a LoopBack® project from the command line.

  2. Create a Dockerfile.

    Create a file name Dockerfile in the Loopback application directory:

    FROM node:slim
    ADD . /app
    WORKDIR /app
    RUN npm install
    CMD npm start
  3. Build and publish the image.

    Build the Docker image and tag it to publish to your Docker registry by entering this command:

    docker build -t 127.0.0.1:30000/apps/testapp .

    Once the image is built, publish it to the registry by entering this command:

    docker push 127.0.0.1:30000/apps/testapp
  4. Create a deployment manifest for the application named app.yml in your home directory:
    apiVersion: extensions/v1beta1
    kind: Deployment
    metadata:
      name: testapp-deployment
    spec:
      replicas: 1
      template:
        metadata:
          labels:
            app: testapp
        spec:
          containers:
          - name: testapp
            image: 127.0.0.1:30000/apps/testapp
            ports:
            - containerPort: 3000
    ---
    kind: Service
    apiVersion: v1
    metadata:
      name: testapp-service
    spec:
      selector:
        app: testapp
      ports:
        - protocol: TCP
          port: 3000
          targetPort: 3000
    ---
    apiVersion: extensions/v1beta1
    kind: Ingress
    metadata:
      name: testapp-ingress
      annotations:
        ingress.kubernetes.io/rewrite-target: /
    spec:
      rules:
      - http:
          paths:
          - path: /testapp
            backend:
              serviceName: testapp-service
              servicePort: 3000
  5. Test the deployed application.

    Once deployed, the application will be available at https://127.0.0.1/testapp. Enter the following command to retrieve data from the deployed app:

    curl -k https://127.0.0.1/testapp/api/CoffeeShops