Kubernetes from Zero to Hero

this blog is consists of topics within intro to K8s.

Welcome to learn k8s with me!!! [Kubernetes Crash Course for Absolute Beginners NEW] - YouTube

0. What’s K8s

a container orchestration tool.

  • Trend from Monolith to Microservices.
  • Increased usage of containers.
  • Demand for a proper way managing those hundreds of containers.

the rise of containers in microservices technology actually resulted in applications that are now comprised of hundreds or sometimes maybe thousands of containers.

Managing those loads of containers across multiple environments using scripts and self-made tools an be really complex and sometimes even impossible.

  • High Availability or no downtime
  • Scalability or high performance
  • Disaster recovery - backup and restore.

1. Kubernetes Architecture

image-20231209095632045

  • control node - several important processes

    image-20231209095602769

  • worker nodes

master nodes are much more important than worker nodes

image-20231209095746464

2. Main Kubernetes Components

image-20231209095806577

image-20231210214631420

image-20231210214659459

image-20231210214713465

image-20231210214725234

2.1 Node & Pod

Node: a virtual or physical machine

image-20231209101129723

IP adjust every time the pod restart,so -> service

2.2 Service &Ingress

image-20231209101335836

image-20231209101642797

Ingress: cause the node-ip is not very practical.

image-20231209101739518

2.3 ConfigMap & Secret

change name: mogo-db-service -> mogo-db

image-20231209103152159

image-20231209104749709

! ConfigMap is for non-confidential data only! ->Secret

image-20231209105013422

2.4 Volume

image-20231210212555824

image-20231210212732924

2.5 Deployment & StatefulSet

my-app died -> replicates everything!

image-20231210212941381

Define blueprint for Pods: specify how many replicas you want to have

work with deployments instead of pods.

image-20231210213512410

! DB can’t be replicated via Deployment!

DB has state -> data inconsistence - > StatefulSet
image-20231210214006357

image-20231210214041123

however, deploying StatefulSet not easy -> common practice: DB hosted outside of k8s cluster.

image-20231210214534765

3. Kubernetes Configuration

talk to API server

image-20231210215309214

C-M

image-20231210215448223

each configuration file has 3 parts

image-20231210220119513

image-20231210220153661

image-20231210220240789

image-20231210220333405

image-20231210220601455

image-20231210220702462

image-20231210220809774

4. Minikube & Kubeetcl - Setup k8s cluster locally

  • what’s MiniKube & Kubeetcl?
  • How to setup?

复查一下哪里出错了。

4.1 MiniKube & Kubeetcl

**MiniKube **

image-20231214210102913

image-20231214210130478

Master and Node processes run in ONE machine

image-20231216102235791

Node: docker runtime pre-installed.

Kubeetcl: Command line tool for K8s cluster - interact

image-20231214212343080

image-20231214212425529

4.2 Deploy WebApp with MongoDB

4.2.1 Demo Project Overview

https://minikube.sigs.k8s.io/docs/start/

container or VM environment

本文参考环境:MacOS Sonoma 14.2

image-20231216203654456

brew install hyperkit

Docker prefered!

image-20231216204302058

	minikube start --vm-driver docker

😑 翻墙也不行,miniKube FAQ已经为我们提供方法权限问题按提示修改即可。

minikube start --image-repository='registry.cn-hangzhou.aliyuncs.com/google_containers'

image-20231216203825336

Or Start your cluster

	minikube start --vm-driver=hyperkit

image-20231216104202349

get status of nodes

	kubectl get nodes
	minikube status

image-20231216210947355

4.2.2 Create Config Files

image-20231216212650844

**MongoDB Endpoint **

  • mongo-config.yaml

    image-20231216224350693

  • mongo-secret.yaml

    image-20231216223106727

	echo -n mogouser | base64
	echo -n mogopassword | base64 
  • mongo.yaml - Deployment & Service in 1 file, because they belong together.

    image-20231216225653892

    image-20231217095947420

    • label: for pods, label is a required field. for other compoments, it is optional.

      image-20231217100442743

      image-20231217100916016

    • Selector: which Pods belong to Deployment?

      image-20231217101207800

    image-20231217101306926

  • Service: --- = you can have multiple YAML configurations within a file

    simple to keep port and target port the same.

    image-20231217102611741

    image-20231217103550008

image-20231217105216919

How do we pass these environment variables to mongo application?

image-20231217105309520

image-20231217113047139

image-20231217113336201

same with password

image-20231217114010439

similar with webapp:

image-20231217114336816

image-20231217114840152

image-20231217115125146

image-20231217115543446

Configure external Service

image-20231217115924194

image-20231217120318547

4.2.3 Deploy all resources in MiniKube cluster

image-20231217170223360

image-20231217171316293

4.3 Interacting with K8s cluster

get all components in cluster

kubectl get all

image-20231217171744566

kubectl get pod | configmap | secret

image-20231217172433199

	kubectl --help

image-20231217172605629

	kubectl get pod
	kubectle logs <podName>

Acess WebApp in Browser

kubectl get svc
minikube ip

不知道怎么回事,上面的不行,下面的行

minikube service webapp-service --url

image-20231219120228131

image-20231219171509186

5. Main Kubectl Commands

minikube start --image-repository='registry.cn-hangzhou.aliyuncs.com/google_containers'

image-20231219171923270

kubectl create deployment nginx-delp --image=nginx

image-20231219192123595

image-20231219195131335replicaset : a layer between deployment and pod.

kubectl get replicaset

image-20231219201508190

Everything below Deployment is handled by k8s.

image-20231219201555312

image-20231219202543346

So let’s make a change. ->deployment

kubectl edit deployment nginx-depl

Auto-genetated configuration file with default values:

Debug pods

kubectl logs [pod-name]

Eg.

kubectl create deployment mongo-depl --image=mongo
kubectl get pod 

kubectl logs mongo-depl-558475c797-sb8nb

image-20231220165437144

kubectl describe pod [pod name] 

好墨迹呀!!
image-20231220170348739

Enter the container

kubectl exec -it [pod name] -- bin/bash

image-20231220171301588

Delete deplyment & Apply configuration file

kubectl delete deployment [name]

Configuration files is a better way.

kubectl apply -f [file name]

Summarize

image-20231220190716912

image-20231220190753533

https://www.youtube.com/watch?v=UPJpmOKGKzU

看完三小时的课程后,学习openFaas,部署k3s+openfaas

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