0%

k8s

Kubernetes的学习

Intro to K8s

What is Kubernetes?

Official Definition

Kubernetes, also known as K8s, is an open-source system for automating deployment, scaling, and management of containerized applications.

  • Open source container orchestration tool

  • Developed by Google

  • Helps you manage containerized applications in different deployment environments

    (physicl / virtual / cloud )

Problem-Solution case study

What are the tasks of an orchestration tool?

The need for a container orchestraton tool

The rise of containers and the microservice technology actually resulted in applications that they’re now comprised of hundreds or sometimes maybe even thousands of containers now managing those loads of containers across multiple environments using scripts and self-made tools can be really complex and sometimes even impossible

  • Trend from Monolith to Microcervices
  • Increased usage of container
  • Demand for a proper way of managing those hundreds of containers
What features do orchestration tools offer?
  • High Availability or no downtime

    high availability means that the application has no downtime so it’s always accessible by the users

  • Scalability or high performance

    application has high performance it loads fast and the users have a very high response rates from the application

  • Disaster recovery -Backup amd restore

    the infrastructure has to have some kind of mechanism to back up the data and to restore it to the latest state so that application doesn’t actually lose any data and the containerized application can run from the latest state after the recovery


Main K8s Components

Node and Pod

Node

a worker machine(工作机)

​ a simple server a physical virtual machine depends on the cluster

Node也被称为Worker或Minion,是部署容器(工作负载)的单机器(或虚拟机)

Pod

  • Smallest unit of K8s

  • Abstraction over container 容器上的抽象

    Containers should only be scheduled together in a single Pod if they are tightly coupled and need to share resources such as disk.

  • Usually 1 applicaion per Pod

  • Each Pod gets its onw IP address

  • New IP address on re-creation

Service and Ingress

Service

  • permanent IP address
  • lifecycle of Pod and Service NOT connected
External service

http://my-app-service-ip:port —> http://124.89.101.2:8080(test) —> https://my-app.cpm

Internal serivece

http://db-service-ip:port

Ingress

—External service

request goes first to ingress and it does the forwarding then to the service

Configmap and Secret

Use them as environment variables or as a properties file

tradition:

re-build —> push it to the repo —> pull it fro the repo

Configmap

  • external configuration of your application
  • Don’t put credential into Configamp

Secret

  • used to store secret data
  • based64 encoded
  • (The built-in security mechanism is not enabled by default)

Volumes

data storage

​ container pr pod restarted, the data will be gone

Volumes attached a physical storage on a hard drive to the pod

  • Storage on local machine
  • or remote, outside of the K8s cluster

distinction betweeen the kubernetes cluster and all of its components and the storage

think of the storage as an external drive plugged in into the kubernetes cluster

K8s doesn’t manage data persistance

Deployment and Stateful Set

The replica is connected to the same Service

Service has 2 functionalities:

  • permanent IP
  • load balancer

Deployment

  • blueprint for my-app pods
  • you create Deployments (can specify how many replicas and can also sacle up or down the number of pods needed)
  • abstraction on pods

in practice, mostly words with the deployment and not with pods

We need replicate database as well, but DB can’t be replicated via Deployment because database has a state. All need

to access to the shared data storage.

need a machanism to avoid inconsistencies (pods writing or reading)

StatefulSet

the feature in addition to replicationg feature is offered by StatefulSet

  • for STATEFUL applications

    like elastic, mongoDB, MySQL


Depolyment for stateLess Apps

StatefulSet for stateFul Apps or Databases

but deploying StatefulSet in k8s is not easy. So DB are ofthen hosted outside of K8s cluster.

Main Kubernetes Components summarized

  • abstraction of containers — Pod

  • communication — Service(in communication between pods)

  • roue traffic into cluster — Ingress(used to route traffic into the cluster)

  • external configuration — configmap/secret

  • data processing — volumes

  • pod blueprints with replicating mechanisms — Deployment / StatefulSet

    (StatefulSet is used specifically for stateful applications like databases)

Kubernetes Architecture explained

Node processes

Two types of nodes

  1. master
  2. slave

Worker machines in K8s cluster

  • each Node has multile Pods on it

  • 3 processes must be installed on every node

    Container runtime

    比如docker, 或者别的虚拟化技术

    Kubelet

    • Kubelet interacts with both - the container and node

    • Kubelet starts the pod with a container inside

    process schedules those pods then underneath

    a process of kubernetes itself

    responsible for taking configuration and actually running a pod or starting a pod with a container and then assigning resources from that node to the container (like cpu ram and storage resources)

    usually kubernetes cluster is made up of ,ultipe nodes which also must have container runtime and kubelets installed. you can have hundreds of those worker nodes which will run other parts and containers and replicas of the existing parts like my app and database


    communcation betweeen them uses Services, whihc is a sort of balance that basiclly cathes the request directed to the pod or the applcaition like database and then forwards it to the respective pod and the third process that is responsible for forwardiung requests form services to pods is actually Kube proxy

    Kube proxy

    Kube Proxy forwards the requests

    must be installed on every node

    intelligent forwarding logic inside that makes sure the communication works in a performative way with low overhead

  • Worker Nodes do the actual work


