How to Update a K8s Deployment with More Replicas
Asked Answered
J

1

7

I've created a simple K8s deployment with the kubectl create command

kubectl create -f k8-deployment.yaml

My k8-deployment.yaml file looks like this

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: mage-di
  name: mage-di
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mage-di
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: mage-di
    spec:
      containers:
      - image: astorm/mage-di
        name: mage-di
        imagePullPolicy: Never
        resources: {}
status: {}

This results in a single pod being started.

I want to tell my k8 cluster that more pods are needed to handle an expected traffic spike.

How should I do this? If I look at kubectl help I see there's an edit command that allows me to edit a deployment object's configuration, but this requires an interactive editor. Also, I'm new to K8s and I'm unsure if editing a deployment in place and updating its replica count is enough to trigger the proper creation of new pods. If I look at other kubectl commands I see there's also a rollout, apply and patch command that might do what I want.

Is there a canonically accepted way to do what I want, or is K8s the sort of tech where I just need to experiment and hope for the best?

Jeffiejeffrey answered 13/5, 2021 at 21:4 Comment(1)
If you have some way of measuring how much load you're handling, you can also look at setting up the Horizontal Pod Autoscaler to set the replicas: field automatically.Levee
S
10

You can do this in two ways. Either imperative - a quick command Or declarative - good for a production environment where you store your Deployment-manifest in Git.

Imperative way: (this will then diverge from what you have in your yaml-file)

kubectl scale deployment mage-di --replicas=2

Declarative way, edit this line in your Yaml file:

replicas: 2

then apply it to the cluster with:

kubectl apply -f k8-deployment.yaml

See also:

Spark answered 13/5, 2021 at 21:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.