<a id="tutorial"></a>

# Deploy the Discourse charm for the first time

In this tutorial, we’ll go through each step of the process to get a basic Discourse deployment.

## What you’ll need

You will need a working station, e.g., a laptop, with AMD64 architecture. Your working station
should have at least 4 CPU cores, 8 GB of RAM, and 50 GB of disk space.

#### TIP
You can use Multipass to create an isolated environment by running:

```default
multipass launch 24.04 --name charm-tutorial-vm --cpus 4 --memory 8G --disk 50G
```

When using a Multipass VM, make sure to replace `127.0.0.1` IP addresses with the
VM IP in steps that assume you’re running locally. To get the IP address of the
Multipass instance run `multipass info discourse-tutorial-vm`.

This tutorial requires the following software to be installed on your working station
(either locally or in the Multipass VM):

- Juju 3
- MicroK8s 1.33

Use [Concierge](https://github.com/canonical/concierge) to set up Juju and MicroK8s:

```default
sudo snap install --classic concierge
sudo concierge prepare -p microk8s
```

This first command installs Concierge, and the second command uses Concierge to install
and configure Juju and MicroK8s.

MicroK8s must have an NGINX ingress controller enabled. Complete this requirement by running:

```default
microk8s enable ingress
```

For more details, see [Add-on: Ingress](https://microk8s.io/docs/addon-ingress).

For this tutorial, Juju must be bootstrapped to a MicroK8s controller. Concierge should
complete this step for you, and you can verify by checking for `msg="Bootstrapped Juju" provider=microk8s`
in the terminal output and by running `juju controllers`.

If Concierge did not perform the bootstrap, run:

```default
juju bootstrap microk8s tutorial-controller
```

## What you’ll do

1. Deploy the Discourse charm
2. Integrate with `nginx-ingress-integrator`, `postgresql-k8s` and `redis-k8s` charms
3. Inspect the Kubernetes resources created
4. Create an admin user and log in
5. Clean up the environment

### Shell into the Multipass VM

#### NOTE
If you’re working locally, you don’t need to do this step.

To be able to work inside the Multipass VM first you need to log in with the following command:

```default
multipass shell charm-tutorial-vm
```

### Add a Juju model for the tutorial

To easily clean up the resources and separate your workload from the contents of this tutorial, set up a new Juju model named `discourse-tutorial`:

```default
juju add-model discourse-tutorial
```

### Deploy the charms

Discourse requires connections to PostgreSQL and Redis. For more information, see the [Charm Integrations](https://canonical.com/juju/docs/discourse-k8s-charm/latest/reference/integrations/index.html.md#reference-integrations).

#### NOTE
Discourse requires PostgreSQL extensions to be available in the relation.

Deploy the charms:

```default
juju deploy redis-k8s --channel latest/edge
juju deploy postgresql-k8s --channel 16/stable --trust
juju deploy discourse-k8s
```

Enable the required PostgreSQL extensions (for `16/stable`, use kebab-case option names):

```default
juju config postgresql-k8s plugin-hstore-enable=true plugin-pg-trgm-enable=true plugin-vector-enable=true
```

### Integrate with the Redis k8s charm and the PostgreSQL k8s charm

Integrate `redis-k8s` and `postgresql-k8s` to `discourse-k8s`:

```default
juju integrate redis-k8s discourse-k8s
juju integrate discourse-k8s postgresql-k8s
```

By running `juju status --relations` the current state of the deployment can be queried:

```text
Model               Controller  Cloud/Region        Version  SLA          Timestamp
discourse-tutorial  microk8s    microk8s/localhost  3.5.4    unsupported  14:07:18+03:00

App             Version  Status  Scale  Charm           Channel        Rev  Address         Exposed  Message
discourse-k8s   3.3.0    active      1  discourse-k8s   latest/stable  173  10.152.183.231  no
postgresql-k8s  16.x     active      1  postgresql-k8s  16/stable      381  10.152.183.143  no
redis-k8s       7.2.5    active      1  redis-k8s       latest/edge     36  10.152.183.188  no

Unit               Workload  Agent  Address      Ports  Message
discourse-k8s/0*   active    idle   10.1.32.182
postgresql-k8s/0*  active    idle   10.1.32.184         Primary
redis-k8s/0*       active    idle   10.1.32.181

Integration provider           Requirer                       Interface          Type     Message
discourse-k8s:restart          discourse-k8s:restart          rolling_op         peer
postgresql-k8s:database        discourse-k8s:database         postgresql_client  regular
postgresql-k8s:database-peers  postgresql-k8s:database-peers  postgresql_peers   peer
postgresql-k8s:restart         postgresql-k8s:restart         rolling_op         peer
postgresql-k8s:upgrade         postgresql-k8s:upgrade         upgrade            peer
redis-k8s:redis                discourse-k8s:redis            redis              regular
redis-k8s:redis-peers          redis-k8s:redis-peers          redis-peers        peer
```

The deployment finishes when all the charms show `Active` states.

Run `kubectl get pods -n discourse-tutorial` to see the pods that are being created by the charms:

```text
NAME                             READY   STATUS    RESTARTS   AGE
modeloperator-c584f6f9f-qf9gr    1/1     Running   0          5m30s
redis-k8s-0                      3/3     Running   0          5m22s
discourse-k8s-0                  2/2     Running   0          5m1s
postgresql-k8s-0                 2/2     Running   0          5m9s
```

### Provide ingress capabilities

In order to expose the charm, the Nginx Ingress Integrator needs to be deployed and integrated with Discourse:

```default
juju deploy nginx-ingress-integrator
```

To check if RBAC is enabled run the following command:

```default
microk8s status | grep rbac
```

If it is enabled, then the output should be like the following:

```text
rbac                 # (core) Role-Based Access Control for authorisation
```

If the output is empty then RBAC is not enabled.

If your cluster has RBAC enabled, you’ll be prompted to run the following command:

```default
juju trust nginx-ingress-integrator --scope=cluster
```

Then you need to integrate the charm with Nginx Ingress Integrator:

```default
juju integrate discourse-k8s nginx-ingress-integrator
```

### Validate workload is reachable

To validate that you can successfully reach the deployed workload, run the following command:

```default
curl http://discourse-k8s --resolve discourse-k8s:80:127.0.0.1
```

### Create an admin user and log in

To create an admin user, use the `create-user` action:

```default
juju run discourse-k8s/0 create-user admin=true email=email@example.com
```

The command will return the password of the created user. Discourse will be deployed with `discourse-k8s` as default hostname.

If you are following the tutorial in your local machine, modify your `/etc/hosts` file so that it points to `127.0.0.1`:

```default
echo "127.0.0.1 discourse-k8s" | sudo tee -a /etc/hosts
```

After that, visit `http://discourse-k8s` to reach Discourse, using the credentials returned from the `create-user` action to login.

### Clean up the environment

Congratulations! You have successfully finished the Discourse tutorial. You can now remove the
model environment that you’ve created using the following command:

```default
juju destroy-model discourse-tutorial --destroy-storage
```

If you used Multipass, to remove the Multipass instance you created for this tutorial, use the following command.

```default
multipass delete --purge charm-tutorial-vm
```

Finally, remove the `127.0.0.1 discourse-k8s` line from the `/etc/hosts` file.

## Next steps

You achieved a basic deployment of the Discourse charm. If you want to go further in your deployment
or learn more about the charm, check out these pages:

- Perform basic operations with your deployment like
  [configuring S3](https://canonical.com/juju/docs/discourse-k8s-charm/latest/how-to/configure-s3/index.html.md#how-to-configure-s3)
  or [SMTP](https://canonical.com/juju/docs/discourse-k8s-charm/latest/how-to/configure-smtp/index.html.md#how-to-configure-smtp).
- Make your deployment more secure by [configuring SAML](https://canonical.com/juju/docs/discourse-k8s-charm/latest/how-to/configure-saml/index.html.md#how-to-configure-saml),
  and learn more about the charm’s security in
  [Security overview](https://canonical.com/juju/docs/discourse-k8s-charm/latest/explanation/security/index.html.md#explanation-security).
- Learn more about the available [relation endpoints](https://canonical.com/juju/docs/discourse-k8s-charm/latest/reference/integrations/index.html.md#reference-integrations)
  for the Discourse charm.
