首页 文章

在当地的kubernetes上提供服务

提问于
浏览
5

我在Mac OS上运行捆绑了docker的本地kubernetes .

我如何公开服务,以便通过Mac上的浏览器访问服务?

我创造了:

a)部署包括apache httpd .

b)通过yaml提供的服务:

apiVersion: v1
kind: Service
metadata:
  name: apaches
spec:
  selector:
    app: web
  type: NodePort
  ports:
  - protocol: TCP
    port: 80
  externalIPs:
  - 192.168.1.10 # Network IP of my Mac

我的服务看起来像:

$ kubectl get service apaches
NAME      TYPE       CLUSTER-IP       EXTERNAL-IP    PORT(S)        AGE
apaches   NodePort   10.102.106.158   192.168.1.10   80:31137/TCP   14m

我可以通过 wget $CLUSTER-IP 本地访问我的kubernetes集群中的服务

我试图在我的Mac上调用http://192.168.1.10/,但它不起作用 .

question处理类似的问题 . 但解决方案没有帮助,因为我不知道我可以使用哪种IP .

Update

感谢Michael Hausenblas,我使用Ingress制定了一个解决方案 . 然而,仍有一些悬而未决的问题:

  • 服务的externalIP是什么意思?当我不直接从外部访问服务时,为什么需要externalIP?

  • 服务端口31137是什么意思?

  • kubernetes文档描述了一种[通过NodePort在minikube中发布服务] [4]的方法 . 这个也可以与docker捆绑的kubernetes一起使用吗?

2 回答

  • 4

    正确的方法是使用Ingress,如this post中所述

  • 3

    有几种解决方案可以在kubernetes中公开服务:http://alesnosek.com/blog/2017/02/14/accessing-kubernetes-pods-from-outside-of-the-cluster/

    以下是根据alesnosek针对与docker捆绑的本地kubernetes的解决方案:

    1. hostNetwork

    hostNetwork: true
    

    脏(出于安全原因不应共享主机网络)=>我没有检查此解决方案 .

    2. hostPort

    hostPort: 8086
    

    不适用于服务=>我没有检查此解决方案 .

    3. NodePort

    通过定义nodePort来公开服务:

    apiVersion: v1
    kind: Service
    metadata:
      name: apaches
    spec:
      type: NodePort
      ports:
        - port: 80
          nodePort: 30000
      selector:
        app: apache
    

    4. LoadBalancer

    不适用于本地kubernetes,因为没有负载均衡器 .

    5. Ingress

    a. Install Ingress Controller

    git clone https://github.com/jnewland/local-dev-with-docker-for-mac-kubernetes.git
    
    kubectl apply -f nginx-ingress/namespaces/nginx-ingress.yaml -Rf nginx-ingress
    

    b. Configure Ingress

    kubectl apply -f apache-ing.yaml

    apiVersion: extensions/v1beta1
    kind: Ingress
    metadata:
      name: apache-ingress
    spec:
      rules:
      - host: localhost
        http:
          paths:
          - path: /
            backend:
              serviceName: apaches
              servicePort: 80
    

    现在我可以通过调用http://localhost/访问部署了kubernetes的apache

    Remarks for using local-dev-with-docker-for-mac-kubernetes

    Further documentation

相关问题