Open/listen one port on the container for test

Open/listen one port on the container for test

Example: open port 1234 with the command, nc.


//In one cloud shell terminal;

tom@Azure:~$ kubectl run nginx --image=nginx 
tom@Azure:~$ kubectl exec nginx -it /bin/bash 
root@nginx:/#  
root@nginx:/# apt update 
…… 
root@nginx:/# apt install net-tools 
…… 
root@nginx:/# netstat -tulpn | grep LISTEN 
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1/nginx: master pro  
tcp6       0      0 :::80                   :::*                    LISTEN      1/nginx: master pro 
root@nginx:/# apt install netcat    
…… 
root@nginx:/# nc -4 -l 1234                            //It’s running. The command can be terminated by Ctrl+C at the end of test 

//Open one new cloud shell terminal;

tom@Azure:~$ kubectl exec nginx -it /bin/bash 
root@nginx:/# netstat -tulpn | grep LISTEN                                      // We can see the port 1234 is opened/listened by the process nc 
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1/nginx: master pro  
tcp        0      0 0.0.0.0:1234            0.0.0.0:*               LISTEN      378/nc               
tcp6       0      0 :::80                   :::*                    LISTEN      1/nginx: master pro  
root@nginx:/# 
root@nginx:/# exit 
exit 
tom@Azure:~$ kubectl run nginx2 --image=nginx                     // Create a new pod to test the port with telnet 
pod/nginx2 created 
tom@Azure:~$ kubectl get pod -o wide 
NAME                                                    READY   STATUS    RESTARTS   AGE     IP            NODE                                NOMINATED NODE   READINESS GATES 
azure-vote-back-5f46f8f4cc-xzs74                        1/1     Running   0          2d22h   10.240.0.66   aks-nodepool1-27087028-vmss00004h               
azure-vote-front-5d68fcf586-7mfv7                       1/1     Running   0          8h      10.240.0.10   aks-nodepool1-27087028-vmss00004g               
nginx                                                   1/1     Running   0          71m     10.240.0.83   aks-nodepool1-27087028-vmss00004h               
nginx2                                                  1/1     Running   0          61s     10.240.0.65   aks-nodepool1-27087028-vmss00004h               
node-debugger-aks-nodepool1-27087028-vmss00004g-6bgz9   1/1     Running   0          51m     10.240.0.4    aks-nodepool1-27087028-vmss00004g               
php-apache-d4cf67d68-mcs5t                              1/1     Running   0          72m     10.240.0.75   aks-nodepool1-27087028-vmss00004h               
tom@Azure:~$ 
tom@Azure:~$ kubectl exec nginx2 -it /bin/bash 
root@nginx2:/# 
root@nginx2:/# apt update 
…… 
root@nginx2:/# apt install telnet 
…… 
root@nginx2:/# telnet 10.240.0.83 1234                        // This port can also be telneted from another pod 
Trying 10.240.0.83... 
Connected to 10.240.0.83. 
Escape character is '^]'. 

More reference:

How to create a TCP listener

你可能感兴趣的:(Linux,K8s,Docker,运维,linux,kubernetes)