Installation auf Kubernetes

Yes

Whatever ENV you specify when you finally do you docker run on the image … will take.

And doing those things at every boot of the container is reasonable?

Some people like this pattern, I do not, that is why rails introduces some super fancy locking around migrations to ensure they never run concurrently when a cluster of 20 app try to run migrations at the same time.

Some people just boot the same image and run the migrations in some out-of-band process.

But each container should still rake assets:precompile at least once?

Depends on how do you host those.

If you have a NFS share of some sort (like AWS EFS) or if you upload those to some object storage service that can only be done on bootstrap.

Vielen Dank an alle für die hilfreichen Antworten. Ich habe noch ein paar Fragen dazu, wie man das richtig macht.

Ich versuche, eine minimale app.yaml für das Bootstrapping vorzubereiten, die nur die für den Container-Build erforderlichen Informationen enthält. Einige der darin enthaltenen Informationen sind eindeutig für die Container-Laufzeit gedacht – wie die Volume-Mounts und Port-Mappings. Bei den Umgebungsvariablen bin ich mir jedoch nicht sicher. Ich werde es einfach versuchen, aber werden diese Umgebungsvariablen während des Container-Builds verwendet (irgendwie in das Dockerfile injiziert) oder erst zur Laufzeit des Containers? Falls Letzteres zutrifft, werde ich sicherstellen, dass sie in die entsprechende k8s-Konfigurationsdatei gelangen.

Zweitens: Einige Leute hier haben davon gesprochen, das Image in ein privates Container-Repository zu pushen. Ist das erforderlich? Anders ausgedrückt – enthält das Build-Image vertrauliche Informationen, die nicht in einem öffentlichen Repository wie Docker Hub veröffentlicht werden sollten? (Wir haben noch kein privates Container-Repository und möchten dessen Einrichtung gerne vermeiden.)

Schließlich: Gibt es eine app.yaml-Einstellung, um den Namen des erstellten Containers zu steuern? Das wäre eher eine Sache der Feinabstimmung, aber es wäre schön :slight_smile:.

Vielen Dank im Voraus für die Hilfe! (Entschuldigung, dass ich einen alten Thread wiederbelebe. Dies ist das erste Ergebnis bei Google, wenn man nach der Installation von Discourse auf Kubernetes sucht.)

@Geoffrey_Challen Du kannst ein Image mit dem Discourse-Repository und den Plugins erstellen, die Ruby-Gems und anderen Abhängigkeiten installieren und es in eine Registry (wie DockerHub) hochladen. Dieses Repository wäre umgebungsunabhängig und könnte öffentlich sein (sofern du kein privates Plugin oder Ähnliches einbeziehst). Dieses Basis-Image könnte in Staging- und Produktionsumgebungen sowie in verschiedenen Projekten verwendet werden (sofern sie dieselben Plugins nutzen).

Schritte wie das Vorkompilieren der Assets, die Datenbankmigration und das Erstellen des SSL-Zertifikats sollten jedoch auf dem Zielrechner ausgeführt werden, um das endgültige Image zu generieren.

Ich weiß nicht genau, wie man das in einem k8s-Cluster integriert. Ich habe mich für den konservativen Ansatz entschieden und mich an den offiziellen Leitfaden des Discourse-Teams gehalten, wobei ich die Schritte in zwei Teile aufgeteilt habe.

Bei diesem Teil bin ich mir nicht sicher, ob ich ihn richtig verstehe. Passen diese Vorgänge nicht automatisch innerhalb des Containers, wie sie benötigt werden? Ich hoffe, ich kann das einfach in unsere Cloud hochladen und muss nie wieder Shell-Zugriff auf die Maschine erhalten – ähnlich wie ich selten (wenn überhaupt) das Discourse Docker-Container betreten muss.

