Skip to content

Instantly share code, notes, and snippets.

@aojea
Created August 29, 2024 17:23
Show Gist options
  • Save aojea/5c91922516397cb6ebc63a9945f2ca3d to your computer and use it in GitHub Desktop.
Save aojea/5c91922516397cb6ebc63a9945f2ca3d to your computer and use it in GitHub Desktop.
Running a Pod with A Predefined Mac Address

Running a Pod with a predefined MAC address

There are situation that, for reasons, you want your Pod to have a static MAC.

Since the Pod uses a veth pair, the application running inside the Pod sees the side of the veth that is on its network namespace.

Using an initContainer with enough privileges, we can change the MAC address before the application runs.

  initContainers:
  - name: static-mac
    image: busybox:1.28
    command: ['sh', '-c', "ip link set dev eth0 address 00:11:22:33:44:55"]
    securityContext:
      privileged: true

Apply the manifest with the example:

kubectl apply -f pod-static-mac.yaml
pod/staticmac created

Wait until the Pod is running

 kubectl get pod
NAME                 READY   STATUS    RESTARTS      AGE
staticmac            1/1     Running   0             6s

Check the MAC address is the one we defined

kubectl exec -it staticmac ip a
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
Defaulted container "agnhost" out of: agnhost, static-mac (init)
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0@if95: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
    link/ether 00:11:22:33:44:55 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 10.244.1.94/24 brd 10.244.1.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::888:19ff:fe25:d8ff/64 scope link
       valid_lft forever preferred_lft forever
apiVersion: v1
kind: Pod
metadata:
name: staticmac
spec:
initContainers:
- name: static-mac
image: busybox:1.28
command: ['sh', '-c', "ip link set dev eth0 address 00:11:22:33:44:55"]
securityContext:
privileged: true
containers:
- name: agnhost
image: registry.k8s.io/e2e-test-images/agnhost:2.39
args:
- netexec
- --http-port=80
- --udp-port=80
ports:
- containerPort: 80
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment