我遇到一个问题,我的 minikube 集群中的 pod 无法通过域名看到服务。
要运行我的 minikube,我使用以下命令(在 Windows 10 上运行):
minikube start --vm-driver hyperv;
minikube 插件启用 kube-dns;
minikube 插件启用入口;
这是我的deployment.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
labels:
run: hello-world
name: hello-world
namespace: default
spec:
progressDeadlineSeconds: 600
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
run: hello-world
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
run: hello-world
spec:
containers:
- image: karthequian/helloworld:latest
imagePullPolicy: Always
name: hello-world
ports:
- containerPort: 80
protocol: TCP
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
这是service.yaml
:
apiVersion: v1
kind: Service
metadata:
labels:
run: hello-world
name: hello-world
namespace: default
selfLink: /api/v1/namespaces/default/services/hello-world
spec:
ports:
- nodePort: 31595
port: 80
protocol: TCP
targetPort: 80
selector:
run: hello-world
sessionAffinity: None
type: ExternalName
externalName: minikube.local.com
status:
loadBalancer: {}
这是我的ingress.yaml
:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: minikube-local-ingress
spec:
rules:
- host: minikube.local.com
http:
paths:
- path: /
backend:
serviceName: hello-world
servicePort: 80
因此,如果我进入 hello-world
pod 并从 /bin/bash
运行 curl minikube.local.com
或nslookup minikube.local.com
。
那么我如何确保 Pod 可以解析服务的 DNS 名称?
我知道我可以在部署定义中指定 hostAlias
,但是有没有一种自动方式可以允许更新 kubernetes 的 DNS?
最佳答案
那么,您想在 Minikube 上公开您的应用程序吗?我刚刚使用默认的 ClusterIP
服务类型(本质上是删除您拥有的 ExternalName
内容)和 this YAML file 进行了尝试。我可以在 Ingress Controller 所在的 https://192.168.99.100
上看到您的服务:
服务现在看起来像这样:
apiVersion: v1
kind: Service
metadata:
labels:
run: hello-world
name: hello-world
spec:
ports:
- port: 80
targetPort: 80
selector:
run: hello-world
入口是:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: minikube-local-ingress
annotations:
ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host:
http:
paths:
- path: /
backend:
serviceName: hello-world
servicePort: 80
注意:在集群内,您的服务现在可以通过 hello-world.default
(这是集群内 Kubernetes 分配的 DNS 名称)使用,并且您需要从外部进行映射,例如将主机上的 /etc/hosts
文件中的 hello-world.local
更改为 192.168.99.100。
或者,如果您将 Ingress
资源更改为 - host: hello-world.local
那么您可以(从主机)使用此 FQDN 访问您的服务,如下所示:curl -H“主机:hello-world.local”192.168.99.100
。
关于dns - Pod 不通过 ingress 解析服务的域名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52200448/