I am using AKS in Azure and I applied an nginx server in a Pod in a node in the AKS, and i want to be able to call this pod IP address to get the web content.
The Problem is that after every deployment of the AKS and the Pods in it the IP addresses of the Pods change. And everytime I need to figure out what is the IP Address and the call it. This is bad for automation.
How can I set for a Pod a static IP Address, which I can rely on calling it and getting response?
If I cant set a static IP address how can I solve the problem ?
I am expecting to get one IP Address to call it and to get the web content.
1
David’s suggestion to use a Service rather than direct Pod connectivity is indeed the recommended approach. If you require external access to these services, leveraging NodePort, LoadBalancer, or Ingress resources as appropriate will provide controlled and reliable access.
Avoid Direct Connections to Pods
Why?
Pods in Kubernetes are ephemeral, meaning they can be restarted, rescheduled, or deleted at any time during scaling operations or node maintenance. Each time a Pod is restarted or rescheduled, it’s likely to receive a new IP address.
Why use Services?
A Service in Kubernetes provides a consistent IP address and DNS name that remains stable, regardless of changes in the backend Pods. This abstraction allows applications to maintain a consistent point of access.
Therefore, to address your scenario where you want a consistent IP address to access an Nginx server hosted on AKS, you’ll use Kubernetes Services and possibly Ingress (if you need access from outside the cluster).
Example-
First, you’ll need a Deployment that runs your Nginx server.
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
To create a stable endpoint for your Nginx server, define a Service that targets the Nginx Pod. This Service will provide a stable, internal IP address within the cluster and load balance requests across the Nginx Pods.
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
type: ClusterIP
If you need to access the Nginx server from outside the AKS cluster, you can use a LoadBalancer Service. Modify the Service definition as below in that case. Basically just change the type to Loadbalancer
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer
and accordingly save all these files using kubectl apply -f <filename.yaml>