For instance, in a Kubernetes cluster environment created using kubeadm, there are management Pods with the same IP address as the host's IP address like below.
[root@master1 ~]# kubectl get pods -n kube-system -o wide
NAME READY STATUS RESTARTS AGE IP NODE
etcd-master1 1/1 Running 13 49d 10.91.111.113 master1
kube-apiserver-master1 1/1 Running 18 49d 10.91.111.113 master1
kube-controller-manager-master1 1/1 Running 29 49d 10.91.111.113 master1
kube-proxy-6vrvb 1/1 Running 13 81d 10.91.111.113 master1
kube-proxy-gsxcg 1/1 Running 13 81d 10.91.111.114 worker1
kube-proxy-lcjvw 1/1 NodeLost 1 64d 10.91.111.115 worker2
kube-scheduler-master1 1/1 Running 26 49d 10.91.111.113 master1
I would like to know how to write a yaml file for assigning the same IP address as the host to pod.
on your podSpec add hostNetwork: true
You cannot actually bind a same address for your pod, but you can user HostNetwork
in your pod, which is actually will bind ports of your container to IP of your host machine one-to-one. That is actually is Docker feature.
Here is an example of a Pod yaml with that option enabled and Pod will bind port 80 on the host:
apiVersion: v1 kind: Pod metadata: name: myapp spec: hostNetwork: true containers: - name: myapp image: myapp:latest ports: - containerPort: 80
But if you just want to access to your app in cluster using host's IP, it is not necessary to use that option. You can create Service
with NodePort
type, which will provide access to pods with your service. From official documentation:
apiVersion: v1 kind: Service metadata: name: myapp labels: run: myapp spec: type: NodePort ports: - port: 80 protocol: TCP name: http selector: run: myapp
In that case you will able to connect to any node on your cluster to port 80 and that request will be routed to your app.