Overview of Kubernetes Deployment YAML Configuration
1. apiVersion: apps/v1
- Specifies the API version of Kubernetes to be used for this configuration.
apps/v1is the current stable API version forDeploymentobjects.
2. kind: Deployment
- Defines the type of Kubernetes object being created.
- A
Deploymentis used to manage a set of identical Pods, ensuring high availability and scaling.
3. metadata
Contains metadata (like name and labels) to uniquely identify the deployment.
metadata:
metadata:
name: notes-service
name: notes-service- Assigns a unique name (
notes-service) to this deployment. - This name can be used to reference the deployment in other Kubernetes configurations.
- Assigns a unique name (
4. spec
Defines the desired state of the deployment.
4.1 replicas
replicas: 1
- Specifies the number of pod instances to be created and maintained.
- Here,
1means only one replica of the pod will run. - For high availability, you might set this to a higher number.
4.2 selector
selector:
matchLabels:
app: notes-service
- Defines how Kubernetes identifies which pods belong to this deployment.
- The
matchLabelskey ensures that only pods with the labelapp: notes-serviceare managed by this deployment.
5. template
Defines the pod specification that will be created by this deployment.
5.1 metadata (within template)
metadata:
labels:
app: notes-service
- Assigns the label
app: notes-serviceto the pod. - This must match the selector label in the
spec.selector.matchLabels.
5.2 spec (within template)
Defines the specifications for the containers running inside the pod.
6. Containers
containers:
- name: notes-service
image: ram1uj/notes-app-notes-service
-
name: notes-service- The name of the container inside the pod.
- Used for logging and referencing within the pod.
-
image: ram1uj/notes-app-notes-service- Specifies the Docker image to be used for the container.
- The image
ram1uj/notes-app-notes-serviceis pulled from Docker Hub (or a private registry if configured). - The container is created using this image.
7. Environment Variables
env:
- name: DB_HOST
value: mysql
- name: DB_NAME
value: notes_db
- name: DB_USER
value: root
- name: DB_PASSWORD
value: password
-
Specifies environment variables for the container.
-
These variables can be used by the application inside the container.
-
DB_HOST: mysql- The database host is set to
mysql, meaning the application will connect to a MySQL service.
- The database host is set to
-
DB_NAME: notes_db- Specifies the database name as
notes_db.
- Specifies the database name as
-
DB_USER: root- Sets the database username as
root.
- Sets the database username as
-
DB_PASSWORD: password- Sets the database password as
password(should be managed using Kubernetes Secrets in production).
- Sets the database password as
8. Ports
ports:
- containerPort: 8100
- Specifies the container's exposed port.
8100is the port on which the application inside the container will listen.- Other services or pods can communicate with this application on port
8100.
9. Image Pull Policy
imagePullPolicy: Always
- Defines when Kubernetes should pull the image.
Alwaysmeans Kubernetes will always pull the latest image from the registry whenever the pod is created or restarted.- This is useful during development but can be changed to
IfNotPresentorNeverin production for performance reasons.
Best Practices
-
Use ConfigMaps & Secrets
- Instead of hardcoding environment variables (like
DB_PASSWORD), store them in a ConfigMap or Secret.
- Instead of hardcoding environment variables (like
-
Increase Replicas for High Availability
- If you want redundancy, set
replicasto a higher value.
- If you want redundancy, set
-
Use Resource Limits
- You can specify resource limits (
resources.requestsandresources.limits) for better control over CPU/memory usage.
- You can specify resource limits (