Is there a way to do a load balancing between pod in multiple nodes?

4/3/2019

I have a kubernetes cluster deployed with rke witch is composed of 3 nodes in 3 different servers and in those server there is 1 pod which is running yatsukino/healthereum which is a personal modification of ethereum/client-go:stable . The problem is that I'm not understanding how to add an external ip to send request to the pods witch are

My pods could be in 3 states:

  1. they syncing the ethereum blockchain
  2. they restarted because of a sync problem
  3. they are sync and everything is fine

I don't want my load balancer to transfer requests to the 2 first states, only the third point consider my pod as up to date.

I've been searching in the kubernetes doc but (maybe because a miss understanding) I only find load balancing for pods inside a unique node.

Here is my deployment file:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: goerli
  name: goerli-deploy
spec:
  replicas: 3
  selector:
    matchLabels:
      app: goerli
  template:
    metadata:
      labels:
        app: goerli
    spec:
      containers:
        - image: yatsukino/healthereum
          name: goerli-geth
          args: ["--goerli", "--datadir", "/app", "--ipcpath", "/root/.ethereum/geth.ipc"]
          env:
          - name: LASTBLOCK
            value: "0"
          - name: FAILCOUNTER
            value: "0"
          ports:
          - containerPort: 30303
            name: geth
          - containerPort: 8545
            name: console
          livenessProbe:
            exec:
              command:
              - /bin/sh
              - /app/health.sh
            initialDelaySeconds: 20
            periodSeconds: 60
          volumeMounts:
          - name: app
            mountPath: /app
      initContainers: 
      - name: healthcheck
        image: ethereum/client-go:stable
        command: ["/bin/sh", "-c", "wget -O /app/health.sh http://my-bash-script && chmod 544 /app/health.sh"]
        volumeMounts:
        - name: app
          mountPath: "/app"
      restartPolicy: Always
      volumes:
      - name: app
        hostPath:
          path: /app/
-- yatsukino
kubernetes
load-balancing

3 Answers

4/3/2019

When a container is started, Kubernetes can be configured to wait for a configurable amount of time to pass before performing the first readiness check. After that, it invokes the probe periodically and acts based on the result of the readiness probe. If a pod reports that it’s not ready, it’s removed from the service. If the pod then becomes ready again, it’s re-added. Unlike liveness probes, if a container fails the readiness check, it won’t be killed or restarted. This is an important distinction between liveness and readiness probes. Liveness probes keep pods healthy by killing off unhealthy containers and replacing them with new, healthy ones, whereas readiness probes make sure that only pods that are ready to serve requests receive them. This is mostly necessary during container start up, but it’s also useful after the container has been running for a while.

I think you can use probe for your goal

-- yasin lachini
Source: StackOverflow

4/3/2019

The answers above explains the concepts, but about your questions anout services and external ip; you must declare the service, example;

apiVersion: v1
kind: Service
metadata:
  name: goerli
spec:
  selector:
    app: goerli
  ports:
  - port: 8545
  type: LoadBalancer

The type: LoadBalancer will assign an external address for in public cloud or if you use something like metallb. Check your address with kubectl get svc goerli. If the external address is "pending" you have a problem...

If this is your own setup you can use externalIPs to assign your own external ip;

apiVersion: v1
kind: Service
metadata:
  name: goerli
spec:
  selector:
    app: goerli
  ports:
  - port: 8545
  externalIPs:
  - 222.0.0.30

The externalIPs can be used from outside the cluster but you must route traffic to any node yourself, for example;

ip route add 222.0.0.30/32 \
  nexthop via 192.168.0.1 \
  nexthop via 192.168.0.2 \
  nexthop via 192.168.0.3

Assuming yous k8s nodes have ip 192.168.0.x. This will setup ECMP routes to your nodes. When you make a request from outside the cluster to 222.0.0.30:8545 k8s will load-balance between your ready PODs.

-- lgekman
Source: StackOverflow

4/3/2019

For loadbalancing and exposing your pods, you can use https://kubernetes.io/docs/concepts/services-networking/service/

and for checking when a pod is ready, you can use tweak your liveness and readiness probes as explained https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-probes/

for probes you might want to consider exec actions like execution a script that checks what is required and returning 0 or 1 dependent on status.

-- Prateek Jain
Source: StackOverflow