Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
salomon1184 opened this issue Nov 1, 2024 · 0 comments

Comments

@salomon1184
Copy link

salomon1184 commented Nov 1, 2024

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,LOWER_UP> 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant