k8s中MetalLB的使用

1.官方链接
https://metallb.universe.tf/concepts/

操作步骤

step 1 安装MetalLB
kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.12.1/manifests/namespace.yaml
kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.12.1/manifests/metallb.yaml
step 2 配置config, 以layer2协议为例

config.yaml的内容如下:

apiVersion: v1
kind: ConfigMap
metadata:
  namespace: metallb-system
  name: config
data:
  config: |
    address-pools:
    - name: dev-public-ips
      protocol: layer2
      addresses:
      - 10.0.2.100-10.0.2.200

其中,dev-public-ips为自定义名称,后面会用到addresses为自定义地址池

step 3 以nginx为例

(1) Deployment
nginx-deployment.yaml 的内容如下

root@unode1:~/metallb/example# cat nginx-deployment.yaml 
apiVersion: apps/v1
kind: Deployment      
metadata:
name: pc-deployment
namespace: dev
spec: 
replicas: 3
selector:
  matchLabels:
    app: nginx-pod
template:
  metadata:
    labels:
      app: nginx-pod
  spec:
    containers:
    - name: nginx
      image: nginx:1.17.1
      ports:
      - containerPort: 80

应用Deployment该文件
kubectl apply -f nginx-deployment.yaml
(2) Service

root@unode1:~/metallb/example# cat service-metallb.yaml 
apiVersion: v1
kind: Service
metadata:
  name: nginx
  namespace: dev
  annotations:
    metallb.universe.tf/address-pool: dev-public-ips  #注意,dev-public-ips为config.yaml地址池的名称
spec:
  ports:
  - port: 81   #external ip的端口
    targetPort: 80 # 内部容器端口
  selector:
    app: nginx-pod
  type: LoadBalancer

应用Service文件
kubectl apply -f service-metallb.yaml
(3)参看Service

root@unode1:~/metallb/example# kubectl get svc -n dev
NAME    TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
nginx   LoadBalancer   10.109.79.140   10.0.2.101    81:30182/TCP   14h

其中81EXTERNAL-IP的端口, 30182为node端口, 访问测试如下:
测试1

root@unode1:~/metallb/example# curl 10.0.2.101:81
<!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>

测试2

root@unode1:~/metallb/example# curl 10.0.2.4:30182  #10.0.2.4为node的ip
<!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>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值