Sie müssen den Container nicht explizit eingeben. Was ich damit meine, ist, dass Sie damit kein vorkompiliertes Image erstellen können (z. B. in einer CI-Pipeline generiert) und es direkt verwenden können, da dies auf dem Zielrechner ausgeführt werden muss, auf dem sich die Datenbank befindet (dies könnte automatisiert werden, aber ich habe dies in k8s noch nicht umgesetzt, obwohl ich es mit Ansible getan habe).

Ah, okay. Ich verwende die All-in-One-Vorlagen, um die Datenbank in den Container einzubinden. Das ist für unseren Anwendungsfall geeignet, bei dem es um Klassen mit 10 bis 1000 Schülern geht – zumindest hat die All-in-One-Lösung in meiner Klasse in dieser Konfiguration gut funktioniert. Die Datenbank befindet sich also im Container.

Aber unabhängig davon: Führt Discourse beim Start des Containers nicht die Datenbank-Migrationen oder andere Einrichtungsschritte aus?

Bist du sicher, dass sich die Datenbank im Container befindet? Oder ist es das RDBMS (in diesem Fall PostgreSQL)? Die unterstützte Installation verwendet die Datenbank außerhalb des Containers (was erwartet wird) und mappet ein Volume von innen nach außen (auf den Host). Zudem würde nach einem Neuaufbau des Containers dieser neu erstellt werden und du würdest alle Daten verlieren.

Wenn die Datenbank wirklich im Container liegt, weiß ich nicht genau, wie du basierend auf der offiziellen Installation ein Upgrade durchführen könntest, da das Launcher-Skript beim Neuaufbau den Container mehrmals erstellt und zerstört (und mit --rm ausgeführt wird, was bedeutet, dass du nach dem Stoppen des Containers alle Daten, einschließlich der Datenbank, verlieren würdest).

Ich habe nicht versucht, die Art und Weise zu ändern, wie der Neuaufbau durchgeführt wird, aber angenommen, du könntest ihn so anpassen, dass alles im Container läuft, ohne ihn neu zu erstellen, dann solltest du in der Lage sein, den Container in eine Registry zu pushen (stelle sicher, dass sie privat ist, da die Geheimnisse dort enthalten wären). Dennoch empfehle ich diesen Ansatz aus mehreren Gründen nicht (einige davon wurden bereits erwähnt).

Die Standardinstallation enthält nginx, Rails, Postgres und Redis innerhalb des Containers. Für die Daten von Postgres und Redis werden externe Volumes verwendet. Diese werden bei einem Neuaufbau oder Upgrade nicht zerstört.

Ja, es ist nur seltsam, dass er sagte, die Datenbank liege innerhalb des Containers, es sei denn, er hat die Funktionsweise der Standardinstallation geändert oder meinte PostgreSQL, nicht die Datenbank selbst.

Nein – die Migrationen und Schritte zur Kompilierung von Assets erfolgen während der Phase ./launcher bootstrap, nachdem Plugins aufgelöst wurden. Sobald dies abgeschlossen ist, kann der Container beliebig oft neu gestartet werden, oder die Web-Prozesse können auf mehrere Maschinen verteilt werden, usw.


Im Idealfall sollte die Einrichtung etwa so aussehen:

  • Offizielle Builds stellen das Discourse-Basis-Image bereits bereit.
    • Wir benötigen zudem einen Container für discourse_docker, der über eine eigene Docker-Installation verfügt.
  • Einrichtung eines privaten Registries innerhalb des Clusters.
  • Einrichtung eines ConfigMap mit dem Inhalt der app.yml.
  • Ausführung eines Jobs, der ./launcher bootstrap unter Verwendung von verschachteltem Docker auf einem VM-basierten Knoten (ohne Zugriff auf den Docker-Socket) ausführt, das resultierende Image umbenennt und in das private Registry pusht (mit einem zeitstempelbasierten Label, nicht latest; local_discourse ist hier kein guter Name) und das Deployment auf das neue Label rollt.
    • Wow, das sind wirklich viele Berechtigungen für den Upgrade-Job.

