Pod无法通过入口解析服务的域名

时间:2018-09-06 09:05:43

标签: dns kubernetes minikube

我有一个问题,我的minikube集群中的Pod无法通过域名查看服务。

要运行我的minikube,我使用以下命令(在Windows 10上运行):
minikube start --vm-driver hyperv;
minikube addons enable kube-dns;
minikube addons enable ingress;

这是我的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窗格,并且从/bin/bash开始运行curl minikube.local.comnslookup minikube.local.com

那么我如何确保Pod可以解析服务的DNS名称? 我知道我可以在部署定义中指定hostAlias,但是有没有一种自动方式来更新kubernetes的DNS?

1 个答案:

答案 0 :(得分:3)

那么,您想在Minikube上公开您的应用程序吗?我已经使用默认的ClusterIP服务类型(实际上是删除了您拥有的ExternalName东西)进行了尝试,并使用this YAML file可以在https://192.168.99.100上看到您的服务,入口控制器的寿命:

screen shot of hello-world app in browser

该服务现在看起来像这样:

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名称)从群集中进行服务,并且需要从外部将hello-world.local映射到192.168。您的/etc/hosts文件中的.99.100。

或者,如果您将Ingress资源更改为- host: hello-world.local,则可以(通过主机)使用此FQDN来访问服务,如下所示:curl -H "Host: hello-world.local" 192.168.99.100