Kubernetes基础功能教学

#Why Kubernetes?

  • Kubernetes(K8S)是一个可以帮助我们管理微服务(microservices)的系统,他可以自动化地部署及管理多台机器上的多个容器(Container)。简单来说,他可以做到:

    • 同时部署多个容器到多台机器上(Deployment)
    • 服务的乘载量有变化时,可以对容器做自动扩展(Scaling)
    • 管理多个容器的状态,自动侦测并重启故障的容器(Management)
  • 虚拟机布建方法vs货柜布建方法布建方法 :

    • 耗费资源多
    • 新增/删除管理较困难
      https://ithelp.ithome.com.tw/upload/images/20210705/20139199jFwUczFiwh.png

    #Kubernetes Install preparation

  • 安装好Docker的virtualbox VM

  • 准备Repository

  • 安装kubeadm(自动布署工具),kubelet(管理Pods),kubectl(人机介面)

    • apt-get install –y kubelet kubeadm kubectl
    • apt-mark hold kubelet kubeadm kubectl (锁定版本)
  • 确认版本

    • kubeadm version
    • kubectl version
  • 下载Kubernetes建置相关Image档案

    • kubeadm config images pull
  • 指定安装特定版本

    • apt-cache Madison kubeadm (搜寻特定版本)
    • apt-get install –y kubelet=[版本编号]
    • apt-get install –y kubectl=[版本编号]
    • apt-get install –y kubeadm=[版本编号]

#Kubernetes 基本元件

  • Kubernetes基本元件如下
    • Container : 运作单位,使用Docker Container运作基本功能
    • Pod : 服务单位,下面会有多个Container运行同样的微服务
    • Node : 就是一部电脑,下面会跑各种微服务(Pods),Kubernetes有两种角色
    • Master : 用来管理其他Nodes管理单位
    • Worker : 用来运行各种微服务的运作单位
    • Cluster : Kubernetes中一堆Nodes合起来的的总称
      https://ithelp.ithome.com.tw/upload/images/20210705/201391996b5vXLfVTi.png

#Kubernetes Architecture

https://ithelp.ithome.com.tw/upload/images/20210705/20139199ps7x3U16tu.jpg

#Kubernetes Install (Master)

  • 设定Kubernetes使用的VM的网路模式为桥接介面卡
  • 关闭swap功能
    • sudo swapoff –a (建议写在.bashrc里面,因为 重新开机的时候你会发现Kubernetes怎麽跑都跑不起来就是这个swap又自动开了)
    • 确认CPU数目>2 (不满2似乎会报Error,不过可以用—ingnore-preflight-errors=NumCPU关掉)
  • 自动布署kubernetes (Master)
    • sudo kubeadm init –pod-network-cidr=192.168.244.0/24
  • 设定本机为Kubernetes的使用者
    • mkdir -p $HOME/.kube
    • sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
    • sudo chown $(id -u):$(id -g) $HOME/.kube/config
  • 确认images都有正常运行
    • Kubectl get node (确认node有正常运行)
    • Kubectl get pods –all-namespaces (确认pods皆有正常运行)
  • 证实会看到 Coredns Ready状态为0这是因为CNI还没有安装 下一页会继续讲解如何安装
    https://ithelp.ithome.com.tw/upload/images/20210705/20139199ZA0YS58JNZ.jpg

#Kubernetes Install (Master)

  • 布署Flannel (Container Network Interface - CNI)
  • 检查运行状况
    • Kubectl get pods –all-namespaces
  • 处理CrashLoop问题 (Ubuntu会有的Bug)
    • Kubectl edit cm coredns –n kube-system
    • 删除文件内loop那行後存档
    • 使用Kubectl get pods –all-namespaces确认运行正常
  • 假设想用Master Node来建立pod无法运行问题
    • Kubectl taint nodes –all node-role.ku
  • 寻找系统运行问题可以使用
    • Kubectl describe pod [pod name]

https://ithelp.ithome.com.tw/upload/images/20210705/20139199FzqHD7pAgb.jpg

#Kubernetes Install (Worker)

  • 设定Kubernetes使用的VM的网路模式为桥接介面卡
  • 关闭swap功能
    • sudo swapoff –a (建议写在.bashrc里面,因为 重新开机的时候你会发现Kubernetes怎麽跑都跑不起来就是这个swap又自动开了)
    • 确认CPU数目>2 (不满2似乎会报Error,不过可以用—ingnore-preflight-errors=NumCPU关掉)
  • 将Worker Node加入Cluster
    • 找寻加入Cluster的Token Key Kubeclt token create –print-join-command (在Master Node 输入)
    • 在Worker Node输入Token Key 加入Cluster Sudo kubeadm join …. (上面查询到的字串)
  • 检查是否成功加入Worker Node
    • Kubectl get node (在Master Node 输入)
      https://ithelp.ithome.com.tw/upload/images/20210705/20139199RHRJ8qOice.jpg

