落烨无痕

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

一、需求场景:同一个子网(subnet)下,子网内的虚机可以访问外网、虚机之间可以互通

二、环境准备
假设已经安装好k3s、multus、kube-ovn、kubevirt
 
本案例安装版本为:v1.26.4
安装k3s时,需要把自带的flannel CNI关掉,因为在此场景中,使用kube-ovn CNI。启动参数添加:--flannel-backend=none --disable-network-policy
 
本案例安装版本为:v0.59.0
同时安装virtctl,用于虚机完成创建后,连接进入虚机
 
本案例镜像版本为:ghcr.io/k8snetworkplumbingwg/multus-cni:snapshot
 
当前kube-ovn版本要求(v1.11.x)(最新要求以官网为准):
  • Kubernetes >= 1.16
  • Docker >= 1.12.6
  • OS: CentOS 7/8, Ubuntu 16.04/18.04
  • Other Linux distributions with geneve, openvswitch and ip_tables module installed. You can use commands modinfo geneve, modinfo openvswitch and modinfo ip_tables to verify
  • Kernel boot with ipv6.disable=0
  • Kube-proxy MUST be ready so that Kube-OVN can connect to apiserver by service address
三、搭建步骤
1.1 定义attach(att.yaml)
apiVersion: "k8s.cni.cncf.io/v1"
kind: NetworkAttachmentDefinition
metadata:
  name: kube-ovn-att1
  namespace: default
spec:
  # value:attach的name.namespace,如果type=kube-ovn,需要再加上“.ovn”
  config: '{
      "cniVersion": "0.3.0",
      "type": "kube-ovn",
      "server_socket": "/run/openvswitch/kube-ovn-daemon.sock",
      "provider": "kube-ovn-att1.default.ovn"
    }'

说明:

使用type=kube-ovn,不能使用type=macvlan。因为type=macvlan,虚机之间网络无法互通

官方说明:https://link.zhihu.com/?target=https%3A//kubevirt.io/user-guide/virtual_machines/interfaces_and_networks/

 

 
1.2 创建attach
kubectl apply -f att.yaml

 

1.3 验证是否创建成功
kubectl get http://networkattachmentdefinition.k8s.cni.cncf.io -A

 
2.1 定义对应的subnet(kube-ovn-subnet.yaml)
apiVersion: kubeovn.io/v1
kind: Subnet
metadata:
  name: kubeovn-subnet
spec:
  protocol: IPv4
  provider: kube-ovn-att1.default.ovn
  cidrBlock: 172.55.0.0/16
  gateway: 172.55.0.1
  excludeIps:
  - 172.55.0.1..172.55.0.10

说明:
a. provider要和步骤1中attach定义的provider保持一致。虚机为附加网卡分配ip时,就是根据provider关联subnet的。

b. 可用ip域、gateway、排除的ip段均根据实际情况,自行设定

 

2.2 创建subnet
kubectl apply -f kube-ovn-subnet.yaml

 

2.3 验证是否创建成功
kubectl get subnet

 

3.1 定义2台vm虚机(use.att.fedora.vm-kubeovnAtt1.yaml、use.att.fedora.vm-kubeovnAtt2.yaml)
用于验证虚机之间网络互通
apiVersion: kubevirt.io/v1
kind: VirtualMachine
metadata:
  name: vm.fedora.use.kubeovnatt.1
  annotations:
    k8s.v1.cni.cncf.io/networks: kube-ovn-att1
spec:
  running: true
  template:
    metadata:
      labels:
        kubevirt.io/size: small
        kubevirt.io/domain: vm.danny
    spec:
      domain:
        devices:
          disks:
            - name: containerdisk
              disk:
                bus: virtio
            - name: cloudinitdisk
              disk:
                bus: virtio
          interfaces:
          - name: default
            bridge: {}
          - name: eth1
            bridge: {}
        resources:
          requests:
            memory: 1024M
      networks:
      - name: default
        pod: {}
      - name: eth1
        multus:
          networkName: default/kube-ovn-att1
      volumes:
        - name: containerdisk
          containerDisk:
            image: kubevirt/fedora-cloud-registry-disk-demo
        - name: cloudinitdisk
          cloudInitNoCloud:
            userData: |
              #!/bin/bash
              echo "fedora" | passwd fedora --stdin
              dhclient eth1
apiVersion: kubevirt.io/v1
kind: VirtualMachine
metadata:
  name: vm.fedora.use.kubeovnatt.2
  annotations:
    k8s.v1.cni.cncf.io/networks: kube-ovn-att1
spec:
  running: true
  template:
    metadata:
      labels:
        kubevirt.io/size: small
        kubevirt.io/domain: vm.danny
    spec:
      domain:
        devices:
          disks:
            - name: containerdisk
              disk:
                bus: virtio
            - name: cloudinitdisk
              disk:
                bus: virtio
          interfaces:
          - name: default
            bridge: {}
          - name: eth1
            bridge: {}
        resources:
          requests:
            memory: 1024M
      networks:
      - name: default
        pod: {}
      - name: eth1
        multus:
          networkName: default/kube-ovn-att1
      volumes:
        - name: containerdisk
          containerDisk:
            image: kubevirt/fedora-cloud-registry-disk-demo
        - name: cloudinitdisk
          cloudInitNoCloud:
            userData: |
              #!/bin/bash
              echo "fedora" | passwd fedora --stdin
              dhclient eth1

说明:

fedora:

dhclient eth1 # 虚机启动时,配置并开启附属网卡。否则附属网卡无法完成ip配置。不同操作系统,配置不一样。需要去对应的官网查看

 

3.2 创建vm虚机
kubectl apply -f use.att.fedora.vm-kubeovnAtt1.yaml

kubectl apply -f use.att.fedora.vm-kubeovnAtt2.yaml

 

3.3 验证虚机是否创建成功
kubectl get vm

kubectl get pod

 

3.4 进入虚机,验证是否能访问外网,能否互通
virtctl console vm.fedora.use.kubeovnatt.1

virtctl console vm.fedora.use.kubeovnatt.2

说明:virtctl需要提前安装

账号密码在定义虚机的yaml中指定(fedora:fedora)

证明attach已经成功分配给虚机

 

证明attach已经成功分配给虚机

 

ping 百度能成功返回数据包,证明已通外网 

 

 ping 百度能成功返回数据包,证明已通外网

 

两台虚机通过attach网段相互ping对方都能成功收到数据包,证明虚机之间的attach网络互通

 

 两台虚机通过attach网段相互ping对方都能成功收到数据包,证明虚机之间的attach网络互通

 

安装过程中可能遇到的问题:

问题一:

error adding container to network "macvlan": DelegateAdd: cannot set "macvlan" interface name to "net1": validateIfName: interface name net1 already exists

解决方案:/etc/cni/net.d/00-multus.conf 自动维护有bug,删除让其自动重建【目前是手工删除。最好通过定时器删除】

参考:


问题二:

附加网卡ping不通同网段机器(attach的type=macvlan)

解决方案:macvlan CNI就是无法互通,attach使用kube-ovn即可

 

posted on 2023-08-25 14:43  落烨无痕  阅读(812)  评论(0编辑  收藏  举报