YAML Syntax

    YAML is popular:
  1. Configuration files all written in YAML
  2. widely used format
  3. for different DevOps tools and applications
  4. e.g. Docker, Kubernetes,Ansible...

    YAML is a data serialization language, just like XML and JSON.
  1. Serialization language means that applications written with different technologies etc which have different data structures can transfer data to each other using a common agreed-on standard forma. YAML are not Markup language.
  2. YAML advantages:
    • human readable and intuitive
    • Same contents written in different languages:
       // YAML
       microservice:
         - app: user-authentication
           port: 9000
           version: 1.0
      
      // XML
      <microservices>
        <microservice>
          <app>user-authentication</app>
          <port>9000<port>
          <version>1.0<version>
        </microservice>
      </microservices><>
      
      
      // JSON
      {
        microservices: [
          {
            app: "user-authentication",
            port: 9000,
            version: '1.0'
          }
        ]
      }
                            
    • YAML uses line separation and indentation
    • Syntax of YAML:
      • key-value pairs
      • objects
      • comments
      • lists
      • booleans
      • multi line strings
      • environment variables
    • in kubernetes configuration file, we have
      key-value pairs
      metadata = object
      labels = object
      spec = object
      containers = list of objects
      
      # Minus sign (-) is used to indicate a list item in YAML syntax.
      # It signifies that "microservice" is a list and each item in the list is denoted by a hyphen followed by a space. Each item in the list represents a separate microservice.
      
      
      microservices:
        - user-authentication
        - shopping-cart
      
      microservice1s:
        - app: shopping-cart
          port: 9000
          versions: [1.9, 2.0, 2.1]
      
      singleLine: >
        this is a single line.
        that should be interpreted into single line.
      
      multipleLine: |
        this is part of  multiple lines .
        these are real multiple lines.
      
      # using dollar($) sign before environmental variables
      command:
        - '/bin/sh'
        - -ec
        - >-
          mysql -h 127.0.0.1 -u root -p$MYSQL_ROOT_PASSWORD -e 'SELECT 1'
      
      # use double curly brackets around the placeholder and this value gets replaced using template generator
      apiVersion: v1
      kind: Pod
      metadata:
        name: { { .Values.service.name } }
      
      # in one yaml file, you can define multiple components  and
      # you can separate these components using three dishes like ---
      ---
      apiVersion: v2
      kind: Pod
      
      
      apiVersion: v1
      kind: Pod
      data:
        mosquitto.conf: |
          log_dest stdout
          log_type all
          log_timestamp true
      metadata:
        name: nginx
        labels:
          app: nginx
      spec:
        containers:
        - name: nginx-container
          image: nginx
          ports:
          - containerPort: 80
          volumeMounts:
          - name: nginx-vol
            mountPath: /usr/nginx/html
        - name: sidebar-container
          image: some-image/curl
          command: ["/bin/sh"]
          args: ["-c", "echo Hello"]
      
      
                            
<<previous article

Front-end Security

next article>>

Table of Contents