k8s内利用kong实现外部流量负载均衡

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再来做一次负载均衡
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Kubernetes中,如果连接到PostgreSQL数据库的Kong服务超时,可能有几个原因。首先,确保Kong服务的配置正确。根据引用\[1\]中的命令,您需要确保Kong服务正确配置了外部的PostgreSQL数据库。检查以下配置项是否正确设置:postgresql.external.host、postgresql.external.password和postgresql.external.user。确保这些配置与您的实际数据库设置相匹配。 其次,检查Kong服务是否能够访问到PostgreSQL数据库。确保Kong服务和PostgreSQL数据库在同一个网络中,并且网络连接是正常的。您可以尝试在Kong服务所在的节点上使用telnet命令测试与PostgreSQL数据库的连接。 另外,还需要确保PostgreSQL数据库已正确安装和配置。根据引用\[2\]中的命令,您需要确保PostgreSQL数据库已正确映射数据目录,并且数据库的用户名和密码已正确创建。您可以使用psql命令检查数据库是否已正确创建。 最后,如果您在Kong服务启动后仍然遇到连接超时的问题,可以尝试运行Kong migrations up命令来更新/初始化数据库模式。根据引用\[3\]中的错误信息,Kong可能需要更新数据库模式以与当前版本匹配。请注意,运行Kong migrations up命令应该只在单个节点上运行,并且并发运行可能会导致冲突和数据库模式损坏。 综上所述,如果在Kubernetes中连接到PostgreSQL数据库的Kong服务超时,请确保Kong服务的配置正确,Kong服务能够访问到PostgreSQL数据库,并且PostgreSQL数据库已正确安装和配置。如果问题仍然存在,可以尝试运行Kong migrations up命令来更新/初始化数据库模式。 #### 引用[.reference_title] - *1* *2* [k8s上使用Helm安装kong2.0.4(对应Chart Versions1.1.7)](https://blog.csdn.net/math_code/article/details/106536814)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [KubernetesK8s)](https://blog.csdn.net/weixin_40618648/article/details/96634689)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旺仔_牛奶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值