How do you scale applications in Kubernetes?
Kubernetes provides manual and automatic scaling methods. Manual scaling uses kubectl scale for direct control, ideal for predictable workloads, but requires manual adjustment back down. Automatic scaling uses the Horizontal Pod Autoscaler (HPA) based on metrics like CPU usage, adjusting replicas between defined min and max values, requiring resource limits and a metrics server. Cluster-level scaling uses the Cluster Autoscaler to add or remove nodes based on scheduling needs, best suited for unpredictable or large-scale workloads. Each method has trade-offs in setup complexity, cost, and responsiveness.
Scaling applications in Kubernetes boils down to two main methods: manual and automatic scaling. Both rely on Kubernetes’ built-in controllers like Deployments and ReplicaSets, which manage how many copies (replicas) of your app are running at any given time.
Manual Scaling with kubectl scale
If you want direct control over how many replicas of your application are running, you can use the kubectl scale command. It's straightforward and good for predictable workloads or one-off adjustments.
For example:
kubectl scale deployment my-app --replicas=5
This tells Kubernetes to run 5 instances of your app. You can check the current number of replicas by looking at the output of:
kubectl get deployments
- This method is fast and reliable.
- Great for scheduled traffic spikes you know about in advance.
- No need to set up extra resources like metrics servers.
Just keep in mind that once you scale manually, you’ll have to scale back down when the load decreases — unless you want to keep paying for unused capacity.
Automatic Scaling Based on Metrics
When your workload fluctuates unpredictably, it’s better to let Kubernetes handle the scaling automatically. That’s where the Horizontal Pod Autoscaler (HPA) comes in.
To use HPA, you first need to define what metric it should watch — usually CPU usage or memory consumption. Here's a basic example using CPU:
kubectl autoscale deployment my-app --cpu-percent=50 --min=2 --max=10
This means Kubernetes will try to keep CPU around 50% by adjusting the number of replicas between 2 and 10.
- Make sure your pods have resource limits defined; otherwise, HPA won’t have a baseline to work from.
- You'll also need a metrics server running in your cluster.
- You can create more advanced HPAs using custom metrics like request latency or queue size if you have the right tools (e.g., Prometheus adapter).
It takes a little setup, but once it’s working, it adjusts quietly in the background without you needing to lift a finger.
Cluster-Level Scaling with Cluster Autoscaler
Sometimes your nodes themselves become the bottleneck — maybe all your nodes are full and there’s no space to schedule new pods. That’s where the Cluster Autoscaler comes in.
The Cluster Autoscaler watches for pending pods due to lack of resources and responds by asking your cloud provider to add more nodes. When those nodes aren't needed anymore, it removes them to save costs.
- Works best when used with auto-scaling groups (like AWS ASGs or GCP MIGs).
- Needs proper tagging and IAM permissions depending on your cloud provider.
- Be aware of cooldown periods and delays — sometimes it takes a few minutes before new nodes show up.
This level of scaling isn’t always necessary, especially if you're running a small or predictable workload. But for larger, variable apps, it helps prevent downtime during unexpected traffic surges.
That’s basically it. Whether you’re doing it manually or letting Kubernetes take the wheel, the tools are there to make your life easier — as long as you set them up right.
The above is the detailed content of How do you scale applications in Kubernetes?. For more information, please follow other related articles on the PHP Chinese website!
Hot AI Tools
Undress AI Tool
Undress images for free
Undresser.AI Undress
AI-powered app for creating realistic nude photos
AI Clothes Remover
Online AI tool for removing clothes from photos.
Clothoff.io
AI clothes remover
Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!
Hot Article
Hot Tools
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
How to get started with docker
Aug 16, 2025 pm 01:46 PM
Dockerisaplatformforpackaging,shipping,andrunningapplicationsinlightweight,isolatedcontainersthatsharethehostOSkernel,unlikevirtualmachines.2.InstallDockerDesktoponWindowsormacOS,orusethecurlcommandonLinux,thentestwithdocker--versionanddockerrunhello
How do you use Docker with AWS (Amazon Web Services)?
Aug 03, 2025 pm 04:24 PM
TouseDockerwithAWSeffectively,startbysettingupyourDockerenvironmentonAWSusingEC2ormanagedserviceslikeECSorEKS;next,choosecontainerorchestrationoptionssuchasECSforscaleandintegrationorEKSforKubernetessupport;then,storeandmanageDockerimagesusingAmazonE
How to run multiple services with Docker Compose?
Aug 07, 2025 pm 03:26 PM
To run multiple services, you need to define the service in docker-compose.yml, communicate with the service name, and start with dockercomposeup. 1. Define web, db, redis and other services under the services of docker-compose.yml, and specify configurations such as build, image, ports, environment, volumes and depends_on; 2. DockerCompose automatically creates a shared network, and services can communicate through the service name (such as db:5432); 3. Run dockercomposeup--build to build and start all services.
How to run a command in a docker container
Aug 20, 2025 am 05:09 AM
Use dockerrun to run commands in a new container, and use dockerexec to execute commands in a running container. The specific methods are: 1. Use dockerrun to start a new container and execute commands, such as dockerrun--rmubuntuls/tmp; 2. Use dockerexec to execute commands in a running container, such as dockerexecmy-nginx-servicepsaux, and interactive operations need to add -it, such as dockerexec-itmy-container/bin/bash; 3. Overwrite the default commands when starting the container, such as dockerrunnginx:latestnginx-T
How to inspect a docker container
Aug 17, 2025 pm 12:47 PM
dockerinspect is the main command to view container details. 1. Use dockerinspect to obtain the complete information of the container, including status, network, mount, environment variables, etc.; 2. Use the -f parameter to extract specific fields, such as IP address, running status, mirror name, mount volume and environment variables; 3. Use dockerlogs and dockertop to view container logs and processes to assist in diagnosis; 4. It is often used to troubleshoot the network, verify mounts, check health status and automated scripts. In combination with dockerps-a, you can find all containers. This command is the core tool for in-depth debugging and automated operations.
How to reduce docker image size
Aug 22, 2025 am 01:04 AM
Using smaller basic images, multi-stage construction and reasonable layering are the keys to reducing Docker images size. 1. Priority is given to lightweight basic images such as alpine, slim or distroless, which can greatly reduce the volume; 2. Adopt multi-stage construction to separate the construction dependency from the runtime, avoiding bringing source code, dependency packages and construction tools into the final image; 3. Merge RUN instructions and clean cache and temporary files in the same layer, such as using apt-getupdate and install and clean commands to execute in a chain, and use --no-install-recommends or apk--no-cache to reduce redundant packages; 4. Configure.dockeringore files,
How to write a dockerfile
Aug 23, 2025 am 03:46 AM
The key to writing a Dockerfile is to understand each instruction and its layer caching mechanism. 1. Use FROM to specify the basic image, and prefer lightweight and safe images such as node:18-alpine; 2. Use WORKDIR to set the working directory in the container to /app; 3. Use COPY to copy the file, it is recommended to copy package.json step by step to utilize the cache; 4. Use RUN to install dependencies and merge commands to reduce layers, such as RUNapt-getupdate&&apt-getinstall-ycurl&&rm-rf/var/lib/apt/lists/*; 5. Use CMD to set the default startup command
How to run a command as root in a docker container
Aug 17, 2025 am 03:59 AM
Bydefault,Dockercontainersrunasroot,whichcanbeverifiedusingwhoamioridcommands.2.Torunacommandasrootinanewcontainer,usedockerrunwithofficialimagesthatdefaulttoroot,orexplicitlyspecify-uroottooverrideanynon-rootusersettings.3.Forarunningcontainer,usedo


