setting up multiple instances of service on dedicated port in kubernetes

7/13/2019

I'm trying to understand how properly configure kubernetes service which will allow me to have the following setup:

  • I want to have multiple instances of the same service running on specified port
  • I want to access these instances within k8s network transparently

After reading documentation I came up with two solutions. want to configure particular service to have multiple instances. Since our apache server has hard-coded redirect rules (i.e. relies on specific ports) I have two options for backend:

  1. use hostNetwork: true such that my service ports will be consistent and visible to internal network
  2. use LoadBalancer service and hostNetwork: false, the LoadBalancer can be configure to expose specific service ports

If I use option #1 I can't have multiple instances since hostNetwork does not allow usage of the same port. While using option #2 I think I can have desired functionality but I'm having OpenStack errors about loadbalancer quotas (may be it is internal issue). What I'm seeking here is suggestion how to make this work, does my understanding of LoadBalancer is correct and I can reach this functionality. For the record here is a yaml I'm using:

kind: Service
apiVersion: v1
metadata:
  name: aaa-global-r
spec:
  selector:
    app: aaa-global-r
  type: LoadBalancer
  ports:
    - name: aaa-global-r
      port: 8252
      targetPort: 8252
      protocol: TCP
    - name: aaa-gr-mon
      port: 18252
      targetPort: 18252
      protocol: TCP

The point here is that I want to preserve my ports (since we're using another auth layer which is configured to talk to these ports). And, I want to have multiple instances of this app where all of them will run on these ports in different pods and I want to access them on internal k8s network.

-- Valentin
kubernetes
kubernetes-ingress
kubernetes-pod
networking

1 Answer

7/13/2019
  1. Multiple instance is handled through RepilcaSets

  2. Ports are handled automatically by Kubernetes Services

Example:

Running 3 instances of nginx and access all of them on a single port:

C02W84XMHTD5:test iahmad$ kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-65899c769f-h5khl   1/1     Running   1          19m
C02W84XMHTD5:test iahmad$ 
C02W84XMHTD5:test iahmad$ 
C02W84XMHTD5:test iahmad$ kubectl scale deployment nginx --replicas=3
deployment.extensions/nginx scaled
C02W84XMHTD5:test iahmad$ 
C02W84XMHTD5:test iahmad$ kubectl get pods
NAME                     READY   STATUS              RESTARTS   AGE
nginx-65899c769f-h5khl   1/1     Running             1          19m
nginx-65899c769f-m2pkp   0/1     ContainerCreating   0          5s
nginx-65899c769f-mwlqn   0/1     ContainerCreating   0          5s
C02W84XMHTD5:test iahmad$ 
C02W84XMHTD5:test iahmad$ kubectl get pods
NAME                     READY   STATUS              RESTARTS   AGE
nginx-65899c769f-h5khl   1/1     Running             1          20m
nginx-65899c769f-m2pkp   0/1     ContainerCreating   0          13s
nginx-65899c769f-mwlqn   0/1     ContainerCreating   0          13s
C02W84XMHTD5:test iahmad$ kubectl get pods
NAME                     READY   STATUS              RESTARTS   AGE
nginx-65899c769f-h5khl   1/1     Running             1          20m
nginx-65899c769f-m2pkp   0/1     ContainerCreating   0          15s
nginx-65899c769f-mwlqn   1/1     Running             0          15s
C02W84XMHTD5:test iahmad$ 
C02W84XMHTD5:test iahmad$ kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-65899c769f-h5khl   1/1     Running   1          20m
nginx-65899c769f-m2pkp   1/1     Running   0          22s
nginx-65899c769f-mwlqn   1/1     Running   0          22s
C02W84XMHTD5:test iahmad$ 
C02W84XMHTD5:test iahmad$ kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        30d
nginx        NodePort    10.107.66.154   <none>        80:32622/TCP   18m
C02W84XMHTD5:test iahmad$ 
C02W84XMHTD5:test iahmad$ 
C02W84XMHTD5:test iahmad$ curl 192.168.99.105:32622
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
-- Ijaz Ahmad Khan
Source: StackOverflow