kube-eip出现背景
kubevirt中虚机是跑在virt-launcher的pod中的qemu进程。默认通过pod ip访问虚机,所以当虚机发生重启,迁移后pod ip将会发生变化。
但是对于虚机而言,使用固定ip,并且ip可以被云外网络访问是很普遍的需求。而kubevirt虚机使用pod ip访问,首先pod ip属于k8s内部网络无法被云外访问,并且当pod发生重启后pod ip将会变化,所以现有网络无法满足需求。并且对于某些嵌入式系统,无法支持多网卡。
kubevirt默认网络
virt-launcher的pod中虚机的tap设备接在linux bridge上,通过nat将pod veth设备的ip和虚机tap设备的ip做转换,以此通过访问pod ip来访问虚机。
kube-eip实现的eip方案
在vmi所在宿主机上通过在iptables的nat表新增KUBE-EIP-PREROUTING和KUBE-EIP-POSTRONGTING链来对进出虚机的流量做劫持。对于云外访问的流量,根据iptables规则做nat转换,从而实现通过eip来访问虚机。
新增k8s_internal_net的ipset集合用于区分云内外的流量。并新增kube-eip-eip,kube-eip-vmi两个ipset集合来匹配已经做了eip绑定的流量。并配合策略路由实现虚机访问云外流量通过eip走public network出去。
如果public network对接了交换机,可以通过bgp来同步eip路由给外部。
kube-eip的实现
kube-eip提供以下三个组件
- eipbinding-operator: k8s的operator用于监听eipbinding和kubevirt vmi的crd资源变化来通知agent更新eip规则
- eip-agent: 以daemonset的形式运行与每个k8s节点,接收operator的grpc请求来下发或者清理eip转换规则
- eipctl: 命令行工具用于绑定或者解绑eip
kube-eip部署及使用
# 克隆kube-eip项目
git clone https://github.com/lucheng0127/kube-eip.git
# 部署operator和agent
IMG=quay.io/shawnlu0127/eipbinding_operator:20231130 make deploy
make deploy-agent
# 清理
make undeploy
make undeploy-agent
目前稳定版operator和agent镜像
- quay.io/shawnlu0127/eipbinding_operator:20231130
- quay.io/shawnlu0127/eip_agent:20231204
绑定eip给kubevirt虚机
apiVersion: extension.lucheng0127/v1
kind: EipBinding
metadata:
labels:
app.kubernetes.io/name: eipbinding
app.kubernetes.io/instance: cirros
app.kubernetes.io/part-of: kube-eip
app.kubernetes.io/managed-by: kustomize
app.kubernetes.io/created-by: kube-eip
name: cirros
spec:
eipAddr: 192.168.18.15
vmiName: cirros
定义eipbinding的crd的yaml, vmiName为kubevirt vmi的名字,eipAddr为eip的ip地址,后创建eipbinding的crd即可。解绑删除eipbinding的crd即可。
root@shawn-server:~# kubectl get eipbinding cirros -o yaml
apiVersion: extension.lucheng0127/v1
kind: EipBinding
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"extension.lucheng0127/v1","kind":"EipBinding","metadata":{"annotations":{},"labels":{"app.kubernetes.io/created-by":"kube-eip","app.kubernetes.io/instance":"cirros","app.kubernetes.io/managed-by":"kustomize","app.kubernetes.io/name":"eipbinding","app.kubernetes.io/part-of":"kube-eip"},"name":"cirros","namespace":"default"},"spec":{"eipAddr":"192.168.18.15","vmiName":"cirros"}}
creationTimestamp: "2023-12-04T07:24:21Z"
finalizers:
- extension.lucheng0127/finlizer
generation: 4
labels:
app.kubernetes.io/created-by: kube-eip
app.kubernetes.io/instance: cirros
app.kubernetes.io/managed-by: kustomize
app.kubernetes.io/name: eipbinding
app.kubernetes.io/part-of: kube-eip
name: cirros
namespace: default
resourceVersion: "10121351"
uid: 3e980554-7fd1-4d94-9b57-d2179211c372
spec:
currentHyper: 172.17.15.125
currentIPAddr: 10.244.3.88
eipAddr: 192.168.18.15
lastHyper: 172.17.15.133
lastIPAddr: 10.244.0.91
phase: Ready
vmiName: cirros