The Ops Community

Joseph D. Marhee
Joseph D. Marhee

Posted on

Deploying WikiJS to Kubernetes

WikiJs is an open-source Wiki project. Deploying it to Kubernetes requires just a container for the Wiki software, and then a database, which you can also run inside your Kubernetes cluster.

To start, create a Secret resource that will contain some things we will use for configuring the database:

apiVersion: v1
kind: Secret
metadata:
  name: wikijs-db-secret
type: Opaque
data:
  mysql_database: YWRtaW4=
  mysql_password: MWYyZDFlMmU2N2Rm
  mysql_user: some_base64_value
  mysql_root_user: some_base64_value
Enter fullscreen mode Exit fullscreen mode

These values can be whatever you'd like (pick a username, a database name, password, etc.) and then base64 encode them:

echo -n 'super-secret-password' | base64
Enter fullscreen mode Exit fullscreen mode

and put the return value into the appropriate field above.

You'll use this in the two deployments you're going to create for each of the components. The first component is the database:

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
  labels:
    app: wikijs-db
  name: wikijs-db
spec:
  replicas: 1
  strategy:
    type: Recreate
  template:
    spec:
      containers:
        - env:
            - name: MYSQL_DATABASE
              valueFrom:
                secretKeyRef:
                  name: wikijs-db-secret
                  key: mysql_database
            - name: MYSQL_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: wikijs-db-secret
                  key: mysql_password
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: wikijs-db-secret
                  key: mysql_root_password
            - name: MYSQL_USER
              valueFrom:
                secretKeyRef:
                  name: wikijs-db-secret
                  key: mysql_user
            - name: PGID
              value: "1000"
            - name: PUID
              value: "1000"
            - name: TZ
              value: Europe/London
          image: lscr.io/linuxserver/mariadb
          name: wikijs-db
          ports:
            - containerPort: 3306
          resources: {}
          volumeMounts:
            - mountPath: /config
              name: wikijs-db-hostpath0
      restartPolicy: Always
      volumes:
        - hostPath:
            path: /mnt/wikijs/wikijs-db-data
          name: wikijs-db-hostpath0
status: {}
Enter fullscreen mode Exit fullscreen mode

For the purposes of the demonstration, I've used a hostPath volume and a single replica:

      volumes:
        - hostPath:
            path: /mnt/wikijs/wikijs-db-data
          name: wikijs-db-hostpath0
Enter fullscreen mode Exit fullscreen mode

If using this for anything you'd like to keep, I recommend using a PersistentVolume.

Before you can proceed to deploy the WikiJS deployment that will connect to this database, you need a Service which will create the cluster DNS entry and access to the database:

apiVersion: v1
kind: Service
metadata:
  name: wikijs-db-service
spec:
  ports:
  - protocol: TCP
    port: 3306
    targetPort: 3306
  selector:
    app: wikijs-db
Enter fullscreen mode Exit fullscreen mode

Now the app will be able to locate the database at wikijs-db.default.svc.cluster.local. Now, we can create the WikiJS Deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: kompose convert -f wikijks.yaml --volumes hostPath
    kompose.version: 1.26.1 (a9d05d509)
  creationTimestamp: null
  labels:
    io.kompose.service: wikijs
    app: wikijs
  name: wikijs
spec:
  replicas: 1
  selector:
    matchLabels:
      io.kompose.service: wikijs
      app: wikijs
  strategy:
    type: Recreate
  template:
    metadata:
      annotations:
        kompose.cmd: kompose convert -f wikijks.yaml --volumes hostPath
        kompose.version: 1.26.1 (a9d05d509)
      creationTimestamp: null
      labels:
        io.kompose.service: wikijs
        app: wikijs
    spec:
      containers:
        - env:
            - name: DB_HOST
              value: wikijs-db.default.svc.cluster.local
            - name: DB_NAME
              valueFrom:
                secretKeyRef:
                  name: wikijs-db-secret
                  key: mysql_database
            - name: DB_PASS
              valueFrom:
                secretKeyRef:
                  name: wikijs-db-secret
                  key: mysql_pass
            - name: DB_PORT
              value: "3306"
            - name: DB_TYPE
              value: mariadb
            - name: DB_USER
              valueFrom:
                secretKeyRef:
                  name: wikijs-db-secret
                  key: mysql_user
          image: ghcr.io/requarks/wiki:2
          name: wikijs
          ports:
            - containerPort: 3000
          resources: {}
          volumeMounts:
            - mountPath: /config
              name: wikijs-hostpath0
      restartPolicy: Always
      volumes:
        - hostPath:
            path: /root/wikijs/Wikijs
          name: wikijs-hostpath0
Enter fullscreen mode Exit fullscreen mode

using the same Secrets as above, and the same hostPath volume for your Wikijs config files, etc.

Now, after you apply the above, you'll have a working WikiJS connecting to your MySQL database, however, it won't yet be accessible. The two remaining pieces are a Service for WikiJS, and then an Ingress object that will route requests for your site URL to that service.

The Service should look like this:

apiVersion: v1
kind: Service
metadata:
  labels:
    app: wikijs
  name: wikijs-service
spec:
  ports:
    - protocol: TCP
      port: 80
      targetPort: 3000
  selector:
    app: wikijs
Enter fullscreen mode Exit fullscreen mode

which will create the wikijs-service that we'll refer to in the Ingress:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: wikijs-ingress
spec:
  rules:
  - host: wiki.c00lz0ne.internal
    http:
      paths:
      - backend:
          service:
            name: wikijs-service
            port:
              number: 80
        path: /
        pathType: Prefix
#status:
#  loadBalancer:
#    ingress:
#    - ip: ${UPSTREAM_LB_IP}
Enter fullscreen mode Exit fullscreen mode

So now, requests from the specified Load Balancer upstream that come through for wiki.c00lz0ne.internal to this Ingress will be routed to the wikijs-service application.

Discussion (0)