postgresql - Docker for desktop - kubernetes - postgres 本地客户端
问题描述
我是 Kubernetes 新手,我正在尝试开始在本地部署 Postgres 并能够与客户端连接。
这是设置
分泌物postgres-secrets.yaml
apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
data:
MAPPAPI_DATABASE_NAME: d2hhdGV2ZXIx
MAPPAPI_DATABASE_USERNAME: d2hhdGV2ZXIy
MAPPAPI_DATABASE_PASSWORD: d2hhdGV2ZXIz
贮存postgres-storage.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: db-data-pv
labels:
type: local
spec:
storageClassName: generic
capacity:
storage: 1Gi
accessModes:
- ReadWriteMany
hostPath:
path: "/var/lib/postgresql/data"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: db-data-pvc
spec:
storageClassName: generic
accessModes:
- ReadWriteMany
resources:
requests:
storage: 500Mi
部署postgres-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: postgres-db
name: postgres-db
spec:
replicas: 1
selector:
matchLabels:
app: postgres-db
template:
metadata:
labels:
app: postgres-db
spec:
containers:
- name: postgres-db
image: postgres:12.4
ports:
- containerPort: 5432
volumeMounts:
- mountPath: /var/lib/postgresql/data
name: postgres-db
env:
- name: POSTGRES_USER
valueFrom:
secretKeyRef:
name: mysecret
key: MAPPAPI_DATABASE_NAME
- name: POSTGRES_DB
valueFrom:
secretKeyRef:
name: mysecret
key: MAPPAPI_DATABASE_NAME
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: mysecret
key: MAPPAPI_DATABASE_PASSWORD
volumes:
- name: postgres-db
persistentVolumeClaim:
claimName: db-data-pvc
服务svc.yaml
apiVersion: v1
kind: Service
metadata:
labels:
app: postgres-db
name: postgresdb-service
spec:
type: NodePort
ports:
- port: 5432
selector:
app: postgres
我执行了$ kubectl get svc postgresdb-service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
postgresdb-service NodePort 10.101.101.67 <none> 5432:32043/TCP 15s
我正在尝试与 Postico 或命令行连接以localhost:32043
邮局
server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
命令行
psql: error: could not connect to server: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
如果我检查 pod 日志
PostgreSQL Database directory appears to contain a database; Skipping initialization
2020-10-11 19:30:41.296 UTC [1] LOG: starting PostgreSQL 12.4 (Debian 12.4-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
2020-10-11 19:30:41.297 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
2020-10-11 19:30:41.297 UTC [1] LOG: listening on IPv6 address "::", port 5432
2020-10-11 19:30:41.300 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2020-10-11 19:30:41.321 UTC [25] LOG: database system was shut down at 2020-10-11 01:05:04 UTC
2020-10-11 19:30:41.332 UTC [1] LOG: database system is ready to accept connections
解决方案
我发现您为 postgresdb-service 指定了错误的选择器。
所有 pod 都有标签app: postgres-db
,并且您正在app: postgres
使用服务进行选择。这就是为什么kuebctl get endpoints
显示:
$ kubectl get endpoints
NAME ENDPOINTS AGE
postgresdb-service <none> 7m39s
更改选择器后,一切都按预期工作。
推荐阅读
- python - 为什么在非最大抑制期间出现 zerdivision 错误?
- r - sys.which('make') 返回一个空向量,导致用.cpp构建的包安装失败
- flutter - 如何使行文本居中
- vbscript - 如何在 UFT 中使用 getoproperty
- django-rest-framework - rest_framework 中的 ImportError
- node.js - 在 React 中使用 Infura API 和 ipfs-http-client 检索存储在 IPFS 上的 JPG 图像
- node.js - 我们可以在elasticsearch中将已经存在的索引的ignore_malformed标志设置为真吗
- scheme - 每次 MIT-Scheme 初始化时加载某个文件
- php - 如何在 Doctrine 迁移中从数据数组中插入多行?
- laravel - Maatwebsite 在打开时导出(生成)Excel(Laravel Excel)文件性能问题