This topic describes how to use Helm to deploy a microservice application in Container Service for Kubernetes. In the following example, two methods will be introduced to deploy a PiggyMetrics application on Kubernetes.

Deployment methods

  • Deploy the application and the required Spring Cloud components at the same time.
  • Deploy the required Spring Cloud components on Kubernetes first and then deploy the application.

PiggyMetrics

PiggyMetrics is a Spring Cloud project that has more than 3,400 stars on GitHub. The project provides complete source code and ready-to-use container images. You can use Docker Compose to deploy the main components of the project.
This project consists of three core microservices, namely, statistics service, account service, and notification service. Each service uses a separate MongoDB database. The architecture design of the microservices is as follows:

Basic Spring Cloud components are responsible for the following services: registry service (Eureka service registration), config service (configuration management), gateway service (API gateway, also the JavaScript web Interface), and monitoring service (Hystrix Dashboard/Turbine).

Method: Use Helm to deploy the application and required components together

  1. Modify the Docker compose files that define the PiggyMetrics application.
    1. Change the Docker compose file version.
      To deploy a PiggyMetrics application to a single machine, a docker-compose.yml file is used. To deploy it on Kubernetes, you need to convert the file to a Kubernetes deployment file in YAML format.
      Note PiggyMetrics uses the version 2.1 docker compose template, which is not supported by Kompose. You must change the docker compose file version from 2.1 to 2.
    2. In the docker-compose.yml file,
      delete the following fields that are not supported by Kompose.
      depends_on:
            config:
              condition: service_healthy  # condition is not supported
      Add a Kubernetes server type annotation.
      depends_on:
        - config
      labels:
        kompose.service.type: loadbalancer
    3. In the docker-compose.dev.yml file, enable port 27017 on the four MongoDB databases used by PiggyMetrics to allow external access.
      Note The PiggyMetrics application uses four MongoDB databases, which are defined by the following four fields: auth-mongodb, account-mongodb, statistics-mongodb, and notification-mongodb.

      The complete YAML file is as follows:

      version: '2'
      services:
        rabbitmq:
          image: rabbitmq:3-management
          restart: always
          labels:
            kompose.service.type: nodeport
          ports:
            - 5672
            - 15672:15672
          logging:
            options:
              max-size: "10m"
              max-file: "10"
      
        config:
          environment:
            CONFIG_SERVICE_PASSWORD: $CONFIG_SERVICE_PASSWORD
          image: sqshq/piggymetrics-config
          restart: always
          ports:
            - 8888
          logging:
            options:
              max-size: "10m"
              max-file: "10"
      
        registry:
          environment:
            CONFIG_SERVICE_PASSWORD: $CONFIG_SERVICE_PASSWORD
          image: sqshq/piggymetrics-registry
          restart: always
          depends_on:
            - config
          labels:
            kompose.service.type: loadbalancer
          ports:
            - 8761:8761
          logging:
            options:
              max-size: "10m"
              max-file: "10"
      
        gateway:
          environment:
            CONFIG_SERVICE_PASSWORD: $CONFIG_SERVICE_PASSWORD
          image: sqshq/piggymetrics-gateway
          restart: always
          depends_on:
            - config
          labels:
            kompose.service.type: loadbalancer
          ports:
            - 4000:4000
          logging:
            options:
              max-size: "10m"
              max-file: "10"
      
        auth-service:
          environment:
            CONFIG_SERVICE_PASSWORD: $CONFIG_SERVICE_PASSWORD
            NOTIFICATION_SERVICE_PASSWORD: $NOTIFICATION_SERVICE_PASSWORD
            STATISTICS_SERVICE_PASSWORD: $STATISTICS_SERVICE_PASSWORD
            ACCOUNT_SERVICE_PASSWORD: $ACCOUNT_SERVICE_PASSWORD
            MONGODB_PASSWORD: $MONGODB_PASSWORD
          image: sqshq/piggymetrics-auth-service
          restart: always
          ports:
            - 5000
          depends_on:
            - config
          logging:
            options:
              max-size: "10m"
              max-file: "10"
      
        auth-mongodb:
          environment:
            MONGODB_PASSWORD: $MONGODB_PASSWORD
          image: sqshq/piggymetrics-mongodb
          restart: always
          ports:
            - 27017
          logging:
            options:
              max-size: "10m"
              max-file: "10"
      
        account-service:
          environment:
            CONFIG_SERVICE_PASSWORD: $CONFIG_SERVICE_PASSWORD
            ACCOUNT_SERVICE_PASSWORD: $ACCOUNT_SERVICE_PASSWORD
            MONGODB_PASSWORD: $MONGODB_PASSWORD
          image: sqshq/piggymetrics-account-service
          restart: always
          ports:
            - 6000
          depends_on:
            - config
          logging:
            options:
              max-size: "10m"
              max-file: "10"
      
        account-mongodb:
          environment:
            INIT_DUMP: account-service-dump.js
            MONGODB_PASSWORD: $MONGODB_PASSWORD
          image: sqshq/piggymetrics-mongodb
          restart: always
          ports:
            - 27017
          logging:
            options:
              max-size: "10m"
              max-file: "10"
      
        statistics-service:
          environment:
            CONFIG_SERVICE_PASSWORD: $CONFIG_SERVICE_PASSWORD
            MONGODB_PASSWORD: $MONGODB_PASSWORD
            STATISTICS_SERVICE_PASSWORD: $STATISTICS_SERVICE_PASSWORD
          image: sqshq/piggymetrics-statistics-service
          restart: always
          ports:
            - 8888
          depends_on:
            - config
          logging:
            options:
              max-size: "10m"
              max-file: "10"
      
        statistics-mongodb:
          environment:
            MONGODB_PASSWORD: $MONGODB_PASSWORD
          image: sqshq/piggymetrics-mongodb
          restart: always
          ports:
            - 27017
          logging:
            options:
              max-size: "10m"
              max-file: "10"
      
        notification-service:
          environment:
            CONFIG_SERVICE_PASSWORD: $CONFIG_SERVICE_PASSWORD
            MONGODB_PASSWORD: $MONGODB_PASSWORD
            NOTIFICATION_SERVICE_PASSWORD: $NOTIFICATION_SERVICE_PASSWORD
          image: sqshq/piggymetrics-notification-service
          restart: always
          ports:
            - 8000
          depends_on:
            - config
          logging:
            options:
              max-size: "10m"
              max-file: "10"
      
        notification-mongodb:
          image: sqshq/piggymetrics-mongodb
          restart: always
          environment:
            MONGODB_PASSWORD: $MONGODB_PASSWORD
          ports:
            - 27017
          logging:
            options:
              max-size: "10m"
              max-file: "10"
      
        monitoring:
          environment:
            CONFIG_SERVICE_PASSWORD: $CONFIG_SERVICE_PASSWORD
          image: sqshq/piggymetrics-monitoring
          restart: always
          depends_on:
            - config
          labels:
            kompose.service.type: loadbalancer
          ports:
            - 9000:8080
            - 8989:8989
          logging:
            options:
              max-size: "10m"
              max-file: "10"
      ---
      
  2. Use Kompose to generate a Kubernetes configuration file for deploying the PiggyMetrics application.
    1. Set the required environment variables to deploy the application.
      export NOTIFICATION_SERVICE_PASSWORD=passw0rd
      export CONFIG_SERVICE_PASSWORD=passw0rd
      export STATISTICS_SERVICE_PASSWORD=passw0rd
      export ACCOUNT_SERVICE_PASSWORD=passw0rd
      export MONGODB_PASSWORD=passw0rd
    2. Run the following command to convert the Docker compose files to a Kubernetes configuration file:
      kompose is a tool to convert Docker compose files to Kubernetes configuration files.
      kompose convert -f docker-compose.yml -f docker-compose.dev.yml -o piggymetrics -c
    3. Run the helm install command to deploy the PiggyMetrics application in the target Kubernetes cluster.
      For example, you can run the following command to deploy an application named piggy in the pm namespace:
      helm install --namespace pm --name piggy piggymetrics/
  3. Make sure that you are using the same Helm client version on your local machine and remote server.
    Note If the Helm client versions are not the same, the following error message appears:
    Error: incompatible versions client[v2.14.1] server[v2.11.0]
    Skip this step if no such error message appears.
    • If you are using a Mac OS, run the following commands to install Helm client version 2.11.0.
      brew unlink kubernetes-helm
      brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/ee94af74778e48ae103a9fb080e26a6a2f62d32c/Formula/kubernetes-helm.rb               
    • If you are using a Windows or Linux system, install the corresponding Helm client version.
  4. Access the application.
    You can deploy the application on Minikube or Container Service for Kubernetes. For more information, see Kubernetes. After the application is deployed, you can go to the Container Service console, choose Ingresses and Load Balancing > Services, and view all services and their LoadBalancer types and external endpoints on the Services page.
    • Click registry service to go to the PiggyMetrics page.

      PiggyMetrics is a personal finance service. You can enter income and expense data and the service will generate well-organized reports accordingly.

    • When you access the registry service, you can view all services that are registered with the Eureka Server.
  5. To delete PiggyMetrics, run the following command:
    helm delete --purge piggymetrics
    release "piggymetrics" deleted