Deploy on Local Storage

DANGER

Translation

以下是基于单机本地存储编译 PolarDB 源码并启动一写多读实例的步骤。

我们推荐在 Docker 中编译源码并运行示例,从而尽可能减少配置环境的操作。我们提供了一个基于 CentOS 7 官方 Docker 镜像Deploy on Local Storage - 图1 centos:centos7 构建出的 PolarDB 开发镜像Deploy on Local Storage - 图2,里面包含了编译运行 PolarDB 需要的所有依赖。您可以直接使用这个镜像进行实例搭建,也可以自行修改我们在下面提供的 Dockerfile 以满足您的定制需求。

当然,不使用 Docker 也完全没有问题。我们提供了基于纯净 CentOS 7 操作系统的依赖安装脚本,助您快速完成环境准备。🎉

安装 Docker

TIP

如果不使用 Docker,可跳过本小节。

请参阅 Docker 官方文档Deploy on Local Storage - 图3 完成不同平台上 Docker 的安装。

编译环境准备

以下两种方式任选一种即可:

基于 PolarDB Docker 开发镜像

该方式使您可以在 Docker 容器中编译并部署示例。

Docker 镜像准备

我们在 DockerHub 上提供了构建完毕的镜像 polardb/polardb_pg_devel:centos7Deploy on Local Storage - 图10 可供直接使用(支持 AMD64 和 ARM64 架构)😁。

另外,我们也提供了构建上述开发镜像的 Dockerfile,从 CentOS 7 官方镜像 centos:centos7 开始构建出一个安装完所有开发和运行时依赖的镜像。您可以根据自己的需要在 Dockerfile 中添加更多依赖。以下是手动构建镜像的 Dockerfile 及方法,如果您决定直接使用 DockerHub 上构建完毕的镜像,则跳过该步骤。

1. FROM centos:centos7

3. CMD bash

5. # avoid missing locale problem
6. RUN sed -i 's/override_install_langs/# &/' /etc/yum.conf

8. # add EPEL and scl source
9. RUN rpmkeys --import file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 && \
10. yum install -y epel-release centos-release-scl && \
11. rpmkeys --import file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7 && \
12. rpmkeys --import file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo && \
13. yum update -y

15. # GCC and LLVM
16. RUN yum install -y devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-gdb \
17. devtoolset-9-libstdc++-devel devtoolset-9-make && \
18. yum install -y llvm-toolset-7.0-llvm-devel llvm-toolset-7.0-clang-devel \
19. llvm-toolset-7.0-cmake

21. # dependencies
22. RUN yum install -y libicu-devel pam-devel readline-devel libxml2-devel \
23. libxslt-devel openldap-devel openldap-clients \
24. openldap-servers libuuid-devel xerces-c-devel \
25. bison flex gettext tcl-devel python-devel perl-IPC-Run \
26. perl-Expect perl-Test-Simple perl-DBD-Pg perl-ExtUtils-Embed \
27. perl-ExtUtils-MakeMaker zlib-devel krb5-devel \
28. krb5-workstation krb5-server protobuf-devel

30. RUN ln /usr/lib64/perl5/CORE/libperl.so /usr/lib64/libperl.so

32. # tools
33. RUN yum install -y git lcov psmisc sudo vim

35. # set to empty if GitHub is not barriered
36. ENV GITHUB_PROXY=https://ghproxy.com/

38. ENV OPENSSL_VERSION=OpenSSL_1_1_1k

40. # install dependencies from GitHub mirror
41. RUN yum install -y libaio-devel wget && \
42. cd /usr/local && \
43. # zlog for PFSD
44. wget --no-verbose --no-check-certificate \
45. "${GITHUB_PROXY}https://github.com/HardySimpson/zlog/archive/refs/tags/1.2.14.tar.gz" && \
46. # PFSD
47. wget --no-verbose --no-check-certificate \
48. "${GITHUB_PROXY}https://github.com/ApsaraDB/PolarDB-FileSystem/archive/refs/tags/pfsd4pg-release-1.2.41-20211018.tar.gz" && \
49. # OpenSSL 1.1.1
50. wget --no-verbose --no-check-certificate \
51. "${GITHUB_PROXY}https://github.com/openssl/openssl/archive/refs/tags/${OPENSSL_VERSION}.tar.gz" && \
52. # enable build tools
53. echo "source /opt/rh/devtoolset-9/enable" >> /etc/bashrc && \
54. echo "source /opt/rh/llvm-toolset-7.0/enable" >> /etc/bashrc && \
55. source /etc/bashrc && \
56. # unzip and install zlog
57. tar -zxf 1.2.14.tar.gz && \
58. cd zlog-1.2.14 && \
59. make && make install && \
60. cd .. && \
61. rm 1.2.14.tar.gz && \
62. rm -rf zlog-1.2.14 && \
63. # unzip and install PFSD
64. tar -zxf pfsd4pg-release-1.2.41-20211018.tar.gz && \
65. cd PolarDB-FileSystem-pfsd4pg-release-1.2.41-20211018 && \
66. ./autobuild.sh && ./install.sh && \
67. cd .. && \
68. rm pfsd4pg-release-1.2.41-20211018.tar.gz && \
69. rm -rf PolarDB-FileSystem-pfsd4pg-release-1.2.41-20211018 && \
70. # unzip and install OpenSSL 1.1.1
71. tar -zxf $OPENSSL_VERSION.tar.gz && \
72. cd /usr/local/openssl-$OPENSSL_VERSION && \
73. ./config --prefix=/usr/local/openssl && make -j64 && make install && \
74. cp /usr/local/openssl/lib/libcrypto.so.1.1 /usr/lib64/ && \
75. cp /usr/local/openssl/lib/libssl.so.1.1 /usr/lib64/ && \
76. cp -r /usr/local/openssl/include/openssl /usr/include/ && \
77. ln -sf /usr/lib64/libcrypto.so.1.1 /usr/lib64/libcrypto.so && \
78. ln -sf /usr/lib64/libssl.so.1.1 /usr/lib64/libssl.so && \
79. rm -f /usr/local/$OPENSSL_VERSION.tar.gz && \
80. rm -rf /usr/local/openssl-$OPENSSL_VERSION

