What is Kubernetes?
Kubernetes (or K8s) is an open-source container orchestration system. It is used for application deployment and scaling. It has become very popular recently, partly due to the rise of cloud-native solutions. It lets you deploy containers to clusters, meaning a network of virtual machines. The basic idea of Kubernetes is to further abstract machines, storage, and networks away from their physical implementation.
When should you use Kubernetes?
Kubernetes is best used in microservice architectures, since it makes it easier to split your application into smaller components with containers that can then be run on different cloud environments, giving you the option to choose the best host for your needs. Kubernetes is built to be used anywhere, so you can deploy to public/private/hybrid clouds, enabling you to reach users where they’re at, with greater availability and security.
Kubernetes is also great for scaling your applications, since it provides all these:
- Horizontal infrastructure scaling: New servers can be added or removed easily.
- Auto-scaling: Automatically change the number of running containers, based on CPU utilization or other application-provided metrics.
- Manual scaling: Manually scale the number of running containers through a command or the interface.
- Replication controller: The replication controller makes sure your cluster has an equal amount of pods running. If there are too many pods, the replication controller terminates the extra pods. If there are too few, it starts more pods.
- Health checks and self-healing: Kubernetes can check the health of nodes and containers ensuring your application doesn’t run into any failures. Kubernetes also offers self-healing and auto-replacement so you don’t need to worry about if a container or pod fails.
- Traffic routing and load balancing: Traffic routing sends requests to the appropriate containers. Kubernetes also comes with built-in load balancers so you can balance resources in order to respond to outages or periods of high traffic.
- Automated rollouts and rollbacks: Kubernetes handles rollouts for new versions or updates without downtime while monitoring the containers’ health. In case the rollout doesn’t go well, it automatically rolls back.
- Canary Deployments: Canary deployments enable you to test the new deployment in production in parallel with the previous version.
It also allows for better management of your applications. It has many benefits and a steep learning curve.
Who uses Kubernetes?
Kubernetes is used by many companies, like adidas, spotify, cern and many more. You can see the full list of Kubernetes’ case studies here.
When you shouldn’t use it
You shouldn’t use Kubernetes for simple, lightweight applications, since it wouldn’t be really useful for you.
For comparison, this is how deploying a simple app on Kubernetes looks like:
We’re gonna do it anyway and create a React app, to learn the basics of Kubernetes.
A React app in Kubernetes
1. Create a react app
First, we need to create a React app. The easiest way of doing this is with create-react-app
npx create-react-app my-app
If you don’t want to create your own react app and config files, you can use this repository I created for this post.
2. Dockerize the app
In order to deploy the React app to Kubernetes, we need to put it inside a container. Docker is currently the most used container engine.
This is the dockerfile I used. This just copies the React production build to the container.
To speed things up, add
.dockerignore to your project and exclude
Then we run this command to create a docker image:
docker build -t my-react-app .
3. Connect to a Kubernetes cluster
Open your Docker Desktop preferences and switch to your Kubernetes tab. Enable Kubernetes support here. It automatically creates a cluster for you.
Once it is running, connect to it with kubectl
kubectl config use-context docker-for-desktop
4. Upload the Docker image to your container registry
To be able to pull the Docker image within the Kubernetes cluster, we need to upload the image to a Docker registry. We’re deploying the app into a local cluster, so we need a local registry.
You can create one with this command:
docker run -d -p 5000:5000 --restart=always --name registry registry:2
To upload our Docker image, we have to tag it with the hostname and port of our registry:
docker tag my-react-app localhost:5000/my-react-app
And then we push the image to our Docker registry:
docker push localhost:5000/my-react-app
5. Deploy the React application
For deploying the React application to Kubernetes we need a
deployment file. This makes sure our application will have as many replicas as we define. In addition, we can define the Docker image we want to use, what ports are used and further metadata for our application
With just the
deployment we wouldn’t be able to access our application from outside. To expose applications, Kubernetes offers a
service. Using a
service we can define which ports to expose to the cluster/outside.
You can now use this file to deploy your application to Kubernetes with:
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
You can then check if everything’s running using:
kubectl get pods
kubectl get deployment
kubectl get service
If everything went right, you should be able to go to http://localhost:31000 and see your react app, now served from a Kubernetes cluster
Kubernetes is a hard to learn but powerful tool for automating deployment, scaling, and management of containerized applications, especially when you’re using microservices. It is used by many big companies and helps you scale your app without increasing your ops team.