Skip to content

Rezakazemi890/KubernetesForHumans

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Kubernetes For Humans:

Kubernetes Features:

  • High Availability and No Downtime
  • Scalability and High Performance
  • Disaster Recovery

Kubernetes Components:

  • Pod (Smallest unit of k8s or abstraction over a container)

    • Usually, 1 app per pod
    • Each pod gets its own IP address (New IP address on recreation)
  • Service (Each pod has a service that provides by the pod, but the lifecycle of the pod is not connected to the service)

    • Has a permanent IP address
    • Service can be shared between replica nodes
    • Service has a load balancer
  • Ingress (Service Discovery of each service of the node)

  • ConfigMap (External configuration of the application)

  • Secret (Used to store secret data like credentials - base64 encoded)

  • Volumes (Data store component of Kubernetes that can store on a local machine or remote storage)

  • Node (A node can have one or more (Pod + Service)) like:

    • node1: app (Pod + Service) + db (Pod + Service) + Ingress + ConfigMap + Secret + Volumes
  • Deployment (One or more node(s) that have only one duty to do something) - for Stateless apps

  • StatefulSet (One or more node(s) that have only one duty to do something) - for Stateful apps

    Components.png Layers.png

Kubernetes Architecture:

  • About the worker node:
    • Each node has multiple Pods on it
    • 3 Processes must be installed on every node
      • Container runtime (like Docker or container d)
      • Kubelet (interacts with both the container and node)
      • KubeProxy (Communication with services and forwards the requests)
    • Worker nodes do the actual work
  • About the Master Node:
    • 4 Processes must be installed on every node
      • API Server (is a cluster gateway that clients can communicate with Kubernetes)
        • like a panel or CLI
        • get request then validate then send that for process
      • Scheduler (schedule new pod or schedule terminate pod)
        • Scheduler just decides on which Node new Pod should be scheduled
        • Scheduler Knows how much resource is used and needed, based on it decides new pod must schedule on which Node
      • Controller manager
        • detects cluster state changes (pod dies, or needs to reschedule)
        • if detects one of the states that must have a reaction, send a request to the scheduler to schedule that
      • etcd (is the cluster brain, cluster changes get stored in the key-value store)
        • note: application data is not stored in etcd.

Minikube and Kubectl:

  • Minikube
    • Used for test purposes.
    • Master and node processes run on one machine.
    • Has a preinstalled Docker runtime as a container runtime.
    • Creates Virtual Box on your machine, and nodes run on Virtual Box.
    • One Node k8s cluster
  • Kubectl
    • Enables interaction with the cluster (CLI)
    • Used for any type of cluster (Minikube cluster and Cloud cluster)

Main Kubectl Commands:

  • CRUD Commands
    • kubectl create deployment [name] --image=[image name]
    • kubectl edit deployment [name] --image=[image name]
    • kubectl delete deployment [name]
    • kubectl apply -f [DeploymentConfig.yaml]
  • Status of component
    • kubectl get nodes
    • kubectl get pod
    • kubectl get services
    • kubectl get replicaset
    • kubectl get deployment
  • Debug
    • kubectl logs [pod name]
    • kubectl exec -it [pod name] -- bin/bash

YAML Configuration file:

  • Sample:
    • Deployment & Service
apiVersion: v1
kind: PersistentVolume
metadata:
  name: nginx-pv
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data/nginx"
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nginx-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 500Mi
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.16
        ports:
        - containerPort: 8080
        volumeMounts:
        - mountPath: "/usr/share/nginx/html"
          name: nginx-storage
      volumes:
      - name: nginx-storage
        persistentVolumeClaim:
          claimName: nginx-pvc
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080

Service types

ServiceType.png

