<p>need an advice. There’s a need to expose every pod individually to be externally reachable by some fqdn:port. It’s needed because pod is running stateful workload (media session).
It should be possible to run more than one pod on the node.</p>
<p>What is the scalable way to make every pod reachable and addressable?
What if it’s not necessarily HTTPS traffic that needs to be addressed to specific pod, but just TCP?</p>

miraer
<p>here is my idea</p>
<ol>
<li>create a headless service, then you can access <code>pod-0.my-svc.my-ns.svc</code> from other pod.</li>
<li>create a proxy(nginx), config rewrite rule to rewrite <code>http://my-domain.com/pod-0</code> to <code>http://pod-0.my-svc.my-ns.svc/</code></li>
<li>expose proxy(nginx) to <code>http://my-domain.com/</code></li>
</ol>
<hr />
<p>I make a example for http, access <code>http://localhost:30000/nginx-sts-2/</code>
will response by pod <code>nginx-sts-2</code>. TCP should be similar but need to route by sub domain instead of path.</p>
<p>Here is that statfulset only response their pod name</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: nginx-sts
spec:
  replicas: 3
  serviceName: nginx-sts-service
  selector:
    matchLabels:
      app: nginx-sts
  template:
    metadata:
      labels:
        app: nginx-sts
    spec:
      containers:
      - name: nginx-sts
        image: nginx
        ports:
        - containerPort: 80
        volumeMounts:
        - name: nginx-sts-config
          mountPath: /etc/nginx/conf.d
      volumes:
      - name: nginx-sts-config
        configMap:
          name: nginx-sts-configmap
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-sts-configmap
data:
  nginx.conf: |
    server {
        listen 80;
        location / {
            return 200 &quot;$hostname\n&quot;;
        }
    }
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-sts-service
spec:
  clusterIP: None
  selector:
    app: nginx-sts
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
</code></pre>
<p>here is the proxy that route <code>http://localhost:30000/nginx-sts-0</code> to pod <code>nginx-sts-0</code></p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-proxy
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-proxy
  template:
    metadata:
      labels:
        app: nginx-proxy
    spec:
      containers:
      - name: nginx-proxy
        image: nginx
        ports:
        - containerPort: 80
        volumeMounts:
        - name: nginx-proxy-config
          mountPath: /etc/nginx/conf.d
      volumes:
      - name: nginx-proxy-config
        configMap:
          name: nginx-proxy-configmap
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-proxy-configmap
data:
  nginx.conf: |
    server {
        resolver kube-dns.kube-system.svc.cluster.local valid=5s;
        listen 80;
        location ~ ^/nginx-sts-(\d+)/(.*)$ {
            set $backend http://nginx-sts-$1.nginx-sts-service.default.svc.cluster.local:80/$2;
            proxy_pass $backend;
        }
    }
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-proxy-service
spec:
  selector:
    app: nginx-proxy
  type: NodePort
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
      nodePort: 30000
</code></pre>