Postgres läuft im Container. Es speichert die Daten außerhalb des Containers, aber es läuft im Container, wenn du den Standard-Installationsvorlagen folgst. Gleiches gilt für Redis. Ich denke, die Verwirrung entsteht, wenn ich sage „die Datenbank läuft im Container

Ich denke, das ergibt für mich jetzt mehr Sinn, besonders nach dem Lesen der langen verlinkten Diskussion zu docker-compose, dem Launcher-Skript usw.

Hier ist, was ich gerne tun möchte:

  • ./launcher bootstrap lokal ausführen, um ein „fat

Vielleicht interessieren Sie sich für eine Multi-Site-Konfiguration.

Sie stoßen auf zwei große Probleme: Discourse ist noch nicht für Kubernetes bereit, sodass benutzerdefinierter Code erforderlich ist. Außerdem bewegen Sie sich in den Bereich, in dem das Discourse-Team Geld verdient (Betreuung einer großen Anzahl von Foren), sodass das Unterstützungsniveau sinken wird.

Mein Rat? Führen Sie eine Multi-Site-Konfiguration mit statischer Planung auf VMs durch, die sich vollständig außerhalb Ihres Clusters befinden. (Oder nutzen Sie einen Service vom Typ ExternalName, der auf die VM zeigt, um denselben Ingress beizubehalten.)

OK… I managed to figure out one way of doing this. I’m not 100% happy with it, but it does work and may be appealing to others that are trying a simple single-container fat (includes postgres, redis, etc.) Discourse image deployment to Kubernetes.

My Approach

After examining the bootstrap process it became clear to me that unfortunately it mixes in two different kinds of operations—ones that only affect the underlying container, and others that poke out into the surrounding environment, mainly through the /shared volume mount where the postgres data files live. Rather than trying to tease these steps apart, it seems more sane to just run the bootstrapping steps in the environment where the container is actually going to be deployed.

Unfortunately, launcher bootstrap wants to create a container and use Docker. So running launcher inside another container (for example, in a container running on our cloud) means either tangling with a Docker-in-Docker setup (doable, but not considered best practices) or exposing the underlying Docker daemon. I’m not even sure that that second approach would work, since I think that it would interpret a volume mount against the node’s local filesystem, whereas in our scenario we want to volume mount /shared to a persistent Kubernetes volume. Maybe the Docker-in-Docker route would work, but then you’d also have a weird triple volume mount from inside the nested container into the outer container and from there to the persistent Kubernetes volume. That sounds… unwise.

However, essentially launcher bootstrap creates one large .yml file by processing the templates value in the app.yml and then passes that to the Discourse base image when finishes the bootstrap process. So if we can extract the configuration file we can generate the configuration on any machine and then we only need to figure out how to pass it to a container we start in the cloud.

So as an overview, here are the steps we are going to follow:

  1. Generate the bootstrap configuration using a modified launcher
  2. Pass that to a modified Discourse base image that will perform the bootstrapping (using pups) and then start Discourse

Generating the Bootstrapping Configuration

Here’s the required change to launcher to support a dump command that writes the merged configuration to STDOUT:

run_dump() {
  set_template_info
  echo "$input"
}

(Note that this command is available in our fork of discourse_docker.)

So the first step is to use the new launcher dump command added above to create our bootstrap configuration:

# Substitute whatever your container configuration is called for app
./launcher dump app > bootstrap.yml

Initial Container Creation

Next we need a container that knows to run pups to bootstrap the container before booting via /sbin/boot. I used the following Dockerfile to make a tiny change to the base discourse image:

FROM discourse/base:2.0.20191219-2109
COPY scripts/bootstrap.sh /
CMD bash bootstrap.sh

Where scripts/bootstrap.sh contains:

cd /pups/ && /pups/bin/pups --stdin < /bootstrap/bootstrap.yml && /sbin/boot

I published this as geoffreychallen:discourse_base:2.0.20191219-2109. (Note that you could probably also accomplish the same thing by modifying the boot command of the base Discourse docker image, but I was having a hard time getting that to work with the shell redirection required to get pups to read the configuration file.)

