Completek8s Classroomnotes 02/Aug/2023

Kubernetes Pods

  • There can be applications which are expected to run multiple processes. Docker goes with a golden rule of single process applications, In the case of k8s we can run each process in a container if we need
  • Sometimes we might need extra functionality to inject log exports, network monitoring, tracing, then we can add one more container as side car in your pod
    Preview
  • In k8s there are two ways of creating resources

    • imperative:
      • This is good for one time jobs as we create a command
    • declartive
      • This is good for repetitive tasks as we create a manifest
      • we create a desired state by expressing in a yaml file called as manifest
  • Composing YAML Files
apiVersion:
kind:
metadata:
spec:
  • Versioning Refer Here
  • kubernetes has resources which are grouped and there are stages for maturity levels and k8s apiVersion tries to express them all
apiVersion: <group>/<version>  # for all the other groups
apiVersion: <version> # if the group is core
  • Api reference Refer Here
    Preview
  • kind: This represents the resource which we are trying to create
  • metadata: This object represents information about the resources
    • name
    • labels
    • annotations
  • spec: This is specification of resource
  • Lets create a simple manifest file for nginx pod
apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
  - name: nginx
    image: nginx
  • Create a nginx.yaml file and execute by using kubectl apply -f nginx.yaml
    Preview
  • The equivalent in declarative is kubectl run nginx --image=nginx
  • Try to come up with a docker command

    • to run the alpine container with cmd overwritten to sleep 1d
      • docker command: docker container run -d --name alp alpine sleep 1d
    • to run the jenkins/jenkins container
  • Manifest for alpine docker container run -d --name alp alpine sleep 1d
---
apiVersion: v1
kind: Pod
metadata:
  name: alp
spec:
  containers:
    - name: alp
      image: alpine
      command:
        - sleep
        - 1d

Preview
* Manifest for jenkins docker container run -d --name jenkinscicd jenkins/jenkins

---
apiVersion: v1
kind: Pod
metadata:
  name: jenkinscicd
spec:
  containers:
    - name: jenkinscicd
      image: jenkins/jenkins

Activity

  • Write a Manifest for a Pod which runs two containers
    • name is first & image is alpine sleep 1d
    • name is second & image is ubuntu sleep 1d
---
apiVersion: v1
kind: Pod
metadata:
  name: mcpod
spec:
  containers:
    - name: first
      image: alpine
      command:
        - sleep
        - 1d
    - name: second
      image: ubuntu
      command:
        - sleep
        - 1d

Preview
* Login into alpine container

kubectl exec -it -c first  mcpod -- /bin/sh
  • login into ubuntu container
kubectl exec -it -c second  mcpod -- /bin/bash
  • Experiments:
    • Try creating a pod with
      • alpine container with sleep 10s
      • observe the failures after 10 s
    • Try creating a pod with 2 containers
      • one container put sleep 1d
      • other container put sleep 5s
      • observe failures
    • Try to run a jenkins container in docker and see the logs docker logs
    • Try running a container in docker with memory restrictions such as 256 MB RAM

Leave a Reply

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

About continuous learner

devops & cloud enthusiastic learner