DevOps Classroomnotes 12/Feb/2022

Kubernetes API Objects

  • Everything containered in K8S is represented by RESTful resource, we refer to those resources as kubernetes objects.
  • The most basic command of viewing k8s objects is get.
kubectl get <resource-name> => all resources 
kubectl get <resource-name> <object-name> => to view the specific resource
  • To get all the resources of the k8s we can execute kubectl api-resources
    Preview
    Preview
    Preview
  • We can also view the k8s object in the json or yaml format kubectl get <resource-name> <object-name> -o json or kubectl get <resource-name> <object-name> -o yaml
    Preview

Pods

  • Lets assume we have an application which has webserver and database
    Preview
  • One Option is to create two containers in a Pod
    Preview
  • The above option is considered to be bad practice or antipattern
  • Generally we will have one container in a Pod to make scaling effective.
  • In some cases we might have two containers in a pod
  • maincar
  • sidecar
    Preview

Representing an K8s object in yaml file

  • Refer Here for understaning API and objects
  • In Kubernetes since resources are exposed as REST APIs, We have to understand API Versioining
  • Kubernetes groups resources in API Group
  • core: Referred as legacy group . In REST API => api/v1 and in yaml apiVersion: v1
  • named groups: In REST API apis/$GROUP_NAME/$VERSION and in yaml => apiVersion: $GROUPNAME/$VERSION
  • To describe a k8s object we have the following nested fields
  • Object Spec:
    • Here we describe the desired state of the object
  • Object Status
    • Actual state of object
  • When we try to write a desired state / k8s manifest file in yaml
apiVersion: <version>
kind: <kind of the resource>
metadata:
  name: <name of the object>
spec: 
  <object-spec>
  • Now lets look at the example which we have used earlier
apiVersion: v1
kind: Pod
metadata:
  name: httpd
spec:
  containers:
    - name: httpd
      image: httpd:latest
      ports:
        - containerPort: 80

  • When we execute the following command kubectl get pods httpd -o yaml
apiVersion: v1
items:
- apiVersion: v1
  kind: Pod
  metadata:
    annotations:
      kubectl.kubernetes.io/last-applied-configuration: |
        {"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"name":"httpd","namespace":"default"},"spec":{"containers":[{"image":"httpd:latest","name":"httpd","ports":[{"containerPort":80}]}]}}
    creationTimestamp: "2022-02-12T14:08:29Z"
    name: httpd
    namespace: default
    resourceVersion: "10096"
    uid: cac0c00f-f6e7-4684-9eb4-cf99e1ada436
  spec:
    containers:
    - image: httpd:latest
      imagePullPolicy: Always
      name: httpd
      ports:
      - containerPort: 80
        protocol: TCP
      resources: {}
      terminationMessagePath: /dev/termination-log
      terminationMessagePolicy: File
      volumeMounts:
      - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
        name: kube-api-access-zw8x6
        readOnly: true
    dnsPolicy: ClusterFirst
    enableServiceLinks: true
    nodeName: ip-172-31-33-127
    preemptionPolicy: PreemptLowerPriority
    priority: 0
    restartPolicy: Always
    schedulerName: default-scheduler
    securityContext: {}
    serviceAccount: default
    serviceAccountName: default
    terminationGracePeriodSeconds: 30
    tolerations:
    - effect: NoExecute
      key: node.kubernetes.io/not-ready
      operator: Exists
      tolerationSeconds: 300
    - effect: NoExecute
      key: node.kubernetes.io/unreachable
      operator: Exists
      tolerationSeconds: 300
    volumes:
    - name: kube-api-access-zw8x6
      projected:
        defaultMode: 420
        sources:
        - serviceAccountToken:
            expirationSeconds: 3607
            path: token
        - configMap:
            items:
            - key: ca.crt
              path: ca.crt
            name: kube-root-ca.crt
        - downwardAPI:
            items:
            - fieldRef:
                apiVersion: v1
                fieldPath: metadata.namespace
              path: namespace
  status:
    conditions:
    - lastProbeTime: null
      lastTransitionTime: "2022-02-12T14:08:29Z"
      status: "True"
      type: Initialized
    - lastProbeTime: null
      lastTransitionTime: "2022-02-12T14:08:31Z"
      status: "True"
      type: Ready
    - lastProbeTime: null
      lastTransitionTime: "2022-02-12T14:08:31Z"
      status: "True"
      type: ContainersReady
    - lastProbeTime: null
      lastTransitionTime: "2022-02-12T14:08:29Z"
      status: "True"
      type: PodScheduled
    containerStatuses:
    - containerID: docker://4cf2b8387d82cd5ddd280b4be549496c663bb0dfb05d950d7d86c1e58f05e66e
      image: httpd:latest
      imageID: docker-pullable://httpd@sha256:5cc947a200524a822883dc6ce6456d852d7c5629ab177dfbf7e38c1b4a647705
      lastState: {}
      name: httpd
      ready: true
      restartCount: 0
      started: true
      state:
        running:
          startedAt: "2022-02-12T14:08:31Z"
    hostIP: 172.31.33.127
    phase: Running
    podIP: 192.168.2.6
    podIPs:
    - ip: 192.168.2.6
    qosClass: BestEffort
    startTime: "2022-02-12T14:08:29Z"
kind: List
metadata:
  resourceVersion: ""
  selfLink: ""
  • To write manifests lets use the reference page
  • Refer Here for the reference links
  • Since we are using 1.23 we use Refer Here
  • Since we are writing Pod Manifest Refer Here
  • Activity 1: Create a Pod to run nginx container in it
  • Create a yaml and add apiVersion => v1 & kind => Pod
  • Now we need to add metadata Refer Here
  • To write Pod Spec Refer Here
  • Since we decided to use nginx container in the pod we use containers section
  • To describe container Refer Here
  • Refer Here for the pod manifest created
    Preview
  • Now delete the pod
    Preview
  • Activity 2: Lets create a Pod with mysql container and set the following environmental variables
  • MYSQL_ROOT_PASSWORD => root123
  • MYSQL_DATABASE => qtecommerce
  • MYSQL_USER => qtdevops
  • MYSQL_PASSWORD => qtdevops123
  • solution: Refer Here
    Preview
  • Lets login into the container
    Preview

Published
Categorized as Uncategorized Tagged

By continuous learner

devops & cloud enthusiastic learner

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Please turn AdBlock off
Floating Social Media Icons by Acurax Wordpress Designers

Discover more from Direct DevOps from Quality Thought

Subscribe now to keep reading and get access to the full archive.

Continue reading

Visit Us On FacebookVisit Us On LinkedinVisit Us On Youtube