Kubernetes系列:(1) 初探

1. 背景

在部门内容组织了一次K8s的培训,普及了下K8s的概念、框架、操作等,为便于后期查阅,也为了进一步深究K8s,因此开展K8s系列,周期不定…

2. 概念

(1) 含义:来自希腊语,意为”舵手”,又称K8s

(2) 历史:2014年由Google创建,是十多年大规模容器管理技术Borg的开源版

(3) 功能:为容器化应用提供资源调度,即容器编排

严格意义:容器是将代码以及所有的依赖打包,以便应用能够快速运行,以及在环境间的可靠移植。

通俗意义:容器就像一个集装箱,将应用封装起来。这样应用和应用之间,就因为有了边界而不至于相互干扰;而被装进集装箱的应用,可以被方便的搬来搬去。

3. K8s Vs Docker Swarm

image

4. 架构

K8s并未将Docker作为整个架构的核心,仅仅把它作为最底层的一个容器运行时

image

(1) master

a. controller-manager:容器编排

b. uapi-server:提供api服务

c. scheduler:负责调度

(2) Etcd:用于a: K8s集群持久化数据; b: 由api-server处理后保存

(3) worker

1) kubelet

a. 负责同容器运行时打交道,定义容器运行时的各种操作

b. 依赖CRI远程调用接口(Container Runtime Interface)

c. 通过api-server同master通信

2) Container Runtime

a.同底层OS交互,将CRI请求转化为对Linux OS的调用

b. 依赖OCI协议(Open Container Interface)

3) Device Plugin

a. kubelet通过gRPC与Device plugin交互

b. 管理GPU宿主机物理设备,用于机器学习、高性能作业

4) Networking

a. kubelet调用网络插件为容器配置网络

b. 通过CNI协议(Container Networking Interface)交互

5) Volume Plugin

a. kubelet调用存储插件为容器配置持久化存储

b. 通过CSI协议(Container Storeage Interface)交互

5. 核心

(1) 设计思想:从更宏观的角度,以统一的方式来定义任务之间的各种关系

image

1) Pod

a. K8s的最小、最简单的单元

b. 代表集群中的运行进程

c. K8s可将多个容器划分为一个Pod,Pod中的容器共享同一个Network,同一组数据卷

2) Service

a. 对于容器来说,IP地址信息并非固定,Service声明IP地址与Pod绑定,提供固定IP地址

b. 作为Pod的代理入口,代替Pod对外暴露一个固定网络地址,以提供外部访问

3) Deployment

a. 管理Pod,如启动多个应用实例

4) Secret

a. 将鉴权信息(数据库密码)以Secret方式存储在Etcd中的键值对

b. 启动Pod应用时,可自动把Secret中的数据以Volume的方式挂载到容器中

5) Job

a. 描述一次性运行任务,如大数据任务

6) DaemonSet

a. 每个宿主机上必须且只能运行一个副本的守护进程服务

7) CronJob

a. 定时任务

8) Ingress

a. 为K8s的Service配置HTTP负载均衡器,将服务暴露给K8s集群外的客户端

9) StatefulSet

a. 管理有状态应用,提供Pod唯一标识

b. 保证部署和扩展Pod的顺序

10) ConfigMap

a. 容器应用的配置管理

6. 安装

a. https://github.com/opsnull/follow-me-install-kubernetes-cluster

b. https://git.xfyun.cn/container/kdeploy

c. https://kubernetes.io/docs/setup/independent/install-kubeadm/

d. https://kubernetes.io/docs/tasks/tools/install-minikube/

7. 实践

(1) Cluster

image

1) 主节点

a. 管理集群: 调度应用、维护应用所需状态、应用滚动更新

2) 工作节点

a. kubelet: 管理工作节点,并负责与主节点通信,处理容器操作

3) 工作节点与主节点通过api-server通信,开发者也可调用api-server

4) 命令:

kubectl version # 查询版本
kubectl cluster-info #查询集群的细节
kubectl get nodes #查询集群节点信息

(2) Deployment

image

1) 执行格式:kubectl action resources

2) 执行流程

a. 寻找合适工作节点运行应用实例

b. 调度应用在该节点上运行

c. 需要时在新节点上重新调度实例

3) 当工作节点上的应用挂掉或删除,K8s将替换并重启一个

4) 命令:

kubectl -n test run hello-world --replicas=2 --labels=“run=load-balancer-example” --image=anjia0532/google-samples.node-hello:1.0 --port=8080  # 创建Deployment
kubectl –n test get deployments #查询当前Deployment
(3) Pod

image    image

1) 一个Pod可以有多个容器,共享存储、网络等信息

2) 每个Pod具有独立且唯一的网络IP

a. 集群内的Pod和Service相互可见,集群外不可见

b. 通过kubectl proxy代理转发,实现外界与集群内Pod通信

3) 命令:

kubectl get object # 显示指定对象
kubectl describe object # 对象具体细节
kubectl logs pod #打印pod容器中的日志
kubectl exec pod #执行Pod容器命令

