How to configure an echo pod on different port instead of port 80 in kubernetes

6/29/2020
apiVersion: v1
    kind: Pod
    metadata:
       name: echo-pod
       namespace: echo
    spec:
       containers:
       - name: nginx
         image: nginx
         ports:
            -  containerPort: 81

Tried to connect on port 81, by running above yaml file, but it is still connecting on port 80. Checked connection using telnet

The ip of echo -pod is 192.168.211.1

Able to connect echo pod from busybox pod(which is already exists) on port 80 but not on port 81. you can observe following

root@ip-172-31-16-143:~# kubectl exec busybox -- telnet 192.168.211.1 80
Connected to 192.168.211.1

root@ip-172-31-16-143:~# kubectl exec busybox -- telnet 192.168.211.1 81
telnet: can't connect to remote host (192.168.211.1): Connection refused
command terminated with exit code 1
-- Ramreddy Amanaganti
kubernetes

2 Answers

6/29/2020

By defining containerPort: 81 on your pod template, you are only changing the exposed container port, but your Nginx server will still listen on port 80 (as it is configured by default).

You need to change the Nginx listen configuration to match your new exposed port.

Different from mostly docker implementations, Nginx doesn't support such configs by using environment variables (see Using environment variables in Nginx configuration on their Docker Hub page).

If you wish to adapt the Nginx default configuration, you need to create a new nginx.conf with the listen 81; config, then replace the original using COPY in your Dockerfile to create a custom image FROM nginx.

If your prefer an "one-line workaround" still using the original Nginx image, you can change the command/args to replace the listen config on every start:

  containers:
  - name: nginx
    image: nginx
    command: ["/bin/sh","-c"]
    args: ["sed -i 's/listen  .*/listen 81;/g' /etc/nginx/conf.d/default.conf && exec nginx -g 'daemon off;'"]
    ports:
    - containerPort: 81
-- Eduardo Baitello
Source: StackOverflow

6/29/2020

Use Kubernetes service(cluster IP type) for connecting from one pod to another pod.

Create a service as below.

apiVersion: v1
kind: Service
metadata:
  name: my-service
  namespace: echo
spec:
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 81
      targetPort: 80 

Add a label app: nginx to the nginx pod so that the service selects the pod as backend.

apiVersion: v1
kind: Pod
metadata:
  name: echo-pod
  namespace: echo
  labels:
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx
    ports:
    - containerPort: 80

Then you can use the cluster IP of the service or better yet DNS of the service and port 81 to connect to the nginx pod from another pod.

To check the cluster IP run kubectl get svc my-service -n echo

The DNS will be my-service.echo.svc.cluster.local

Then you can use clusterip:81 or my-service.echo.svc.cluster.local:81 to connect to nginx pod from another pod.

-- Arghya Sadhu
Source: StackOverflow