NLB工作在4层,使用LoadBalancer
类型的service创建,支持TCP/UDP协议。如果是7层的协议,要使用Ingress
来创建ALB
一般推荐使用AWS Load Balancer Controller
来创建NLB
NLB和Service共享生命周期
适用于传统应用或有自定义协议的应用
默认情况下不保留客户端源IP
创建IP类型的target:
将下面内容保存为nlb.yaml
kind: Service
apiVersion: v1
metadata:
name: nginx-service
annotations:
service.beta.kubernetes.io/aws-load-balancer-type: nlb-ip # nlb, clb
alb.ingress.kubernetes.io/scheme: internet-facing
spec:
externalTrafficPolicy: Cluster
type: LoadBalancer
selector:
app: nginx
ports:
- name: http
protocol: TCP
port: 8080
targetPort: 80
---
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
service.beta.kubernetes.io/aws-load-balancer-type: "external"
表示使用AWS Load Balancer Controller
来创建NLB,而不是AWS cloud provider load balancer controller
service.beta.kubernetes.io/aws-load-balancer-scheme: "internet-facing"
表示将NLB创建在public subnet,以前使用
service.beta.kubernetes.io/aws-load-balancer-type: "nlb-ip"
写法,现在推荐使用上面yaml中的写法
部署:
kubectl apply -f nlb.yaml
部署完成后,在AWS Console中可以看到创建的 对应NLB:
获取nlb的URL:
由于NLB上不绑定任何安全组,所以要使用node-group的安全组来放通8080端口流量的访问。
在浏览器中访问NLB的8080端口:
在创建完成service后,就不要更改annotation了;如果需要更改,请先删除service对象,更改完annotation后再重新创建它
参考:
https://docs.aws.amazon.com/eks/latest/userguide/network-load-balancing.html