Kubernetes Configuration

Now we need our Kubernetes configuration. Mine looks like this:

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: kotlin-forum-pvc
  namespace: ikp
spec:
  storageClassName: rook-ceph-block
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 64Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: kotlin-forum-deployment
  namespace: ikp
spec:
  replicas: 0
  selector:
    matchLabels:
      app: kotlin-forum
  template:
    metadata:
      labels:
        app: kotlin-forum
    spec:
      volumes:
      - name: kotlin-forum
        persistentVolumeClaim:
          claimName: kotlin-forum-pvc
      - name: bootstrap
        configMap:
          name: kotlin-forum-bootstrap
      containers:
      - name: kotlin-forum
        image: geoffreychallen/discourse_base:2.0.20191219-2109
        imagePullPolicy: Always
        volumeMounts:
        - name: kotlin-forum
          mountPath: /shared/
        - name: bootstrap
          mountPath: /bootstrap/
        ports:
        - containerPort: 80
        env:
        - name: TZ
          value: "America/Chicago"
        - name: LANG
          value: en_US.UTF-8
        - name: RAILS_ENV
          value: production
        - name: UNICORN_WORKERS
          value: "3"
        - name: UNICORN_SIDEKIQS
          value: "1"
        - name: RUBY_GLOBAL_METHOD_CACHE_SIZE
          value: "131072"
        - name: RUBY_GC_HEAP_GROWTH_MAX_SLOTS
          value: "40000"
        - name: RUBY_GC_HEAP_INIT_SLOTS
          value: "400000"
        - name: RUBY_GC_HEAP_OLDOBJECT_LIMIT_FACTOR
          value: "1.5"
        - name: DISCOURSE_DB_SOCKET
          value: /var/run/postgresql
        - name: DISCOURSE_DEFAULT_LOCALE
          value: en
        - name: DISCOURSE_HOSTNAME
          value: kotlin-forum.cs.illinois.edu
        - name: DISCOURSE_DEVELOPER_EMAILS
          value: challen@illinois.edu
        - name: DISCOURSE_SMTP_ADDRESS
          value: outbound-relays.techservices.illinois.edu
        - name: DISCOURSE_SMTP_PORT
          value: "25"
---
apiVersion: v1
kind: Service
metadata:
  name: kotlin-forum
  namespace: ikp
spec:
  type: NodePort
  ports:
  - name: http
    port: 80
    targetPort: 80
  selector:
    app: kotlin-forum
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  namespace: ikp
  annotations:
    nginx.ingress.kubernetes.io/ssl-redirect: "true"
  name: kotlin-forum-ingress
spec:
  rules:
  - host: kotlin-forum.cs.illinois.edu
    http:
      paths:
      - backend:
          serviceName: kotlin-forum
          servicePort: 80

Yours will look different. Note that I’m terminating HTTPS upstream, hence the modifications to the Ingress configuration. I also like to put everything in one file, delete pieces that don’t work as I iterate, and then let Kubernetes skip duplicates on the next kubectl create -f. Also note that I set replicas: 0 so that the deployment doesn’t start as soon as its configured. That’s because we have one bit of additional configuration to finish.

I copied the list of environment variables from what I saw being passed to the container by launcher start. I don’t know if all of these are necessary and others may be missing depending on your configuration. YMMV.

Note that we have two volume maps pointing into the container: the first is for postgres, configured as a persistent volume that will survive pod restarts. The second is a configuration mapping created like this:

kubectl create configmap kotlin-forum-bootstrap --from-file=bootstrap.yml=<path/to/bootstrap.yml>

Where kotlin-forum-bootstrap needs to match your Kubernetes configuration and path/to/bootstrap.yml is the path to the bootstrap.yml file we created using launcher dump above.

Once your configmap is in place, you should be able to scale your deployment to one replica and see Discourse booting and running the same bootstrap process that launcher bootstrap would have performed. That takes a few minutes. When that is done, your Discourse installation will boot.

Others Bits of Configuration

