Skip to content

Kubernetes Using configuration files

Links: 110 Kubernetes Index

  • We create YAML configuration files for deployments, services and other resources in k8s and then apply them (declarative approach).
  • The configuration file is our desired state. It is kubernetes job to see that the actual state == desired state.
  • Applying a file (creating resources):
    • kubectl apply -f deployment.yaml service.yaml
These files are also known as manifests
If you change anything in the file such as number of replicas or the image then you need to apply the yaml file again.
  • Deleting resources:

    • kubectl delete -f deployment.yaml service.yaml
  • Every configuration file will generally have 5 parts: apiVersion, kind, metadata , spec and status.

    • status is automatically generated by k8s. k8s gets the current status from etcd.
    • Attributes of spec are specific to the kind of the resource.
    • apiVersion is different for different component.

Simple Full Stack App Deployment

  • Architecture overview

    • attachments/Pasted image 20220829191505.jpg
  • labels in metadata

    • In k8s you can give any component a label.
    • A label is a key value pair that are attached to the k8s resources.
    • We can have anything as key and anything as value for labels.
      • attachments/Pasted image 20220829191843.jpg
    • You can label anything like pod, deployment, services, ConfigMap etc.
    • Labels are additional identifiers which are meaningful to the user.
    • Labels do not provide uniqueness. All pod replicas have the same label. But each pod does have a unique name.
      • attachments/Pasted image 20220829191904.jpg
      • Hence we can identify all the pod replicas of the same application/deployment using a specific label.
For pods label is a required field. For other components like config map, deployment etc labels are optional. But it is a good practice to set them.
So when we create pod replicas how does k8s know which pods belong to which deployment?

attachments/Pasted image 20220829192216.jpg
- This is defined using matchLabels - Line 15 and line 11 should be same. Lines 5,6 are optional. - You can have any name for key value pairs. Like instead of app: nginx you can have mykey: myvalue.

  • matchLabels in selector in service

    • We need a selector in service since it needs to forward the request to a pod.
    • Now to know which pods to forward this request to we have to use matchLabels.
    • attachments/Pasted image 20220829192640.jpg
  • Generally in k8s deployment and service configurations are kept in the same yaml file.

    • container port should be same as target port
    • We can have multiple yaml configurations in the same file separated by three dashes(---).
    • attachments/Pasted image 20220829192746.jpg
  • Sample yaml file which contains both service & deployment

    apiVersion: apps/v1
    kind: Deployment
      name: mongo-deployment
        app: mongo
      replicas: 1
          app: mongo
            app: mongo
          - name: mongodb
            image: mongo:5.0
            - containerPort: 27017
            - name: MONGO_INITDB_ROOT_USERNAME
                  name: mongo-secret
                  key: mongo-user
            - name: MONGO_INITDB_ROOT_PASSWORD
                  name: mongo-secret
                  key: mongo-password  
    apiVersion: v1
    kind: Service
      name: mongo-service
        app: mongo
        - protocol: TCP
          port: 27017
          targetPort: 27017

  • Reference: Nana Janashia / K8s-in-1-hour ยท GitLab

Last updated: 2022-08-29