Kubernetes for Beginners: Essential Concepts Without the Hassle
The adoption of Kubernetes has grown exponentially in recent years, and for good reason. When dealing with applications that need to be scalable and manageable efficiently, Kubernetes stands out as a powerful solution. However, for those just starting out, the environment can seem complex and intimidating. This article aims to demystify the core concepts of Kubernetes, making them accessible and easy to understand.
Pods and Deployments
At the heart of Kubernetes, we have pods, which are the smallest deployable units. A pod can contain one or more containers that share the same network and storage space. Let's consider a simple example of a pod running a Node.js application.
apiVersion: v1
kind: Pod
metadata:
name: meu-app
spec:
containers:
- name: meu-container
image: node:14
ports:
- containerPort: 3000
In the example above, we create a pod named meu-app that runs a container with the Node.js image. Now, to manage the creation and updates of pods, we use deployments. A deployment ensures that the desired number of pods is always running.
apiVersion: apps/v1
kind: Deployment
metadata:
name: meu-app-deployment
spec:
replicas: 3
selector:
matchLabels:
app: meu-app
template:
metadata:
labels:
app: meu-app
spec:
containers:
- name: meu-container
image: node:14
ports:
- containerPort: 3000
Here, we define a deployment that creates three replicas of our meu-app pod. If one of the pods fails, Kubernetes will automatically restart it, ensuring high availability.
Services
With pods running, we need a way to access them. That's where services come in. A service is an abstraction that defines a way to access one or more pods.
apiVersion: v1
kind: Service
metadata:
name: meu-app-service
spec:
type: NodePort
selector:
app: meu-app
ports:
- port: 3000
targetPort: 3000
nodePort: 30001
In this example, we create a service called meu-app-service, which exposes our pod on port 30001. Thus, we can access our application via the address http://<node-address>:30001.
## Ingress
For more advanced traffic management, we use **ingress**. An ingress allows control over access to services, typically through a load balancer. It is especially useful when we need to manage multiple services under a single domain.
```yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: meu-app-ingress
spec:
rules:
- host: meu-app.exemplo.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: meu-app-service
port:
number: 3000
With this configuration, we can access our service meu-app-service through the domain meu-app.exemplo.com.
```markdown
## ConfigMaps and Secrets
In real applications, we often have configurations that need to be kept separate from the code. For this, we use **ConfigMaps** and **Secrets**. A ConfigMap is used to store non-sensitive configuration data, while a Secret is intended for sensitive information, such as passwords.
Example of a ConfigMap:
```yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: my-app-config
data:
DATABASE_URL: "mongodb://localhost:27017/myapp"
And an example of a Secret:
apiVersion: v1
kind: Secret
metadata:
name: my-app-secret
type: Opaque
data:
PASSWORD: bXlwYXNzd29yZA==
In the Secret example, the value bXlwYXNzd29yZA== is the string "mypassword" encoded in base64.
## Scaling
One of the great advantages of Kubernetes is the ability to scale applications. We can increase or decrease the number of replicas of a pod with a simple command. To scale our deployment, we use:
```bash
kubectl scale deployment meu-app-deployment --replicas=5
This command will change the number of replicas of the deployment to five, allowing the application to support more users or workloads.
Minikube for Development
For those who are just starting and want to experiment with Kubernetes locally, Minikube is an excellent tool. It allows you to create a Kubernetes cluster on your local machine, making development and testing easier.
To install Minikube, you can follow the steps in the official documentation. After the installation, simply start the cluster with:
minikube start
Then, you can apply your YAML configurations directly to your local cluster.
Conclusion
Here, we explored the core concepts of Kubernetes in an accessible way. By understanding how pods, deployments, services, ingress, ConfigMaps, secrets, and application scaling work, you will be better prepared to tackle the challenges of managing containerized applications.
Practical takeaways:
- Pods are the smallest execution unit in Kubernetes, and they can contain one or more containers.
- Deployments manage the creation and maintenance of pods, ensuring high availability.
- Services expose pods for external access, making communication easier.
- Ingress allows advanced traffic management and routing.
- ConfigMaps and Secrets help manage configurations and sensitive information.
- Minikube is an ideal tool for developers who want to experiment with Kubernetes locally.
Post glossary
- node
- Node.js: a JavaScript runtime outside the browser, built on the V8 engine, used mainly for servers and command-line tools.
- service
- A software unit with a well-defined responsibility, exposed through an interface and reusable by other system parts.
- http
- Hypertext Transfer Protocol: the communication protocol used between browsers and web servers.
- mongodb
- A document-oriented NoSQL database that stores data in BSON, popular in apps with flexible schemas.
