Kubernetes (k8s) is a container orchestration system that enables automated deployment, scaling, and management of applications. To define Kubernetes components, we use YAML files that describe how containers should be managed within the cluster.
Pod
A Pod is the smallest unit in Kubernetes. It can contain one or more containers. Pods are created and destroyed as needed. They are not persistent by default (but can use volumes to persist data).
Networking: Containers in the same Pod share the same IP address and ports.
Storage: Volumes can be mounted and shared between containers within the Pod.
Life Cycle: All containers in the Pod start and stop together. If the Pod is deleted, all containers within it are terminated.
Example of how to create a YAML for pods.
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: app
image: nginx
ReplicaSet
Ensures that a fixed number of Pod replicas are running.
If a Pod fails or is deleted, the ReplicaSet automatically creates a new Pod to replace it.
Selectors identify which Pods the ReplicaSet should manage.
Example of how to create a YAML for ReplicaSet.
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: my-replicaset
spec:
replicas: 3
selector:
matchLabels:
app: minha-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: app
image: nginx
Deployment
Manages application updates and rollbacks (without downtime). Manages ReplicaSets to ensure the desired number of Pod replicas.
Each Deployment update creates a new ReplicaSet, allowing control over different application versions. It gradually replaces old Pods with new ones, ensuring the application remains available during updates.
Example of how to create a YAML for Deployment.
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: app
image: nginx
StatefulSet
Used for applications that need a fixed identity, such as databases. Each Pod in a StatefulSet receives a unique and stable identifier, ensuring predictable network names and storage.
Ensures each Pod has an exclusive persistent volume, even after failures or restarts.
Maintains static network addresses for each Pod even after restarts.
Example of how to create a YAML for StatefulSet.
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: my-statefulset
spec:
serviceName: "my-service"
replicas: 2
selector:
matchLabels:
app: database
template:
metadata:
labels:
app: database
spec:
containers:
- name: db
image: mysql
Service
Defines how a set of Pods is accessed. Provides stable IP and DNS addresses to access Pods, even if they are recreated or moved.
Distributes network traffic among Pods that match the Service selector.
Example of how to create a YAML for Service.
apiVersion: v1
kind: Service
metadata:
name: my-servico
spec:
selector:
app: my-app
type: LoadBalancer
ports:
- protocol: TCP
port: 80
targetPort: 8080
ConfigMap
Stores configuration variables for Pods. Data in a ConfigMap is stored as key-value pairs, making it easy for Pods to access and use.
Using ConfigMaps separates application settings from code, making the application easier to manage. Settings can be changed without rebuilding the container image.
A ConfigMap has a maximum size of 1MB. For larger configurations, other solutions like persistent volumes may be needed.
Example of how to create a YAML for Service.
apiVersion: v1
kind: ConfigMap
metadata:
name: my-config
data:
APP_MODE: "production"
TIMEOUT: "30"
Conclusion
Kubernetes has several components that help manage applications in a scalable and reliable way. Its ability to efficiently orchestrate containers automatically allows development and operations teams to focus on creating business value, rather than worrying about infrastructure complexity.
Top comments (0)