前言

K8S Service主要负责集群内访问,Ingress负责外网流量(7层)。本文主要记录网络流向,便于业务选型

Service

ClusterIP

source pod — > VIP ClusterIP(iptables规则、本机与跨主机在规则上有点区别) ----> dest pods

NodePort

client —> Node上的kube-proxy(port) —> ClusterIP(iptables规则)—> dest pods

LoadBalancer

不加annotation默认创建的是CLB(Classic Load Balancer)

client —> CLB —> Node上的kube-proxy(port) —> ClusterIP(iptables规则) —> dest pods

---
apiVersion: v1
kind: Service
metadata:
  name: nginx-service-lb
  namespace: net-test
#There is no any annotation
spec:
  type: LoadBalancer
  selector:
    app: nginx-net
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 80

annotation为nlb则创建NLB instance模式(Network Load Balancer)

client —> NLB(instance) —> Node上的kube-proxy(port) —> ClusterIP(iptables规则) —> dest pods

---
apiVersion: v1
kind: Service
metadata:
  name: nginx-service-lb-2
  namespace: net-test
 #There is an important annotation
  annotations:
    service.beta.kubernetes.io/aws-load-balancer-type: nlb
spec:
  type: LoadBalancer
  selector:
    app: nginx-net
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 80

annotation为nlb-ip则创建NLB ip模式(Network Load Balancer)

client —> NLB(ip) —> dest pods
这里是直接将数据包发到pod的ip地址,而且pod也是从VPC CIDR中分配 IP 地址,直接arp获得pod对应的内网ip地址的mac,流量就直接转到承载此pod的node上

---
apiVersion: v1
kind: Service
metadata:
  name: nginx-service-lb-3
  namespace: net-test
 #There is an important annotation
  annotations:
    service.beta.kubernetes.io/aws-load-balancer-type: nlb-ip
spec:
  type: LoadBalancer
  selector:
    app: nginx-net
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 80

小记

本质上NLB instance模式和CLB没有本质区别,网络流量完全相同,均需要将流量先转发到Node port。但是使用NLB ip模式则可以跨过ClusterIP流量直达pod,因此可以通过此模式获取client真实ip地址。

Ingress

client —> ALB(ip) —> dest pods
ingress负责7层协议http/https的转发,通过AWS默认创建的是ALB,通过如下配置可以看到,流量是导到Service ClusterIP。但是因为其也开启了ip模式(target-type: ip ),因此其流量也是跨过Service ClusterIP 直达pod。

---
apiVersion: v1
kind: Service
metadata:
  name: nginx-service-clusterip
  namespace: net-test
spec:
  type: ClusterIP
  selector:
    app: nginx-net
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 80
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  namespace: net-test
  name: nginx-ingress
  annotations:
    kubernetes.io/ingress.class: alb
    alb.ingress.kubernetes.io/scheme: internet-facing
    alb.ingress.kubernetes.io/target-type: ip
spec:
  rules:
    - http:
        paths:
          - path: /*
            backend:
              serviceName: nginx-service-clusterip
              servicePort: 8080

参考文档
https://aws.amazon.com/cn/blogs/china/in-depth-analysis-and-research-on-service-and-ingress-in-amazon-eks/?nc1=h_ls

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