pod failing with crash loop while initializing , No logs to debug

1/29/2022

I am trying to build and deploy an image in Kubernetes, but when I deploy, it failed with crash loop error, I did add sleep time in that so now it is not failing, but the application is not accessible via the service .

I tried to check pod logs as well but there is no logs at all. Any Kubernetes expert can help me solve this please there is a dependency of redis but the basic is not working Image that I am using is at

The definition file for pod and service file is as below pod file

apiVersion: v1
kind: Pod
metadata:
  name: voting-app-pod
  labels:
    name: voting-app-pod
    app: demo-voting-app
spec:
  containers:
  - name: voting-app
    image: 
    command: ["sleep"]
    args: ["3200"]
    ports:
         - containerPort: 5000

Service file

apiVersion: v1
kind: Service
metadata:
  name: voting-service
  labels:
    name: voting-service
    app: demo-voting-app
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 5000
  type: LoadBalancer
  selector:
     name: voting-app-pod
     app: demo-voting-app
-- Waseem Mir
kubernetes

2 Answers

1/29/2022

I see some environment variables are mandatory to startup the service:

apiVersion: v1
kind: Pod
metadata:
  name: voting-app-pod
  labels:
    name: voting-app-pod
    app: demo-voting-app
spec:
  containers:
  - name: voting-app
    image: mirwasim0/bloomreach
    env:
    - name: REDIS_HOST
      value: my-redis-host
    - name: REDIS_PORT
      value: "6379"
    - name: USERNAME
      value: my-user
    - name: PASSWORD
      value: my-password
    ports:
    - containerPort: 5000
-- Marco Caberletti
Source: StackOverflow

1/29/2022

You're missing apiVersion: v1 at the top of the yaml.

apiVersion: v1
kind: Pod
metadata:
  name: voting-app-pod
  labels:
    name: voting-app-pod
    app: demo-voting-app
spec:
  containers:
  - name: voting-app
    image: mirwasim0/bloomreach
    command: ["sleep"]
    args: ["3200"]
    ports:
         - containerPort: 5000

Other than that, I tried your pod yaml in a test cluster and it started up fine with no crashloops


UPDATE

Removing the sleep command

apiVersion: v1
kind: Pod
metadata:
  name: voting-app-pod
  labels:
    name: voting-app-pod
    app: demo-voting-app
spec:
  containers:
  - name: voting-app
    image: mirwasim0/bloomreach
    # command: ["sleep"]
    # args: ["3200"]
    ports:
         - containerPort: 5000
---
apiVersion: v1
kind: Service
metadata:
  name: voting-service
  labels:
    name: voting-service
    app: demo-voting-app
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 5000
  type: LoadBalancer
  selector:
     name: voting-app-pod
     app: demo-voting-app

Gives me the following crash out in the logs:

$ kubectl logs -f voting-app-pod 
 * Serving Flask app 'app.py' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
Traceback (most recent call last):
  File "/usr/local/bin/flask", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.7/site-packages/flask/cli.py", line 994, in main
    cli.main(args=sys.argv[1:])
  File "/usr/local/lib/python3.7/site-packages/flask/cli.py", line 600, in main
    return super().main(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/click/core.py", line 1053, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.7/site-packages/click/core.py", line 1659, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/lib/python3.7/site-packages/click/core.py", line 1395, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.7/site-packages/click/core.py", line 754, in invoke
    return __callback(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/click/decorators.py", line 84, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/click/core.py", line 754, in invoke
    return __callback(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/flask/cli.py", line 849, in run_command
    app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)
  File "/usr/local/lib/python3.7/site-packages/flask/cli.py", line 324, in __init__
    self._load_unlocked()
  File "/usr/local/lib/python3.7/site-packages/flask/cli.py", line 350, in _load_unlocked
    self._app = rv = self.loader()
  File "/usr/local/lib/python3.7/site-packages/flask/cli.py", line 406, in load_app
    app = locate_app(self, import_name, name)
  File "/usr/local/lib/python3.7/site-packages/flask/cli.py", line 260, in locate_app
    __import__(module_name)
  File "/code/app.py", line 11, in <module>
    cache = redis.Redis(host=os.environ['REDIS_HOST'], port=os.environ['REDIS_PORT'])
  File "/usr/local/lib/python3.7/os.py", line 681, in __getitem__
    raise KeyError(key) from None
KeyError: 'REDIS_HOST'

This indicates you've omitted you add the environment variable REDIS_HOST. After trying a few more times, I found there's a few other environments variables you're missing such as REDIS_HOST, REDIS_PORT, USERNAME, PASSWORD. You just need to add those in like this, (and any other environment variables your script requires)

apiVersion: v1
kind: Pod
metadata:
  name: voting-app-pod
  labels:
    name: voting-app-pod
    app: demo-voting-app
spec:
  containers:
  - name: voting-app
    image: mirwasim0/bloomreach
    # command: ["sleep"]
    # args: ["3200"]
    ports:
         - containerPort: 5000
    env:
      - name: REDIS_HOST
        value: "127.0.0.1"
      - name: REDIS_PORT
        value: "1234"
      - name: USERNAME
        value: "my-user-name"
      - name: PASSWORD
        value: "my-password"
      ...
      ...
-- Blender Fox
Source: StackOverflow