This topic shows how to enable a Kubernetes application to use Tanzu Postgres database services.

Tanzu Postgres includes a sample configuration to run the spring-music app in Kubernetes.

The application is in the sample-app directory where you extracted the Tanzu Postgres release.

Follow these steps to build and deploy the sample application.

  1. Build the spring-app image and make it accessible to your Kubernetes cluster.

    • For a Minikube cluster, set the environment by running eval $(minikube docker-env).
    • For a GKE or TKGi cluster, be sure to tag and push the generated spring-music image to the proper location and modify the image field in the spring-music.yaml file to include the remote registry prefix and tag.
    $ cd sample-app
    $ docker build . -t spring-music:latest -f Dockerfile
    $ docker images
    
  2. Connect to the Postgres instance and verify that no app data has been saved in the database.

    $ kubectl exec -it my-postgres-0 -- /bin/bash
    $ psql testdb -c '\dt'
    
    Did not find any relations.
    
  3. Deploy the app defined in the spring-music.yaml sample file, after customizing it with your environments values. The file specifies a deployment and a service.

    $ kubectl create -f spring-music.yaml
    
    deployment.apps/spring-music created
    service/spring-music-service created
    
  4. List the deployments, pods, and services in the Kubernetes cluster.

    $ kubectl get deployments
    
    NAME                READY   UP-TO-DATE   AVAILABLE   AGE
    postgres-operator   1/1     1            1           8m24s
    spring-music        0/1     1            0           9s
    
    $ kubectl get pods
    
    NAME                                 READY   STATUS             RESTARTS   AGE
    my-postgres-0                        1/1     Running            0          11m
    postgres-operator-558964b7d4-6fr6k   1/1     Running            0          12m
    spring-music-97c79dc7f-4nq5d         0/1     ImagePullBackOff   0          4m28s
    
    $ kubectl get services
    
    NAME                                TYPE           CLUSTER-IP    EXTERNAL-IP     PORT(S)          AGE
    kubernetes                          ClusterIP      10.48.0.1     <none>          443/TCP          18m
    my-postgres                         LoadBalancer   10.48.12.85   35.192.97.253   5432:30655/TCP   12m
    postgres-operator-webhook-service   ClusterIP      10.48.8.185   <none>          443/TCP          13m
    spring-music-service                LoadBalancer   10.48.6.247   35.238.254.98   80:31937/TCP     5m13s
    

    The command kubectl get all shows all resources in the Kubernetes cluster.

  5. Verify that the spring-music app has created the album table in the testdb database.

    $ kubectl exec -it my-postgres-0 -- /bin/bash
    $ psql testdb -c 'select count(*) from album;'
    
    count
    -------
       29
    (1 row)
    
  6. Connect to the spring-music app web interface.

    • Minikube:

      $ host=$(minikube ip)
      $ port=$(kubectl get service spring-music-service -o jsonpath='{.spec.ports[0].nodePort}')
      $ echo "Point your browser to http://${host}:${port}/"
      
    • GKE/TKGi:

      $ host=$(kubectl get service spring-music-service -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
      $ port=$(kubectl get service spring-music-service -o jsonpath='{.spec.ports[0].port}')
      $ echo "Point your browser to http://${host}:${port}/
      

      Note: The Kubernetes cluster must allow traffic on port 80. Depending on your cloud provider, creating the external IP address of the load balancer takes some time.

Add, delete, or edit albums in the web interface and then connect to the Postgres instance and query the album table to demonstrate that your changes are saved in the database.

check-circle-line exclamation-circle-line close-line
Scroll to top icon