How to interact with this cluster?

How to:

  • sechedule pod?
  • monitor?
  • re-schedule /re-start pod?
  • join a new Node?

Managing processes are done bu Master Nodes

Master processes

4 processes run on every master node that control cluster state and the worker nodes

Api server
  • cluster gateway

    get the initial request of updates into the cluster or even the query from the cluster

    use some ui, like kubernetes dashboard, command line or api

  • acts as a gatekeeper for authentication

  • 流程:

    some requests —> API Server —> validates request —> other processes —> Pod

  • good for security because only 1 entrypoint into the cluster

Scheduler
  • 流程:

    Schedule Pod —> API Server —> Scheduler —> where to put the Pod —> Kubulet

Scheduler just decides on which Node new Pod should be scheduled

(Kubulet starts a podwith a container)

Controller manager
  • detect cluster state changes (like crashing of pods)

  • 流程:

    Controller Manager —> Scheduler —> Kubelet —> Pods

etcd

a key value store of a cluster state

  • etcd is the the cluster brain
  • Cluster changes stored in the key value store

cluster state data

  • Is the cluster healthy?
  • what resources are available?
  • Did the cluster state change

Application data is NOT stored in etcd


kubernetes cluster is made up of multiple masters where each master node runs its master processes, where the

  • api server is load lalanced
  • etcd store forms a Distributed storage across all master nodes

Example Cluster Set-Up

In a very small cluster, you probably have 2 Master Nodes and 3 +++Worker Nodes

and hardware reousrces of master and node servers differ

master — more important / less resources

worker nodes — more resources

Add new Master / Node server

  1. get new bare server
  2. install all the master / worker node process
  3. add it to the cluster

Minikube and Kubectl - Local Setup

What is minikube

Production Cluster Setup

  • Multiple master and Worker nodes
  • Separate virtual or physical machines

Test on local machine?

Test/Local Cluster Set up

minikube is basically one node cluster where the Master and Node processes run on ONE machine

This node will have docker runtime pre-installed, so could ru the containers or the pods with container on this node on your laptop through a vrtual box or some othe rhypervisor

minikube

  • creates Virtual Box on you rlaptop
  • Node runs in that Virtual Box
  • 1 Node K8s cluster
  • for testing purposes

What is Kubectl

need some way to interact with a cluster, want to create components configure etcd

a command line tool for kubernetes clusters

Master process — Api Server enable interaction with cluster (the main entrypoint into the kubernetes cluster)

if want to do anything to kubernetes, configure anything, create any components, you first talk to api server. You have to use different clients , so you have a ui like a dashboard you can talk to it using kubernetes api, or a command line tool which is kubectl

Kubectl is the most powerful of 3 clients

​ enable pods to run on node (create pods, destory pods, create services)

kubectl isn’t just for minikube cluster

kubectl is a tool used to interacts with any type of kubernetes cluster setup

Installation and create minikube cluster

(virtualization on the machine needed)

Step 1: Install Hypervisor

Step2:Install Minikube

image-20220419140131907

image-20220419140718492

安装Minikube的时候,kubectl也会安装好

image-20220419140640397

minikube start

有了docker就不用hyperkit

image-20220419140955358

image-20220419141159210

minikube start

image-20220419141401301

Kubectl get nodes

​ get status of nodes

image-20220419141430306

Minikube status

image-20220419141528902

kubectl version

image-20220419141644537

(Client version + Server Version indicate that minikube is correctlly installed)


Kubectl CLI

​ for configuring the Minikube cluster

Minikube CLI

​ for start up/deleting the cluster

Main Kubectl Commands - K8s CLI

Pre-requisities:

  • minikube installed
  • kubectl installed

Basic kubectl commands

Get status of different components / Create and Edit a Pod

  1. kubectl get nodes

image-20220419142304867

  1. kubectl get pods

image-20220419142346780

  1. Kubectl get services

image-20220419142437094

  1. kubectl create -h

    image-20220419142555745

    Pod is the smallest unit. But you are not creating pods …

    Deployment - abstraction over Pods

    Usage:

    kubectl create deployment NAME –image=image [–dry-run] [options]

    • Blueprint for creating pods
    • most basic configuration for deployment (name and image to use)
    • rest defaults
  2. kubectl create deployment nginx-depl –image=nginx

    image-20220419143121449

  3. kubectl get deploymentimage-20220419143152551

  4. kubectl get pod

    image-20220419143329719

  5. kubectl get replicaset

    Replicates is managing the replicas of a Pod

    image-20220419143719763


Layers of Abstraction

  1. Depolyment manages a rs
  2. ReplicaSet manages a pod
  3. Pod is an absratction of Container
  4. Container

Everything below Deployment is handled by Kubernetes


  1. kubectl edit deployment [name]

    and we get auto-generated configuration file with default values

    image-20220419144418780

​ 修改image版本

image-20220419144542842

Save the change image-20220419145203991

kubectl get pod检查

image-20220419145437036

We can get a new one with a new image

Kubectl get replicassets

image-20220419145625690

