mirror of
https://hubproxy.babadafafafafa.cn/https://github.com/kube-vip/kube-vip.git
synced 2026-09-20 08:03:47 +08:00
46
demo/README.md
Normal file
46
demo/README.md
Normal file
@@ -0,0 +1,46 @@
|
||||
# Demo client-server
|
||||
|
||||
This contains some example code to determine how long "failovers" are taking within kube-vip, the server component should live within the cluster and the client should be externally.
|
||||
|
||||
## Deploy the server
|
||||
|
||||
Simply apply the manifest to a working cluster that has kube-vip deployed:
|
||||
|
||||
```
|
||||
kubectl apply -f ./demo/server/deploy.yaml
|
||||
```
|
||||
|
||||
Retrieve the loadBalancer IP that is fronting the service:
|
||||
|
||||
```
|
||||
kubectl get svc demo-service
|
||||
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
|
||||
demo-service LoadBalancer 10.104.18.147 192.168.0.217 10002:32529/UDP 117m
|
||||
```
|
||||
|
||||
## Connect the client
|
||||
|
||||
From elsewhere, clone the kube-vip repository and connect the client to the server endpoint (loadBalancer IP) with the following command:
|
||||
|
||||
```
|
||||
go run ./demo/client/main.go -address=<vip>
|
||||
```
|
||||
|
||||
You will only see output when the client has reconcilled the connection to a pod beneath the service, where it will print the timestamp to reconnection along with the time in milliseconds it took:
|
||||
|
||||
```
|
||||
15:58:35.916952 3008
|
||||
15:58:45.947506 2005
|
||||
15:58:57.983151 3007
|
||||
15:59:08.013450 2005
|
||||
15:59:20.046491 3008
|
||||
15:59:30.076341 2507
|
||||
15:59:42.110747 3008
|
||||
```
|
||||
|
||||
## Kill some pods to test
|
||||
|
||||
On a machine or control plane that has `kubectl` and has the credentials to speak to the cluster we will run a command to find the demo pod and kill it every 10 seconds:
|
||||
|
||||
`while true ; do kubectl delete pod $(kubectl get pods | grep -v NAME | grep vip| awk '{ print $1 }'); sleep 10; done`
|
||||
|
||||
56
demo/client/main.go
Normal file
56
demo/client/main.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"flag"
|
||||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
)
|
||||
|
||||
const udpdata = "a3ViZS12aXAK=kube-vip"
|
||||
|
||||
func main() {
|
||||
address := flag.String("address", "127.0.0.1", "The address of the server")
|
||||
port := flag.Int("port", 10002, "the port of the server")
|
||||
interval := flag.Float64("interval", 1000, "Interval in milliseconds")
|
||||
flag.Parse()
|
||||
var errorTime time.Time
|
||||
var errorOccured bool
|
||||
for {
|
||||
p := make([]byte, 2048)
|
||||
conn, err := net.Dial("udp", fmt.Sprintf("%s:%d", *address, *port))
|
||||
conn.SetDeadline(time.Now().Add(time.Duration(*interval) * time.Millisecond))
|
||||
if err != nil {
|
||||
//fmt.Printf("Connectivity error [%v]", err)
|
||||
if !errorOccured {
|
||||
errorTime = time.Now()
|
||||
errorOccured = true
|
||||
}
|
||||
conn.Close()
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Fprintf(conn, udpdata)
|
||||
|
||||
_, err = bufio.NewReader(conn).Read(p)
|
||||
if err != nil {
|
||||
//fmt.Printf("read error %v\n", err)
|
||||
if !errorOccured {
|
||||
errorTime = time.Now()
|
||||
errorOccured = true
|
||||
}
|
||||
conn.Close()
|
||||
continue
|
||||
}
|
||||
time.Sleep(time.Duration(*interval) * time.Millisecond)
|
||||
if errorOccured {
|
||||
finishTime := time.Since(errorTime)
|
||||
//fmt.Printf("connectivity reconciled in %dms\n", finishTime.Milliseconds())
|
||||
//t :=time.Now().Format("15:04:05.000000")
|
||||
fmt.Printf("%s %d\n", time.Now().Format("15:04:05.000000"), finishTime.Milliseconds())
|
||||
|
||||
errorOccured = false
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ metadata:
|
||||
app: kube-vip-demo
|
||||
name: kube-vip-demo
|
||||
spec:
|
||||
replicas: 3
|
||||
replicas: 1
|
||||
selector:
|
||||
matchLabels:
|
||||
app: kube-vip-demo
|
||||
@@ -30,4 +30,25 @@ spec:
|
||||
ports:
|
||||
- containerPort: 10001
|
||||
- containerPort: 10002
|
||||
status: {}
|
||||
status: {}
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: demo-service
|
||||
namespace: default
|
||||
labels:
|
||||
app: demo-service
|
||||
annotations:
|
||||
kube-vip.io/egress: "true"
|
||||
spec:
|
||||
type: LoadBalancer
|
||||
# "Local" preserves the client source IP and avoids a second hop for
|
||||
# LoadBalancer and NodePort
|
||||
externalTrafficPolicy: Local
|
||||
ports:
|
||||
- name: demo-udp
|
||||
port: 10002
|
||||
protocol: UDP
|
||||
selector:
|
||||
app: kube-vip-demo
|
||||
@@ -60,7 +60,7 @@ func main() {
|
||||
fmt.Println("error: ", err)
|
||||
}
|
||||
|
||||
ServerConn.WriteTo(buf[0:n], addr)
|
||||
ServerConn.WriteTo(buf[0:n], )
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user