#Kubernetes 基本指令

  • kubectl create –f [yaml档] : 建立kubenetes的功能元件
  • kubectl delete –f [yaml档] : 删除kubenetes的功能元件
  • kubectl get [all/pods/service/ingress] : 查询kubenetes功能元件运作状态
  • kubectl describe [all/pods/service/ingress] : 查询kubenetes功能元件详细资料
  • Kubectl edit [pod/deployments/service] [element Name] : 修改功能元件的属性配置

#建立Pods by Deployment

  • Yaml档案格式
    • 由Key : Value组成
    • 使用空格表示结构空格数目相同代表同个阶层
    • 使用(- )减号加一个空格表示list
    • 使用---分隔物件
  • 建立Deployment使用的yaml档案 :
    • ApiVersion : 元件版本号
    • Kind : 元件属性
    • metadata-name : pod名称
    • spec-replicas : 要建立多少个相同的Pod
    • Template : pod使用container的设定
    • Containers-image : 使用的映像档
    • Containers-ports : Container外接的Port
    • Selector : 指定这份文件要作用在哪,通常就跟template labels一致就可以
  • 布建Deployment
    • Kubectl create –f [deployment yaml file]
      https://ithelp.ithome.com.tw/upload/images/20210705/20139199dVid8OjR4C.jpg
  • Yaml File的内容
  • Yaml范例
Eample1:
  a : 1
  b :
    b-1 : 2
  c :
    - c-1 : 3
      c-2 : 4
---
Eample2 : 5
  • Demo范例 [Deploy.yaml]
apiVersion: apps/v1
kind: Deployment
metadata: 
  name: demoapp
spec: 
  replicas: 3
  template:  
    metadata:   
      labels:    
        app: demoapp  
    spec:   
      containers:
        - name: qbc  
          image: hcwxd/kubernetes-demo   
          ports:       
            - containerPort: 3000
  selector: 
    matchLabels:  
      app: demoapp

#Kubernetes Service

  • Service功能 : 把Pods的指定Port外接出去,就不用一个一个pod寻找IP
  • Service.yaml档案 :
    • Selector-app : 会去找pod template的Labels来对应取出pod
    • TargetPort : pod上要映射出去的port (IP 可由kubectl get pods –o wide找到)
    • port : service上将pods映射转接出来的port (IP可由kubectl get service找到)
    • nodePort : Node上将service转接出来的port (IP可由ifconfig找到)
  • 布建Service
    • Kubectl create –f [service yaml file]
  • Kubernetes Service 网路架构
    https://ithelp.ithome.com.tw/upload/images/20210705/20139199tQQLPoamHb.png
    https://ithelp.ithome.com.tw/upload/images/20210705/20139199WmoHeOH8Sz.png
  • Yaml File [Service.yaml]
apiVersion: v1
kind: Service
metadata: 
  name: my-service
spec:
  selector:  
    app: demoapp 
  type: NodePort
   ports:  
     - protocol: TCP    
       targetPort: 3000 
       port: 3001    
       nodePort: 30390

Kunbernetes布建Nginx-Ingress


#Kunbernetes Nginx-Ingress 网路架构

https://ithelp.ithome.com.tw/upload/images/20210705/20139199RMYNlEVjs0.jpg

Kubernetes Ingress

  • 建立好Deployment.yaml档案,并运行 :
    • kubectl create –f Deployment.yaml
  • 建立好Service.yaml档案 ,并运行 :
    • kubectl create –f Service.yaml
  • 建立好Ingress.yaml档案并运行 :
    • Kubectl create –f Ingress.yaml
  • 检查是否皆顺利运行 :
    • Kubectl get all
    • Kubectl get ingress –o wide
      https://ithelp.ithome.com.tw/upload/images/20210705/20139199bSxLWwMtJE.png
  • Yaml File [Service.yaml]
apiVersion: v1
kind: Service
metadata: 
  name: blue-service
spec: 
  selector: 
    app: blue-nginx 
  type: NodePort 
  ports: 
   - protocol: TCP  
     port: 80   
    targetPort: 3000
---
apiVersion: v1
kind: Service
metadata: 
  name: purple-service
spec: 
  selector: 
    app: purple-nginx 
  type: NodePort 
  ports: 
   - protocol: TCP  
     port: 80   
    targetPort: 3000
  • Yaml File [Deployment.yaml]
apiVersion: apps/v1
kind: Deployment
metadata: 
  name: blue-nginx
spec: 
  replicas: 2
  template:
    metadata:   
      labels:   
        app: blue-nginx 
  spec:   
    containers:    
      - name: nginx     
         image: hcwxd/blue-whale    
         ports:      
          - containerPort: 3000
  selector:
    matchLabels:
      app: blue-nginx
---
apiVersion: apps/v1
kind: Deployment
metadata: 
  name: purple-nginx