(4) Service

image

1) 定义一组逻辑Pod及访问Pod的策略,4种类型

a. ClusterIP(默认): 集群内为Service保留IP,仅集群内访问

b. NodePort: 使用NAT在集群的每个节点同一端口公开,可通过<NodeIp>:<NodePort>在集群外部访问

c. LoadBalancer: 外部负载均衡,为Service指定固定外部IP

d. ExternalName: 通过返回带有名称CNAME记录,使用任意名称公开服务,需kube-dns支撑

2) 命令:

kubectl -n test expose deployments/hello-world --port=8080 --type=“NodePort” # 公开服务
kubectl -n test get services # 查看服务
kubectl -n test describe service hello-world # 查看服务详情
curl 192.168.86.156:39018 #测试服务
kubectl -n test delete service hello-world #删除Service
kubectl -n test exec -ti hello-world-7b97bf7768-cldrm curl localhost:8080 # 验证容器内的服务仍在运行

(5) Label

1) 使用键值对存储,用途:

a. 指定测试、开发、生产环境的对象

b. 嵌入版本标签

c. 使用标签分类对象

2) 命令:

kubectl -n test get pods -l run=load-balancer-example # 使用标签查询Pod
kubectl -n test get services -l run=load-balancer-example # 使用标签查询Service
kubectl -n test label pod hello-world-7b97bf7768-cldrm app=v1 # 为Pod打新的标签

(6) Scale

image   image

1) 流量增加时,需对应用进行扩展

2) K8s基于Deployment中的副本数实现扩展

3) 命令

kubectl -n test scale deployments/hello-world --replicas=4 # 扩展副本集
kubectl -n test get pods -o wide #查看扩展
kubectl -n test describe deployments/hello-world # 查看deployment详情
kubectl -n test expose deployments/hello-world --port=8080 --type=“NodePort“ # 公开服务
kubectl -n test describe service hello-world # 查看暴露的端口
curl 192.168.86.1:11235

(7) Rollout Update

imageimage

imageimage

1) Deployment滚动更新时,Service将流量负载均衡至可用状态的Pod

2) Rollout Update时,Pods的最大可用数和新的Pod的最大创建你数,默认均为1,但可以设置

3) 滚动更新版本化,任何Deployment更新均可还原之前的版本

4) 支持如下操作

a. 将应用从一个环境推广到另一个环境

b. 回溯至以前的版本

c. 应用的CI/CD

5) 命令:

kubectl -n test describe pod hello-world-7b97bf7768-lftt5 # 显示Pod中的镜像
kubectl –n test set image deployments/hello-world hello-world=anjia0532/google-samples.hello-app:2.0 #通知Deployment使用不同镜像,滚动更新
kubectl -n test rollout status deployments/hello-world # 验证更新
kubectl -n test describe pod hello-world-855cb96d-qx827 #查看镜像是否更新
kubectl -n test rollout undo deployments/hello-world #版本还原
kubectl -n test describe pod hello-world-7b97bf7768-sbmq4

(8) ConfigMap

1) 允许配置与镜像内容分离,进而保持容器应用可移植性

2) 创建格式:kubectl create configmap <map-name> <data-source>

a. data-source: 可来自于文件、目录或字面值,均以键值对表示

3) 命令:

kubectl -n test create configmap game-config2 --from-file=game.properties --from-file=ui.properties
kubectl -n test get configmap game-config2 -o yaml
kubectl -n test describe configmap game-config2
kubectl -n test create configmap game-config-env-file --from-env-file=game-env-file.properties
kubectl -n test get configmap game-config-env-file -o yaml
kubectl -n test create configmap special-config --from-literal=special.how=very --from-literal=special.type=charmükubectl -n test create configmap game-config2 --from-file=game.properties --from-file=ui.properties

kubectl -n test get configmap game-config2 -o yaml
kubectl -n test describe configmap game-config2
kubectl -n test create configmap game-config-env-file --from-env-file=game-env-file.properties
kubectl -n test get configmap game-config-env-file -o yaml
kubectl -n test create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm
kubectl -n test create configmap env-config --from-literal=log_level=INFO
kubectl -n test get configmap special-config -o yaml

(9)  基于yaml文件配置

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
  namespace: test
spec:
  containers:
    - name: test-container
      image: anjia0532/google-containers.busybox:1.27.2
      command: [ "/bin/sh", "-c", "env" ]
      env:
        - name: SPECIAL_LEVEL_KEY
          valueFrom:
            configMapKeyRef:
              name: special-config
              key: special.how
  restartPolicy: Never

命令:

kubectl apply –f dapi-test-pod.yaml
kubectl –n test describe pod dapi-test-pod
kubectl –n test logs pods/dapi-test-pod

8. 参考

1) --help/-h: 如果对于某个命令不熟悉,可以直接在命令后增加--help或-h,查看用途及示例

2) 在线教程

a. https://www.katacoda.com/courses/kubernetes/

