kong外部流量负载到K8S内部Service

流量负载到多个service

  • 该模式下,需要对每个deployment,或者pod单独建一个pod,service,十分麻烦
  • 该模式下,能对每个service的流量进行权重控制
测试deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  name: busybox-httpd-1
  namespace: kong
spec:
  selector:
    matchLabels:
      application: busybox-httpd-1
  replicas: 1
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  template:
    metadata:
      labels:
        application: busybox-httpd-1
    spec:
      containers:
      - name: busybox
        image: busybox:1.28
        command: ["/bin/sh","-c"]
        args: # 生成随机网页
        - |
          head -c 16 /dev/urandom | od -An -t x | tr -d ' ' > /var/www/index.html ;
          httpd -f -h /var/www/ ;
        ports:
        - name: httpd
          containerPort: 80
          protocol: TCP
---
apiVersion: v1
kind: Service
metadata:
  name: busybox-httpd-1
  namespace: kong
spec:
  ports:
  - name: httpd
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    application: busybox-httpd-1
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: busybox-httpd-2
  namespace: kong
spec:
  selector:
    matchLabels:
      application: busybox-httpd-2
  replicas: 1
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  template:
    metadata:
      labels:
        application: busybox-httpd-2
    spec:
      containers:
      - name: busybox
        image: busybox:1.28
        command: ["/bin/sh","-c"]
        args: # 生成随机网页
        - |
          head -c 16 /dev/urandom | od -An -t x | tr -d ' ' > /var/www/index.html ;
          httpd -f -h /var/www/ ;
        ports:
        - name: httpd
          containerPort: 80
          protocol: TCP
---
apiVersion: v1
kind: Service
metadata:
  name: busybox-httpd-2
  namespace: kong
spec:
  ports:
  - name: httpd
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    application: busybox-httpd-2
创建upstream

在这里插入图片描述

在这里插入图片描述

创建service

在这里插入图片描述

# service实际对应nginx配置如下
upstseam busybox-httpd {
	server 192.168.0.10:8080 weight=10;
}

server {
	listen 80;
	server_name www.busybox.com
	location /path {
		proxy_paxx http://busybox-httpd:80/upstream_path ;  
		# host对应busybox-httpd,转发到哪个upstream
		# port 对应转发到upstream的哪个端口
		# path 转发到upstream时携带什么路径
	}
}
创建route

在这里插入图片描述

# route实际对应nginx配置如下
upstseam busybox-httpd {
	server 192.168.0.10:8080 weight=10;
}

server {
	listen 80;
	server_name www.busybox.com   # hosts对应了域名,可以有多个
	location /path {			  # patths对应了nginx中的location
		proxy_paxx http://busybox-httpd:80/upstream_path ;  
	}
}

# strip path作用
# 当client访问 www.busybox.com:80/path/xxx/xxx时,如果开启截取路径(target端无法看到用户输入的域名全路径),转发给target时路径为:
	target_ip:port/upstream_path/xxx/xxx

# Preserve Host如果开启,upstream主机头将是服务主机的主机头
	当client访问 www.busybox.com:80/path/xxx/xxx时,会使用www.busybox.com:80/path/xxx/xxx来匹配后端的服务
创建windows本机域名解析

访问测试

在这里插入图片描述
在这里插入图片描述

  • 可见这二次访问被负载到不同的busybox pod上了

流量负载到单个service

  • 只需要将upstream修改一下即可
  • 该模式下,当流量到达service后,后续由service再来做一次负载均衡
点击阅读全文
Logo

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

更多推荐