Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令

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

文章目录

  • Kubernetes from Zero to Hero
    • 0. What's K8s
    • 1. Kubernetes Architecture
    • 2. Main Kubernetes Components
      • 2.1 Node & Pod
      • 2.2 Service &Ingress
      • 2.3 ConfigMap & Secret
      • 2.4 Volume
      • 2.5 Deployment & StatefulSet
    • 3. Kubernetes Configuration
    • 4. Minikube & Kubeetcl - Setup k8s cluster locally
      • 4.1 MiniKube & Kubeetcl
      • 4.2 Deploy WebApp with MongoDB
        • 4.2.1 Demo Project Overview
        • 4.2.2 Create Config Files
        • 4.2.3 Deploy all resources in MiniKube cluster
      • 4.3 Interacting with K8s cluster
  • 5. Main Kubectl Commands

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

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第1张图片

  • control node - several important processes

    Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第2张图片

  • worker nodes

master nodes are much more important than worker nodes

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第3张图片

2. Main Kubernetes Components

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第4张图片

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第5张图片

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第6张图片

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第7张图片

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第8张图片

2.1 Node & Pod

Node: a virtual or physical machine

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第9张图片

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

2.2 Service &Ingress

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第10张图片

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第11张图片

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

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第12张图片

2.3 ConfigMap & Secret

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

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第13张图片

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第14张图片

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

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第15张图片

2.4 Volume

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第16张图片

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第17张图片

2.5 Deployment & StatefulSet

my-app died -> replicates everything!

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第18张图片

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

work with deployments instead of pods.

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第19张图片

! DB can’t be replicated via Deployment!

DB has state -> data inconsistence - > StatefulSet
Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第20张图片

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第21张图片

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

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第22张图片

3. Kubernetes Configuration

talk to API server

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第23张图片

C-M

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第24张图片

each configuration file has 3 parts

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第25张图片

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第26张图片

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第27张图片

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第28张图片

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第29张图片

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第30张图片

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第31张图片

4. Minikube & Kubeetcl - Setup k8s cluster locally

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

复查一下哪里出错了。

4.1 MiniKube & Kubeetcl

**MiniKube **

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第32张图片

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第33张图片

Master and Node processes run in ONE machine

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第34张图片

Node: docker runtime pre-installed.

Kubeetcl: Command line tool for K8s cluster - interact

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第35张图片

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第36张图片

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

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第37张图片

brew install hyperkit

Docker prefered!

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第38张图片

	minikube start --vm-driver docker

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

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

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第39张图片

Or Start your cluster

	minikube start --vm-driver=hyperkit

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第40张图片

get status of nodes

	kubectl get nodes
	minikube status

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第41张图片

4.2.2 Create Config Files

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第42张图片

**MongoDB Endpoint **

  • mongo-config.yaml

    Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第43张图片

  • mongo-secret.yaml

    Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第44张图片

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

    Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第45张图片

    Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第46张图片

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

      Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第47张图片

      Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第48张图片

    • Selector: which Pods belong to Deployment?

      Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第49张图片

    Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第50张图片

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

    simple to keep port and target port the same.

    Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第51张图片

    Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第52张图片

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第53张图片

How do we pass these environment variables to mongo application?

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第54张图片

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第55张图片

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第56张图片

same with password

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第57张图片

similar with webapp:

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第58张图片

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第59张图片

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第60张图片

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第61张图片

Configure external Service

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第62张图片

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第63张图片

4.2.3 Deploy all resources in MiniKube cluster

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第64张图片

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第65张图片

4.3 Interacting with K8s cluster

get all components in cluster

kubectl get all

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第66张图片

kubectl get pod | configmap | secret

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第67张图片

	kubectl --help

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第68张图片

	kubectl get pod
	kubectle logs 

Acess WebApp in Browser

kubectl get svc
minikube ip

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第69张图片

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

minikube service webapp-service --url

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第70张图片

5. Main Kubectl Commands

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

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第71张图片

kubectl create deployment nginx-delp --image=nginx

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第72张图片

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第73张图片replicaset : a layer between deployment and pod.

kubectl get replicaset

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第74张图片

Everything below Deployment is handled by k8s.

image-20231219201555312

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第75张图片

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

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第76张图片

kubectl describe pod [pod name] 

好墨迹呀!!
Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第77张图片

Enter the container

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

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第78张图片

Delete deplyment & Apply configuration file

kubectl delete deployment [name]

Configuration files is a better way.

kubectl apply -f [file name]

Summarize

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第79张图片

Kubernetes 从入门到实战 (feel more confident with me~ 进阶教程敬请期待!!) - k8s 架构及组件 | minikube实战 | kubectl指令_第80张图片

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

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

你可能感兴趣的:(云原生,kubernetes,架构,容器,云原生,docker,微服务,kubelet)