From 1d58f7469bc2262adca17b3b7d12e6b31fdaba40 Mon Sep 17 00:00:00 2001 From: Trekkie Coder Date: Sun, 7 Jan 2024 10:29:16 +0900 Subject: [PATCH] cicd for kube-proxy replacement --- .../Vagrantfile | 62 +++++++++ .../k3s-flannel-cluster-ipvs-compat/config.sh | 5 + cicd/k3s-flannel-cluster-ipvs-compat/host.sh | 5 + .../iperf-service.yml | 32 +++++ .../kube-loxilb.yml | 129 ++++++++++++++++++ .../k3s-flannel-cluster-ipvs-compat/loxilb.sh | 13 ++ .../loxilb.yml | 59 ++++++++ .../k3s-flannel-cluster-ipvs-compat/master.sh | 12 ++ .../k3s-flannel-cluster-ipvs-compat/nginx.yml | 28 ++++ .../rmconfig.sh | 5 + cicd/k3s-flannel-cluster-ipvs-compat/sctp.yml | 41 ++++++ cicd/k3s-flannel-cluster-ipvs-compat/udp.yml | 30 ++++ .../validation.sh | 58 ++++++++ .../wait_ready.sh | 37 +++++ .../k3s-flannel-cluster-ipvs-compat/worker.sh | 12 ++ 15 files changed, 528 insertions(+) create mode 100644 cicd/k3s-flannel-cluster-ipvs-compat/Vagrantfile create mode 100755 cicd/k3s-flannel-cluster-ipvs-compat/config.sh create mode 100644 cicd/k3s-flannel-cluster-ipvs-compat/host.sh create mode 100644 cicd/k3s-flannel-cluster-ipvs-compat/iperf-service.yml create mode 100644 cicd/k3s-flannel-cluster-ipvs-compat/kube-loxilb.yml create mode 100644 cicd/k3s-flannel-cluster-ipvs-compat/loxilb.sh create mode 100644 cicd/k3s-flannel-cluster-ipvs-compat/loxilb.yml create mode 100644 cicd/k3s-flannel-cluster-ipvs-compat/master.sh create mode 100644 cicd/k3s-flannel-cluster-ipvs-compat/nginx.yml create mode 100755 cicd/k3s-flannel-cluster-ipvs-compat/rmconfig.sh create mode 100644 cicd/k3s-flannel-cluster-ipvs-compat/sctp.yml create mode 100644 cicd/k3s-flannel-cluster-ipvs-compat/udp.yml create mode 100755 cicd/k3s-flannel-cluster-ipvs-compat/validation.sh create mode 100755 cicd/k3s-flannel-cluster-ipvs-compat/wait_ready.sh create mode 100644 cicd/k3s-flannel-cluster-ipvs-compat/worker.sh diff --git a/cicd/k3s-flannel-cluster-ipvs-compat/Vagrantfile b/cicd/k3s-flannel-cluster-ipvs-compat/Vagrantfile new file mode 100644 index 000000000..6e11e9fbb --- /dev/null +++ b/cicd/k3s-flannel-cluster-ipvs-compat/Vagrantfile @@ -0,0 +1,62 @@ +# -*- mode: ruby -*- +# vi: set ft=ruby : + +workers = (ENV['WORKERS'] || "1").to_i +box_name = (ENV['VAGRANT_BOX'] || "sysnet4admin/Ubuntu-k8s") +box_version = "0.7.1" +Vagrant.configure("2") do |config| + config.vm.box = "#{box_name}" + config.vm.box_version = "#{box_version}" + + if Vagrant.has_plugin?("vagrant-vbguest") + config.vbguest.auto_update = false + end + + config.vm.define "host" do |host| + host.vm.hostname = 'host' + host.vm.network :private_network, ip: "192.168.90.8", :netmask => "255.255.255.0" + host.vm.provision :shell, :path => "host.sh" + host.vm.provider :virtualbox do |vbox| + vbox.memory = "4096" + vbox.cpus = "2" + vbox.default_nic_type = "virtio" + end + end + + config.vm.define "loxilb" do |loxilb| + loxilb.vm.hostname = 'llb1' + loxilb.vm.network :private_network, ip: "192.168.80.9", :netmask => "255.255.255.0" + loxilb.vm.network :private_network, ip: "192.168.90.9", :netmask => "255.255.255.0" + loxilb.vm.provision :shell, :path => "loxilb.sh" + loxilb.vm.provider :virtualbox do |vbox| + vbox.memory = "6000" + vbox.cpus = "4" + vbox.default_nic_type = "virtio" + end + end + + config.vm.define "master" do |master| + master.vm.hostname = 'master' + master.vm.network :private_network, ip: "192.168.80.10", :netmask => "255.255.255.0" + master.vm.provision :shell, :path => "master.sh" + master.vm.provider :virtualbox do |vbox| + vbox.memory = "4096" + vbox.cpus = "2" + vbox.default_nic_type = "virtio" + end + end + + (1..workers).each do |node_number| + config.vm.define "worker#{node_number}" do |worker| + worker.vm.hostname = "worker#{node_number}" + ip = node_number + 100 + worker.vm.network :private_network, ip: "192.168.80.#{ip}", :netmask => "255.255.255.0" + worker.vm.provision :shell, :path => "worker.sh" + worker.vm.provider :virtualbox do |vbox| + vbox.memory = "4096" + vbox.cpus = "2" + vbox.default_nic_type = "virtio" + end + end + end +end diff --git a/cicd/k3s-flannel-cluster-ipvs-compat/config.sh b/cicd/k3s-flannel-cluster-ipvs-compat/config.sh new file mode 100755 index 000000000..b19ef9386 --- /dev/null +++ b/cicd/k3s-flannel-cluster-ipvs-compat/config.sh @@ -0,0 +1,5 @@ +#!/bin/bash +vagrant global-status | grep -i virtualbox | cut -f 1 -d ' ' | xargs -L 1 vagrant destroy -f +vagrant up +vagrant ssh host -c 'sudo ip route add 123.123.123.0/24 via 192.168.90.9' + diff --git a/cicd/k3s-flannel-cluster-ipvs-compat/host.sh b/cicd/k3s-flannel-cluster-ipvs-compat/host.sh new file mode 100644 index 000000000..4f18105e7 --- /dev/null +++ b/cicd/k3s-flannel-cluster-ipvs-compat/host.sh @@ -0,0 +1,5 @@ +apt-get update +apt-get install -y software-properties-common +#curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - +#add-apt-repository -y "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" +apt-get install -y iperf iperf3 diff --git a/cicd/k3s-flannel-cluster-ipvs-compat/iperf-service.yml b/cicd/k3s-flannel-cluster-ipvs-compat/iperf-service.yml new file mode 100644 index 000000000..1d23f273c --- /dev/null +++ b/cicd/k3s-flannel-cluster-ipvs-compat/iperf-service.yml @@ -0,0 +1,32 @@ +apiVersion: v1 +kind: Service +metadata: + name: iperf-service + annotations: + loxilb.io/lbmode: "onearm" +spec: + externalTrafficPolicy: Local + loadBalancerClass: loxilb.io/loxilb + selector: + what: perf-test + ports: + - port: 55001 + targetPort: 5001 + type: LoadBalancer +--- +apiVersion: v1 +kind: Pod +metadata: + name: iperf1 + labels: + what: perf-test +spec: + containers: + - name: iperf + image: eyes852/ubuntu-iperf-test:0.5 + command: + - iperf + - "-s" + ports: + - containerPort: 5001 + diff --git a/cicd/k3s-flannel-cluster-ipvs-compat/kube-loxilb.yml b/cicd/k3s-flannel-cluster-ipvs-compat/kube-loxilb.yml new file mode 100644 index 000000000..5246d4bfe --- /dev/null +++ b/cicd/k3s-flannel-cluster-ipvs-compat/kube-loxilb.yml @@ -0,0 +1,129 @@ +--- +apiVersion: v1 +kind: ServiceAccount +metadata: + name: kube-loxilb + namespace: kube-system +--- +kind: ClusterRole +apiVersion: rbac.authorization.k8s.io/v1 +metadata: + name: kube-loxilb +rules: + - apiGroups: + - "" + resources: + - nodes + verbs: + - get + - watch + - list + - patch + - apiGroups: + - "" + resources: + - pods + verbs: + - get + - watch + - list + - patch + - apiGroups: + - "" + resources: + - endpoints + - services + - services/status + verbs: + - get + - watch + - list + - patch + - update + - apiGroups: + - discovery.k8s.io + resources: + - endpointslices + verbs: + - get + - watch + - list + - apiGroups: + - authentication.k8s.io + resources: + - tokenreviews + verbs: + - create + - apiGroups: + - authorization.k8s.io + resources: + - subjectaccessreviews + verbs: + - create +--- +kind: ClusterRoleBinding +apiVersion: rbac.authorization.k8s.io/v1 +metadata: + name: kube-loxilb +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: ClusterRole + name: kube-loxilb +subjects: + - kind: ServiceAccount + name: kube-loxilb + namespace: kube-system +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: kube-loxilb + namespace: kube-system + labels: + app: loxilb +spec: + replicas: 1 + selector: + matchLabels: + app: loxilb + template: + metadata: + labels: + app: loxilb + spec: + hostNetwork: true + tolerations: + - effect: NoSchedule + operator: Exists + # Mark the pod as a critical add-on for rescheduling. + - key: CriticalAddonsOnly + operator: Exists + - effect: NoExecute + operator: Exists + priorityClassName: system-node-critical + serviceAccountName: kube-loxilb + terminationGracePeriodSeconds: 0 + containers: + - name: kube-loxilb + image: ghcr.io/loxilb-io/kube-loxilb:latest + imagePullPolicy: Always + command: + - /bin/kube-loxilb + args: + - --loxiURL=http://192.168.80.9:11111 + - --externalCIDR=123.123.123.1/24 + #- --monitor + #- --setBGP + #- --setLBMode=1 + #- --config=/opt/loxilb/agent/kube-loxilb.conf + resources: + requests: + cpu: "100m" + memory: "50Mi" + limits: + cpu: "100m" + memory: "50Mi" + securityContext: + privileged: true + capabilities: + add: ["NET_ADMIN", "NET_RAW"] diff --git a/cicd/k3s-flannel-cluster-ipvs-compat/loxilb.sh b/cicd/k3s-flannel-cluster-ipvs-compat/loxilb.sh new file mode 100644 index 000000000..74e66ae9b --- /dev/null +++ b/cicd/k3s-flannel-cluster-ipvs-compat/loxilb.sh @@ -0,0 +1,13 @@ +export LOXILB_IP=$(ip a |grep global | grep -v '10.0.2.15' | grep -v '192.168.80' | awk '{print $2}' | cut -f1 -d '/') + +apt-get update +apt-get install -y software-properties-common +curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - +add-apt-repository -y "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" +apt-get update +apt-get install -y docker-ce +docker run -u root --cap-add SYS_ADMIN --restart unless-stopped --privileged -dit -v /dev/log:/dev/log --net=host --name loxilb ghcr.io/loxilb-io/loxilb:latest +echo alias loxicmd=\"sudo docker exec -it loxilb loxicmd\" >> ~/.bashrc +echo alias loxilb=\"sudo docker exec -it loxilb \" >> ~/.bashrc + +echo $LOXILB_IP > /vagrant/loxilb-ip diff --git a/cicd/k3s-flannel-cluster-ipvs-compat/loxilb.yml b/cicd/k3s-flannel-cluster-ipvs-compat/loxilb.yml new file mode 100644 index 000000000..31e609927 --- /dev/null +++ b/cicd/k3s-flannel-cluster-ipvs-compat/loxilb.yml @@ -0,0 +1,59 @@ +apiVersion: apps/v1 +kind: DaemonSet +metadata: + name: loxilb-lb + namespace: kube-system +spec: + selector: + matchLabels: + app: loxilb-app + template: + metadata: + name: loxilb-lb + labels: + app: loxilb-app + spec: + hostNetwork: true + dnsPolicy: ClusterFirstWithHostNet + tolerations: + - key: "node-role.kubernetes.io/master" + operator: Exists + - key: "node-role.kubernetes.io/control-plane" + operator: Exists + containers: + - name: loxilb-app + image: "ghcr.io/loxilb-io/loxilb:latest" + imagePullPolicy: Always + command: [ "/root/loxilb-io/loxilb/loxilb", "--bgp", "--egr-hooks", "--blacklist=veth.|flannel.|cali.|tunl.|vxlan[.]calico", "--ipvs-compat" ] + ports: + - containerPort: 11111 + - containerPort: 179 + - containerPort: 50051 + securityContext: + privileged: true + capabilities: + add: + - SYS_ADMIN +--- +apiVersion: v1 +kind: Service +metadata: + name: loxilb-lb-service + namespace: kube-system +spec: + clusterIP: None + selector: + app: loxilb-app + ports: + - name: loxilb-app + port: 11111 + targetPort: 11111 + protocol: TCP + - name: loxilb-app-bgp + port: 179 + targetPort: 179 + protocol: TCP + - name: loxilb-app-gobgp + port: 50051 + targetPort: 50051 + protocol: TCP diff --git a/cicd/k3s-flannel-cluster-ipvs-compat/master.sh b/cicd/k3s-flannel-cluster-ipvs-compat/master.sh new file mode 100644 index 000000000..b2d757a51 --- /dev/null +++ b/cicd/k3s-flannel-cluster-ipvs-compat/master.sh @@ -0,0 +1,12 @@ +export MASTER_IP=$(ip a |grep global | grep -v '10.0.2.15' | grep '192.168.80' | awk '{print $2}' | cut -f1 -d '/') + +curl -sfL https://get.k3s.io | INSTALL_K3S_EXEC="--disable traefik --disable servicelb --disable-cloud-controller --kube-proxy-arg proxy-mode=ipvs \ +--node-ip=${MASTER_IP} --node-external-ip=${MASTER_IP} \ +--bind-address=${MASTER_IP}" sh - + +echo $MASTER_IP > /vagrant/master-ip +sudo cp /var/lib/rancher/k3s/server/node-token /vagrant/node-token +sudo cp /etc/rancher/k3s/k3s.yaml /vagrant/k3s.yaml +sudo sed -i -e "s/127.0.0.1/${MASTER_IP}/g" /vagrant/k3s.yaml +sudo kubectl apply -f /vagrant/kube-loxilb.yml +/vagrant/wait_ready.sh diff --git a/cicd/k3s-flannel-cluster-ipvs-compat/nginx.yml b/cicd/k3s-flannel-cluster-ipvs-compat/nginx.yml new file mode 100644 index 000000000..e6d7ccec4 --- /dev/null +++ b/cicd/k3s-flannel-cluster-ipvs-compat/nginx.yml @@ -0,0 +1,28 @@ +apiVersion: v1 +kind: Service +metadata: + name: nginx-lb1 + annotations: + loxilb.io/lbmode: "onearm" +spec: + externalTrafficPolicy: Local + loadBalancerClass: loxilb.io/loxilb + selector: + what: nginx-test + ports: + - port: 55002 + targetPort: 80 + type: LoadBalancer +--- +apiVersion: v1 +kind: Pod +metadata: + name: nginx-test + labels: + what: nginx-test +spec: + containers: + - name: nginx-test + image: ghcr.io/loxilb-io/nginx:stable + ports: + - containerPort: 80 diff --git a/cicd/k3s-flannel-cluster-ipvs-compat/rmconfig.sh b/cicd/k3s-flannel-cluster-ipvs-compat/rmconfig.sh new file mode 100755 index 000000000..569846667 --- /dev/null +++ b/cicd/k3s-flannel-cluster-ipvs-compat/rmconfig.sh @@ -0,0 +1,5 @@ +#!/bin/bash +vagrant destroy -f worker1 +vagrant destroy -f master +vagrant destroy -f loxilb +vagrant destroy -f host diff --git a/cicd/k3s-flannel-cluster-ipvs-compat/sctp.yml b/cicd/k3s-flannel-cluster-ipvs-compat/sctp.yml new file mode 100644 index 000000000..292c2584e --- /dev/null +++ b/cicd/k3s-flannel-cluster-ipvs-compat/sctp.yml @@ -0,0 +1,41 @@ +apiVersion: v1 +kind: Service +metadata: + name: sctp-lb1 + annotations: + loxilb.io/liveness: "yes" + loxilb.io/lbmode: "onearm" +spec: + loadBalancerClass: loxilb.io/loxilb + externalTrafficPolicy: Local + selector: + what: sctp-test + ports: + - port: 55004 + protocol: SCTP + targetPort: 9999 + type: LoadBalancer +--- +apiVersion: v1 +kind: Pod +metadata: + name: sctp-test + labels: + what: sctp-test +spec: + containers: + - name: sctp-test + image: ghcr.io/loxilb-io/alpine-socat:latest + command: [ "sh", "-c"] + args: + - while true; do + socat -v -T2 sctp-l:9999,reuseaddr,fork system:"echo 'server1'; cat"; + sleep 20; + done; + ports: + - containerPort: 9999 + env: + - name: MY_POD_IP + valueFrom: + fieldRef: + fieldPath: status.podIP diff --git a/cicd/k3s-flannel-cluster-ipvs-compat/udp.yml b/cicd/k3s-flannel-cluster-ipvs-compat/udp.yml new file mode 100644 index 000000000..d56720dee --- /dev/null +++ b/cicd/k3s-flannel-cluster-ipvs-compat/udp.yml @@ -0,0 +1,30 @@ +apiVersion: v1 +kind: Service +metadata: + name: udp-lb1 + annotations: + loxilb.io/liveness: "yes" + loxilb.io/lbmode: "onearm" +spec: + loadBalancerClass: loxilb.io/loxilb + externalTrafficPolicy: Local + selector: + what: udp-test + ports: + - port: 55003 + protocol: UDP + targetPort: 33333 + type: LoadBalancer +--- +apiVersion: v1 +kind: Pod +metadata: + name: udp-test + labels: + what: udp-test +spec: + containers: + - name: udp-test + image: ghcr.io/loxilb-io/udp-echo:latest + ports: + - containerPort: 33333 diff --git a/cicd/k3s-flannel-cluster-ipvs-compat/validation.sh b/cicd/k3s-flannel-cluster-ipvs-compat/validation.sh new file mode 100755 index 000000000..c96f4ff18 --- /dev/null +++ b/cicd/k3s-flannel-cluster-ipvs-compat/validation.sh @@ -0,0 +1,58 @@ +#!/bin/bash +source ../common.sh +echo k3s-flannel-cluster + +if [ "$1" ]; then + KUBECONFIG="$1" +fi + +# Set space as the delimiter +IFS=' ' + +sleep 45 +extIP="123.123.123.1" +echo $extIP + +echo "Service Info" +vagrant ssh master -c 'sudo kubectl get svc' +echo "LB Info" +vagrant ssh loxilb -c 'sudo docker exec -i loxilb loxicmd get lb -o wide' +echo "EP Info" +vagrant ssh loxilb -c 'sudo docker exec -i loxilb loxicmd get ep -o wide' + +print_debug_info() { + echo "llb1 route-info" + vagrant ssh loxilb -c 'ip route' + vagrant ssh master -c 'sudo kubectl get pods -A' + vagrant ssh master -c 'sudo kubectl get svc' + vagrant ssh master -c 'sudo kubectl get nodes' +} + +out=$(curl -s --connect-timeout 10 http://$extIP:55002) +if [[ ${out} == *"Welcome to nginx"* ]]; then + echo "k3s-flannel-cluster (kube-loxilb) tcp [OK]" +else + echo "k3s-flannel-cluster (kube-loxilb) tcp [FAILED]" + print_debug_info + exit 1 +fi + +out=$(timeout 10 ../common/udp_client $extIP 55003) +if [[ ${out} == *"Client"* ]]; then + echo "k3s-flannel-cluster (kube-loxilb) udp [OK]" +else + echo "k3s-flannel-cluster (kube-loxilb) udp [FAILED]" + print_debug_info + exit 1 +fi + +out=$(timeout 10 ../common/sctp_client 192.168.90.1 41291 $extIP 55004) +if [[ ${out} == *"server1"* ]]; then + echo "k3s-flannel-cluster (kube-loxilb) sctp [OK]" +else + echo "k3s-flannel-cluster (kube-loxilb) sctp [FAILED]" + print_debug_info + exit 1 +fi + +exit diff --git a/cicd/k3s-flannel-cluster-ipvs-compat/wait_ready.sh b/cicd/k3s-flannel-cluster-ipvs-compat/wait_ready.sh new file mode 100755 index 000000000..5ff06e373 --- /dev/null +++ b/cicd/k3s-flannel-cluster-ipvs-compat/wait_ready.sh @@ -0,0 +1,37 @@ +#!/bin/bash + +function wait_cluster_ready { + Res=$(sudo kubectl get pods -A | + while IFS= read -r line; do + if [[ "$line" != *"Running"* && "$line" != *"READY"* ]]; then + echo "not ready" + return + fi + done) + if [[ $Res == *"not ready"* ]]; then + return 1 + fi + return 0 +} + +function wait_cluster_ready_full { + i=1 + nr=0 + for ((;;)) do + wait_cluster_ready + nr=$? + if [[ $nr == 0 ]]; then + echo "Cluster is ready" + break + fi + i=$(( $i + 1 )) + if [[ $i -ge 40 ]]; then + echo "Cluster is not ready.Giving up" + exit 1 + fi + echo "Cluster is not ready...." + sleep 10 + done +} + +wait_cluster_ready_full diff --git a/cicd/k3s-flannel-cluster-ipvs-compat/worker.sh b/cicd/k3s-flannel-cluster-ipvs-compat/worker.sh new file mode 100644 index 000000000..75c49ddcd --- /dev/null +++ b/cicd/k3s-flannel-cluster-ipvs-compat/worker.sh @@ -0,0 +1,12 @@ +export WORKER_ADDR=$(ip a |grep global | grep -v '10.0.2.15' | grep '192.168.80' | awk '{print $2}' | cut -f1 -d '/') +export MASTER_ADDR=$(cat /vagrant/master-ip) +export NODE_TOKEN=$(cat /vagrant/node-token) + +sudo mkdir -p /etc/rancher/k3s +sudo cp -f /vagrant/k3s.yaml /etc/rancher/k3s/k3s.yaml +curl -sfL https://get.k3s.io | K3S_URL="https://${MASTER_ADDR}:6443" K3S_TOKEN="${NODE_TOKEN}" INSTALL_K3S_EXEC="--node-ip=${WORKER_ADDR} --node-external-ip=${WORKER_ADDR} --kube-proxy-arg proxy-mode=ipvs" sh - +#sudo kubectl apply -f /vagrant/nginx.yml +#sudo kubectl apply -f /vagrant/udp.yml +sudo kubectl apply -f /vagrant/iperf-service.yml +sudo kubectl apply -f /vagrant/loxilb.yml +/vagrant/wait_ready.sh