Bedjase
Bedjase

Reputation: 79

Best practices for deploying/updating containers with Kubernetes?

I am trying to understand how I should deploy containers with Kubernetes. I am new at this topic so at this moment I am testing all these ideas in a virtual machine.

I'm using Git, Jenkins, Docker, Docker Hub and Kubernetes.

Also, I have a Master node and only one Slave node.

I created a YAML deploy file to start the pod and create a new container.

kubectl apply -f deployment.yaml

Then I expose the deploy.

kubectl expose deployment my-app --type=LoadBalancer --name=my-app

YAML File for deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
  labels:
    app: app
spec:
  selector:
    matchLabels:
      app: app
      role: master
      tier: backend
  replicas: 1
  template:
    metadata:
      labels:
        app: app
        role: master
        tier: backend
    spec:
      containers:
      - name: appcontainer
        image: repository:1.0
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        ports:
        - containerPort: 8085
      imagePullSecrets:
      - name: regcred

Now that I have all working, for example, I give a new update on the image and I need to update this new image, for example, 1.0 to 1.1, on the deployment done. I need to know the proper way to do this action.

Because I think I'm doing it wrong like I'm trying to smash the image on the container created with the new image and I don't know if the proper way is deploying with a new YAML file and if Kubernetes builds a new container with that image and consequently kill the old deploy, but I don't know how I can do that if this the right thing to do.

Upvotes: 2

Views: 338

Answers (2)

Sebin
Sebin

Reputation: 1044

The solution you are looking for is called Rolling update. You can have the deployment.yaml updated like this:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
  labels:
    app: app
spec:
  selector:
    matchLabels:
      app: app
      role: master
      tier: backend
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate
  replicas: 1
  template:
    metadata:
      labels:
        app: app
        role: master
        tier: backend
    spec:
      containers:
      - name: appcontainer
        image: repository:1.0
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        ports:
        - containerPort: 8085
      imagePullSecrets:
      - name: regcred

Please note the rolling update configuration:

strategy:
  rollingUpdate:
    maxSurge: 1
    maxUnavailable: 0
  type: RollingUpdate

maxSurge indicates the number of extra pods that can be run during the deployment rollout. maxUnavailable is about the number of pods that can be taken down during the rollout. These values can either be an integer value - exact number of pods or a percentage of pods. You can adjust these values to suit your usecase.

Then you can change the image tag in the yaml and do kubectl apply to update the image.

Upvotes: 0

kool
kool

Reputation: 3613

The easiest way to update a deployment with new image will be to run:

kubectl set image deployment/my-app appcontainer=repository:1.1 --record

This way it will first create a new pod(s) with newer version of image and once successfully deployed it will terminate old pod or pods depending on number of replicas you have specified in the replicas field.

You can check status of the update by running

kubectl rollout status deployment.v1.apps/my-app

Upvotes: 1

Related Questions