Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to kubernetes "kubectl apply" does not update existing deployments

I have a .NET-core web application. This is deployed to an Azure Container Registry. I deploy this to my Azure Kubernetes Service using

kubectl apply -f testdeployment.yaml

with the yaml-file below

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myweb
spec:
  replicas: 1
  selector:
    matchLabels:
      app: myweb
  template:
    metadata:
      labels:
        app: myweb
    spec:
      containers:
      - name: myweb
        image: mycontainerregistry.azurecr.io/myweb:latest
        ports:
        - containerPort: 80
      imagePullSecrets:
        - name: my-registry-key

This works splendid, but when I change some code, push new code to container and run the

kubectl apply -f testdeployment

again, the AKS/website does not get updated, until I remove the deployment with

kubectl remove deployment myweb

What should I do to make it overwrite whatever is deployed? I would like to add something in my yaml-file. (Im trying to use this for continuous delivery in Azure DevOps).

like image 328
Cowborg Avatar asked Oct 10 '19 12:10

Cowborg


People also ask

Does kubectl apply overwrite?

kubectl apply .. will use various heuristics to selectively update the values specified within the resource. kubectl replace ... will replace / overwrite the entire object with the values specified.

Does kubectl apply update image?

You can use kubectl set to make changes to an object's image , resources (compute resource such as CPU and memory), or selector fields. The kubectl set image command updates the nginx image of the Deployment's Pods one at a time. You can use kubectl apply to update a resource by applying a new or updated configuration.

What happens after kubectl apply?

The command set kubectl apply is used at a terminal's command-line window to create or modify Kubernetes resources defined in a manifest file. This is called a declarative usage. The state of the resource is declared in the manifest file, then kubectl apply is used to implement that state.

Does kubectl apply restart?

Restarting Kubernetes Pods Using kubectl You can use docker restart {container_id} to restart a container in the Docker process, but there is no restart command in Kubernetes. In other words, there is no kubectl restart {podname}.


2 Answers

I believe what you are looking for is imagePullPolicy. The default is ifNotPresent which means that the latest version will not be pulled.

https://kubernetes.io/docs/concepts/containers/images/

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myweb
spec:
  replicas: 1
  selector:
    matchLabels:
      app: myweb
  template:
    metadata:
      labels:
        app: myweb
    spec:
      containers:
      - name: myweb
        image: mycontainerregistry.azurecr.io/myweb
        imagePullPolicy: Always
        ports:
        - containerPort: 80
      imagePullSecrets:
        - name: my-registry-key

To ensure that the pod is recreated, rather run:

kubectl delete -f testdeployment && kubectl apply -f testdeployment
like image 82
Daniel Lee Avatar answered Oct 09 '22 12:10

Daniel Lee


kubectl does not see any changes in your deployment yaml file, so it will not make any changes. That's one of the problems using the latest tag.

Tag your image to some incremental version or build number and replace latest with that tag in your CI pipeline (for example with envsubst or similar). This way kubectl knows the image has changed. And you also know what version of the image is running. The latest tag could be any image version.

Simplified example for Azure DevOps:

# <snippet>
        image: mycontainerregistry.azurecr.io/myweb:${TAG}
# </snippet>

Pipeline YAML:

stages:
- stage: Build
  jobs:
  - job: Build
    variables:
    - name: TAG
      value: $(Build.BuildId)
    steps:
    - script: |
        envsubst '${TAG}' < deployment-template.yaml > deployment.yaml
      displayName: Replace Environment Variables

Alternatively you could also use another tool like Replace Tokens (different syntax: #{TAG}#).

like image 30
Markus Dresch Avatar answered Oct 09 '22 12:10

Markus Dresch