spec: 
  replicas: 2
  template:
    metadata:   
      labels:   
        app: purple-nginx 
  spec:   
    containers:    
      - name: nginx     
         image: hcwxd/purple-whale    
         ports:      
          - containerPort: 3000
  selector:
    matchLabels:
      app: blue-nginx
  • Yaml File [Ingress.yaml]

apiVersion: extensions/v1beta1
kind: Ingress
metadata: 
  name: web
spec:
  rules: 
   - host: blue.demo.com   
     http:     
      paths:     
       - backend:      
           serviceName: blue-service
           servicePort: 80  
   - host: purple.demo.com    
     http:   
       paths:     
        - backend:         
            serviceName: purple-service
            servicePort: 80

#Kubernetes Ingress

  • 查询Ingress external-IP是否正确设置
    • Kubebctl get svc –n ingress-niginx
  • 查询nginx-ingress-controller里面是否有把设置传进去
    • Kubectl get pod –n=ingress-nginx (查询pod名称)
    • Kubectl exec –n=ingress-nginx [pod Name] – [command]
  • 於Node设置网址转译
  • 於浏览器测试
    https://ithelp.ithome.com.tw/upload/images/20210705/20139199xX6PXuBegy.jpg

#Kubernetes Install Helm

  • Helm的功能 :
    • 藉由脚本(Chart)自动部属kubernetes的pods, service, ingress来完成微服务的建立
  • 下载Helm安装脚本
  • 将执行权限在开起来并执行
    • chmod 700 get_helm.sh
    • ./get_helm.sh
  • 建立helm的脚本template
    • helm create [专案名称]
      • 创立的档案 :
        • Values.yaml : 定义Chart中pod, service, ingress…使用的参数与设定
        • Templates/deployment.yaml : 吃Values.yaml的参数并建立pods
        • Templates/Service.yaml : 吃Values.yaml的参数并建立Services
        • Templates/Ingress.yaml : 吃Values.yaml的参数并建立ingress
  • 修改template的内容 [下一页]
  • 使用Helm快速布置脚本
    • Helm install [Chart Name] [摆放设定档的资料夹]

#Kubernetes Helm build

  • 检查运行成功与否 :
    • Kubectl get all
    • 开启服务测试
      https://ithelp.ithome.com.tw/upload/images/20210705/20139199X8fNNEqmxE.png
  • yamlFile [Value.yaml]
replicaCount: 2

image: 
  repository: hcwxd/blue-whale

service: 
  type: NodePort
  port: 80

ingress: 
  enabled: true
  hosts:  
   - host: blue.demo.com 
     paths: [/]

serviceAccount: 
  create: false
autoscaling: 
  enabled: false
  • yamlFile [Deployment.yaml]
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "demo.fullname" . }}
spec:
  replicas: {{ .Values.replicaCount }}
  template:
    metadata:
      labels: 
        app: {{ include "demo.fullname" . }}  
    spec:
      containers:
        - name: {{ .Chart.Name }}
          image: '{{ .Values.image.repository }}‘
          ports:       - containerPort: 3000
    selector:
      matchLabels:
        app: {{ include "demo.fullname" . }}
  • yamlFile [Service.yaml]
apiVersion: v1
kind: Service
metadata:
  name: {{ include "demo.fullname" . }}
spec:
  type: {{ .Values.service.type }}
  ports:
    - port: {{ .Values.service.port }}
      targetPort: 3000
      protocol: TCP
  selector:
    app: {{ include "demo.fullname" . }}
  • yamlFile [Ingress.yaml]
{{- if .Values.ingress.enabled -}}
{{- $fullName := include "demo.fullname" . -}}

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: web #{{ $fullName }}
spec:
  rules:
   {{- range .Values.ingress.hosts }}
    - host: {{ .host | quote }}
      http:
        paths:
          {{- range .paths }}
           - backend:
             serviceName:
               test-demo #{{ $fullName }}
              servicePort: 80
          {{- end }}
   {{- end }}
{{- end }}

#Reference

https://github.com/HcwXd/kubernetes-tutorial


<<:  解决SDK版本不一致的问题的方法

>>:  Day5 参加职训(机器学习与资料分析工程师培训班),Python程序设计

Day27:测试 Coroutine

Coroutine 是非同步程序的解决方案,我们将耗时的任务置放在 suspend 函式中,在正常的...

#4 - The Global Object &Function Expressions

今天来讲讲两个之後会用到的两个小观念: The Global Object &Function Ex...

【Day 06】C 的资料型态(下)

今天,我们来介绍一下常见的基本型别吧~ 基本型别 - 整数型别 - int int 型态是有正负号的...

【Day 05】领域驱动设计的启动

观察的视角 我们要如何描述一个系统呢? 可以从不同的角度观察,好比瞎子摸象,你摸到甚麽部位,系统就像...

岔路上的风景 - 递回

在学习JS的路上意外接触到了递回的概念,一开始觉得这是什麽鬼东西,回圈用的好端端的,为什麽需要学习难...