Setup of spring boot application & initialization of PostgreSQL database on Kubernetes – PART 2

  • In the PART 1 of this blog series, we deployed a PostgreSQL database on minikube.
  • In this part, we will deploy spring boot application .
  • I have mentioned in PART 1 , how to dockerize and upload the order service to docker registry.
  • The application.properties of the service , will get the details of database username, password and connection details from the environment variables mentioned in order service deployment yaml
spring.datasource.username=${SPRING_DATASOURCE_USERNAME}
spring.datasource.password=${SPRING_DATASOURCE_PASSWORD}
spring.datasource.url=${SPRING_DATASOURCE_URL}
]spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update
  • We will define a service object for us to access the application. yaml for service object
apiVersion: v1
kind: Service
metadata:
  name: order-svc-service
spec:
  ports:
    - protocol: "TCP"
      port: 8080
      targetPort: 8080
  selector:
    app: order-svc
  type: NodePort
  • Service type is NodePort . It makes the service accessible on a static port on each Node in the cluster. This means that the service can handle requests that originate from outside the cluster.
  • Next is deployment of order service and the yaml for that is
apiVersion: apps/v1
kind: Deployment
metadata:
  name: order-svc
spec:
  replicas: 1
  selector:
    matchLabels:
      app: order-svc
  template:
    metadata:
      labels:
        app: order-svc
    spec:
      containers:
       - image: raje/order-svc-k8
         name: order-svc
         ports:
           - containerPort: 8080
         env:
            - name: SPRING_DATASOURCE_PASSWORD
              value: changeme
            - name: SPRING_DATASOURCE_URL
              value: jdbc:postgresql://postgresql:5432/northwind?useSSL=false
            - name: SPRING_DATASOURCE_USERNAME
              value: postgres
            - name: SPRING_JPA_HIBERNATE_DDL_AUTO
              value: update
  • Key elements of the deployment yaml
    • uses – raje/order-svc-k8 container image
    • Defines the enviornment variable
    • database is accessed with the name postgresql which is the name of the service for database
  • Deploy the service and deployment objects by giving below command
kubectl apply -f order-svc-service.yaml
kubectl apply -f order-svc-deployment.yaml
  • You can verify the deployment with the command – kubectl get all
  • During the process if you want to check the logs of order service pod , you can give command kubectl logs <<podname>>
  • To access and test the application , issue the below command
kubectl port-forward service/order-svc-service 7080:8080
  • To access the API
curl http://localhost:7080/api/v1/customers

Source code for the application: https://github.com/rajeshsgr/order-svc-k8

Thank You . Please do share your comments and feedback !

One thought on “Setup of spring boot application & initialization of PostgreSQL database on Kubernetes – PART 2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s