<p>I created a server pod using the following yaml file :</p>
<pre><code>apiVersion: v1
kind: Pod
 metadata:
   name: server
   labels:
     app: server
 spec:
   containers:
     - name: server
       imagePullPolicy: Never
       image: localhost:5000/server
       command: ['sh', '-c', 'echo Hello Kubernetes! &amp;&amp; sleep 3600']
       ports:
         - containerPort: 80
</code></pre>
<p>I created a service for this pod using following yaml file :</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
        name: server-svc
        labels:
                app: server
spec:
        ports:
        - port: 80
          protocol: TCP
        selector:
                app: server
</code></pre>
<p>I created a client pod using the following yaml file in the same cluster as server pod:</p>
<pre><code> apiVersion: v1
 kind: Pod
 metadata:
   name: client
   labels:
     app: client
 spec:
   containers:
     - name: client
       imagePullPolicy: Never
       image: localhost.localdomain:5000/client
       command: ['sh', '-c', 'echo Hello Kubernetes! &amp;&amp; sleep 3600']
       ports:
         - containerPort: 80
</code></pre>
<p>I want to verify if connecting to this server-svc service is enabling connecting to server pod.</p>
<p>For the I exec into client pod and server pod and tried to establish a tcp socket connection between the two using the following files.</p>
<p>server.c inside the server pod.</p>
<pre><code>#include &lt;stdio.h&gt; 
#include &lt;netdb.h&gt; 
#include &lt;netinet/in.h&gt; 
#include &lt;stdlib.h&gt; 
#include &lt;string.h&gt; 
#include &lt;sys/socket.h&gt; 
#include &lt;sys/types.h&gt; 
#define MAX 80 
#define PORT 8080 
#define SA struct sockaddr 

 

// Function designed for chat between client and server. 
void func(int sockfd) 
{ 
    char buff[MAX]; 
    int n; 
    // infinite loop for chat 
    for (;;) { 
        bzero(buff, MAX); 

 

        // read the message from client and copy it in buffer 
        read(sockfd, buff, sizeof(buff)); 
        // print buffer which contains the client contents 
        printf(&quot;From client: %s\t To client : &quot;, buff); 
        bzero(buff, MAX); 
        n = 0; 
        // copy server message in the buffer 
        while ((buff[n++] = getchar()) != '\n') 
            ; 

 

        // and send that buffer to client 
        write(sockfd, buff, sizeof(buff)); 

 

        // if msg contains &quot;Exit&quot; then server exit and chat ended. 
        if (strncmp(&quot;exit&quot;, buff, 4) == 0) { 
            printf(&quot;Server Exit...\n&quot;); 
            break; 
        } 
    } 
} 

 

// Driver function 
int main() 
{ 
    int sockfd, connfd, len; 
    struct sockaddr_in servaddr, cli; 

 

    // socket create and verification 
    sockfd = socket(AF_INET, SOCK_STREAM, 0); 
    if (sockfd == -1) { 
        printf(&quot;socket creation failed...\n&quot;); 
        exit(0); 
    } 
    else
        printf(&quot;Socket successfully created..\n&quot;); 
    bzero(&amp;servaddr, sizeof(servaddr)); 

 

    // assign IP, PORT 
    servaddr.sin_family = AF_INET; 
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY); 
    servaddr.sin_port = htons(PORT); 

 

    // Binding newly created socket to given IP and verification 
    if ((bind(sockfd, (SA*)&amp;servaddr, sizeof(servaddr))) != 0) { 
        printf(&quot;socket bind failed...\n&quot;); 
        exit(0); 
    } 
    else
        printf(&quot;Socket successfully binded..\n&quot;); 

 

    // Now server is ready to listen and verification 
    if ((listen(sockfd, 5)) != 0) { 
        printf(&quot;Listen failed...\n&quot;); 
        exit(0); 
    } 
    else
        printf(&quot;Server listening..\n&quot;); 
    len = sizeof(cli); 

 

    // Accept the data packet from client and verification 
    connfd = accept(sockfd, (SA*)&amp;cli, &amp;len); 
    if (connfd &lt; 0) { 
        printf(&quot;server acccept failed...\n&quot;); 
        exit(0); 
    } 
    else
        printf(&quot;server acccept the client...\n&quot;); 

 

    // Function for chatting between client and server 
    func(connfd); 

 

    // After chatting close the socket 
    close(sockfd); 
} 
</code></pre>
<p>client.c inside the client pod :</p>
<pre><code>#include &lt;netdb.h&gt; 
#include &lt;stdio.h&gt; 
#include &lt;stdlib.h&gt; 
#include &lt;string.h&gt; 
#include &lt;sys/socket.h&gt; 
#define MAX 80 
#define PORT 8080 
#define SA struct sockaddr 
void func(int sockfd) 
{ 
    char buff[MAX]; 
    int n; 
    for (;;) { 
        bzero(buff, sizeof(buff)); 
        printf(&quot;Enter the string : &quot;); 
        n = 0; 
        while ((buff[n++] = getchar()) != '\n') 
            ; 
        write(sockfd, buff, sizeof(buff)); 
        bzero(buff, sizeof(buff)); 
        read(sockfd, buff, sizeof(buff)); 
        printf(&quot;From Server : %s&quot;, buff); 
        if ((strncmp(buff, &quot;exit&quot;, 4)) == 0) { 
            printf(&quot;Client Exit...\n&quot;); 
            break; 
        } 
    } 
} 

 

int main() 
{ 
    int sockfd, connfd; 
    struct sockaddr_in servaddr, cli; 

 

    // socket create and varification 
    sockfd = socket(AF_INET, SOCK_STREAM, 0); 
    if (sockfd == -1) { 
        printf(&quot;socket creation failed...\n&quot;); 
        exit(0); 
    } 
    else
        printf(&quot;Socket successfully created..\n&quot;); 
    bzero(&amp;servaddr, sizeof(servaddr)); 

 

    // assign IP, PORT 
    servaddr.sin_family = AF_INET; 
    servaddr.sin_addr.s_addr = inet_addr(&quot;10.233.90.193&quot;); 
    servaddr.sin_port = htons(PORT); 

 

    // connect the client socket to server socket 
    if (connect(sockfd, (SA*)&amp;servaddr, sizeof(servaddr)) != 0) { 
        printf(&quot;connection with the server failed...\n&quot;); 
        exit(0); 
    } 
    else
        printf(&quot;connected to the server..\n&quot;); 

 

    // function for chat 
    func(sockfd); 

 

    // close the socket 
    close(sockfd); 
} 
</code></pre>
<p>The problem is when I used the server pod's IP address in client.c it estabilished a TCP connection. But when I used server-svc Cluster IP address it could not connect.</p>
<p>I am doubtful if this service is even connecting to the server pod. Is there any way to verify that? Also if it is connecting how can I estabilish a TCP connection using the same?</p>

Neha
<p>Looks like your server is listening on port 8080, you have to set your service to port 8080 instead of 80 and then the service should be able to pass the traffic to the right place.</p>
