我有一个私有网络上的Kubernetes集群(不是AWS或Google Cloud),我创建了一个服务以便访问,但我需要能够从集群外部访问。为此,我在集群中创建了Ingress,并添加了ingress-nginx。
这是我在多次尝试后使用的YAML:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: demo-ingress
annotations:
kubernetes.io/ingress.class: "nginx"
nginx.ingress.kubernetes.io/ssl-redirect: "false"
spec:
rules:
- host: k8s.local
http:
paths:
- path: /
backend:
serviceName: nginx
servicePort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx
spec:
type: ClusterIP
selector:
name: nginx
ports:
- port: 80
targetPort: 80
protocol: TCP
# selector:
# app: nginx
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 1
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: echoserver
image: nginx
ports:
- containerPort: 80
我这样运行了yaml:kubectl create -f file.yaml 我在/etc/hosts文件中将k8s.local添加到主服务器的IP地址中。
在尝试在主服务器内或外执行该命令时,出现“连接被拒绝”的消息: $ curl http://172.16.0.18:80/ -H 'Host: k8s.local' 我不知道这是否重要,但我在集群中使用Flannel。
我的想法只是创建一个“hello world”并将其暴露在集群之外!
我需要更改任何配置以允许此访问吗?
编辑后的YAML文件:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: demo-ingress
annotations:
kubernetes.io/ingress.class: "nginx"
nginx.ingress.kubernetes.io/ssl-redirect: "false"
# nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: k8s.local
http:
paths:
- path: /teste
backend:
serviceName: nginx
servicePort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx
spec:
type: LoadBalancer # NodePort
selector:
app: nginx
ports:
- port: 80
targetPort: 80
protocol: TCP
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 1
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: echoserver
image: nginx
ports:
- containerPort: 80