我是Azure Kubernetes Service(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?
对于您的问题,您似乎想将PostgreSQL公开到Internet。在我看来,你犯了两个错误。
首先是YMAL文件,您需要将服务更改为以下内容:
apiVersion: v1
kind: Service
metadata:
name: postgres
labels:
app: postgres
spec:
type: LoadBalancer
ports:
- port: 5432
targetPort: 5432
selector:
app: postgres
第二个是connect命令。我认为你在Azure云外壳中连接PostgreSQL,所以你需要使用服务的外部IP来连接:
psql -h external-IP -U postgresadmin --password -p 5432 postgresdb
对我来说,只有当我将服务类型从LoadBalancer更改为NodePort 时,它才有效