Debugging pods

  1. kubectl logs [pod name]

    image-20220419150020649

    image-20220419150137877

  2. Kubectl describe pod [pod name]

    image-20220419150251228

    image-20220419150318935

  3. kubectl exec-it [pod name] – bin/bash

    image-20220419150635772

  4. kubectl delete deployment mongo-depl

    image-20220419150759525

    image-20220419150926815

  5. kubectl get replicaset

    image-20220419151121482

  6. kubectl create deployment name omage option1 option2

kubectl apply -f [file name]


kubectl apply -f nginx-deployment.yaml

image-20220419152046702

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
apiVersion: apps/v1
kind: Deployment # what i want to create
metadata:
name: nginx-deployment # name of the deployment
labels:
app: nginx
spec: # for deployment
replicas: 1 # how many replicas of pods I want to create
selector:
matchLabels:
app: nginx
template: # the blueprint of pods
metadata:
labels:
app: nginx
spec: # for pod
containers:
- name: nginx
image: nginx:1.16 # fix image version
resources:
limits:
memory: "128Mi"
cpu: "500m"
ports:
- containerPort: 80 # where I want to bind

apply the configuration file

image-20220419152714534

change the number of replicas

image-20220419152829089

K8s knows when to create or update deployment

We can get

image-20220419153105258

Summarize the kubectl commands

CRUD commands

功能 指令
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 | pod | services | relicaset | deployment

Debugging pods

功能 指令
Log to console kubectl logs [pod name]
Get Interactive Terminal Kubectl exec -it [pod name] – bin/bash
Git info about pod kubectl describe pod [pod name]

Use configuration file for CRUD

功能 指令
Apply a configuration file kubectl apply -f [file name]
Delete with configuration file kubectl delete -f [file name]

K8s Yaml Configuration File

3 parts of a K8s configuration file

Service Template

image-20220419154624462

image-20220419154635700

Deployment Template

image-20220419154824687

image-20220419154839653

Each configuration file 3 parts

kind declares what we want to create

  1. metadata

    Name

  2. specification

    details we want to deploy

    Attributes of “spec“ are specfic to the kind

  3. status

    automatically generated and added by Kubernetes

    Desired != Actual ?

    K8s updates state continuously

Where does K8s get this status data?

etcd

Etcd holds the current status of any K8s component

Format of configuration file

YAML configuration file

智能,人性化,好修改;可以自主编辑然后能保存到云库再共享

Blueprint for pods (Template)

Deployment manage Pods

Template

image-20220419181143019

  • Has it’s own “metadata” and “spec” section
  • Applies to pod
  • Blueprint for a pod
    • Name?
    • Ports?
    • Image?

Connecting components

Labels & Selectors & Ports

Deployment metadata

image-20220419181429570

Service metadata

image-20220419181516872

connecting Deployment to Pods

any key-value pair for component

image-20220419181632363

Pods get the label through the template blueprint

image-20220419181727727

The label is matched by the selector in services

image-20220419181944314


Selector in service match labels (2) in deployment

Ports in Service and Pod

DB service —> (port:80) ngnix Service —> (targetPort:8080) Pod

image-20220419183403873

image-20220419183435668

对应service中的部分:

image-20220419183515353

more information

1
kubectl get pod -o wide

image-20220419183650580

Status automatically generated?
1
kubectl get deployment nginx-deployment -o yaml  > nginx-deployment-result.yaml

image-20220419184032325

删除配置和服务
1
2
kubectl delete -f nginx-deployment.yaml
kubectl delete -f nginx-service.yaml

image-20220419184227917

Hands-On Demo

Mongo-express + mongoDB

1
Kubectl get all

image-20220419184612946

create mongodb file

Pod blueprint

image-20220419184947067

mongodb port: 27017

image-20220419185113884

Deployment Config File is checked into repository

Username and password should not go here

Secret lives in k8s, not in the repository

Value: Username and password


Secret Configuration File

– kind: Secret

– metadata/name: a random name

– type: “Opaque” -default for arbitrary key - value pairs

– data: the actual contents - key-value pairs


加密用户名和密码

1
2
echo -n 'username' | base64
echo -n 'password' | base64

image-20220419190501641

Secret must be created before the Deployment

image-20220419190701444

Secret can be referenced now in Deployment

image-20220419192255490

image-20220419192500377

需要限制资源

image-20220419192541715

image-20220419194105278

Error:

1
2
kubectl get pods
kubectl describe pod [pod name]

image-20220419194249506

Possible Solution:

image-20220419195222059

Solution:

create secrets before deployment!!!!!

image-20220419195701568

创建ConfigMAp

image-20220419200805943

Database connected

image-20220419201006366

Create mongoexpress-service

How to make it an EXternal Service?

-type: “LoadBalancer”

BUT, internal service also acts as a loaderbalancer!

nodePort: for exteral IP address; set port you need to put into the browser

(30000-32767)

image-20220419201939104

1
minikube service mongo-express-service

Advanced Concepts

K8s Namespaces - Organize your Components

K8s Ingress

Helm - Package Manager

Volumes - Persisting Data in K8s

K8s StatefulSet - Deploying Stateful Apps

K8s Services