A few other notes that I ran on the way to getting this (at least for now) fully configured:

  • Any upstream proxies must forward X-Forwarded headers, including both X-Fowarded-For, X-Forwarded-Proto, X-Forwarded-Port. Not doing so will result in strange authentication errors when trying to use Google login and probably other login providers.
  • Your nginx ingress controller must be configured to pass headers by setting use-forwarded-headers in the global config map. This took me a while to get right, since at least several times I edited the wrong configuration map, and then expected my ingress containers to restart when the configuration map changed. (They didn’t.)

Updating

To update the deployed installation, you regenerate the new bootstrap.yml file, update the config map, and then restart the container (easiest by scaling to 0 and then back to 1 replica).

This does incur a bit of downtime since the bootstrapping happens before the container is built. But this seems inevitable to me in cases where you need to update the configuration and/or change the base image. launcher rebuild is documented as stop; bootstrap; start, meaning that the bootstrap process will still cause downtime even if performed using the launcher script.

Comments

This fat container Discourse deployment pattern would be much easier to support if the launcher script would more cleanly separate (a) bootstrap steps that could be performed offline and only affect the files in the container and (b) bootstrap steps that modify or need access to the database or other state outside the container. The approach described above is a bit frustrating because you do see all kinds of JS uglification, asset minification, and other things that could be done with the previous deployment running… but they are just too mixed in with other things (like database migrations) that can’t be done without access to the database. I briefly thought about creating a container that would only perform the steps in templates/postgres.yml, but then noticed that database migrations were being done by the web template, and thought about plugins, and then just gave up :slight_smile:.

With better separation redeployment for fat containers could work something like this:

  • Build new fat container offline performing all steps internal to the container
  • Publish that container
  • When ready to upgrade, stop the previous container, start the new fat container, and let it finish any steps that need database access. Based on my experimentation those seem faster than some of the other bootstrap steps.

That would result in a bit less downtime. It’s probably not worth the effort for that reason alone, but I can imagine that this might also simplify more complex deployment scenarios involving shared databases or whatever.

Das ergibt mehr Sinn. Das habe ich getan, als ich den Bootstrap-Prozess in zwei Schritte aufgeteilt habe. Der erste kann in einer isolierten Umgebung (wie einer CI-Pipeline) ausgeführt werden, um ein Basis-Image mit dem Discourse-Repository, den Gems und installierten Plugins zu erstellen. Der zweite Schritt muss auf der Zielmaschine ausgeführt werden (oder zumindest Zugriff auf die Produktionsdatenbank haben), um die Datenbankmigration durchzuführen und die Assets zu generieren (dies erfolgt im Bootstrap-Prozess, nicht beim Starten des Containers).

Ja, das wäre großartig. Ich habe das bereits angefragt, aber ich weiß nicht, ob und wann das umgesetzt wird.

Das vollständig in einer separaten Umgebung umzusetzen, wäre schwierig, da die Aufgabe zum Precompilieren der Assets Zugriff auf die Datenbank benötigt (für Dinge wie benutzerdefiniertes CSS). Es wäre jedoch großartig, wenn nur der Teil, der von der Datenbank abhängt, in einem separaten Schritt erledigt werden könnte (und alle anderen Assets, die nicht von der Datenbank abhängen, separat vorkompiliert werden könnten). Ich weiß jedoch nicht, wie technisch umsetzbar das wäre.

Das ist im Wesentlichen das, was ich bei den von mir durchgeführten Kubernetes-Installationen mache. Ich kann mir nicht vorstellen, wie oder warum man Kubernetes ohne separate Daten- und Web-Container (oder eine andere Art von externem PostgreSQL und Redis) einsetzen sollte – die Installationen, die ich für Kunden durchgeführt habe, nutzen dafür GCP-Ressourcen.

Außerdem gibt es eine Umgebungsvariable namens skip_post_migration_updates, die man für echte Upgrades ohne Ausfallzeit verstehen muss. Sie wird hier beschrieben.