DevOps Classroomnotes 02/Jul/2022

K8s API

  • The core of k8s control plane is API Server.
  • This API Server exposes HTTP API that lets users, different parts o cluster and external components to interact with one another
  • Most of the operations which we perform are through kubectl cli interface.

K8s Objects

  • k8s objects are persistent entities in the k8s system
  • Object Spec and Status: Almost every k8s object includes two nested object fields
    • spec: Here we specify the description of the characteristice you want the resources to have: desired state
    • status: This describes the current state of an object.
  • To describe a k8s object we create a yaml file and pass it kubectl. kubectl converts the information to JSON when making the API request
  • Required Fields:
    • apiVersion: Which version of k8s api we are using to create the object
    • kind: what kind of object we want to create
    • metadata: Data that helps to uniquely identify the object
    • spec: what we desired to create.
  • API Versioning:
    • Levels of API Version
      • Alpha:
        • version contains alpha (v1alpha1)
        • Not recommended for production usage
      • Beta
        • version contains beta (v1beta1)
        • This is well tested & enabling is safe
        • The support for this feature will not be dropped but details might change
        • Not recommended for production usage
      • Stable
        • version name is vX where X is an integer (v1, v3)
        • Recommended for production usage
  • API Groups:
    * k8s api’s are grouped to make it easier to extend k8s API
    * There are several groups
    * core group: apiVersion: <version> => apiVersion: v1
    * other groups: apiVersion: <group>/<version> => apiVersion: batch/v1
  • Lets have a look at all the api-resources in k8s cluster
    Preview

Creating Pod in k8s using yaml

  • Start with basic skeleton
---
apiVersion:
kind:
metadata:
spec:
  • Since we need to create Pod, let use k8s api reference Refer Here
  • Since we need to create Pod Refer Here
    Preview
  • From the above image we can fill apiVersion & kind
---
apiVersion: v1
kind: Pod
metadata:
spec:
  • Lets focus on metadata
    Preview
  • Refer Here for the specification. It has name as a required field
---
apiVersion: v1
kind: Pod
metadata:
  name: hello-pod
spec:
  • Now lets focus on spec which is of type PodSpec Refer Here
---
apiVersion: v1
kind: Pod
metadata:
  name: hello-pod
spec:
  containers:
    - image: jenkins/jenkins:lts-jdk11
      name: jenkins
      ports:
        - containerPort: 8080

  • Now lets create the pod
    Preview
    Preview
  • Lets describe the pod in the yaml format and see how it looks like kubectl get pod hello-pod -o yaml
apiVersion: v1
kind: Pod
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"name":"hello-pod","namespace":"default"},"spec":{"containers":[{"image":"jenkins/jenkins:lts-jdk11","name":"jenkins","ports":[{"containerPort":8080}]}]}}
  creationTimestamp: "2022-07-02T15:37:11Z"
  name: hello-pod
  namespace: default
  resourceVersion: "7560"
  uid: 4f468d28-26df-45ac-913e-48696dab9011
spec:
  containers:
  - image: jenkins/jenkins:lts-jdk11
    imagePullPolicy: IfNotPresent
    name: jenkins
    ports:
    - containerPort: 8080
      protocol: TCP
    resources: {}
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    volumeMounts:
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: kube-api-access-p9d92
      readOnly: true
  dnsPolicy: ClusterFirst
  enableServiceLinks: true
  nodeName: ip-172-31-13-163
  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-p9d92
    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-07-02T15:37:11Z"
    status: "True"
    type: Initialized
  - lastProbeTime: null
    lastTransitionTime: "2022-07-02T15:37:31Z"
    status: "True"
    type: Ready
  - lastProbeTime: null
    lastTransitionTime: "2022-07-02T15:37:31Z"
    status: "True"
    type: ContainersReady
  - lastProbeTime: null
    lastTransitionTime: "2022-07-02T15:37:11Z"
    status: "True"
    type: PodScheduled
  containerStatuses:
  - containerID: docker://a1a8f8ccbda8c90d0bb45f7ee7d4139b65b1ebe721e11ddcd162fac42254f0e4
    image: jenkins/jenkins:lts-jdk11
    imageID: docker-pullable://jenkins/jenkins@sha256:f6dfae7da7e3f93e3b214a432cfa32e1d1e581986a0d03d5ba8f44d2b8860862
    lastState: {}
    name: jenkins
    ready: true
    restartCount: 0
    started: true
    state:
      running:
        startedAt: "2022-07-02T15:37:30Z"
  hostIP: 172.31.13.163
  phase: Running
  podIP: 172.17.0.2
  podIPs:
  - ip: 172.17.0.2
  qosClass: BestEffort
  startTime: "2022-07-02T15:37:11Z"

Preview
* To delete the pod kubectl delete -f hello-pod.yml

Leave a Reply

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

About continuous learner

devops & cloud enthusiastic learner