对Pod中同一应用的多个容器进行负载平衡 [英] Load balancing to multiple containers of same app in a pod
问题描述
我有一个场景,我需要在同一容器中运行一个应用程序容器的两个实例. 我将它们设置为在不同的端口上侦听.以下是部署清单的外观. 使用预期数量的容器,Pod可以很好地启动. 我什至可以从其他Pod连接到podIP上的两个端口.
I have a scenario where I need to have two instances of an app container run within the same pod. I have them setup to listen on different ports. Below is how the Deployment manifest looks like. The Pod launches just fine with the expected number of containers. I can even connect to both ports on the podIP from other pods.
kind: Deployment
metadata:
labels:
service: app1-service
name: app1-dep
namespace: exp
spec:
template:
spec:
contianers:
- image: app1:1.20
name: app1
ports:
- containerPort: 9000
protocol: TCP
- image: app1:1.20
name: app1-s1
ports:
- containerPort: 9001
protocol: TCP
我什至可以为容器的每个端口创建两个不同的服务,而且效果也很好. 我可以分别访问这两个服务,最后到达Pod内的相应容器.
I can even create two different Services one for each port of the container, and that works great as well. I can individually reach both Services and end up on the respective container within the Pod.
apiVersion: v1
kind: Service
metadata:
name: app1
namespace: exp
spec:
ports:
- name: http
port: 80
protocol: TCP
targetPort: 9000
selector:
service: app1-service
sessionAffinity: None
type: ClusterIP
---
apiVersion: v1
kind: Service
metadata:
name: app1-s1
namespace: exp
spec:
ports:
- name: http
port: 80
protocol: TCP
targetPort: 9001
selector:
service: app1-service
sessionAffinity: None
type: ClusterIP
我希望单个服务背后的两个容器实例在两个容器之间轮循. 我该如何实现?在服务领域内可能吗?还是我需要探索类似这样的入口?
I want both the instances of the container behind a single service, that round robins between both the containers. How can I achieve that? Is it possible within the realm of services? Or would I need to explore ingress for something like this?
推荐答案
Kubernetes服务具有三种代理模式:iptables
(默认设置),userspace
,IPVS
.
Kubernetes services has three proxy modes: iptables
(is the default), userspace
, IPVS
.
-
Userspace
:是较旧的方法,它是循环分发的唯一方法. -
Iptables
:是默认设置,并随机选择一个吊舱并坚持使用. -
IPVS
:具有多种分发流量的方法,但是首先您必须使用以下命令将其安装在您的节点上,例如在centos节点上:yum install ipvsadm
,然后使其可用.
Userspace
: is the older way and it distribute in round-robin as the only way.Iptables
: is the default and select at random one pod and stick with it.IPVS
: Has multiple ways to distribute traffic but first you have to install it on your node, for example on centos node with this command:yum install ipvsadm
and then make it available.
就像我说的那样,默认情况下,Kubernetes服务没有轮询.
要激活IPVS
,您必须向kube-proxy添加一个参数
Like i said, Kubernetes service by default has no round-robin.
To activate IPVS
you have to add a parameter to kube-proxy
--proxy-mode=ipvs
--ipvs-scheduler=rr
(用于选择循环)
这篇关于对Pod中同一应用的多个容器进行负载平衡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!