背景:
因业务上线需要,研发中心要求在kubernetes测试集群部署一个postgre 12.6的数据库,用于业务功能调试。
一、实施部署postgre数据库:
1、拉取postgre 12.6的镜像:
[root@harbor-02 ~]# docker pull registry.cn-hangzhou.aliyuncs.com/images-speed-up/postgres:12.6
2017-latest: Pulling from mssql/server
699451820735: Pull complete
861e6e6e8e5e: Pull complete
4403d783b046: Pull complete
Digest: sha256:3b913841850a4d57fcfcb798be86acc88ea0f2acc5478bc0c140a43e91c4a545
Status: Downloaded newer image for registry.cn-hangzhou.aliyuncs.com/images-speed-up/postgres:12.6
2、上传postgre 12.6镜像到私库:
先tag处理一下。
[root@harbor-02 ~]# docker tag registry.cn-hangzhou.aliyuncs.com/images-speed-up/postgres:12.6 dockerhub.jiang.com/jiang-public/postgres:12.6
再上传到私有harbor库
[root@harbor-02 ~]# docker push dockerhub.jiang.com/jiang-public/postgres:12.6
The push refers to repository [dockerhub.jiang.com/jiang-public/postgres]
fdfec26b634f: Pushed
ef3ecb9d5e46: Pushed
548a79621a42: Pushed
2017-latest: digest: sha256:ff8a63a2c900cf3201b9485267ff804db58fbeaed83287c94edd3b3359708854 size: 954
3、创建PersistentVolume、PersistentVolumeClaim存储:
因kubernetes集群环境是对接了hpe(华三)的iscsi存储,所以直接通过stoageclass创建pvc存储即可。不过这里会展示pv、pvc存储的yml配置内容。
pvc存储yml配置:
apiVersion: storage.k8s.io/v1
kind: PersistentVolumeClaim
metadata:
name: postgre-db-pvc
namespace: main-data-test
selfLink: /api/v1/namespaces/main-data-test/persistentvolumeclaims/postgre-db-pvc
annotations:
boundinfo: >-
[{"podName":"postgre-db-nrzqq-664c5d78c7-ms4t5","containerName":"postgre","volumeMount":{"name":"postgre-db-pvc","mountPath":"/var/lib/postgresql/data"}}]
pv.kubernetes.io/bind-completed: 'yes'
pv.kubernetes.io/bound-by-controller: 'yes'
system/support-expansion: 'true'
volume.beta.kubernetes.io/storage-provisioner: csi.hpe.com
finalizers:
- kubernetes.io/pvc-protection
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
volumeName: pvc-5fdfd0ae-4c77-423b-87f0-991508b76f60
storageClassName: hpe-san
volumeMode: Filesystem
status:
phase: Used
accessModes:
- ReadWriteOnce
capacity:
storage: 10Gi
4、创建无状态的postgre数据库:
这里的无状态就是deployment控制器的配置。
kind: Deployment
metadata:
name: postgre-db-nrzqq
namespace: main-data-test
generation: 1
labels:
app: postgre-db-nrzqq
name: postgre-db
version: v1
annotations:
deployment.kubernetes.io/revision: '1'
sidecar.istio.io/inject: 'false'
spec:
replicas: 1
selector:
matchLabels:
app: postgre-db-nrzqq
name: postgre-db
template:
metadata:
labels:
app: postgre-db-nrzqq
name: postgre-db
version: v1
annotations:
cni.projectcalico.org/ipv4pools: '["172.25.0.0/16"]'
sidecar.istio.io/inject: 'false'
system/container-registry-map: '{"postgre":"default"}'
system/registry: default
v1.multus-cni.io/default-network: kube-system/calico@eth0
spec:
volumes:
- name: postgre-db-pvc
persistentVolumeClaim:
claimName: postgre-db-pvc
containers:
- name: postgre
image: 'dockerhub.jiang.com/jiang-public/postgres:12.6'
ports:
- containerPort: 5432
protocol: TCP
env:
- name: POSTGRES_USER
value: postgres
- name: POSTGRES_PASSWORD
value: postgres
- name: POSTGRES_DB
value: postgres
- name: TZ
value: Asia/Shanghai
- name: POSTGRES_MAX_CONNECTIONS
value: '20000'
resources:
limits:
cpu: '2'
memory: 4Gi
requests:
cpu: '1'
memory: 1Gi
volumeMounts:
- name: postgre-db-pvc
mountPath: /var/lib/postgresql/data
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
imagePullPolicy: IfNotPresent
securityContext:
privileged: false
restartPolicy: Always
terminationGracePeriodSeconds: 30
dnsPolicy: ClusterFirst
serviceAccountName: default
serviceAccount: default
securityContext: {}
strategy:
type: Recreate
minReadySeconds: 10
revisionHistoryLimit: 10
progressDeadlineSeconds: 600
1、volumes存储的声明:
2、volumeMounts:存储的挂载
3、env:环境变量的声明
- name: POSTGRES_USER(postgre的用户)
value: postgres
- name: POSTGRES_PASSWORD(postgre用户的密码)
value: postgres
- name: POSTGRES_DB(postgre数据库的库名称)
value: postgres
- name: TZ(时区)
value: Asia/Shanghai
- name: POSTGRES_MAX_CONNECTIONS(postgre的连接数)
value: '20000'4、ports:端口的声明
这里默认的是5432
5、创建servcie控制器配置,对外提供访问:
kind: Service
metadata:
name: postgre-db
namespace: main-data-test
selfLink: /api/v1/namespaces/main-data-test/services/postgre-db
labels:
name: postgre-db
system/appName: master-data-base
annotations:
binding_domains: ''
system/https: 'false'
system/lbgroup: group-dgdxw
system/ruleComment: ''
system/ruleName: postgre-db-out
system/schemaPortname: tcp-port-0/TCP/21951
spec:
ports:
- name: tcp-port-0
protocol: TCP
port: 5432
targetPort: 5432
selector:
name: postgre-db
clusterIP: 172.37.137.21
type: ClusterIP
sessionAffinity: None
二、使用Navicat Premium连接:
可以直接通过
Navicat Premium
来连接postgre数据库。
登陆之后可以检查一下postgre的数据库名是否存在,进行测试一下。
标签:name,kubernetes,db,postgre,pvc,12.6,postgres From: https://blog.csdn.net/jiang0615csdn/article/details/141026566注:到此,在kubernetes集群上部署postgre数据库就完成了。希望可以帮助到大家。