1. 背景

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

2. 概念

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

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

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

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

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

3. K8s Vs Docker Swarm

4. 架构

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

(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) 设计思想:从更宏观的角度,以统一的方式来定义任务之间的各种关系

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

1) 主节点

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

2) 工作节点

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

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

4) 命令:

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

(2) Deployment

1) 执行格式:kubectl action resources

2) 执行流程

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

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

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

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

4) 命令:

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

(3) Pod

   

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

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

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

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

3) 命令:

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

(4) Service

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

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

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

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

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

2) 命令:

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

(5) Label

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

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

b. 嵌入版本标签

c. 使用标签分类对象

2) 命令:

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

(6) Scale

  

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

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

3) 命令

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

(7) Rollout Update

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

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

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

4) 支持如下操作

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

b. 回溯至以前的版本

c. 应用的CI/CD

5) 命令:

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

(8) ConfigMap

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

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

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

3) 命令:

  1. kubectl -n test create configmap game-config2 --from-file=game.properties --from-file=ui.properties
  2. kubectl -n test get configmap game-config2 -o yaml
  3. kubectl -n test describe configmap game-config2
  4. kubectl -n test create configmap game-config-env-file --from-env-file=game-env-file.properties
  5. kubectl -n test get configmap game-config-env-file -o yaml
  6. 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
  7.  
  8. kubectl -n test get configmap game-config2 -o yaml
  9. kubectl -n test describe configmap game-config2
  10. kubectl -n test create configmap game-config-env-file --from-env-file=game-env-file.properties
  11. kubectl -n test get configmap game-config-env-file -o yaml
  12. kubectl -n test create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm
  13. kubectl -n test create configmap env-config --from-literal=log_level=INFO
  14. kubectl -n test get configmap special-config -o yaml

(9)  基于yaml文件配置

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: dapi-test-pod
  5. namespace: test
  6. spec:
  7. containers:
  8. - name: test-container
  9. image: anjia0532/google-containers.busybox:1.27.2
  10. command: [ "/bin/sh", "-c", "env" ]
  11. env:
  12. - name: SPECIAL_LEVEL_KEY
  13. valueFrom:
  14. configMapKeyRef:
  15. name: special-config
  16. key: special.how
  17. restartPolicy: Never

命令:

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

Kubernetes系列:(1) 初探的更多相关文章

  1. 构建安全的Xml Web Service系列之初探使用Soap头

    原文:构建安全的Xml Web Service系列之初探使用Soap头 Xml Web Service 从诞生那天就说自己都么都么好,还津津乐道的说internet也会因此而进入一个新纪元,可5年多来 ...

  2. Kubernetes系列之Helm介绍篇

    本次系列使用的所需部署包版本都使用的目前最新的或最新稳定版,安装包地址请到公众号内回复[K8s实战]获取 介绍 Helm 是 Deis 开发的一个用于 Kubernetes 应用的包管理工具,主要用来 ...

  3. kubernetes系列之ConfigMap使用方式

    作用理解 核心用途就是容器和配置的分离解耦. 如启用一个mysql容器,mysql容器重要的文件有两部分,一部分为存储数据文件,一部分为配置文件my.cnf,存储数据可以用持久存储实现和容器的分离解耦 ...

  4. Kubernetes系列之理解K8s Service的几种模式

    今天给大家介绍下k8s的service的几种访问模式. 概述 我们知道pod的ip不是固定的,是根据所在宿主机的docker0网卡生成的,每次重启,更新,调度等情况IP都会变,那pod与pod之间需要 ...

  5. kubernetes系列03—kubeadm安装部署K8S集群

    本文收录在容器技术学习系列文章总目录 1.kubernetes安装介绍 1.1 K8S架构图 1.2 K8S搭建安装示意图 1.3 安装kubernetes方法 1.3.1 方法1:使用kubeadm ...

  6. kubernetes系列

    目录: 介绍的全部可以在github上找到,链接  haoprogrammer kubernetes学习:(一).kubeadm搭建kubernetes(v1.13.1)单节点集群 kubernete ...

  7. Kubernetes 系列(四):使用Traefik访问.net core api

    一. 准备 本篇的要求是在前三篇的基础上已经搭建好的本地k8s以及部署了Traefik,我们将会使用Traefik Ingress来访问.net core api,比较简单,做个记录,如果还没有搭建k ...

  8. openlayers5-webpack 入门开发系列一初探篇(附源码下载)

    前言 openlayers5-webpack 入门开发系列环境知识点了解: node 安装包下载webpack 打包管理工具需要依赖 node 环境,所以 node 安装包必须安装,上面链接是官网下载 ...

  9. leaflet-webpack 入门开发系列一初探篇(附源码下载)

    前言 leaflet-webpack 入门开发系列环境知识点了解: node 安装包下载webpack 打包管理工具需要依赖 node 环境,所以 node 安装包必须安装,上面链接是官网下载地址 w ...

  10. Windows玩转Kubernetes系列2-Centos安装Docker

    接上一章,Windows玩转Kubernetes系列1-VirtualBox安装Centos,我们开始学习如何在Centos中安装Docker 准备 关闭防火墙 防火墙一定要提前关闭,否则在后续安装K ...

