K3s Cluster in a Weekend – Part 5 – Accessing BookStack

PCs Connected to Globe

In the same way you would to assign an IP address to a service in a traditional data center, Kubernetes Pod must also have a known address within the cluster. This is created by using a Service resource.

BookStack will have two defined Services, one for the front end, bookstack-svc and a second one for bookstack-db-svc, the SQL database.

bookstack-svc.yaml manifest

---
apiVersion: v1
kind: Service
metadata:
  annotations:
  labels:
    app: bookstack
    partOf: bookstack
  name: bookstack-svc
spec:
  type: LoadBalancer
  ports:
    - name: http
      port: 80
      protocol: TCP
      targetPort: 80
  selector:
    app: bookstack
    partOf: bookstack

Key lines here come in the spec: ... section. MetalLB will assign the next available IP from its block when type: LoadBalancer is set. This gives the Services an address external to the cluster.

Next, port: 80 and targetPort: 80 indicates which port the Services exposes and on which port the Pod is listening, respectively.

Finally, the **selector: ... ** crucially describes what Pod(s) should be matched to this services based on a label, a label of app: bookstack.

bookstack-db-svc.yaml manifest

apiVersion: v1
kind: Service
metadata:
  labels:
    partOf: bookstack
  name: bookstack-db-svc
spec:
  type: ClusterIP
  clusterIP: None
  ports:
    - name: mysql
      port: 3306
      protocol: TCP
      targetPort: 3306
  selector:
    app: mariadb
    partOf: bookstack

The Service for bookstacks-db-svc uses type: ClusterIP which assigns an IP address from the clusters pool to allow internal network traffic only.

Deploy these manifests to K3s with: kubectl apply -f bookstack-db-svc.yaml -f bookstack-svc.yaml

We can access BookStack through the IP address MetalLB assigned to bookstack-svc displayed as the External-IP. kubectl get svc bookstack-svc