[英]Kubernetes Service connection refused
I want to create a sample application with Kubernetes but i get connection refused if i try to connect to the responsive service in Kubernetes.我想用 Kubernetes 创建一个示例应用程序,但是如果我尝试连接到 Kubernetes 中的响应式服务,连接被拒绝。
For example if i connect from another pod to http://random-generator-svc:5050/ i get an error which says connection refused.例如,如果我从另一个 pod 连接到http://random-generator-svc:5050/,我会收到一个错误,提示连接被拒绝。
this is the yaml file to create the Service and the Deployment for the Random Generator:这是用于为随机生成器创建服务和部署的 yaml 文件:
apiVersion: v1
kind: Service
metadata:
name: random-generator-svc
labels:
app: rand-gen
spec:
selector:
app: rand-gen
type: NodePort
ports:
- protocol: "TCP"
port: 5050
targetPort: 5050
name: http
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: random-generator-deployment
labels:
app: rand-gen
spec:
replicas: 2
selector:
matchLabels:
app: rand-gen
template:
metadata:
labels:
app: rand-gen
spec:
containers:
- name: random-generator-container
image: toky03/random-generator-image:1.2
ports:
- containerPort: 5050
This is the yaml File wich specifies the Service and the Deployment of the "Caller" Application:这是指定服务和“调用者”应用程序部署的 yaml 文件:
apiVersion: v1
kind: Service
metadata:
name: middle-tier-svc
labels:
app: rand-gen
spec:
selector:
app: rand-gen
type: NodePort
ports:
- protocol: "TCP"
port: 7070
targetPort: 7070
name: http
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: middle-tier-controller
labels:
app: rand-gen
spec:
replicas: 1
selector:
matchLabels:
app: rand-gen
template:
metadata:
labels:
app: rand-gen
spec:
containers:
- name: random-controller-container
image: toky03/random-controller-image:1.2
ports:
- containerPort: 7070
I changed the type to NodePort
to try if this error also exists there but i am able to acess the service from outside of a cluster.我将类型更改为NodePort
以尝试那里是否也存在此错误,但我能够从集群外部访问该服务。 Is there probably a problem with my Kubernetes DNS resolver?我的 Kubernetes DNS 解析器可能有问题吗?
Thank you very much in advance for your help.非常感谢您的帮助。
By default, kubernetes creates a virtual proxy.默认情况下,kubernetes 创建一个虚拟代理。 You can then access your service after port forwarding it.然后,您可以在端口转发后访问您的服务。
kubectl port-forward svc random-generator-svc 5050:5050
That's because of a slight typo in your yaml indent.那是因为你的 yaml 缩进中有一个轻微的错字。 try this尝试这个
apiVersion: v1
kind: Service
metadata:
name: random-generator-svc
labels:
app: rand-gen
spec:
selector:
app: rand-gen
type: NodePort
ports:
- protocol: "TCP"
port: 5050
targetPort: 5050
name: http
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: random-generator-deployment
labels:
app: rand-gen
spec:
replicas: 2
selector:
matchLabels:
app: rand-gen
template:
metadata:
labels:
app: rand-gen
spec:
containers:
- name: random-generator-container
image: toky03/random-generator-image:1.2
ports:
- containerPort: 5050
if you only want your app to be exposed inside your cluster, just remove type: NodePort
eg:如果您只希望您的应用程序在您的集群中公开,只需删除type: NodePort
例如:
apiVersion: v1
kind: Service
metadata:
name: random-generator-svc
labels:
app: rand-gen
spec:
selector:
app: rand-gen
ports:
- protocol: "TCP"
port: 5050
targetPort: 5050
name: http
Try "http://random-generator-svc.default.svc.cluster.local:5050" .试试 "http://random-generator-svc.default.svc.cluster.local:5050" 。 You can replace the "default" with the namespace if you are using any.如果您正在使用任何名称,则可以将“默认”替换为名称空间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.