shellgraph

Run in Kubernetes

Run shellgraph-server in a Kubernetes cluster with a Secret, a Deployment, a Service and an Ingress.

Before you start

  • A Kubernetes cluster with an Ingress controller and a TLS certificate for the public name of the server.
  • A PostgreSQL database that the pods can reach. Install the server gives the steps to make the user and the database.
  • An LDAP directory or an OIDC identity provider. See Log in with LDAP or Log in with Pocket ID.
  • The image git.rda.run/shellgraph/server:<version>, or your own image from make image.

The image has the binary and the public CA certificates only. It runs as the user 10001. It writes nothing to disk, so it needs no volume.

Make the master key

The command keygen only prints 32 random bytes in base64. You do not have to run it in the cluster.

  1. Make a key on your computer. Use one of these commands.

    openssl rand -base64 32
    
    podman run --rm git.rda.run/shellgraph/server:<version> keygen
    
  2. Keep a copy of the key in a safe place outside the cluster, for example a password manager. If you lose the key, the server cannot read the credentials.

All the pods must use the same key. Do not change the key after the first start.

Make the Secret

  1. Put the master key, the database password and the login secrets in a Secret.

    kubectl create secret generic shellgraph \
      --from-literal=SHELLGRAPH_KEY='<key>' \
      --from-literal=PGPASSWORD='<database password>' \
      --from-literal=SHELLGRAPH_OIDC_CLIENT_SECRET='<client secret>'
    

For LDAP, put SHELLGRAPH_LDAP_BIND_PASSWORD in the Secret instead of SHELLGRAPH_OIDC_CLIENT_SECRET.

Make the Deployment, the Service and the Ingress

  1. Write the manifests to a file, for example shellgraph.yaml. Change the names, the URLs and the version.

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: shellgraph-server
    spec:
      replicas: 2
      selector:
        matchLabels:
          app: shellgraph-server
      template:
        metadata:
          labels:
            app: shellgraph-server
        spec:
          securityContext:
            runAsNonRoot: true
            runAsUser: 10001
            runAsGroup: 10001
          containers:
            - name: server
              image: git.rda.run/shellgraph/server:<version>
              ports:
                - containerPort: 8080
              env:
                - name: SHELLGRAPH_DATABASE_URL
                  value: postgres://shellgraph@postgres.db.svc:5432/shellgraph?sslmode=verify-full
                - name: SHELLGRAPH_PUBLIC_URL
                  value: https://shellgraph.example.net
                - name: SHELLGRAPH_ADMIN_GROUP
                  value: shellgraph-admins
                - name: SHELLGRAPH_VIEWER_GROUP
                  value: shellgraph-viewers
                - name: SHELLGRAPH_OIDC_ISSUER
                  value: https://id.example.net
                - name: SHELLGRAPH_OIDC_CLIENT_ID
                  value: <client ID>
              envFrom:
                - secretRef:
                    name: shellgraph
              readinessProbe:
                httpGet:
                  path: /healthz
                  port: 8080
              livenessProbe:
                httpGet:
                  path: /healthz
                  port: 8080
              securityContext:
                readOnlyRootFilesystem: true
                allowPrivilegeEscalation: false
                capabilities:
                  drop: [ALL]
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: shellgraph-server
    spec:
      selector:
        app: shellgraph-server
      ports:
        - port: 80
          targetPort: 8080
    ---
    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: shellgraph-server
    spec:
      tls:
        - hosts: [shellgraph.example.net]
          secretName: shellgraph-tls
      rules:
        - host: shellgraph.example.net
          http:
            paths:
              - path: /
                pathType: Prefix
                backend:
                  service:
                    name: shellgraph-server
                    port:
                      number: 80
    
  2. Apply the file.

    kubectl apply -f shellgraph.yaml
    
  3. Make sure that the pods are ready.

    kubectl rollout status deployment/shellgraph-server
    
  4. Do a health check through the Ingress.

    curl https://shellgraph.example.net/healthz
    

    The server returns {"status":"ok"}.

If a pod does not start, read its log with kubectl logs deployment/shellgraph-server. The server prints the missing or incorrect variable. For all the variables, see Environment variables.

Set runAsUser to the number 10001. The image gives the user by name, and Kubernetes cannot make sure that a user name is not root.

Run more than one pod

You can set replicas to more than 1. The pods do not need sticky sessions:

  • The first pod that starts makes the tables. The other pods wait for it.
  • The sessions are in the database, and all the pods make the cookie secret from the same master key and a salt in the database. Thus all the pods accept the same session cookie. This is the reason why all the pods must use the same SHELLGRAPH_KEY.

Use TLS at the Ingress

The Ingress controller sends the header X-Forwarded-Proto: https. The server then marks the session cookie as Secure. Set SHELLGRAPH_PUBLIC_URL to the public URL, so that the OIDC callback is correct. The callback is <public URL>/api/auth/oidc/callback.

Add a private CA

The image knows the public CA certificates. If the OIDC provider, the LDAP server or PostgreSQL has a certificate from a private CA, add that CA. The public CAs stay valid.

  1. Put the CA certificate (PEM) in a ConfigMap.

    kubectl create configmap shellgraph-ca --from-file=private-ca.pem=/path/to/private-ca.pem
    
  2. Add the volume and the variable SSL_CERT_DIR to the Deployment.

        spec:
          containers:
            - name: server
              env:
                - name: SSL_CERT_DIR
                  value: /etc/shellgraph/ca
              volumeMounts:
                - name: ca
                  mountPath: /etc/shellgraph/ca
                  readOnly: true
          volumes:
            - name: ca
              configMap:
                name: shellgraph-ca
    
  3. Apply the file again.

    kubectl apply -f shellgraph.yaml
    

Back up

Back up the database and the master key to different places. See Install the server. A copy of the Secret in the same cluster is not a backup of the key.