Note

I will be taking multiple k8s tutorials and the gist of it I will summarize and write here.

"Cloud native technologies empower organizations to build and run scalable applications in modern, dynamic environments such as public, private, and hybrid clouds. Containers, service meshes, microservices, immutable infrastructure, and declarative APIs exemplify this approach." - CNCF

Cloud-native technologies are open-source projects designed to deploy and scale applications automatically by cloud computing services.

Pasted image 20240803075851.webp|800
source: ByteByteGo.com
Pasted image 20240803080220.webp|800
source: ByteByteGo.com
There are two main components of k8s:

  1. Control Plane or Master node is responsible for managing the state of the cluster.
    • There are four core components of the control plane:
      1. API Server
        • It exposes a RESTful API to communicate between the workers and Admin.
        • it also acts as a gatekeeper for authentication.
      2. etcd
        • It is a distributed key-value store. It stores a cluster's persistence state.
      3. Scheduler
        • It schedules the pods.
        • however, the actual work is done by the kubelet inside every node.
      4. Control Manager
        • It is responsible for running controllers to manage the state of the cluster.
        • A few examples like ReplicationController, DeploymentController.
        • It detects the state changes of a pod.
        • It makes changes to the scheduler.
  2. Worker nodes run the containerized application workloads.
    • Pods are the smallest deployable units in the k8s.
    • Pods can run 1 or more containers and provides shared storage and networking for them.
    • Pods are manged by the control plane.

Pasted image 20240803081845.webp|800
source: ByteByteGo.com
Further detailed components of the Worker nodes:

Pros

Cons

# Kubernetes Tutorial for Beginners

by Nana
!300

Minikube

!500

Commands