b. https://training.play-with-kubernetes.com/kubernetes-workshop/

9. 命令汇总

  1 1. Cluster
  2 # 查看K8s的版本
  3 kubectl version
  4 
  5 #查看集群信息
  6 kubectl cluster-info
  7 
  8 # 查看当前集群节点信息
  9 kubectl get nodes
 10 
 11 
 12 2. Deployment
 13 # 创建deployment
 14 kubectl -n test run hello-world --image=anjia0532/google-samples.node-hello:1.0
 15 # 查询当前deployments(完成部署时AVAILABLE才会为2)
 16 kubectl -n test get deployments
 17 
 18 
 19 3. Pod
 20 # 显示Pods
 21 kubectl -n test get pods
 22 
 23 # 显示指定Pod详情
 24 kubectl -n test describe pod hello-world-9d675f6bf-dg4bh
 25 
 26 # 打印容器日志(应用通常发送给STDOUT的任何内容,均会成为Pod容器中的日志)
 27 kubectl -n test logs hello-world-9d675f6bf-dg4bh
 28 
 29 # 查看容器内部的信息
 30 kubectl -n test exec hello-world-9d675f6bf-dg4bh env
 31 kubectl -n test exec hello-world-9d675f6bf-dg4bh -it bash
 32 
 33 # 查看服务
 34 curl localhost:8080
 35 
 36 
 37 4. Service
 38 # 使用NodePort方式公开服务
 39 kubectl -n test expose deployments/hello-world --port=8080 --type="NodePort"
 40 
 41 # 查看服务
 42 kubectl -n test get services
 43 
 44 # 查看服务详情
 45 kubectl -n test describe services hello-world
 46 
 47 # 验证公开服务 <NodeIp>:<NodePort>
 48 curl 192.168.86.156:29463
 49 
 50 # 删除Service
 51 kubectl -n test delete service hello-world
 52 
 53 # 查看Service
 54 kubectl -n test get services
 55 
 56 # 集群内的服务仍在运行
 57 kubectl -n test exec hello-world-675c948d88-c6df2 -it curl localhost:8080
 58 
 59 5. Label
 60 # 使用标签查询Pod
 61 kubectl -n test get pods -l run=hello-world
 62 
 63 # 使用标签查询Service
 64 kubectl -n test get services -l run=hello-world
 65 
 66 # 为Pod创建新的标签
 67 kubectl -n test label pod hello-world-675c948d88-c6df2 app=v1
 68 
 69 6. Scale
 70 # 扩展副本集
 71 kubectl -n test scale deployments/hello-world --replicas=2
 72 
 73 # 查看Pod
 74 kubectl -n test get pods -o wide
 75 
 76 # 查看Deployments的详情
 77 kubectl -n test describe deployment hello-world
 78 
 79 # 公开服务
 80 kubectl -n test expose deployments/hello-world --port=8080 --type="NodePort"
 81 
 82 # 查看公开的端口
 83 kubectl -n test get service -o wide
 84 
 85 # 测试负载均衡
 86 curl 192.168.86.156:18047
 87 
 88 7. Rollout Update
 89 # 查找Deployment中的镜像
 90 kubectl -n test describe deployment hello-world
 91 
 92 # 更新应用镜像,使用set image命令
 93 kubectl -n test set image deployments/hello-world hello-world=anjia0532/google-samples.hello-app:2.0
 94 
 95 # 验证更新
 96 kubectl -n test describe deployment hello-world
 97 kubectl -n test rollout status deployment hello-world
 98 
 99 # 版本还原(滚动更新异常,如镜像无法拉取等)
100 kubectl -n test rollout undo deployments/hello-world
101 
102 # 查看还原后的版本
103 kubectl -n test describe deployment hello-world
104 
105 8. ConfigMap
106 # 基于文件创建ConfigMap
107 kubectl -n test create configmap game-config --from-file=game.properties --from-file=ui.properties
108 
109 # 检测创建结果
110 kubectl -n test get configmap game-config -o yaml
111 
112 # 基于环境变量配置文件创建
113 kubectl -n test create configmap game-config-env-file --from-env-file=game-env-file.properties --from-env-file=ui-env-file.properties
114 
115 # 查看创建结果(当多次使用多个数据来源通过--from-env-file创建时,只有最后一个生效)
116 kubectl -n test get configmap game-config-env-file -o yaml
117 
118 # 基于字面值创建
119 kubectl -n test create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm
120 kubectl -n test create configmap env-config --from-literal=log_level=INFO
121 
122 # 查看字面值创建
123 kubectl -n test get configmap special-config  -o json
124 
125 # 基于yaml文件配置
126 kubectl apply -f dapi-test-pod.yaml
127 
128 # 查看该Pod
129 kubectl -n test describe pod dapi-test-pod
130 
131 # 查看输出日志是否生效
132 kubectl -n test logs pods/dapi-test-pod
View Code
原文地址:https://www.cnblogs.com/mengrennwpu/p/9902448.html