DEV Community

Cover image for KUBERNETES: FROM ZERO TO HERO (PART 4) - KUBERNETES CONFIGURATION FILES💥🔥
Samuel Ogunmola
Samuel Ogunmola

Posted on

KUBERNETES: FROM ZERO TO HERO (PART 4) - KUBERNETES CONFIGURATION FILES💥🔥

Kubernetes configuration files are used to define the desired state of a cluster and the resources that run on it. They are written in YAML or JSON format and are used to create, update, and delete resources in the cluster. A kubernetes config file consists of about 5 parts, the:

💎apiVersion: The version of the Kubernetes API that the configuration file is using. This field is used to ensure that the configuration file is compatible with the version of the API server.

💎kind: The type of resource that the configuration file is defining. This can be a pod, service, deployment, or any other resource type supported by Kubernetes.

💎metadata: Metadata about the resource, such as its name, labels, and annotations. This information is used to identify and classify the resource.

💎spec: The specification of the resource, including its properties and behavior. The exact contents of this field depend on the type of resource being defined.

💎status: The current status of the resource, including its conditions and any other relevant information. This field is usually generated by the API server and is not typically specified in the configuration file.

There are several types of resources that can be defined in a Kubernetes configuration file, including pods, services, and deployments. Each resource type has its own set of properties that can be specified in the configuration file.

Here are some examples of Kubernetes configuration files for different resource types:

Pods

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: my-container
    image: my-container-image:latest
    command: ["/bin/bash", "-c"]
    args: ["echo hello world"]
Enter fullscreen mode Exit fullscreen mode

This configuration file creates a pod called my-pod with a single container, my-container, based on the my-container-image:latest image. The container runs the echo hello world command.

Service

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: my-app
  ports:
  - protocol: TCP
    port: 80
    targetPort: 8080

Enter fullscreen mode Exit fullscreen mode

This configuration file creates a service called my-service that exposes the pods with the app: my-app label on port 80. The service routes traffic to the pods on port 8080.

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: my-container
        image: my-container-image:latest
        command: ["/bin/bash", "-c"]
        args: ["echo hello world"]
Enter fullscreen mode Exit fullscreen mode

This configuration file creates a deployment called my-deployment with three replicas of a pod. The pod contains a single container, my-container, which is based on the my-container-image:latest image and runs the echo hello world command.

ConfigMap

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-configmap
data:
  # Add key-value pairs here
  key1: value1
  key2: value2
  key3: value3

Enter fullscreen mode Exit fullscreen mode

This configuration file creates a configmap named my-configmap with three key-value pairs: key1: value1, key2: value2, and key3: value3.

Secrets

apiVersion: v1
kind: Secret
metadata:
  name: my-secret
type: Opaque
data:
  # Add key-value pairs here, with the value in base64 encoding
  key1: dmFsdWUx
  key2: dmFsdWUy
  key3: dmFsdWUz

Enter fullscreen mode Exit fullscreen mode

This configuration file creates a secret named my-secret with three key-value pairs: key1: value1, key2: value2, and key3: value3. The values are encoded in base64.

To create any resource in the cluster, you can use the kubectl create -f command, passing the configuration file as an argument, for example:

kubectl create -f configmap.yaml
Enter fullscreen mode Exit fullscreen mode

Overall, Kubernetes configuration files are a powerful tool for defining the desired state of a cluster and the resources that run on it. They can be used to create, update, and delete resources in the cluster and allow you to easily deploy and manage applications in a cluster.

🌟 🔥 If you want to switch your career into tech and you are considering DevOps, you can join our online community here for live classes and FREE tutorial videos.

Latest comments (0)