cni-genie / CNI-Genie

CNI-Genie for choosing pod network of your choice during deployment time. Supported pod networks - Calico, Flannel, Romana, Weave
https://github.com/cni-genie/CNI-Genie/
Apache License 2.0
535 stars 121 forks source link

macvlan static pod IP address #200

Open vduduh opened 4 years ago

vduduh commented 4 years ago

How to add static IP address to pod with annotations fields? Example:

apiVersion: v1
kind: Pod
metadata:
  annotations:
    cni: macvlan,flannel
    multi-ip-preferences: '{"ips": {"macvlan":{"ip":"10.10.10.10"}}}'
  name: some-worker
spec:
  containers:
  - args:
vduduh commented 4 years ago

Resolved. Create network attachment definition:

---
apiVersion: "k8s.cni.cncf.io/v1"
kind: NetworkAttachmentDefinition
metadata:
  name: flannel-conf
spec: 
  config: '{
    "name": "cbr0",
    "cniVersion": "0.3.1",
    "plugins": [
      {
        "type": "flannel",
        "delegate": {
          "hairpinMode": true,
          "ipMasq": false,
          "isDefaultGateway": true
        }
      },
      {
        "type": "portmap",
        "capabilities": {
          "portMappings": true
        }
      }
    ]
  }'

Add annotation to pod:

    k8s.v1.cni.cncf.io/networks: |
      [
        {
          "name":"macvlan-conf",
          "interface": "eth1",
          "ips": ["{{ .pod_ip }}"]
        },
        {
          "name":"flannel-conf",
          "interface": "eth2"
        }
      ]

BUT In fact, there will be three interfaces in the container, those that we specified, and the default interface specified in genie. How to remove default from container? Workaround:

...
spec:
  containers:
  - args:
    - -c
    - 'ip route replace default via $(ip r|egrep 18.*via.*eth2|cut -f 3 -d" "); /sbin/init'
    command:
    - /bin/sh
    securityContext:
      privileged: true // not secure
...