k8s如何配置加权轮训
时间: 2023-07-07 11:35:05 浏览: 58
在Kubernetes中,可以通过配置`Service`的`Endpoint`来实现加权轮询。下面是具体实现步骤:
1. 创建一个带有多个pod的deployment。
```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: example
spec:
replicas: 3
selector:
matchLabels:
app: example
template:
metadata:
labels:
app: example
spec:
containers:
- name: example
image: example:latest
ports:
- containerPort: 80
```
2. 创建一个`Service`,指向该deployment。
```yaml
apiVersion: v1
kind: Service
metadata:
name: example-service
spec:
selector:
app: example
ports:
- name: http
port: 80
targetPort: 80
```
3. 创建`Endpoint`,指定每个pod的ip地址和端口以及权重。
```yaml
apiVersion: v1
kind: Endpoints
metadata:
name: example-service
subsets:
- addresses:
- ip: 10.0.0.1
- ip: 10.0.0.2
- ip: 10.0.0.3
ports:
- name: http
port: 80
protocol: TCP
# 权重设置
# 第一个pod的权重为5,第二个pod的权重为3,第三个pod的权重为2
# 总权重为10
# 5/10的请求被转发到第一个pod,3/10的请求被转发到第二个pod,2/10的请求被转发到第三个pod
# 如果不设置权重,默认权重为1
- name: http
port: 80
protocol: TCP
targetPort: 80
weight: 5
targetRef:
kind: Pod
name: example-68b9d9d5f9-9t7r6
- name: http
port: 80
protocol: TCP
targetPort: 80
weight: 3
targetRef:
kind: Pod
name: example-68b9d9d5f9-8j8g5
- name: http
port: 80
protocol: TCP
targetPort: 80
weight: 2
targetRef:
kind: Pod
name: example-68b9d9d5f9-2c5t9
```
4. 通过`kubectl get endpoints example-service`检查`Endpoint`是否正确配置。
配置完成后,通过访问`example-service`来测试加权轮询的效果。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)