k8s网路解决的主要问题

服务发现:如何找到Pod,Pod如何找到其他的Pod

  • Pod内部通信:local host:port

  • 用户查找Pod:Pod的ip:port

  • Pod与Pod通信:跨节点,但是不用nat

CNI插件实现Pod间通信

  • Flannel:使用VXLAN“隧道”技术,适合中小型集群

  • Calico:适合于大规模集群;BGP 边界网关协议(Border cateway Protocol,BGP):是互联网上一个核心的去中心化自治路由协议。BGP不使用传统的内部网关协议(IGP)的指标。

服务发现:

通过service实现

  • 通过“标签”选中Pod,提供固定的入口,类似“VIP”,然后提供负载均衡;

  • 给Pod提供DNS负载均衡,Pod通过域名访问Service

Service -> EndPoint(动态的IP列表) -> Pod

创建Service时,自动创建EndPoints

  • 网络和负载均衡:kube-proxy -> 系统内核(lvs/iptables)

Service三大功能
  1. 服务发现:通过DNS域名(如web-service.default.svc.cluster.local)

    • ServiceName.namespaces.svc.cluster.local 替代动态Pod IP

  2. 负载均衡:把请求平均分给背后的多个Pod,避免单个Pod过载

  3. 端口映射:隐藏Pod内部端口(如外部访问80端口,转发到Pod的8080端口)。

四大模式
  • ClusterIP(默认):只允许集群内部访问,外部不可见,用于集群内部Pod间通信,例如:后端调用,数据库连接等

  • NodePort:外部通过任意Node的IP:端口进行访问,NodePort 的默认范围是 30000-32767,但可以手动指定。简单访问,开发测试

  • LoadBalencer:使用云厂商的负载均衡器,实现对外的服务,适合在生产环境云平台上使用

  • ExternalName:域名转换,给域名起别名, Service 通过 DNS 解析将请求转发到指定的外部域名。

特殊模式:

  • Headless:Service没有IP,也不做负载均衡,仅返回Pod

vim my-clusterip-service.yaml
​
---
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: myapp
  name: myapp
spec:
  replicas: 3
  selector:
    matchLabels:
      app: myapp
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: myapp
    spec:
      containers:
      - image: 192.168.57.200:8099/library/nginx:1.24
        name: nginx
        resources: {}
status: {}
---
apiVersion: v1
kind: Service
metadata:
  name: my-clusterip-service
spec:
  type: ClusterIP # 可省略,默认就是 ClusterIP
  selector:
    app: myapp  # 匹配 Pod 的标签
  ports:
    - protocol: TCP
      port: 80    # Service的端口
      targetPort: 80 # Pod 的端口
​
​
kubectl apply -f my-clusterip-service.yaml

NodePort

---
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: myapp
  name: myapp
spec:
  replicas: 3
  selector:
    matchLabels:
      app: myapp
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: myapp
    spec:
      containers:
      - image: 192.168.57.200:8099/library/nginx:1.21
        name: nginx
        resources: {}
status: {}
---
apiVersion: v1
kind: Service
metadata:
  name: my-nodeport-service
spec:
  type: NodePort #类型
  selector:
    app: myapp  # 匹配 Pod 的标签
  ports:
    - protocol: TCP
      port: 80        # Service的端口
      targetPort: 80  # Pod的端口,内部容器开发的端口
      nodePort: 30000 # Node对外服务端口 30000-32767
      
      
  
wget https://raw.githubusercontent.com/metallb/metallb/v0.14.9/config/manifests/metallb-native.yaml
​
 kubectl get configmap kube-proxy -n kube-system -o yaml | \
sed -e "s/strictARP: false/strictARP: true/"  | \
sed -e 's#mode: ""#mode: "ipvs"#' | \
kubectl apply -f - -n kube-system
​
kubectl apply -f 
kubectl get all -o wide -n metallb-system
​
​
vim 21.metallb-ip-pool.yaml
​
apiVersion: metallb.io/v1beta1
kind: IPAddressPool
metadata:
  name: address
  namespace: metallb-system
spec:
  addresses:
  - 192.168.8.80-192.168.8.100
 
---
apiVersion: metallb.io/v1beta1
kind: L2Advertisement
metadata:
  name: l2a
  namespace: metallb-system
spec:
  ipAddressPools:
  - address
​
​
​
22.deployment-svc-lb.yaml 
​

Ingress

Ingress是k8s中web服务访问的入口;是基于Service,并且可以管理多个Service访问

Ingress主要负责七层负载,将外部 HTTP/HTTPS 请求路由到集群内部的服务。它可以基于域名和路径定义规则,从而将外部请求分配到不同的服务。

  • Ingress本质是反向代理服务器;Ingress-Nginx

  • 通过不同域名或路径,将流量转发给不同的Service

  • Ingress是七层(应用层),Service是四层(传输层)(TCP/UDP)

应表会传网数物

Ingress的搭建与使用
  • Ingress:kubernetes中的一个对象,作用是定义请求如何转发到Service的规则

  • Ingress Controller:具体实现反向代理及负载均衡的程序,对ingress定义的规则进行解析,根据配置的规则来实现请求转发,实现方式有很多,比如Nginx, Contour, Haproxy等等,需要安装使用

安装Ingress Controller

192.168.57.200:8099/ingress-nginx/kube-webhook-certgen:v20220916-gd32f8c343

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: dev
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx-pod
  template:
    metadata:
      labels:
        app: nginx-pod
    spec:
      containers:
      - name: nginx
        image:  192.168.57.200:8099/library/nginx:1.21
        ports:
        - containerPort: 80
​
---
​
apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat-deployment
  namespace: dev
spec:
  replicas: 3
  selector:
    matchLabels:
      app: tomcat-pod
  template:
    metadata:
      labels:
        app: tomcat-pod
    spec:
      containers:
      - name: tomcat
        image:  192.168.57.200:8099/library/tomcat:8
        ports:
        - containerPort: 8080
​
---
​
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
  namespace: dev
spec:
  selector:
    app: nginx-pod
  clusterIP: None
  type: ClusterIP
  ports:
  - port: 80
    targetPort: 80
​
---
​
apiVersion: v1
kind: Service
metadata:
  name: tomcat-service
  namespace: dev
spec:
  selector:
    app: tomcat-pod
  clusterIP: None
  type: ClusterIP
  ports:
  - port: 8080
    targetPort: 8080

更多推荐