在当地的kubernetes上提供服务

Mat*_*s M 9 kubernetes

我在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
Run Code Online (Sandbox Code Playgroud)

我的服务看起来像:

$ 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
Run Code Online (Sandbox Code Playgroud)

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

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

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

更新

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

  • 服务的externalIP是什么意思?当我不直接从外部访问服务时,为什么需要externalIP?
  • 服务端口31137是什么意思?
    • kubernetes文档描述了一种[通过NodePort在minikube中发布服务]的方法[4].这个也可以与docker捆绑的kubernetes一起使用吗?

小智 9

对于那些仍在寻找答案的人。我通过添加另一个 Kube 服务来实现这一点,只是为了将我的应用程序暴露给本地主机调用(通过浏览器或 Postman):

kind: Service
apiVersion: v1
metadata:
  name: apaches-published
spec:
  ports:
    - name: http
      port: 8080
      targetPort: 80
      protocol: TCP
  selector:
    app: web
  type: LoadBalancer
Run Code Online (Sandbox Code Playgroud)

立即尝试:http://localhost:8080

  • 我没有获得以下类型的外部 IP: Loadbalancer: apaches LoadBalancer 10.98.28.154 <pending> ... (3认同)

cod*_*diz 7

非常简单的例子

方法1

$ kubectl create deployment nginx-dep --image=nginx --replicas=2
Run Code Online (Sandbox Code Playgroud)
  • 获取豆荚
$ kubectl get pods
NAME                        READY   STATUS    RESTARTS   AGE
nginx-dep-5c5477cb4-76t9q   1/1     Running   0          7h5m
nginx-dep-5c5477cb4-9g84j   1/1     Running   0          7h5m
Run Code Online (Sandbox Code Playgroud)
  • 使用以下命令访问 Podkubectl port
$ kubectl port-forward nginx-dep-5c5477cb4-9g84j 8888:80
Forwarding from 127.0.0.1:8888 -> 80
Forwarding from [::1]:8888 -> 80
Run Code Online (Sandbox Code Playgroud)
  • 现在做curl一个localhost:8888
$ curl -v http://localhost:8888 
Run Code Online (Sandbox Code Playgroud)

方法2

您可以port 80通过 NodePort 公开部署(应用程序在其中运行,即 nginx 端口)

$ kubectl expose deployment nginx-dep --name=nginx-dep-svc --type=NodePort --port=80
Run Code Online (Sandbox Code Playgroud)
  • 获取服务
$ kubectl get svc 
NAME            TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
kubernetes      ClusterIP   10.96.0.1      <none>        443/TCP        31d
nginx-dep-svc   NodePort    10.110.80.21   <none>        80:31239/TCP   21m
Run Code Online (Sandbox Code Playgroud)
  • 使用 NodePort 访问部署
$ curl http://localhost:31239  
Run Code Online (Sandbox Code Playgroud)


Mat*_*s M 5

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

这是根据alesnosek针对与docker捆绑在一起的本地kubernetes的解决方案:

1. hostNetwork

hostNetwork: true
Run Code Online (Sandbox Code Playgroud)

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

2. hostPort

hostPort: 8086
Run Code Online (Sandbox Code Playgroud)

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

3. NodePort

通过定义一个nodePort公开服务:

apiVersion: v1
kind: Service
metadata:
  name: apaches
spec:
  type: NodePort
  ports:
    - port: 80
      nodePort: 30000
  selector:
    app: apache
Run Code Online (Sandbox Code Playgroud)

4.负载均衡器

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

5.入口

一种。安装入口控制器

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
Run Code Online (Sandbox Code Playgroud)

b。配置入口

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
Run Code Online (Sandbox Code Playgroud)

现在,我可以通过调用http:// localhost /访问使用kubernetes部署的Apache。

mac-kubernetes使用docker本地开发的说明

进一步的文件