Kubernetes, often simply referred to as K8s, is a powerful open-source platform designed for automating deployment, scaling, and management of containerized applications. It's widely used in production environments due to its robustness and flexibility. A significant part of working with Kubernetes involves creating and managing configuration files, often in YAML format. This article explores a practical Kubernetes example using YAML, diving into the details of a simple deployment and service configuration.
Understanding Kubernetes YAML Files
YAML Ain't Markup Language (YAML) is a human-friendly data serialization standard for all programming languages. In Kubernetes, YAML files are used to define the desired state of your cluster. They're easy to read and write, making them an excellent choice for configuration files. Let's start with a basic example of a Kubernetes deployment and service.
Kubernetes Deployment YAML Example
A Kubernetes deployment ensures that a specified number of pod replicas are running. If a pod dies, the deployment will create a new one. Here's a simple deployment YAML example:

```yaml apiVersion: apps/v1 kind: Deployment metadata: name: my-app-deployment spec: replicas: 3 selector: matchLabels: app: my-app template: metadata: labels: app: my-app spec: containers: - name: my-app image: my-app:1.0 ports: - containerPort: 8080 ```
Let's break down this YAML file:
apiVersionandkindspecify the API version and the type of resource we're creating (a deployment in this case).- The
metadatasection contains data that helps uniquely identify the deployment. - The
specsection defines the desired state of the deployment. Here, we're specifying that we want 3 replicas of the pod. - The
selectormatches the labels of the pods that should be managed by this deployment. - The
templatedefines the pods that will be created. It includes the pod's labels, and the container specification, including the container's name, image, and exposed ports.
Kubernetes Service YAML Example
A Kubernetes service exposes a network service that is accessible to client pods within the cluster. Here's how you can create a service for our deployment:
```yaml apiVersion: v1 kind: Service metadata: name: my-app-service spec: selector: app: my-app ports: - protocol: TCP port: 80 targetPort: 8080 type: LoadBalancer ```
This service will route traffic to the pods with the label app: my-app. The targetPort specifies which port on the pod to send the traffic to.

Applying Kubernetes YAML Files
To apply these YAML files to your Kubernetes cluster, you can use the `kubectl apply` command. For example:
```bash kubectl apply -f deployment.yaml kubectl apply -f service.yaml ```
This will create the deployment and service in your cluster according to the specifications defined in the YAML files.
Conclusion
Kubernetes YAML files are a powerful tool for managing your cluster's resources. By understanding the structure and components of these files, you can effectively define and control your deployments, services, and other Kubernetes objects. This article has provided a practical example to help you get started with Kubernetes YAML files. Happy coding!























