我想用Kubernetes创建一个示例应用程序,但如果我尝试连接到Kubernetes中的响应服务,会拒绝连接。
例如,如果我从另一个pod连接到http:// random-generator-svc:5050 / i会收到一条错误,表示连接被拒绝。
这是用于创建服务和随机生成器部署的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
这是yaml文件,它指定服务和“调用者”应用程序的部署:
apiVersion: v1
kind: Service
metadata:
name: middle-tier-svc
labels:
app: rand-gen
spec:
selector:
app: rand-gen
type: NodePort
ports:
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
我将类型更改NodePort为尝试此错误是否也存在但我能够从群集外部访问服务。我的Kubernetes DNS解析器可能有问题吗?
那是因为你的yaml缩进中有一个小错字。试试这个
apiVersion: v1
kind: Service
metadata:
name: random-generator-svc
labels:
app: rand-gen
spec:
selector:
app: rand-gen
type: NodePort
ports:
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
如果您只希望您的应用在群集中公开,只需删除type: NodePort例如:
apiVersion: v1
kind: Service
metadata:
name: random-generator-svc
labels:
app: rand-gen
spec:
selector:
app: rand-gen
ports:
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。