随机推荐

  1. C#Task学习

    简介: Task 对象是一种的中心思想基于任务的异步模式首次引入.NET Framework 4 中. 因为由执行工作Task对象通常以异步方式执行线程池线程上而不是以同步方式在主应用程序线程中,可以 ...

  2. 同一个程序里有多个版本的App

    在Xcode中添加多个targets进行版本控制,就是同一个app开发多个版本 以Xcode 9.3 为例 1. 创建 点击左侧工程项目文件,选择TARGETS 下的项目右击选择 Duplicate. ...

  3. javascript按键盘上/右/下/左箭头加速运动

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. HDP 中 yarn 和 MR2 的配置

    以下说明均以集群中 slave 结点的配置为 48G内存,12块硬盘,12核(core) CPU 为例. 在 Yarn 中,一个 Container 是一个基础的包含内存和CPU 的单元.为了较好的平 ...

  5. centos7用docker安装elasticsearch5.6.13的主从

    说明: 准备2台机器,我这里有192.168.0.170 和 192.168.0.169 192.168.0.170 作为master 192.168.0.169 作为普通node 一.环境1.doc ...

  6. Squid代理服务器(一)——大家所用的游戏代理软件到底为何物?

    一.代理服务器应用场景分析 想当年大学时候宿舍控制网速,苦了我们这帮魔兽党,一到晚上工会活动我们就得和全校上万名学生抢网速,作为坦克的我总是因为网速问题导致团灭,咱也是有自尊的人,一怒之下花300元办 ...

  7. phpstudy下安装phalcon

    其实,一共也就下面几步,顺利的话,两分钟完事. 第一步:下载和当前php版本对应的php_phalcon.dll 文件 第二步:将此文件放到php版本下的ext里面. 第三步:在php.ini中添加如 ...

  8. if __name__ == '__main__'是什么意思?如何理解?看到一个很有用的解答

    小明.py 朋友眼中你是小明(__name__ == '小明'), 你自己眼中你是你自己(__name__ == '__main__'), 你编程很好, 朋友调你去帮他写程序(import 小明, 这 ...

  9. Jupyter Notebook 使用入门

    Jupyter Notebook 简介与安装 Jupyter Notebook 是一款开放源代码的 Web 应用程序,可让我们创建并共享代码和文档. 它提供了一个环境,你可以在其中记录代码,运行代码, ...

  10. explian执行计划

    MySQL为我们提供了 explain 关键字来直观的查看一条SQL的执行计划. explain显示了MySQL如何使用索引来处理select语句以及连接表,可以帮助选择更好的索引和写出更优化的查询语 ...