In the previous article we discussed how to assemble a docker container and run it. In this article, we will go further and talk about such a thing as Kubernetes.
Kubernetes
Kubernetes is a tool for orchestrating(managing) docker containers. With this tool you can deploy, scale and manage your containerized apps. Kubernetes commonly used in developing and production.
Imagine you have a web application made up of multiple components: a frontend, a backend, and a database. Deploying and managing these parts manually, especially when they need to scale or recover from crashes, can become time-consuming and error-prone. Kubernetes automates this process, ensuring your application stays healthy and performs well.
Manifest
A manifest is a YAML file that describes the desired state of a Kubernetes object—like a Pod, Deployment, or Service. By this way, you give instructions to Kubernetes what to do.
With manifests, you can define:
- What containers to run and where
- How many replicas of a service to keep
- Network policies
- Storage needs
- and more
In this article, we’ll use Minikube and kubectl (the Kubernetes CLI) to run our first manifest.
Minikube is a tool which lets you run Kubernetes locally.
Preparation
Let's say you've finished your app and want to deploy it using Kubernetes. And before deploying we need to do some steps.
First, you need to install Minikube and kubectl. kubectl - the kubernetes command-line(CLI) which allows you to run commands and apply manifests.
When you install Minikube, you need to start it. The command to start Minikube server in terminal:
minikube start
To stop Minikube:
minikube stop
When you successfully start Minikube, you can check all pods across all namespaces:
kubectl get pods -A
To see your current context and namespace you can use this command:
kubectl config get-contexts
If you’re new to Minikube, your default namespace will likely be default
.
You also can change Minikube configs in config
file. This command allows you to see what configurations are set at current moment:
minikube config view
Create an yaml file in the directory of your project:
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: my-namespace
name: my-app
labels:
app.kubernetes.io/name: my-app
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/name: my-app
template:
metadata:
labels:
app.kubernetes.io/name: my-app
spec:
containers:
- name: frontend
image: my-app-image
ports:
- name: http
containerPort: 5173
Breakdown of the manifest:
apiVersion: apps/v1
– Version of the Kubernetes API to use.kind: Deployment
– Specifies that Kubernetes should manage a set of identical Pods.-
metadata:
-
namespace:
my-namespace – Deploys the app to my-namespace. Make sure the namespace exists or create it. -
name: my-app
– Name of the deployment. -
labels:
– Metadata tags used to group and select resources. These should match selector.matchLabels and template.metadata.labels.-
app.kubernetes.io/name: my-app
– A common label format that makes it easier to manage and discover resources.
-
-
-
spec:
-
replicas: 1
– Specifies that one Pod should be running. -
selector:
– Tells Kubernetes which Pods are managed by this Deployment.-
matchLabels:
app.kubernetes.io/name: my-app
-
-
template:
– The template used to create Pods.-
metadata:
-
labels:
app.kubernetes.io/name: my-app
-
-
spec:
-
containers:
– Defines the container(s) that will run in each Pod.- name: frontend
-
image: my-app-image
– Your Docker image. This image should be already built and available locally. -
ports:
– Declares that the container listens on port 5173. - name: http
-
containerPort: 5173
– The internal container port to expose.
-
-
-
Applying
Use kubectl to apply your manifest
kubectl apply -f my-app-manifest.yaml
Kubernetes reads the manifest and creates the necessary objects. You can check if your deployment is running:
kubectl get deployments
kubectl get pods
To access my-app in your browser, you need to expose port:
kubectl expose deployment my-app --type=NodePort --port=5173
Then open it using Minikube:
minikube service my-app
When you done with your working pod in Minikube, you can stop it with the next command:
kubectl scale --replicas=0 deployment/my-app
Top comments (0)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.