kubectl replace -f creates pod indefinitely in Pending state

4/18/2019

I have a k8s deployment - I often deploy a new version to the docker repo - change the image tag - and try to replace the deployment using kubectl replace -f file.yaml. My replicas are set to 1 - I have only 1 pod of the deployment running at a time.

When I change the image tag (e.g changing v1 to v2) and try to replace it - it creates a new pod, but it remains in the 'pending' state indefinitely, while the old pod stays in 'Running' state.

I think the new pod waits for the old pod to be terminated - but it won't terminate by itself. I need it to be deleted by k8s so the new pod can take its place.

Using replace --force fixes this issue - but I'd like it to work using just replace -f. Any ideas how to achieve this?

-- Click Upvote
kubernetes

1 Answer

4/18/2019

The issue you see has nothing to do with kubectl replace/apply. The real reason is that deployments by default use RollingUpdate strategy which by default waits for new pod to be Running and only then kills old pod. The reason why new pod is in Pending state is unclear from your question but in most cases this indicates lack of compute resources for new pod.

You may do two different things:

Use RollingUpdate strategy with maxUnavailable=1. This will do what you want - it will kill old pod and then create a new one.

spec:
  strategy:
    type: RollingUpdate
    rollingUpdate:
       maxUnavailable: 1

OR you can specify Recreate strategy which effectively does the same:

spec:
  strategy:
    type: Recreate

Read more about deployment strategies here: https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#strategy

-- Vasily Angapov
Source: StackOverflow