82. # create default user
83. ENV USER_NAME=postgres
84. RUN echo "create default user" && \
85. groupadd -r $USER_NAME && useradd -g $USER_NAME $USER_NAME -p '' && \
86. usermod -aG wheel $USER_NAME

88. WORKDIR /home/$USER_NAME

90. # modify conf
91. RUN echo "modify conf" && \
92. mkdir -p /run/pfs && chown $USER_NAME /run/pfs && \
93. mkdir -p /var/log/pfs && chown $USER_NAME /var/log/pfs && \
94. echo "ulimit -c unlimited" >> /home/postgres/.bashrc && \
95. echo "export PATH=/home/postgres/tmp_basedir_polardb_pg_1100_bld/bin:\$PATH" >> /home/postgres/.bashrc && \
96. echo "alias pg='psql -h /home/postgres/tmp_master_dir_polardb_pg_1100_bld/'" >> /home/postgres/.bashrc && \
97. rm /etc/localtime && \
98. cp /usr/share/zoneinfo/UTC /etc/localtime && \
99. sed -i 's/4096/unlimited/g' /etc/security/limits.d/20-nproc.conf && \
100. sed -i 's/vim/vi/g' /root/.bashrc

102. USER $USER_NAME

将上述内容复制到一个文件内(假设文件名为 Dockerfile-PolarDB)后,使用如下命令构建镜像:

TIP

💡 请在下面的高亮行中按需替换 <image_name> 内的 Docker 镜像名称

1. docker build --network=host \
2. -t <image_name> \
3. -f Dockerfile-PolarDB .

代码下载

PolarDB for PostgreSQL 的代码托管于 GitHubDeploy on Local Storage - 图11 上,稳定分支为 POLARDB_11_STABLE。如果因网络原因不能稳定访问 GitHub,则可以访问 Gitee 国内镜像Deploy on Local Storage - 图12。

  • GitHub
  • Gitee 国内镜像
1. git clone -b POLARDB_11_STABLE https://github.com/ApsaraDB/PolarDB-for-PostgreSQL.git
1. git clone -b POLARDB_11_STABLE https://gitee.com/mirrors/PolarDB-for-PostgreSQL

创建并启动 Docker 容器

TIP

💡 请在下面的高亮行中按需替换 <> 的部分:

  1. PolarDB for PostgreSQL 的源码路径
  2. 将要启动的 Docker 容器名称
  3. 自行构建或 DockerHub 上的 PolarDB 开发镜像名称

  4. 本地镜像

  5. DockerHub 镜像
1. # 创建容器
2. docker create -it \
3. -v <src_to_polardb>:/home/postgres/PolarDB-for-PostgreSQL \
4. --cap-add=SYS_PTRACE --privileged=true \
5. --name <container_name> \
6. <image_name> bash
1. # 创建容器
2. docker create -it \
3. -v <src_to_polardb>:/home/postgres/PolarDB-for-PostgreSQL \
4. --cap-add=SYS_PTRACE --privileged=true \
5. --name <container_name> \
6. polardb/polardb_pg_devel:centos7 bash
1. # 启动容器
2. docker start <container_name>

镜像构建过程中已经创建了一个 postgres:postgres 用户,从该镜像运行的容器将直接使用这个用户。容器启动后,通过以下命令进入正在运行的容器中:

1. docker exec -it \
2. --env COLUMNS=`tput cols` \
3. --env LINES=`tput lines` \
4. <container_name> bash

