Skip to content

Commit

Permalink
Merge pull request #527 from TrekkieCoder/main
Browse files Browse the repository at this point in the history
PR - Added CICD scenario for multus with k3s
  • Loading branch information
UltraInstinct14 authored Feb 10, 2024
2 parents 2ee2529 + f7a0286 commit 0c643a1
Show file tree
Hide file tree
Showing 16 changed files with 745 additions and 0 deletions.
52 changes: 52 additions & 0 deletions cicd/k3s-flannel-multus/Vagrantfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :

workers = (ENV['WORKERS'] || "1").to_i
#box_name = (ENV['VAGRANT_BOX'] || "ubuntu/focal64")
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 "bastion" do |bastion|
bastion.vm.hostname = 'bastion'
bastion.vm.network :private_network, ip: "4.0.5.3", :netmask => "255.255.255.0"
bastion.vm.provision :shell, :path => "bastion.sh"
bastion.vm.provider :virtualbox do |vbox|
vbox.customize ["modifyvm", :id, "--memory", 2048]
vbox.customize ["modifyvm", :id, "--cpus", 2]
vbox.customize ["modifyvm", :id, "--nicpromisc2", "allow-all"]
end
end

config.vm.define "loxilb" do |loxilb|
loxilb.vm.hostname = 'llb1'
loxilb.vm.network :private_network, ip: "4.0.5.2", :netmask => "255.255.255.0"
loxilb.vm.network :private_network, ip: "4.0.6.149", :netmask => "255.255.255.0"
loxilb.vm.provision :shell, :path => "loxilb.sh"
loxilb.vm.provider :virtualbox do |vbox|
vbox.customize ["modifyvm", :id, "--memory", 6000]
vbox.customize ["modifyvm", :id, "--cpus", 4]
vbox.customize ["modifyvm", :id, "--nicpromisc2", "allow-all"]
vbox.customize ["modifyvm", :id, "--nicpromisc3", "allow-all"]
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.network :private_network, ip: "4.0.6.150", :netmask => "255.255.255.0"
master.vm.provision :shell, :path => "master.sh"
master.vm.provider :virtualbox do |vbox|
vbox.customize ["modifyvm", :id, "--memory", 4096]
vbox.customize ["modifyvm", :id, "--cpus", 4]
vbox.customize ["modifyvm", :id, "--nicpromisc2", "allow-all"]
vbox.customize ["modifyvm", :id, "--nicpromisc3", "allow-all"]
end
end
end
2 changes: 2 additions & 0 deletions cicd/k3s-flannel-multus/bastion.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
apt-get update
apt-get install -y software-properties-common curl wget
3 changes: 3 additions & 0 deletions cicd/k3s-flannel-multus/config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
vagrant global-status | grep -i virtualbox | cut -f 1 -d ' ' | xargs -L 1 vagrant destroy -f
vagrant up
20 changes: 20 additions & 0 deletions cicd/k3s-flannel-multus/host_validation.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash
extIP=$(cat /vagrant/extIP)

tcp_port=55002

code=0
echo Service IP: $extIP

echo "*********************************************"
for((i=0;i<20;i++))
do
out=$(curl -s --connect-timeout 10 http://$extIP:$tcp_port)
if [[ ${out} == *"Welcome to nginx"* ]]; then
echo -e "K3s-multus TCP\t($mode)\t[OK]"
else
echo -e "K3s-multus TCP\t($mode)\t[FAILED]"
code=1
fi
done
exit $code
129 changes: 129 additions & 0 deletions cicd/k3s-flannel-multus/kube-loxilb.yml
Original file line number Diff line number Diff line change
@@ -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://4.0.6.149:11111
- --externalCIDR=4.0.5.2/32
#- --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"]
13 changes: 13 additions & 0 deletions cicd/k3s-flannel-multus/loxilb.sh
Original file line number Diff line number Diff line change
@@ -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
59 changes: 59 additions & 0 deletions cicd/k3s-flannel-multus/loxilb.yml
Original file line number Diff line number Diff line change
@@ -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:debug"
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
29 changes: 29 additions & 0 deletions cicd/k3s-flannel-multus/master.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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_VERSION=v1.22.9+k3s1 INSTALL_K3S_EXEC="--disable traefik --disable servicelb --disable-cloud-controller \
--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
sudo kubectl apply -f /vagrant/multus/multus-daemonset.yml
sudo kubectl apply -f /vagrant/multus/macvlan.yml
/vagrant/wait_ready.sh

sudo apt update
sudo apt install -y snapd
sudo snap install go --classic

git clone https://github.com/containernetworking/plugins.git
cd plugins
./build_linux.sh
ls bin/macvlan
sudo cp -f ./bin/macvlan /var/lib/rancher/k3s/data/current/bin/
sudo ifconfig eth2 promisc

sudo kubectl apply -f /vagrant/multus/multus-pod.yml
sudo kubectl apply -f /vagrant/multus/multus-service.yml
/vagrant/wait_ready.sh
27 changes: 27 additions & 0 deletions cicd/k3s-flannel-multus/multus/macvlan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
apiVersion: "k8s.cni.cncf.io/v1"
kind: NetworkAttachmentDefinition
metadata:
name: macvlan1
spec:
config: '{
"cniVersion": "0.3.1",
"type": "macvlan",
"master": "eth2",
"mode": "bridge",
"ipam": {
"type": "host-local",
"ranges": [
[ {
"subnet": "4.0.6.0/24",
"rangeStart": "4.0.6.3",
"rangeEnd": "4.0.6.100",
"routes": [
{
"dst": "4.0.5.0/24"
}
],
"gateway": "4.0.6.149"
} ]
]
}
}'
Loading

0 comments on commit 0c643a1

Please sign in to comment.