Spring Boot application in Kubernetes with Postgresql and Loki

The topic of deploying a Spring Boot application in a Kubernetes cluster is no longer new, and many resources, including this one, have already written many articles with examples. Today I would like to talk not only about the deployment of the application itself, but also about the related services, namely: a database, a load balancer, and a system for collecting and aggregating logs.





In more detail about all components:





  1. Spring Boot application using PostgreSQL as a database





  2. Docker database server image





  3. Docker Grafana (dashboard for displaying logs)





  4. Docker image Loki(



    log collection system)





  5. Promtail (agent for sending logs to Loki).



    Kubernetes cluster will be deployed using microk8s . Nginx, or rather nginx-ingress-controller, which is in microk8s, will act as a load balancer and also a web server.









Let's look at the deployment of each component separately.





Step 1: Database

For the database, use the following yaml





apiVersion: v1
kind: Service
metadata:
  name: db
spec:
  ports:
    - port: 5432
  selector:
    app: db
  clusterIP: None
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: db
spec:
  selector:
    matchLabels:
      app: db
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: db
    spec:
      containers:
        - image: postgres:9.6
          name: db
          env:
            - name: POSTGRES_USER
              value: admin

            - name: POSTGRES_PASSWORD
              value: admin

            - name: POSTGRES_DB
              value: dbname

          ports:
            - containerPort: 5432
              name: db

      
      



The file immediately describes both the service and the deployment of the base. As an image, the Postgres 9.6 image is used.



To create the deployment , run the commandkubectl apply -f db.yaml







Step 2: Grafana

For Grafana, use the following yaml





apiVersion: v1
kind: Service
metadata:
  name: grafana
spec:
  ports:
    - port: 3000
  selector:
    app: grafana
  clusterIP: None
---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: grafana
spec:
  selector:
    matchLabels:
      app: grafana
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: grafana
    spec:
      containers:
        - image: grafana/grafana:master
          name: grafana


          ports:
            - containerPort: 3000
              name: grafana
      
      



The deployment is similar to the one used for the database. The difference is in the image (grafana / grafana: master) and in the exposed port.





Similarly, execute the commandkubectl apply -f grafana.yaml







3: Loki

yaml





apiVersion: v1
kind: Service
metadata:
  name: loki
spec:
  ports:
    - port: 3100
  selector:
    app: loki
  clusterIP: None
---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: loki
spec:
  selector:
    matchLabels:
      app: loki
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: loki
    spec:
      containers:
        - image: grafana/loki:latest
          name: loki


          ports:
            - containerPort: 3100
              name: loki

      
      



kubectl apply -f grafana.yaml







4: Promtail

promtail Helm. helm, microk8s( 2 3). Helm . config, .kube, ip . microk8s config







5: Ingress

nginx .





apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: server
spec:
  rules:
     #for nginxinc controller host should be set
    - http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: server
                port:
                  number: 8024
                  
          - path: /grafana
            pathType: Prefix
            backend:
              service:
                name: grafana
                port:
                  number: 3000       

      
      



kubectl apply -f ingress.yaml







7:

. yaml Docker . . Maven + jkube maven plugin





install



jar , k8s:resource



, k8s:build



Docker o k8s:deploy .







<profile>
            <id>kube</id>
            <properties>
                <spring.profiles.active>docker</spring.profiles.active>
            </properties>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.eclipse.jkube</groupId>
                        <artifactId>kubernetes-maven-plugin</artifactId>
                        <version>1.1.1</version>
                        <configuration>
                            <verbose>true</verbose>
          
                            <images>
                                <image>
                                    <name>imagename:latest</name>
                                    <alias>some-alias/alias>
                                    <build>
                                        <maintainer>John Smith</maintainer>
                                        <from>fabric8/java-centos-openjdk11-jre</from>
                                        <assembly>
                                            <inline>
                                                <baseDirectory>/deployments</baseDirectory>
                                            </inline>

                                        </assembly>
                                    </build>
                                </image>
                            </images>

                        </configuration>
                        <executions>
                            <execution>
                                <id>run</id>
                                <goals>
                                    <goal>resource</goal>
                                    <goal>build</goal>
                                    <goal>deploy</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>

        </profile>
      
      



image. .



.



kubectl expose deployment server --type=LoadBalancer --name=server --port=<some-port>







, ? , : ip , tcp://<ip-service>.



NumberFormatException.





8:

curl , localhost , localhost/grafana Grafana.





Step 9: Display logs

To do this, you need to log into Grafana using the username / password admin. After that, you must specify Loki (http: // loki: 3000) as the data source. Then in explore enter {app = "application-name"}.





PS

Collecting logs was based on this article .








All Articles