我在玩kubenetes。我创建了一个运行Postgresql的StatefulSet。我已经使用ClusterIP: None
创建了服务。我已经用pgadmin4启动了一个pod。我可以从浏览器访问pgadmin。当我尝试从pgadmin进入pgsql服务器时,它告诉我ip或端口均不可访问。该错误消息显示IP地址,因此我知道它正在解析正确的Pod名称。
这是Ubuntu上的MicroK8。
这是我的配置。
-pomodoro-pgsql StatefulSet-
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: pomodoro-pgsql
namespace: pomodoro-services
spec:
selector:
matchLabels:
app: pomodoro-pgsql
env: development
serviceName: pomodoro-pgsql
replicas: 1
template:
metadata:
labels:
app: pomodoro-pgsql
env: development
spec:
containers:
- name: pomodoro-pgsql
image: localhost:32000/pomodoro-pgsql
env:
- name: POSTGRES_PASSWORD
value: blahblah
- name: POSTGRES_USER
value: blahblah
- name: POSTGRES_DB
value: blahblah
ports:
- name: pgsql
containerPort: 5432
volumeMounts:
- name: data
mountPath: /var/lib/postgresql/data
volumeClaimTemplates:
- metadata:
name: data
spec:
resources:
requests:
storage: 1Gi
accessModes:
- ReadWriteOnce
--- pomodoro-pgsql无头服务---
apiVersion: v1
kind: Service
metadata:
name: pomodoro-pgsql
namespace: pomodoro-services
spec:
clusterIP: None
selector:
app: pomodoro-pgsql
env: development
ports:
- name: pgsql
port: 5432
--- pgadmin4荚-
apiVersion: v1
kind: Pod
metadata:
name: pomodoro-pgadmin
namespace: pomodoro-services
labels:
env: development
spec:
containers:
- name: pomodoro-pgadmin
image: localhost:32000/pomodoro-pgadmin
env:
- name: PGADMIN_DEFAULT_EMAIL
value: blahblah
- name: PGADMIN_DEFAULT_PASSWORD
value: blahblah
imagePullPolicy: IfNotPresent
restartPolicy: Always
--- pgadmin4服务-
apiVersion: v1
kind: Service
metadata:
name: pomodoro-pgadmin
namespace: pomodoro-services
spec:
type: NodePort
ports:
- port: 5002
targetPort: 80
selector:
app: pomodoro-pgadmin
env: development
我可以通过dig查看IP地址
microk8s.kubectl run `
--namespace pomodoro-services `
-it srvlookup `
--image=tutum/dnsutils --rm `
--restart=Never `
-- dig SRV pomodoro-pgsql.pomodoro-services.svc.cluster.local
这是pgadmin的错误。请注意,该IP对Pod是正确的。
Unable to connect to server:
could not connect to server: Operation timed out
Is the server running on host "pomodoro-pgsql-0.pomodoro-pgsql.pomodoro-
services.svc.cluster.local" (10.10.10.219) and accepting
TCP/IP connections on port 5432?
这是pgsql pod的日志
2019-01-02 04:23:05.576 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
2019-01-02 04:23:05.576 UTC [1] LOG: listening on IPv6 address "::", port 5432
2019-01-02 04:23:05.905 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2019-01-02 04:23:06.430 UTC [21] LOG: database system was shut down at 2019-01-01 20:01:36 UTC
2019-01-02 04:23:06.630 UTC [1] LOG: database system is ready to accept connections
根据要求,这是
kubectl get services
的结果(IP已更改。)NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
pomodoro-pgadmin NodePort 10.1.18.45 <none> 5002:30437/TCP 12h
pomodoro-pgsql ClusterIP None <none> 5432/TCP 46h
pomodoro-ping-rapi ClusterIP 10.1.18.36 <none> 8888/TCP 47h
[更新1/2/2019]我连接到集群中的另一个容器,并尝试
telnet
,然后将psql
转换为postgres。我无法连接任何一个程序。我可以在运行postgresql服务器的容器上运行psql。我当前的理论是服务器已在本地公开5432,但已从其他Pod中过滤掉。我已经确认
/var/lib/postgresql/data/postgresql.conf
包含以下内容:listen_addresses = '*'
使用
microk8s.kubctl port-forward pomodoro-pgsql-0 5432:5432
,我能够通过telnet连接到5432。_> telnet localhost 5432
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
[更新1/2/2019]
结果
kubctl exec pomodoro-pgsql-0 -- nslookup pomodoro-pgsql
nslookup: can't resolve '(null)': Name does not resolve
nslookup: can't resolve 'pomodoro-pgsql': Try again
command terminated with exit code 1
结果
kubctl exec pomodoro-pgsql-0 -- nslookup pomodoro-pgsql-0
Name: pomodoro-pgsql-0
Address 1: 10.1.1.19 pomodoro-pgsql-0.pomodoro-pgsql.pomodoro-services.svc.cluster.local
nslookup: can't resolve '(null)': Name does not resolve
注意:重新启动计算机后,IP会更改。
最佳答案
问题是运行microk8s的计算机的防火墙规则。我发现这记录在他们的web page上,文档告诉我们这样做:
sudo iptables -P FORWARD ACCEPT
sudo apt-get install iptables-persistent
关于kubernetes - 如何从Kubernetes上的pgadmin访问pgsql,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54001531/