不要用wsl2,netem 的模块默认不编译
----终端1-----
❯ docker run -d --name redisa redis
❯ docker run -d --name redisb redis
docker inspect redisa --format '{{ .NetworkSettings.IPAddress }}'
172.17.0.2
docker inspect redisa --format '{{ .NetworkSettings.IPAddress }}'
172.17.0.3
# 负载均衡算法不要选择
❯ sudo ipvsadm -A -t 100.100.100.100:6379 -s rr
❯ sudo ipvsadm -a -t 100.100.100.100:6379 -r 172.17.0.2 -m
❯ sudo ipvsadm -a -t 100.100.100.100:6379 -r 172.17.0.3 -m
# 代码里面并发数量如过太低了是没办法复现的,大体上看到ss 看 6379 的链接在200附近的时候能复现
# 如果想要更明显可以把200改成400
git clone [email protected]:phantooom/troubleshooting-lab.git
cd troubleshooting-lab/2024-06-16-redis-balance
mvn package
java -cp 'target/lib/*:target/*' cool.zou.RedisUtils
----终端2-----
❯ sudo ipvsadm -ln
IP Virtual Server version 1.2.1 (size=32768)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 100.100.100.100:6379 rr
-> 172.17.0.2:6379 Masq 1 17 0
-> 172.17.0.3:6379 Masq 1 25 0
❯ sudo ipvsadm -ln
IP Virtual Server version 1.2.1 (size=32768)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 100.100.100.100:6379 rr
-> 172.17.0.2:6379 Masq 1 30 31
-> 172.17.0.3:6379 Masq 1 45 25
❯ sudo ipvsadm -ln
IP Virtual Server version 1.2.1 (size=32768)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 100.100.100.100:6379 rr
-> 172.17.0.2:6379 Masq 1 48 0
-> 172.17.0.3:6379 Masq 1 57 1
❯ docker inspect -f {{.State.Pid}} redisa
6963
❯ sudo nsenter -t 6963 -n
# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host proto kernel_lo
valid_lft forever preferred_lft forever
2: tunl0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN group default qlen 1000
link/ipip 0.0.0.0 brd 0.0.0.0
3: sit0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN group default qlen 1000
link/sit 0.0.0.0 brd 0.0.0.0
16: eth0@if17: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
# tc qdisc add dev eth0 root netem delay 200ms
# exit
❯ sudo ipvsadm -ln
IP Virtual Server version 1.2.1 (size=32768)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 100.100.100.100:6379 rr
-> 172.17.0.2:6379 Masq 1 148 0
-> 172.17.0.3:6379 Masq 1 59 4
❯ sudo ipvsadm -ln
IP Virtual Server version 1.2.1 (size=32768)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 100.100.100.100:6379 rr
-> 172.17.0.2:6379 Masq 1 153 0
-> 172.17.0.3:6379 Masq 1 54 0
❯ sudo ipvsadm -ln
IP Virtual Server version 1.2.1 (size=32768)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 100.100.100.100:6379 rr
-> 172.17.0.2:6379 Masq 1 154 0
-> 172.17.0.3:6379 Masq 1 53 1