官网部署文档:https://docs.opengauss.org/zh/docs/7.0.0-RC1/docs/InstallationGuide/容器镜像安装.html
获取镜像
docker pull registry.cn-hangzhou.aliyuncs.com/qiluo-images/opengauss:latest
启动并配置容器 默认密码是:Enmo@123
docker run --name OpenGauss --privileged=true --restart=always -u root -p 15432:5432 -e GS_PASSWORD=QYuY482wasErOP1Q -v /etc/localtime:/etc/localtime -v /data/OpenGauss:/var/lib/opengauss registry.cn-hangzhou.aliyuncs.com/qiluo-images/opengauss:latest
账号密码如图所示
复制配置进行修改
docker cp OpenGauss :/var/lib/opengauss/data/postgresql.conf /data/postgresql.conf
复制到容器
docker cp /data/postgresql.conf OpenGauss:/var/lib/opengauss/data/postgresql.conf
docker-compose.yml 方式
version: '3.8'
services:
opengauss:
image: registry.cn-hangzhou.aliyuncs.com/qiluo-images/opengauss:latest
container_name: OpenGauss
environment:
- GS_PASSWORD=QYuY482wasErOP1Q
ports:
- "15432:5432"
volumes:
- /etc/localtime:/etc/localtime
- /data/OpenGauss:/var/lib/opengauss
restart: always
privileged: true
user: root
运行
docker-compose up -d
步骤 1:创建持久卷(Persistent Volume,PV)和持久卷声明(Persistent Volume Claim,PVC)
首先,我们需要创建一个持久卷(PV),用于存储 OpenGauss 的数据,并通过 PVC 将这个存储挂载到容器中。
持久卷(PV)和持久卷声明(PVC)配置:
apiVersion: v1
kind: PersistentVolume
metadata:
name: opengauss-pv
labels:
app: opengauss # 添加标签便于选择
spec:
capacity:
storage: 10Gi
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
hostPath:
path: /data/opengauss
type: DirectoryOrCreate # 确保目录不存在时会自动创建
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: opengauss-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
selector: # 添加选择器确保绑定到正确的PV
matchLabels:
app: opengauss
步骤 2:创建 Deployment
Deployment 定义了 OpenGauss 的容器,并且挂载了持久卷声明(PVC)来确保数据持久化。
apiVersion: apps/v1
kind: Deployment
metadata:
name: opengauss-deployment
spec:
replicas: 1 # 部署单个副本
selector:
matchLabels:
app: opengauss
template:
metadata:
labels:
app: opengauss
spec:
containers:
- name: opengauss
image: registry.cn-hangzhou.aliyuncs.com/qiluo-images/opengauss:latest # 镜像
ports:
- containerPort: 5432 # 映射的端口
env:
- name: GS_PASSWORD
value: "Enmo@123" # 环境变量
volumeMounts:
- mountPath: /var/lib/opengauss # 持久化路径
name: opengauss-storage # 持久卷名称
volumes:
- name: opengauss-storage
persistentVolumeClaim:
claimName: opengauss-pvc # 使用 PVC 挂载
restartPolicy: Always # 总是重启
步骤 3:创建 Service
Service 将 OpenGauss 容器暴露给集群内的其他服务,或者外部访问。
apiVersion: v1
kind: Service
metadata:
name: opengauss-service
spec:
selector:
app: opengauss # 选择与该标签匹配的 Pods
ports:
- protocol: TCP
port: 5432 # 服务端口
targetPort: 5432 # 容器端口
nodePort: 30432 # 外部访问的端口
type: NodePort # 使用 NodePort 类型暴露服务
步骤 4:应用 Kubernetes 资源
将上述配置保存到一个名为 opengauss-k8s.yml 的文件中,然后执行以下命令来应用这些资源:
kubectl apply -f opengauss-k8s.yml
步骤 5:验证部署
可以使用以下命令检查是否成功部署:
kubectl get pods # 查看 Pod 状态
kubectl get svc # 查看服务状态
kubectl get pvc # 查看 PVC 状态
Persistent Volume(PV):hostPath 存储数据在主机的 /data/OpenGauss 路径下。生产环境中,通常会使用云存储(如 AWS EBS、Azure Disk 等)。
Persistent Volume Claim(PVC):从 PV 请求存储空间。
Deployment:定义了 OpenGauss 容器,设置环境变量,挂载 PVC,并确保容器重启策略。
Service:提供容器的网络访问,可以选择暴露给集群内部或外部。
nodePort: 30432:指定了 NodePort,Kubernetes 会在每个节点的 30432 端口上暴露 OpenGauss 服务。
type: NodePort:将服务类型设置为 NodePort,这意味着 Kubernetes 会在每个集群节点的某个端口上暴露这个服务,允许集群外部访问。
使用 NodePort 访问:
现在,你可以通过任何集群节点的 IP:30432 来访问 OpenGauss 服务。
如果你使用的是云服务提供商(如 AWS、Azure 等),你还需要确保安全组或防火墙规则允许通过该端口访问。
k8s使用postgresql.conf
# -----------------------------------------------------------------------------
#
# postgresql_single.conf.sample
# Configuration file for centralized environment
#
# Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
#
#
# IDENTIFICATION
# src/common/backend/utils/misc/postgresql_single.conf.sample
#
#
# This file consists of lines of the form:
#
# name = value
#
# (The "=" is optional.) Whitespace may be used. Comments are introduced with
# "#" anywhere on a line. The complete list of parameter names and allowed
# values can be found in the openGauss documentation.
#
# The commented-out settings shown in this file represent the default values.
# Re-commenting a setting is NOT sufficient to revert it to the default value;
# you need to reload the server.
#
# This file is read on server startup and when the server receives a SIGHUP
# signal. If you edit the file on a running system, you have to SIGHUP the
# server for the changes to take effect, or use "pg_ctl reload". Some
# parameters, which are marked below, require a server shutdown and restart to
# take effect.
#
# Any parameter can also be given as a command-line option to the server, e.g.,
# "postgres -c log_connections=on". Some parameters can be changed at run time
# with the "SET" SQL command.
#
# Memory units: kB = kilobytes Time units: ms = milliseconds
# MB = megabytes s = seconds
# GB = gigabytes min = minutes
# h = hours
# d = days
# -----------------------------------------------------------------------------
#------------------------------------------------------------------------------
# FILE LOCATIONS
#------------------------------------------------------------------------------
# The default values of these variables are driven from the -D command-line
# option or PGDATA environment variable, represented here as ConfigDir.
#data_directory = 'ConfigDir' # use data in another directory
# (change requires restart)
#hba_file = 'ConfigDir/pg_hba.conf' # host-based authentication file
# (change requires restart)
#ident_file = 'ConfigDir/pg_ident.conf' # ident configuration file
# (change requires restart)
# If external_pid_file is not explicitly set, no extra PID file is written.
#external_pid_file = '' # write an extra PID file
# (change requires restart)
#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------
# - Connection Settings -
#listen_addresses = 'localhost' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
#local_bind_address = '0.0.0.0'
#port = 5432 # (change requires restart)
max_connections = 200000 # (change requires restart)
# Note: Increasing max_connections costs ~400 bytes of shared memory per
# connection slot, plus lock space (see max_locks_per_transaction).
#sysadmin_reserved_connections = 3 # (change requires restart)
#unix_socket_directory = '' # (change requires restart)
#unix_socket_group = '' # (change requires restart)
#unix_socket_permissions = 0700 # begin with 0 to use octal notation
# (change requires restart)
#light_comm = off # whether use light commnucation with nonblock mode or latch
# - Security and Authentication -
#authentication_timeout = 1min # 1s-600s
session_timeout = 10min # allowed duration of any unused session, 0s-86400s(1 day), 0 is disabled
#idle_in_transaction_session_timeout = 0 # Sets the maximum allowed idle time between queries, when in a transaction, 0 is disabled
#ssl = off # (change requires restart)
#ssl_ciphers = 'ALL' # allowed SSL ciphers
# (change requires restart)
#ssl_cert_notify_time = 90 # 7-180 days
#ssl_renegotiation_limit = 0 # amount of data between renegotiations, no longer supported
#ssl_cert_file = 'server.crt' # (change requires restart)
#ssl_key_file = 'server.key' # (change requires restart)
#ssl_ca_file = '' # (change requires restart)
#ssl_crl_file = '' # (change requires restart)
# Kerberos and GSSAPI
#krb_server_keyfile = ''
#krb_srvname = 'postgres' # (Kerberos only)
#krb_caseins_users = off
#modify_initial_password = false #Whether to change the initial password of the initial user
#password_policy = 1 #Whether password complexity checks
#password_reuse_time = 60 #Whether the new password can be reused in password_reuse_time days
#password_reuse_max = 0 #Whether the new password can be reused
#password_lock_time = 1 #The account will be unlocked automatically after a specif