怎样在 Kubernetes 上运行 PostgreSQL
Kubernetes PostgreSQL

> 
> 创建统一管理的，具备灵活性的云原生生产部署来部署一个个性化的数据库即服务（DBaaS）。
> 
> 
> 


![cubes coming together to create a larger cube](/data/attachment/album/201904/22/105654bf6mm48m448ml8lj.png "cubes coming together to create a larger cube")


通过在 [Kubernetes](https://www.postgresql.org/) 上运行 [PostgreSQL](https://kubernetes.io/) 数据库，你能创建统一管理的，具备灵活性的云原生生产部署应用来部署一个个性化的数据库即服务为你的特定需求进行量身定制。


对于 Kubernetes，使用 Operator 允许你提供额外的上下文去[管理有状态应用](https://opensource.com/article/19/2/scaling-postgresql-kubernetes-operators)。当使用像PostgreSQL 这样开源的数据库去执行包括配置、扩展、高可用和用户管理时，Operator 也很有帮助。


让我们来探索如何在 Kubernetes 上启动并运行 PostgreSQL。


### 安装 PostgreSQL Operator


将 PostgreSQL 和 Kubernetes 结合使用的第一步是安装一个 Operator。在针对 Linux 系统的Crunchy 的[快速启动脚本](https://crunchydata.github.io/postgres-operator/stable/installation/#quickstart-script)的帮助下，你可以在任意基于 Kubernetes 的环境下启动和运行开源的[Crunchy PostgreSQL Operator](https://github.com/CrunchyData/postgres-operator)。


快速启动脚本有一些必要前提：


* [Wget](https://www.gnu.org/software/wget/) 工具已安装。
* [kubectl](https://kubernetes.io/docs/tasks/tools/install-kubectl/) 工具已安装。
* 在你的 Kubernetes 中已经定义了一个 [StorageClass](https://kubernetes.io/docs/concepts/storage/storage-classes/)。
* 拥有集群权限的可访问 Kubernetes 的用户账号，以安装 Operator 的 [RBAC](https://kubernetes.io/docs/reference/access-authn-authz/rbac/) 规则。
* 一个 PostgreSQL Operator 的 [命名空间](https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/)。


执行这个脚本将提供给你一个默认的 PostgreSQL Operator 部署，其默认假设你采用 [动态存储](https://kubernetes.io/docs/concepts/storage/dynamic-provisioning/)和一个名为 `standard` 的 StorageClass。这个脚本允许用户采用自定义的值去覆盖这些默认值。


通过下列命令，你能下载这个快速启动脚本并把它的权限设置为可执行：



```
wget <https://raw.githubusercontent.com/CrunchyData/postgres-operator/master/examples/quickstart.sh>
chmod +x ./quickstart.sh
```

然后你运行快速启动脚本：



```
./examples/quickstart.sh
```

在脚本提示你相关的 Kubernetes 集群基本信息后，它将执行下列操作：


* 下载 Operator 配置文件
* 将 `$HOME/.pgouser` 这个文件设置为默认设置
* 以 Kubernetes [Deployment](https://kubernetes.io/docs/concepts/workloads/controllers/deployment/) 部署 Operator
* 设置你的 `.bashrc` 文件包含 Operator 环境变量
* 设置你的 `$HOME/.bash_completion` 文件为 `pgo bash_completion` 文件


在快速启动脚本的执行期间，你将会被提示在你的 Kubernetes 集群设置 RBAC 规则。在另一个终端，执行快速启动命令所提示你的命令。


一旦这个脚本执行完成，你将会得到提示设置一个端口以转发到 PostgreSQL Operator pod。在另一个终端，执行这个端口转发操作；这将允许你开始对 PostgreSQL Operator 执行命令！尝试输入下列命令创建集群：



```
pgo create cluster mynewcluster
```

你能输入下列命令测试你的集群运行状况：



```
pgo test mynewcluster
```

现在，你能在 Kubernetes 环境下管理你的 PostgreSQL 数据库了！你可以在[官方文档](https://crunchydata.github.io/postgres-operator/stable/#documentation)找到非常全面的命令，包括扩容，高可用，备份等等。


这篇文章部分参考了该作者为 Crunchy 博客而写的[在 Kubernetes 上开始运行 PostgreSQL](https://info.crunchydata.com/blog/get-started-runnning-postgresql-on-kubernetes)。




---


via: <https://opensource.com/article/19/3/how-run-postgresql-kubernetes>


作者：[Jonathan S. Katz](https://opensource.com/users/jkatz05) 选题：[lujun9972](https://github.com/lujun9972) 译者：[arrowfeng](https://github.com/arrowfeng) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
By running a [PostgreSQL](https://www.postgresql.org/) database on [Kubernetes](https://kubernetes.io/), you can create uniformly managed, cloud-native production deployments with the flexibility to deploy a personalized database-as-a-service tailored to your specific needs.

Using an Operator allows you to provide additional context to Kubernetes to [manage a stateful application](https://opensource.com/article/19/2/scaling-postgresql-kubernetes-operators). An Operator is also helpful when using an open source database like PostgreSQL to help with actions including provisioning, scaling, high availability, and user management.

Let's explore how to get PostgreSQL up and running on Kubernetes.

## Set up the PostgreSQL operator


The first step to using PostgreSQL with Kubernetes is installing an Operator. You can get up and running with the open source [Crunchy PostgreSQL Operator](https://github.com/CrunchyData/postgres-operator) on any Kubernetes-based environment with the help of Crunchy's [quickstart script](https://crunchydata.github.io/postgres-operator/stable/installation/#quickstart-script) for Linux.

The quickstart script has a few prerequisites:

- The
[Wget](https://www.gnu.org/software/wget/)utility installed [kubectl](https://kubernetes.io/docs/tasks/tools/install-kubectl/)installed- A
[StorageClass](https://kubernetes.io/docs/concepts/storage/storage-classes/)defined on your Kubernetes cluster - Access to a Kubernetes user account with cluster-admin privileges. This is required to install the Operator
[RBAC](https://kubernetes.io/docs/reference/access-authn-authz/rbac/)rules - A
[namespace](https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/)to hold the PostgreSQL Operator

Executing the script will give you a default PostgreSQL Operator deployment that assumes [dynamic storage](https://kubernetes.io/docs/concepts/storage/dynamic-provisioning/) and a StorageClass named **standard**. User-provided values are allowed by the script to override these defaults.

You can download the quickstart script and set it to be executable with the following commands:

```
wget https://raw.githubusercontent.com/CrunchyData/postgres-operator/master/examples/quickstart.sh
chmod +x ./quickstart.sh
```


Then you can execute the quickstart script:

`./examples/quickstart.sh`


After the script prompts you for some basic information about your Kubernetes cluster, it performs the following operations:

- Downloads the Operator configuration files
- Sets the
**$HOME/.pgouser**file to default settings - Deploys the Operator as a Kubernetes
[Deployment](https://kubernetes.io/docs/concepts/workloads/controllers/deployment/) - Sets your
**.bashrc**to include the Operator environmental variables - Sets your
**$HOME/.bash_completion**file to be the**pgo bash_completion**file

During the quickstart's execution, you'll be prompted to set up the RBAC rules for your Kubernetes cluster. In a separate terminal, execute the command the quickstart command tells you to use.

Once the script completes, you'll get information on setting up a port forward to the PostgreSQL Operator pod. In a separate terminal, execute the port forward; this will allow you to begin executing commands to the PostgreSQL Operator! Try creating a cluster by entering:

`pgo create cluster mynewcluster`


You can test that your cluster is up and running with by entering:

`pgo test mynewcluster`


You can now manage your PostgreSQL databases in your Kubernetes environment! You can find a full reference to commands, including those for scaling, high availability, backups, and more, in the [documentation](https://crunchydata.github.io/postgres-operator/stable/#documentation).

*Parts of this article are based on *[Get Started Running PostgreSQL on Kubernetes](https://info.crunchydata.com/blog/get-started-runnning-postgresql-on-kubernetes) that the author wrote for the Crunchy blog.

## Comments are closed.