How to remember the Kubernetes yaml syntax and structures? --help

damith144

Well-known member
  • Dec 7, 2021
    737
    276
    63
    mcahan k8s wala godak strctures variety ekak thiyenwane. e kiwwe replicaset, deployment services and other patterns. kohomada mewa ekin eka mathaka thiyaganne. ?mama prepare wenna patan gaththa CKA exam ekata. dn thama therenne meka
     

    NEMISIS

    Well-known member
  • Nov 13, 2013
    11,349
    19,445
    113
    Colombo
    For a real job, it does not matter. you can simply use an IDE or ChatGPT or plain old documentation to figure this out.

    If you are preparing for an exam,
    Write these yaml files using notepad or notepad++ or some other text editor without intele-sense or code suggestion.
     

    damith144

    Well-known member
  • Dec 7, 2021
    737
    276
    63
    For a real job, it does not matter. you can simply use an IDE or ChatGPT or plain old documentation to figure this out.

    If you are preparing for an exam,
    Write these yaml files using notepad or notepad++ or some other text editor without intele-sense or code suggestion.
    Yes I need to prepare for the exam CKA and CKS
     

    miran2012

    Well-known member
  • Apr 24, 2012
    1,149
    314
    83
    During the exam, you have access to the Kubernetes documentation. get the default YAML configuration and modify it as needed for your scenario.
     

    Djice

    Well-known member
  • Jan 17, 2011
    4,409
    3,778
    113
    out of fucked up land
    kubectl explain will essentially show you the documentation and the fields for a resource. for example, kubectl explain deployment

    you can check the documentation for the specific yaml manifest I guess. I've been working with k8s for almost 4 years and can't remember any of the manifests. I just copy paste them for the most part
     

    6h057

    Well-known member
  • Nov 19, 2024
    2,211
    2,099
    113
    Tokyo, Japan
    Hope the following helps:

    1. Pod​

    YAML:
    apiVersion: v1
    kind: Pod
    metadata:
      name: my-pod
      labels:
        app: my-app
    spec:
      containers:
      - name: my-container
        image: nginx
        ports:
        - containerPort: 80
        env:
        - name: ENV_VAR
          value: "value"
        volumeMounts:
        - name: my-volume
          mountPath: /path/in/container
      volumes:
      - name: my-volume
        emptyDir: {}

    2. Deployment​

    YAML:
    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: nginx
            ports:
            - containerPort: 80

    3. Service​

    YAML:
    apiVersion: v1
    kind: Service
    metadata:
      name: my-service
    spec:
      selector:
        app: my-app
      ports:
      - protocol: TCP
        port: 80
        targetPort: 9376
      type: ClusterIP

    4. ConfigMap​

    YAML:
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: my-config
    data:
      key1: value1
      key2: value2

    5. Secret​

    YAML:
    apiVersion: v1
    kind: Secret
    metadata:
      name: my-secret
    type: Opaque
    data:
      username: <base64-encoded>
      password: <base64-encoded>

    6. PersistentVolume (PV)​

    YAML:
    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: my-pv
    spec:
      capacity:
        storage: 10Gi
      accessModes:
        - ReadWriteOnce
      hostPath:
        path: /mnt/data

    7. PersistentVolumeClaim (PVC)​

    YAML:
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: my-pvc
    spec:
      accessModes:
        - ReadWriteOnce
      resources:
        requests:
          storage: 5Gi

    8. Job​

    YAML:
    apiVersion: batch/v1
    kind: Job
    metadata:
      name: my-job
    spec:
      template:
        spec:
          containers:
          - name: my-container
            image: busybox
            command: ["echo", "Hello, Kubernetes!"]
          restartPolicy: Never
      backoffLimit: 4

    9. CronJob​

    YAML:
    apiVersion: batch/v1
    kind: CronJob
    metadata:
      name: my-cronjob
    spec:
      schedule: "*/1 * * * *"
      jobTemplate:
        spec:
          template:
            spec:
              containers:
              - name: my-container
                image: busybox
                command: ["echo", "Hello, Kubernetes!"]
              restartPolicy: OnFailure

    10. Namespace​

    YAML:
    apiVersion: v1
    kind: Namespace
    metadata:
      name: my-namespace

    11. Ingress​

    YAML:
    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: my-ingress
    spec:
      rules:
      - host: my-app.com
        http:
          paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: my-service
                port:
                  number: 80

    12. Role​

    YAML:
    apiVersion: rbac.authorization.k8s.io/v1
    kind: Role
    metadata:
      name: my-role
      namespace: my-namespace
    rules:
    - apiGroups: [""]
      resources: ["pods"]
      verbs: ["get", "list", "create"]

    13. RoleBinding​

    YAML:
    apiVersion: rbac.authorization.k8s.io/v1
    kind: RoleBinding
    metadata:
      name: my-rolebinding
      namespace: my-namespace
    subjects:
    - kind: User
      name: my-user
      apiGroup: rbac.authorization.k8s.io
    roleRef:
      kind: Role
      name: my-role
      apiGroup: rbac.authorization.k8s.io

    14. ServiceAccount​

    YAML:
    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: my-serviceaccount

    15. Resource Quota​

    YAML:
    apiVersion: v1
    kind: ResourceQuota
    metadata:
      name: my-resourcequota
    spec:
      hard:
        requests.cpu: "1"
        requests.memory: 1Gi
        limits.cpu: "2"
        limits.memory: 2Gi

    16. HorizontalPodAutoscaler (HPA)​

    YAML:
    apiVersion: autoscaling/v2
    kind: HorizontalPodAutoscaler
    metadata:
      name: my-hpa
    spec:
      scaleTargetRef:
        apiVersion: apps/v1
        kind: Deployment
        name: my-deployment
      minReplicas: 1
      maxReplicas: 10
      metrics:
      - type: Resource
        resource:
          name: cpu
          target:
            type: Utilization
            averageUtilization: 50

    17. NetworkPolicy​

    YAML:
    apiVersion: networking.k8s.io/v1
    kind: NetworkPolicy
    metadata:
      name: my-networkpolicy
    spec:
      podSelector:
        matchLabels:
          app: my-app
      policyTypes:
      - Ingress
      - Egress
      ingress:
      - from:
        - podSelector:
            matchLabels:
              role: frontend
      egress:
      - to:
        - ipBlock:
            cidr: 10.0.0.0/24

    Key Fields to Remember

    • apiVersion: The API version (e.g., v1, apps/v1, batch/v1).
    • kind: The type of resource (e.g., Pod, Deployment, Service).
    • metadata: Includes name, labels, and namespace.
    • spec: Defines the desired state of the resource.
    • selector: Used to match resources (e.g., in Deployments and Services).
    • template: Defines the Pod template in Deployments and Jobs.
    • containers: List of containers in a Pod.
    • ports: Exposes container ports.
    • env: Environment variables for containers.
    • volumes: Storage volumes for Pods.

    1. ඉහත සඳහන් දේ මුද්‍රණය කරන්න හෝ practice කරන අතරතුර විවෘතව තබා ගන්න.
    2. ඔබේ YAML files cross-check කිරීමට එය භාවිතා කරන්න.
    3. ඔබ ඉගෙන ගන්නා පරිදි ඔබේම සටහන් හෝ උදාහරණ එක් කරන්න.
     
    Last edited: