Saturday, July 14, 2018

Kubernetes and Pod Dependency

KubernetesPodsDependency

Kubernetes Pods Dependency

Using readinessprob and initContainer to solve the problem.

Readinessprob

We can setup a criterian that decides the pods is succesful startup of not. If we don't hope kubernetes marks the container is ok before some daemon is running or some port is enable. We can add some condition by using Readinessprob. We just list three ways

  • command
  • httpGet
  • check port

Command

        image: 192.168.51.130:5000/uwebserverv6
        readinessProbe:
          exec:
            command:
            - /bin/sh
            - -c
            - ps x |grep python
          initialDelaySeconds: 5
          periodSeconds: 20

line 4 command will detect the return value of line 7 is successfor($?=0) or not($?=1).

check port

        image: 192.168.51.130:5000/uwebserverv6
        readinessProbe:
          tcpSocket:
            port: 8000

check url

        image: 192.168.51.130:5000/uwebserverv6
        readinessProbe:
          httpGet:
            path: /
            port: 8000

InitContainer

    spec:
      terminationGracePeriodSeconds: 60
      containers:
      - name: myapp-container
        image: busybox
        command: ['sh', '-c', 'echo The app is running! && sleep 3600']

      initContainers:
      - name: init-myservice
        image: busybox
        command: ['sh', '-c', 'until nslookup webservern; do echo waiting for myservice; sleep 2; done;']

initContainers will wait to start container until command return success.

where webservern is the dependency container with it's service name.

Line 6 is the most simple way to detect a target Pods' status is Ok or not.

How it works

when readinessProbe is ok, it will store the container to etcd and mark it as ok. So DNS will be triggered and register the name to domain name. So client can use nslookup to find out the name.

And of course you need to create service to make other container to connect to them.

So People shoud make sure which condition means the container is ok for server side. And make sure the dependency target for client side.

No comments:

Post a Comment