# Percona Operator for PostgreSQL single-namespace and multi-namespace deployment There are two design patterns that you can choose from when deploying Percona Operator for PostgreSQL and PostgreSQL clusters in Kubernetes: * Namespace-scope - one Operator per Kubernetes namespace, * Cluster-wide - one Operator can manage clusters in multiple namespaces. This how-to explains how to configure Percona Operator for PostgreSQL for each scenario. ## Namespace-scope By default, Percona Operator for PostgreSQL functions in a specific Kubernetes namespace. You can create one during the installation (like it is shown in the [installation instructions](kubernetes.md)) or just use the default namespace. This approach allows several Operators to co-exist in one Kubernetes-based environment, being separated in different namespaces: ![image](assets/images/cluster-wide-1.svg) Normally this is a recommended approach, as isolation minimizes impact in case of various failure scenarios. This is the default configuration of our Operator. Let’s say you will use a Kubernetes Namespace called `percona-db-1`. 1. Clone `percona-postgresql-operator` repository: ``` {.bash data-prompt="$" } $ git clone -b v{{ release }} https://github.com/percona/percona-postgresql-operator $ cd percona-postgresql-operator ``` 2. Create your `percona-db-1` Namespace (if it doesn't yet exist) as follows: ``` {.bash data-prompt="$" } $ kubectl create namespace percona-db-1 ``` 3. Deploy the Operator [using :octicons-link-external-16:](https://kubernetes.io/docs/reference/using-api/server-side-apply/) the following command: ``` {.bash data-prompt="$" } $ kubectl apply --server-side -f deploy/bundle.yaml -n percona-db-1 ``` 4. Once Operator is up and running, deploy the database cluster itself: ``` {.bash data-prompt="$" } $ kubectl apply -f deploy/cr.yaml -n percona-db-1 ``` You can deploy multiple clusters in this namespace. ### Add more namespaces What if there is a need to deploy clusters in another namespace? The solution for namespace-scope deployment is to have more than one Operator. We will use the `percona-db-2` namespace as an example. 1. Create your `percona-db-2` namespace (if it doesn't yet exist) as follows: ``` {.bash data-prompt="$" } $ kubectl create namespace percona-db-2 ``` 2. Deploy the Operator: ``` {.bash data-prompt="$" } $ kubectl apply --server-side -f deploy/bundle.yaml -n percona-db-2 ``` 3. Once Operator is up and running deploy the database cluster itself: ``` {.bash data-prompt="$" } $ kubectl apply -f deploy/cr.yaml -n percona-db-2 ``` !!! note Cluster names may be the same in different namespaces. ## Install the Operator cluster-wide Sometimes it is more convenient to have one Operator watching for Percona Distribution for PostgreSQL custom resources in several namespaces. We recommend running Percona Operator for PostgreSQL in a traditional way, limited to a specific namespace, to limit the blast radius. But it is possible to run it in so-called *cluster-wide* mode, one Operator watching several namespaces, if needed: ![image](assets/images/cluster-wide-2.svg) To use the Operator in such cluster-wide mode, you should install it with a different set of configuration YAML files, which are available in the deploy folder and have filenames with a special `cw-` prefix: e.g. `deploy/cw-bundle.yaml`. While using this cluster-wide versions of configuration files, you should set the following information there: * `subjects.namespace` option should contain the namespace which will host the Operator, * `WATCH_NAMESPACE` key-value pair in the `env` section should have `value` equal to a comma-separated list of the namespaces to be watched by the Operator, *and* the namespace in which the Operator resides. If this key is set to a blank string, the Operator will watch **only the namespace it runs in**, which would be the same as [single-namespace deployment](cluster-wide.md#namespace-scope). !!! note Installing the Operator cluster-wide on OpenShift via the the Operator Lifecycle Manager (OLM) requires [making different selections in the OLM web-based UI](openshift.md#install-the-operator-via-the-operator-lifecycle-manager-olm) instead of patching YAML files. The following simple example shows how to install Operator cluster-wide on Kubernetes. 1. Clone `percona-postgresql-operator` repository: ``` {.bash data-prompt="$" } $ git clone -b v{{ release }} https://github.com/percona/percona-postgresql-operator $ cd percona-postgresql-operator ``` 2. Let’s say you will use `pg-operator` namespace for the Operator, and `percona-db-1` namespace for the cluster. Create these namespaces, if needed: ``` {.bash data-prompt="$" } $ kubectl create namespace pg-operator $ kubectl create namespace percona-db-1 ``` 3. Edit the ``deploy/cw-bundle.yaml`` configuration file to make sure it contains proper namespace name for the Operator: ```yaml ... subjects: - kind: ServiceAccount name: percona-postgresql-operator namespace: pg-operator ... spec: containers: - env: - name: WATCH_NAMESPACE value: "pg-operator,percona-db-1" ... ``` 4. Apply the `deploy/cw-bundle.yaml` file with the following command: ``` {.bash data-prompt="$" } $ kubectl apply --server-side -f deploy/cw-bundle.yaml -n pg-operator ``` Right now the operator deployed in cluster-wide mode will monitor all namespaces in the cluster, either already existing or newly created ones. 5. Deploy the cluster in the namespace of your choice: ``` {.bash data-prompt="$" } $ kubectl apply -f deploy/cr.yaml -n percona-db-1 ``` ## Verifying the cluster operation When creation process is over, you can try to connect to the cluster. {% include 'assets/fragments/connectivity.txt' %}