无法连接到Kubernetes中的蟑螂吊舱



我正在开发一个带有web服务和持久层的简单web应用程序。网络持久层是蟑螂数据库。我正试图用一个命令部署我的应用程序:

kubectl apply -f my-app.yaml

应用程序已成功部署。然而,当后端必须在数据库中存储某些内容时,会出现以下错误:

dial tcp: lookup web-service-cockroach on 192.168.65.1:53: no such host

当我启动我的应用程序时,我向蟑螂数据库提供了以下连接字符串,连接成功,但当我试图在数据库中存储一些东西时,会出现上述错误:

postgresql://root@web-service-db:26257/defaultdb?sslmode=disable

由于某些原因,web pod无法与db pod进行通信。我的整个配置是:


# Service for web application
apiVersion: v1
kind: Service
metadata:
name: web-service
spec:
selector:
app: web-service
type: NodePort
ports:
- protocol: TCP
port: 8080
targetPort: http
nodePort: 30103
externalIPs:
- 192.168.1.9    # < - my local ip
---
# Deployment of web app
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-service
spec:
selector:
matchLabels:
app: web-service
replicas: 1
template:
metadata:
labels:
app: web-service
spec:
hostNetwork: true
containers:
- name: web-service
image: my-local-img:latest
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 8080
hostPort: 8080
env:
- name: DB_CONNECT_STRING
value: "postgresql://root@web-service-db:26257/defaultdb?sslmode=disable"
---
### Kubernetes official doc PersistentVolume
apiVersion: v1
kind: PersistentVolume
metadata:
name: cockroach-pv-volume
labels:
type: local
spec:
storageClassName: manual
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/tmp/my-local-volueme"
---
### Kubernetes official doc PersistentVolumeClaim
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: cockroach-pv-claim
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 4Gi
---
# Cockroach used by web-service
apiVersion: v1
kind: Service
metadata:
name: web-service-cockroach
labels:
app: web-service-cockroach
spec:
selector:
app: web-service-cockroach
type: NodePort
ports:
- protocol: TCP
port: 26257
targetPort: 26257
nodePort: 30104
---
# Cockroach stateful set used to deploy locally
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web-service-cockroach
spec:
serviceName: web-service-cockroach
replicas: 1
selector:
matchLabels:
app: web-service-cockroach
template:
metadata:
labels:
app: web-service-cockroach
spec:
volumes:
- name: cockroach-pv-storage
persistentVolumeClaim:
claimName: cockroach-pv-claim
containers:
- name: web-service-cockroach
image: cockroachdb/cockroach:latest
command:
- /cockroach/cockroach.sh
- start
- --insecure
volumeMounts:
- mountPath: "/tmp/my-local-volume"
name: cockroach-pv-storage
ports:
- containerPort: 26257

部署后,一切看起来都很好。

kubectl get service
NAME                      TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)           AGE
kubernetes               ClusterIP   10.96.0.1      <none>        443/TCP           50m
web-service              NodePort    10.111.85.64   192.168.1.9   8080:30103/TCP    6m17s
webs-service-cockroach   NodePort    10.96.42.121   <none>        26257:30104/TCP   6m8s
kubectl get pods
NAME                                  READY   STATUS    RESTARTS   AGE
web-service-6cc74b5f54-jlvd6   1/1     Running   0          24m
web-service-cockroach-0        1/1     Running   0          24m

提前感谢!

看起来DNS有问题。

dial tcp: lookup web-service-cockroach on 192.168.65.1:53: no such host

地址192.168.65.1不喜欢kube-dns服务ip。

如果您在哪里使用主机网络,这可能会得到解释,令人惊讶的是,您确实这样做了。当使用hostNetwork: true时,使用的默认dns服务器是主机使用的服务器,而不是kube-dns。


要解决它,请设置:

spec:
dnsPolicy: ClusterFirstWithHostNet

它将pod的dns服务器设置为k8s服务器。

有关Pod的DNS策略的更多信息,请查看kubernetes文档。

相关内容

  • 没有找到相关文章

最新更新