Description Command
Create deployment kubectl create deployment [name]
Edit deployment kubectl edit deployment [name]
Delete deployment kubectl delete deployment [name]
Status of different K8s components kubectl get nodes I pods I services I replicasets | deployments
Log to console kubectl logs [pod name]
Get Interactive Terminal kubectl exec -it [pod name] -- bin/bash
Apply a configuration file kubectl apply -f [file name]
Delete with configuration file kubectl delete -f [file name]
Get info about pod kubectl describe pod [pod namel

YAML Configuration file

Example of a deployment config:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.16
        ports:
        - containerPort: 80

Example of a service config:

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 8080

Each config file has three parts:

  1. Metadata
  2. Specification
    1. Inside the specification there will be a template, where it will have its own metadata and specification. This if for the pod.
  3. Status (Auto generated and managed by k8s)
    • k8s gets status data from etcd

Demo

Mongo Express Service --> Mongo Express Pod --> MongoDB service --> MongoDB pod

k8s Namespaces

Resources which can be shared across namespaces in a cluster

> kubectl api-resources --namespaced=false

NAME                              SHORTNAMES   APIVERSION                        NAMESPACED   KIND
componentstatuses                 cs           v1                                false        ComponentStatus
namespaces                        ns           v1                                false        Namespace
nodes                             no           v1                                false        Node
persistentvolumes                 pv           v1                                false        PersistentVolume
mutatingwebhookconfigurations                  admissionregistration.k8s.io/v1   false        MutatingWebhookConfiguration
validatingwebhookconfigurations                admissionregistration.k8s.io/v1   false        ValidatingWebhookConfiguration
customresourcedefinitions         crd,crds     apiextensions.k8s.io/v1           false        CustomResourceDefinition
apiservices                                    apiregistration.k8s.io/v1         false        APIService
selfsubjectreviews                             authentication.k8s.io/v1          false        SelfSubjectReview
tokenreviews                                   authentication.k8s.io/v1          false        TokenReview
selfsubjectaccessreviews                       authorization.k8s.io/v1           false        SelfSubjectAccessReview
selfsubjectrulesreviews                        authorization.k8s.io/v1           false        SelfSubjectRulesReview
subjectaccessreviews                           authorization.k8s.io/v1           false        SubjectAccessReview
certificatesigningrequests        csr          certificates.k8s.io/v1            false        CertificateSigningRequest
flowschemas                                    flowcontrol.apiserver.k8s.io/v1   false        FlowSchema
prioritylevelconfigurations                    flowcontrol.apiserver.k8s.io/v1   false        PriorityLevelConfiguration
etcdsnapshotfiles                              k3s.cattle.io/v1                  false        ETCDSnapshotFile
ingressclasses                                 networking.k8s.io/v1              false        IngressClass
runtimeclasses                                 node.k8s.io/v1                    false        RuntimeClass
clusterrolebindings                            rbac.authorization.k8s.io/v1      false        ClusterRoleBinding
clusterroles                                   rbac.authorization.k8s.io/v1      false        ClusterRole
priorityclasses                   pc           scheduling.k8s.io/v1              false        PriorityClass
csidrivers                                     storage.k8s.io/v1                 false        CSIDriver
csinodes                                       storage.k8s.io/v1                 false        CSINode
storageclasses                    sc           storage.k8s.io/v1                 false        StorageClass
volumeattachments                              storage.k8s.io/v1                 false        VolumeAttachment

Resources which can NOT be shared across namespaces in a cluster

> kubectl api-resources --namespaced=true

NAME                        SHORTNAMES   APIVERSION                     NAMESPACED   KIND
bindings                                 v1                             true         Binding
configmaps                  cm           v1                             true         ConfigMap
endpoints                   ep           v1                             true         Endpoints
events                      ev           v1                             true         Event
limitranges                 limits       v1                             true         LimitRange
persistentvolumeclaims      pvc          v1                             true         PersistentVolumeClaim
pods                        po           v1                             true         Pod
podtemplates                             v1                             true         PodTemplate
replicationcontrollers      rc           v1                             true         ReplicationController
resourcequotas              quota        v1                             true         ResourceQuota
secrets                                  v1                             true         Secret
serviceaccounts             sa           v1                             true         ServiceAccount
services                    svc          v1                             true         Service
controllerrevisions                      apps/v1                        true         ControllerRevision
daemonsets                  ds           apps/v1                        true         DaemonSet
deployments                 deploy       apps/v1                        true         Deployment
replicasets                 rs           apps/v1                        true         ReplicaSet
statefulsets                sts          apps/v1                        true         StatefulSet
localsubjectaccessreviews                authorization.k8s.io/v1        true         LocalSubjectAccessReview
horizontalpodautoscalers    hpa          autoscaling/v2                 true         HorizontalPodAutoscaler
cronjobs                    cj           batch/v1                       true         CronJob
jobs                                     batch/v1                       true         Job
leases                                   coordination.k8s.io/v1         true         Lease
endpointslices                           discovery.k8s.io/v1            true         EndpointSlice
events                      ev           events.k8s.io/v1               true         Event
helmchartconfigs                         helm.cattle.io/v1              true         HelmChartConfig
helmcharts                               helm.cattle.io/v1              true         HelmChart
addons                                   k3s.cattle.io/v1               true         Addon
ingresses                   ing          networking.k8s.io/v1           true         Ingress
networkpolicies             netpol       networking.k8s.io/v1           true         NetworkPolicy
poddisruptionbudgets        pdb          policy/v1                      true         PodDisruptionBudget
rolebindings                             rbac.authorization.k8s.io/v1   true         RoleBinding
roles                                    rbac.authorization.k8s.io/v1   true         Role
csistoragecapacities                     storage.k8s.io/v1              true         CSIStorageCapacity

Best Practices

Ingress

!600

apiVersion: networking.k8s.io/vlbetal
	kind: Ingress
	metadata:
		name: myapp-ingress
	spec:
		rules:
		- host: myapp.com
		  http:
			paths:
			- backend:
				serviceName: myapp-internal-service
				servicePort: 8080

TLS certificate

Check this

Helm Charts

k8s Volumes

There are three components:

  1. Persistent Volume (PV)
  2. Persistent Volume Claim (PVC)
  3. Storage Class (SC)

Storage requirements:

  1. Storage that doesn't depend on the pod lifecycle.
  2. Storage must be available on all nodes.
  3. Storage needs to survive even if cluster crashes.

Persistent Volume

PVC

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
	name: pvc-name
spec:
	storageClassName: manual
	volumeMode: Filesystem
	accessModes:
	- ReadWriteOnce
	resources:
		requests:
			storage: 10Gi

Now this PVC needs to be added in the pods configuration

apiVersion: vl
kind: Pod
metadata:
	name: mypod
spec:
	containers:
		- name: myfrontend
		  image: nginx
		  volumeMounts:
		  - mountPath: "/var/www/html"
			name: mypd
	volumes:
	- name: mypd
	  persistentVolumeClaim:
		claimName: pvc-name

Storage Class

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
	name: storage-class-name
provisioner: kubernetes.io/aws-ebs
parameters:
	type: iol
	iopsPerGB: "10"
	fsType: ext4

Roles

Statefulset

Stateful Stateless
Stores state/data Do not store state
Databases or
any app those store data
Frontend Application
Each request is different Each request is the same
deployed using statefulset deployed using deployments
created from same specification,
but not interchangeable!
identical and interchangable
can't be created/deleted at same time created in random order
with random hashes
can't be randomly addressed
replica Pods are not identical
- Pod Identity
one Service that
load balances to any Pod
The names are predictable and persisted random hashes get attached to the pod names

k8s Services

Source

Also Read

Thoughts 🤔 by Soumendra Kumar Sahoo is licensed under CC BY 4.0