系统环境

  • Kubernetes版本:1.20.0

  • Postgresql版本:14.0

部署Postgresql

[root@k8s01 bitbucket]# cat postgresql.yaml 
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: postgresql-data
  namespace: tools-env
spec:
  storageClassName: managed-nfs-storage
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi
---
kind: Service
apiVersion: v1
metadata:
  name: postgresql
  namespace: tools-env
  labels:
    name: postgresql
spec:
  ports:
    - name: postgres
      protocol: TCP
      port: 5432
      targetPort: postgres
  selector:
    name: postgresql
  type: ClusterIP
---
kind: Deployment
apiVersion: apps/v1
metadata:
  name: postgresql
  namespace: tools-env
  labels:
    name: postgresql
spec:
  replicas: 1
  selector:
    matchLabels:
      name: postgresql
  template:
    metadata:
      name: postgresql
      labels:
        name: postgresql
    spec:
      containers:
      - name: postgresql
        image: postgres:14.0
        ports:
        - name: postgres
          containerPort: 5432
        env:
        - name: POSTGRES_USER
          value: admin
        - name: POSTGRES_PASSWORD
          value: test-pwd
        - name: POSTGRES_DB
          value: postgresdb
        - name: DB_EXTENSION
          value: 'pg_trgm,btree_gist'
        resources: 
          requests:
            cpu: 2
            memory: 2Gi
          limits:
            cpu: 2
            memory: 2Gi
        livenessProbe:
          exec:
            command: ["pg_isready","-h","localhost","-U","postgres"]
          initialDelaySeconds: 30
          timeoutSeconds: 5
          periodSeconds: 10
          successThreshold: 1
          failureThreshold: 3
        readinessProbe:
          exec:
            command: ["pg_isready","-h","localhost","-U","postgres"]
          initialDelaySeconds: 5
          timeoutSeconds: 1
          periodSeconds: 10
          successThreshold: 1
          failureThreshold: 3
        volumeMounts:
        - name: data
          mountPath: /var/lib/postgresql
      volumes:
      - name: data
        persistentVolumeClaim:
          claimName: postgresql-data
[root@k8s01 gitlab]# kubectl apply -f postgresql.yaml 
persistentvolumeclaim/postgresql-data created
service/postgresql created
deployment.apps/postgresql created

变量说明:

参数名称

默认值

描述

DB_USER

-

创建一个数据库用户

DB_PASS

-

指定创建的用户的密码

DB_NAME

-

创建一个数据库并指定库名

DB_EXTENSION

-

指定安装的扩展包

数据库连接

root@postgresql-74755f975c-lxrmq:/# psql -h localhost -U admin --password -p 5432 postgresdb         
Password: 
psql (14.0 (Debian 14.0-1.pgdg110+1))
Type "help" for help.

#创建用户


postgresdb=# create user testuser with password '123456';
CREATE ROLE
#创建测试数据库
postgresdb=# CREATE DATABASE test;
CREATE DATABASE
#授权
postgresdb=# GRANT ALL PRIVILEGES ON DATABASE test TO testuser;
GRANT
postgresdb=# 
文章作者: 鲜花的主人
版权声明: 本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 爱吃可爱多
PostgraSql 数据库
喜欢就支持一下吧
打赏
微信 微信
支付宝 支付宝