Sample Project

  • Steps:
    • Create mongo deployment file
    • Create mongo secret file
    • Execute Command: kubectl apply -f mongo-secret.yaml in the MongoDbSample directory
    • Add Secret reference to mongo deployment file
    • Execute Command: kubectl apply -f mongo.yaml
    • Add Service configuration in mongo deployment file after ---
    • Create mongo-express deployment file
    • Create mongo configmap file
    • Execute Command: kubectl apply -f mongo-configmap.yaml in the MongoDbSample directory
    • Add Service configuration in mongo-express deployment file after ---
    • Execute Command: kubectl apply -f mongo-express.yaml in the MongoDbSample directory
    • You can see the status with the command: kubectl get all | grep mongo
    • To delete the deployment, execute the command: kubectl delete deployment [deployment name]
    • To delete the service, execute the command: kubectl delete services [service name]

Namespace

  • Default namespaces
  • Execute the command: kubectl get namespace
    • default (if you don't create a namespace your resource is located here)
    • kube-public (contains public data like configmap, cluster information, ...)
    • kube-system (Don't create or modify this)
    • kube-node-lease (heartbeats of nodes, each node has a lease object)
    • kubernetes-dashboard
  • You can create a namespace with the command: kubectl create namespace my-namespace
  • You can set the namespace of the component by two approaches:
    • use --namespace=my-namespace for example: kubectl apply -f mongo-configmap.yaml --namespace=my-namespace
    • use namespace: my-namespace in the yaml file under metadata:

Ingress

  • When we don't want to access the internal service IP address

and we want to use a domain name and secure protocol, use the Ingress component. For example, use https://my-app.com instead of http://124.8.2.1:35000

Helm Package

  • Helm is a package manager for Kubernetes
  • Helm can help to package YAML files.
  • Helm Chart:
    • bundle of YAML files
    • Create your Own Helm Chart
    • Push them to Helm Repository
    • Download and use the existing one (reusable)
  • Used for complex setups (Like monitoring apps that have ELK, Grafana, Prometheus, ...)
  • You can share Helm Charts Configurations. Or use other configurations in Helm hub or use the command helm search <Keyword>
  • Helm has a Template Engine for Same deployments.
  • Another feature of Helm is you can deploy the same Applications to different environments.
  • The directory structure must be like my-app-chart directory.
  • You can execute helm install chart.yaml to install all packages and dependencies.
  • If you have dependencies you can create requirements.yaml and execute helm dependency update in this section dependency added to the charts folder.
  • Helm version 2 has two parts:
    • Client(Helm CLI)
    • Server(Tiller) But Tiller got removed in version 3 because it has security issues.

Persistent Volume

  • You can use all types of storage to store your persistent volumes like: local, cloud, nfs, ...
  • To have a local volume:
  • Storage class can create persistent volumes dynamically.

Here's a sample for two related apps with kubernetes config and run guid

Weather and Caller Service Setup

This repository contains two applications, WeatherService and CallerService. Follow the instructions below to build, push, deploy, and test these services locally.

Prerequisites

  • Docker installed and running
  • Kubernetes cluster setup with local registry (localhost:5000)
docker run -d -p 5000:5000 --restart=always --name registry hub.indraproject.ir/hubproxy/library/registry:2
  • kubectl installed and configured

Steps

1. Build and Push Docker Images
WeatherService
cd RelatedAppsSample/WeatherService 

docker build -t localhost:5000/weather-service . 

docker push localhost:5000/weather-service

CallerService
cd ../RelatedAppsSample/CallerService

docker build -t localhost:5000/caller-service .

docker push localhost:5000/caller-service

2. Deploy Applications to Kubernetes
WeatherService
cd ../RelatedAppsSample/WeatherService 

kubectl apply -f weather-service.yaml

CallerService
cd ../RelatedAppsSample/CallerService

kubectl apply -f caller-service.yaml

3. Clean Up Resources

To delete the services from Kubernetes, run the following commands:

kubectl delete -f weather-service.yaml

kubectl delete -f caller-service.yaml

Notes
  • Ensure your local Docker registry (localhost:5000) is configured for Kubernetes.
  • Each service uses YAML configuration files (weather-service.yaml and caller-service.yaml) to define deployments and services.
  • Modify NodePort values in the YAML files if ports are occupied.

Releases

No releases published

Packages

No packages published