没有装traefik 2.x的先看我之前的traefik 2.x安装文档。
一共4个yaml,按顺序执行。
01-appv1.yaml
02-appv2.yaml
03-app-ingress-route.yaml
04-wrr.yaml
# 分别执行四个文件
$ kubectl apply -f xxx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: appv1
spec:
selector:
matchLabels:
app: appv1
template:
metadata:
labels:
use: test
app: appv1
spec:
containers:
- name: whoami
image: containous/whoami
ports:
- containerPort: 80
name: portv1
---
apiVersion: v1
kind: Service
metadata:
name: appv1
spec:
selector:
app: appv1
ports:
- name: http
port: 80
targetPort: portv1
apiVersion: apps/v1
kind: Deployment
metadata:
name: appv2
spec:
selector:
matchLabels:
app: appv2
template:
metadata:
labels:
use: test
app: appv2
spec:
containers:
- name: nginx
image: nginx:1.8
ports:
- containerPort: 80
name: portv2
---
apiVersion: v1
kind: Service
metadata:
name: appv2
spec:
selector:
app: appv2
ports:
- name: http
port: 80
targetPort: portv2
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
name: wrringressroute
namespace: default
spec:
# 同时支持http和https访问
entryPoints:
- web
routes:
- match: Host(`wrr.duliri.com`)
kind: Rule
services:
- name: app-wrr
kind: TraefikService
apiVersion: traefik.containo.us/v1alpha1
kind: TraefikService
metadata:
name: app-wrr
spec:
weighted:
services:
- name: appv1
weight: 3 # 定义权重
port: 80
kind: Service # 可选,默认就是 Service
- name: appv2
weight: 1
port: 80
# 1. hosts添加解析
yourNodeIp wrr.duliri.com
# 2. curl请求多次接口,查看访问权重是否符合设置
$ curl http://wrr.duliri.com:30001
访问4此,3次请求到appv1,1次访问到appv2,查看traefik dashboard权重页面