【发布时间】:2020-03-31 01:15:14
【问题描述】:
我是 Azure Kubernetes 服务 (AKS) 的新手,我为 postgres 创建了如下部署文件
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: postgres-pv-claim
labels:
app: postgres
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 5Gi
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: postgres
spec:
replicas: 1
template:
metadata:
labels:
app: postgres
spec:
containers:
- name: postgres
image: postgres:xx.xx
imagePullPolicy: "IfNotPresent"
ports:
- containerPort: 5432
env:
- name: POSTGRES_DB
value: postgresdb
- name: POSTGRES_USER
value: postgresadmin
- name: POSTGRES_PASSWORD
value: admin123
volumeMounts:
- mountPath: /var/lib/postgresql/data
name: postgredb
volumes:
- name: postgredb
persistentVolumeClaim:
claimName: postgres-pv-claim
apiVersion: v1
kind: Service
metadata:
name: postgres
labels:
app: postgres
spec:
type: NodePort
ports:
- port: 5432
selector:
app: postgres
在 AKS 上部署 postgres 服务后,服务就创建好了。
abc@Azure:~$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 6h45m
postgres NodePort 10.0.165.161 <none> 5432:30692/TCP 9m8s
但是当我尝试使用psql -h localhost -U postgresadmin --password -p 30692 postgresdb 进入PSQL 时,它显示连接被拒绝。
abc@Azure:~$ psql -h localhost -U postgresadmin --password -p 30692 postgresdb
Password:
psql: error: could not connect to server: could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 30692?
could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 30692?
【问题讨论】:
-
你需要把服务改成
LoadBalancer的类型。 -
在这种情况下,数据库在云中运行,而不是在您的本地系统上;
localhost将无法访问它。 -
我试过
LoadBalancer,也用过EXTERNAL-IP,但问题是一样的 -
您可能需要在 AKS 防火墙规则中将您的 IP 列入白名单
-
你用什么命令连接PostgreSQL?你把localhost改成服务的外网IP了吗?并在服务端口中添加targetPort。
标签: postgresql docker kubernetes azure-devops azure-aks