k8snetworkplumbingwg / multus-cni

A CNI meta-plugin for multi-homed pods in Kubernetes
Apache License 2.0
2.41k stars 584 forks source link

set a fixed MAC address for a pod in k8s cluster, but affected network communication #1350

Open salomon1184 opened 3 weeks ago

salomon1184 commented 3 weeks ago

Hi, all

I tried to use macvlan to set a fixed MAC address for a pod in my k8s cluster like this:

#generate_network_config.sh
POD_NAME=$(hostname)
LICENSE_MAC_ADDRESS=${LICENSE_MAC_ADDRESS:-""}

echo "Generating CNI config with the following MAC address: $LICENSE_MAC_ADDRESS"

cat <<EOF >/etc/cni/net.d/10-custom-net.conf
{
  "cniVersion": "0.3.1",
  "type": "macvlan",
  "master": "eth0",
  "mode": "bridge",
  "ipam": {
        "type": "dhcp"
      }
EOF

if [ "$POD_NAME" = "$TARGET_POD_NAME" ] && [ -n "$LICENSE_MAC_ADDRESS" ]; then
  cat <<EOF >>/etc/cni/net.d/10-custom-net.conf
  ,
  "mac": "$LICENSE_MAC_ADDRESS"
EOF

My deployment YAML looks like this:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: xxx-stateful
  namespace: xx-daily
  labels:
    app: xxx
spec:
  serviceName: "xxx"
  replicas: 2
  selector:
    matchLabels:
      app: xxx
  template:
    metadata:
      labels:
        app: xxx
      annotations:
        k8s.v1.cni.cncf.io/networks: '[{"name": "xxx-mac-fixed","interfaceRequest": "net1"]'
    spec:
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      initContainers:
        - name: init-network-config
          image: xxxxx-mac-fixed-init:latest
          securityContext:
            privileged: true  # Ensure initContainer has sufficient privileges
          command: ["/bin/sh", "-c", "/generate_network_config.sh"]
          env:
            - name: TARGET_POD_NAME
              value: "xxxx-stateful-1"
            - name: LICENSE_MAC_ADDRESS
              value: "D0:46:0C:8A:E8:0D"
          volumeMounts:
            - name: cni-config
              mountPath: /etc/cni/net.d

It is truly working; here is the result:

kubectl exec -it xxx-stateful-1 -n xx-daily -- /bin/sh                                                                                                     

# ip addr show eth0

2: eth0@if73: <BROADCAST,MULTICAST,UP,LOWERUP> mtu 1450 qdisc noqueue state UP group default link/ether 0:46:0c:8a:e8:0_ brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet 10.42.0.68/24 brd 10.42.0.255 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80::d246:cff:fe8a:e80d/64 scope link valid_lft forever preferred_lft forever

But my application in the pod isn't working well because it communicates with MySQL and encounters errors.

Anyone can help? Thanks very much! Sorry for my poor English. If any additional information is needed, please feel free to add comments.