通过 bash 进入容器后,进入源码目录,为用户 postgres 获取源代码目录权限,然后编译实例:

1. cd /home/postgres/PolarDB-for-PostgreSQL
2. sudo chown -R postgres:postgres ./
3. ./polardb_build.sh

部署完成后,进行简单的实例检查,确保部署正确:

1. $HOME/tmp_basedir_polardb_pg_1100_bld/bin/psql \
2. -p 5432 -h 127.0.0.1 -c 'select version();'
3. version
4. --------------------------------
5. PostgreSQL 11.9 (POLARDB 11.9)
6. (1 row)

基于 CentOS 7 系统或容器

该方式假设您从一台具有 root 权限的干净的 CentOS 7 操作系统上从零开始,可以是:

  • 安装 CentOS 7 的物理机/虚拟机
  • 从 CentOS 7 官方 Docker 镜像 centos:centos7 上启动的 Docker 容器

建立非 root 用户

PolarDB for PostgreSQL 需要以非 root 用户运行。以下步骤能够帮助您创建一个名为 postgres 的用户组和一个名为 postgres 的用户。

TIP

如果您已经有了一个非 root 用户,但名称不是 postgres:postgres,可以忽略该步骤;但请注意在后续示例步骤中将命令中用户相关的信息替换为您自己的用户组名与用户名。

下面的命令能够创建用户组 postgres 和用户 postgres,并为该用户赋予 sudo 和工作目录的权限。需要以 root 用户执行这些命令。

1. # install sudo
2. yum install -y sudo

4. # create user and group
5. groupadd -r postgres
6. useradd -m -g postgres postgres -p ''
7. usermod -aG wheel postgres

9. # make postgres as sudoer
10. chmod u+w /etc/sudoers
11. echo 'postgres ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers
12. chmod u-w /etc/sudoers

14. # grant access to home directory
15. chown -R postgres:postgres /home/postgres/
16. echo 'source /etc/bashrc' >> /home/postgres/.bashrc
17. # for su postgres
18. sed -i 's/4096/unlimited/g' /etc/security/limits.d/20-nproc.conf

接下来,切换到 postgres 用户,就可以进行后续的步骤了:

1. su postgres
2. source /etc/bashrc
3. cd ~

下载 PolarDB 源代码

PolarDB for PostgreSQL 的代码托管于 GitHubDeploy on Local Storage - 图13 上,稳定分支为 POLARDB_11_STABLE。如果因网络原因不能稳定访问 GitHub,则可以访问 Gitee 国内镜像Deploy on Local Storage - 图14。

  • GitHub
  • Gitee 国内镜像
1. sudo yum install -y git
2. git clone -b POLARDB_11_STABLE https://github.com/ApsaraDB/PolarDB-for-PostgreSQL.git
1. sudo yum install -y git
2. git clone -b POLARDB_11_STABLE https://gitee.com/mirrors/PolarDB-for-PostgreSQL

依赖安装

使用 sudo 执行源代码根目录下的依赖安装脚本 install_dependencies.sh 完成所有的依赖安装。

1. cd PolarDB-for-PostgreSQL
2. sudo ./install_dependencies.sh

编译部署

依赖安装完毕后,刷新用户配置,开始编译部署:

1. source /etc/bashrc
2. ./polardb_build.sh

部署完成后,进行简单的实例检查:

1. $HOME/tmp_basedir_polardb_pg_1100_bld/bin/psql \
2. -p 5432 -h 127.0.0.1 -c 'select version();'
3. version
4. --------------------------------
5. PostgreSQL 11.9 (POLARDB 11.9)
6. (1 row)

编译实例类型

本地单节点实例

  • 1 个主节点(运行于 5432 端口)
1. ./polardb_build.sh

本地多节点实例

  • 1 个主节点(运行于 5432 端口)
  • 1 个只读节点(运行于 5433 端口)
1. ./polardb_build.sh --withrep --repnum=1

本地多节点带备库实例

  • 1 个主节点(运行于 5432 端口)
  • 1 个只读节点(运行于 5433 端口)
  • 1 个备库节点(运行于 5434 端口)
1. ./polardb_build.sh --withrep --repnum=1 --withstandby

本地多节点 HTAP 实例

  • 1 个主节点(运行于 5432 端口)
  • 2 个只读节点(运行于 5433 / 5434 端口)
1. ./polardb_build.sh --initpx

实例回归测试

普通实例回归测试:

1. ./polardb_build.sh -r -e -r-external -r-contrib -r-pl --withrep --with-tde

HTAP 实例回归测试:

1. ./polardb_build.sh -r-px -e -r-external -r-contrib -r-pl --with-tde

分布式实例回归测试:

1. ./polardb_build.sh -r -e -r-external -r-contrib -r-pl --with-tde --with-dma

Edit this page on GitHub Deploy on Local Storage - 图15

Contributors: 棠羽, mrdrivingduck, 北侠