Kubernetes nginx refresh ip address when upstream service IP changes
Best way is to use an DNS sidecar on your nginx pod as below:
apiVersion: v1kind: ConfigMapmetadata: namespace: issue-795 name: nginx-configdata: nginx.conf: |- user nginx; worker_processes 1; events { worker_connections 4096; ## Default: 1024 } http { server { # php/fastcgi listen 80; resolver 127.0.0.1:53 ipv6=off valid=10s; set $upstream http://backend:8080; location / { proxy_pass $upstream; proxy_http_version 1.1; } } }---apiVersion: extensions/v1beta1kind: Deploymentmetadata: namespace: issue-795 name: proxyspec: replicas: 1 template: metadata: labels: app: proxy spec: containers: - name: nginx image: nginx ports: - containerPort: 80 volumeMounts: - name: nginx-config mountPath: /etc/nginx/nginx.conf subPath: nginx.conf - name: dnsmasq image: "janeczku/go-dnsmasq:release-1.0.7" args: - --listen - "127.0.0.1:53" - --default-resolver - --append-search-domains volumes: - name: nginx-config configMap: name: nginx-config---apiVersion: v1kind: Servicemetadata: namespace: issue-795 name: backendspec: ports: - port: 80 targetPort: 8080 clusterIP: None selector: app: backend---apiVersion: apps/v1kind: StatefulSetmetadata: name: backend namespace: issue-795spec: serviceName: "backend" replicas: 2 selector: matchLabels: app: backend template: metadata: labels: app: backend spec: containers: - name: echoserver image: gcr.io/google_containers/echoserver:1.4 imagePullPolicy: Always ports: - containerPort: 8080
I would recommand the use of Ingress resource on Kubernetes with the Nginx Ingress Controller.
Its whole purpose is to have a proxy inside your Kubernetes cluster that redirects the traffic to ClusterIP Services.
So you only have one external ELB that redirects all the traffic into your Kubernetes cluster. The Ingress Controller then redirects the traffic to different services.
For more advanced ingress controllers, you can look at Kong Ingress Controller.