我们在日常开发测试的过程中,对外暴露服务时,通常选择使用NodePort,但是这样用起来会比较麻烦,需要记住端口号。

在使用公有云厂商上提供的Kubernetes时,可以利用他们负载均衡器插件来使用Type=LoadBalancer的Service。
在本地裸金属的情况下,只要交换机/路由器支持BGP协议下,也可以使用porter插件来使用这样的功能。
那在本地开发环境中,有没有办法也用这种方式呢?

下面分享一个使用KubeSphere的Porter工具在本地开发时的经验。

安装porter之后,配置EIP和本地网段相同可以让同网段的客户端访问到后端Service

环境介绍:
本地网段:192.168.1.0/24
本机地址:192.168.1.52
k8s节点地址: 192.168.1.102 192.168.1.103 192.168.1.104
EIP地址: 192.168.1.200/29 这个可以自己规划为其他地址,只要和本地网段一致。

nodes:

root@master:~# kubectl get node -o wide
NAME     STATUS   ROLES    AGE   VERSION   INTERNAL-IP     EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION      CONTAINER-RUNTIME
master   Ready    master   15h   v1.15.5   192.168.1.102   <none>        Ubuntu 18.04.4 LTS   4.15.0-76-generic   docker://18.9.7
node1    Ready    worker   15h   v1.15.5   192.168.1.103   <none>        Ubuntu 18.04.4 LTS   4.15.0-76-generic   docker://18.9.7
node2    Ready    worker   15h   v1.15.5   192.168.1.104   <none>        Ubuntu 18.04.4 LTS   4.15.0-76-generic   docker://18.9.7

本地环境用到的porter安装脚本: https://gist.github.com/zheng1/e97adda2722143b270810fa61b60edb7#file-port-yaml

eip地址范围:

apiVersion: network.kubesphere.io/v1alpha1
kind: Eip
metadata:
    name: eip-sample-pool
spec:
    address: 192.168.1.200/29
    disable: false

测试使用的工作负载:

kind: Service
apiVersion: v1
metadata:
    name:  mylbapp
    annotations:
        lb.kubesphere.io/v1alpha1: porter
spec:
    selector:
        app:  mylbapp
    type:  LoadBalancer
    ports:
    - name:  http
      port:  8088
      targetPort:  80
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: mylbapp
  name: mylbapp
spec:
  replicas: 3
  selector:
    matchLabels:
      app: mylbapp
  template:
    metadata:
      labels:
        app: mylbapp
    spec:
      containers:
      - image: nginx:alpine
        name: nginx
        ports:
        - containerPort: 80

在本机进行测试:

# curl 192.168.1.200:8088
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

    zheng1 本地环境用到的porter安装脚本:

    zheng1 这个安装脚本是在 Layer 2 模式下进行的安装么

      Feynman 这个安装脚本里面的配置是BGP模式,实际上的这个BGP允许不起来,因为里面的对端和本地地址都是错误的。
      但是porter的其他功能可以正常工作,这样运行之后可以使EIP运行在Layer2模式下

      9 个月 后

      你好,devops新建工程时报错,无法创建
      ,这是什么原因了?

      4 个月 后

      zheng1
      EIP配置为master节点的ip,但是EXTERNAL-IP是哪里来的,自动就出来这么一个ip?


      crd中eip对应的东西没有了。一开始eip资源删除不了,各种删,删了半天发现删不掉,然后把crd总跟eip相关的内容delete了一下,过了很久发现跟eip相关的都没了。求助