Kubernetes实战总结
<hr><h1><span style="color: rgba(0, 0, 255, 1)">>>> 目录 <<<</span></h1>
<hr>
<h2><span style="color: rgba(255, 102, 0, 1)">一、概述</span><br><span style="color: rgba(255, 102, 0, 1)">二、核心组件</span><br><span style="color: rgba(255, 102, 0, 1)">三、基本概念</span><br><span style="color: rgba(255, 102, 0, 1)">四、系统架构</span><br><span style="color: rgba(255, 102, 0, 1)">五、镜像制作</span><br><span style="color: rgba(255, 102, 0, 1)">六、服务编排</span><br><span style="color: rgba(255, 102, 0, 1)">七、持续部署</span><br><span style="color: rgba(255, 102, 0, 1)">八、故障排查</span></h2>
<p> </p>
<hr>
<h1> <span style="color: rgba(0, 0, 255, 1)">>>> 正文 <<<</span></h1>
<hr>
<h1> <span style="color: rgba(153, 51, 0, 1)"><strong>一、 </strong><strong>概述</strong></span></h1>
<p><img src="https://img2020.cnblogs.com/blog/1059616/202005/1059616-20200506160121529-1075155306.png"></p>
<p><span style="font-size: 15px"><span style="color: rgba(255, 0, 0, 1)">Kubernetes是容器集群管理系统</span>,是一个开源的平台,可以实现容器集群的自动化部署、自动扩缩容、维护等功能。Kubernetes特点<strong>:</strong></span></p>
<p align="left"><span style="font-size: 15px">☛ <strong>可移植:</strong> 支持公有云,私有云,混合云,多重云</span></p>
<p align="left"><span style="font-size: 15px">☛ <strong>可扩展:</strong> 模块化, 插件化, 可挂载, 可组合</span></p>
<p align="left"><span style="font-size: 15px">☛ <strong>自动化:</strong> 自动部署,自动重启,自动复制,自动伸缩/扩展</span></p>
<p align="left"> </p>
<p align="left"> </p>
<hr>
<h1><span style="color: rgba(153, 51, 0, 1)"><strong>二、 </strong><strong>核心组件</strong></span></h1>
<p> <img src="https://img2020.cnblogs.com/blog/1059616/202005/1059616-20200506160147297-1734941214.png"></p>
<h3><strong>1) </strong><strong>主要组件</strong></h3>
<p><span style="font-size: 15px; color: rgba(0, 0, 255, 1)">● <strong>etcd</strong><strong>:</strong>保存了整个集群的状态;</span></p>
<p><span style="font-size: 15px; color: rgba(255, 0, 0, 1)">● <strong>apiserver</strong><strong>:</strong>提供了资源操作的唯一入口,并提供访问控制、API注册和发现等机制;</span></p>
<p><span style="font-size: 15px">● <strong>scheduler</strong><strong>:</strong>负责资源的调度,按照预定的调度策略将Pod调度到相应的机器上;</span></p>
<p><span style="font-size: 15px">● <strong>controller</strong> <strong>manager</strong><strong>:</strong>负责维护集群的状态,比如故障检测、自动扩展、滚动更新等;</span></p>
<p><span style="font-size: 15px; color: rgba(0, 0, 255, 1)">● <strong>kubelet</strong><strong>:</strong>负责维护容器的生命周期,同时也负责数据卷(CVI)和网络(CNI)的管理;</span></p>
<p><span style="font-size: 15px; color: rgba(255, 0, 0, 1)">● <strong>kube-proxy</strong><strong>:</strong>负责为Service提供集群内部的服务发现和负载均衡;</span></p>
<p><span style="font-size: 15px">● <strong>Container runtime</strong><strong>:</strong>负责镜像管理以及Pod和容器的真正运行(CRI);</span></p>
<p> </p>
<h3><strong>2) </strong><strong>扩展组件</strong></h3>
<p><span style="font-size: 15px; color: rgba(0, 0, 255, 1)">● <strong>kube-dns</strong><strong>:</strong>负责为整个集群提供DNS服务</span></p>
<p><span style="font-size: 15px">● <strong>Metrics</strong><strong>:</strong>提供资源监控</span></p>
<p><span style="font-size: 15px">● <strong>Dashboard</strong><strong>:</strong>提供GUI</span></p>
<p><span style="font-size: 15px; color: rgba(0, 0, 255, 1)">● <strong>Ingress Controller</strong><strong>:</strong>为服务提供外网入口</span></p>
<p><span style="font-size: 15px">● <strong>Federation</strong><strong>:</strong>提供跨可用区的集群</span></p>
<p><span style="font-size: 15px">● <strong>Fluentd-elasticsearch</strong>:提供集群日志采集、存储与查询</span></p>
<p align="left"> </p>
<p align="left"> </p>
<hr>
<h1><span style="color: rgba(153, 51, 0, 1)"><strong>三、 </strong><strong>基本概念</strong></span></h1>
<p><img src="https://img2020.cnblogs.com/blog/1059616/202005/1059616-20200506160220501-101937147.png"></p>
<h3><strong>1) </strong><strong>集群管理</strong></h3>
<p><span style="font-size: 15px">◆ <strong>Master</strong><strong>:</strong>K8s集群的管理节点,负责整个集群的管理和控制。</span></p>
<p><span style="font-size: 15px">◆ <strong>Node</strong><strong>:</strong>K8s集群的工作节点,负责集群中的工作负载。</span></p>
<p><span style="font-size: 15px">◆ <strong>Namespace</strong><strong>:</strong>为K8s集群提供虚拟的隔离作用。</span></p>
<p><span style="font-size: 15px">◆ <strong>Label</strong><strong>:</strong>通过给指定<span style="color: rgba(255, 0, 0, 1)">资源捆绑</span>一个或多个不同的资源标签,来实现多维度的资源分组管理。</span></p>
<p> </p>
<h3><strong>2) </strong><strong>资源管理</strong></h3>
<p><span style="font-size: 15px; color: rgba(255, 0, 0, 1)">◆ <strong>Pod</strong><strong>:</strong>K8s集群中运行部署应用的最小单元,可以支持多容器。</span></p>
<p><span style="font-size: 15px">◆ <strong>RC</strong><strong>:</strong>K8s集群中最早的保证Pod高可用的API对象,之后扩展匹配模式新增了<strong>RS</strong>。</span></p>
<p><span style="font-size: 15px; color: rgba(255, 0, 0, 1)">◆ <strong>Deployment</strong><strong>:</strong>一个应用模式更广的API对象,通过操作RS进行创建、更新、滚动升级服务。</span></p>
<p><span style="font-size: 15px; color: rgba(0, 0, 255, 1)">◆ <strong>StatefulSet</strong><strong>:</strong>K8s提供的管理有状态应用的负载管理控制器API。</span></p>
<p><span style="font-size: 15px; color: rgba(0, 0, 255, 1)">◆ <strong>DaemonSet</strong><strong>:</strong>确保其创建的Pod在集群中的每一台(或指定)Node上都运行一个副本。</span></p>
<p><span style="font-size: 15px">◆ <strong>Job</strong><strong>:</strong>K8s用来控制批处理型任务的API对象,之后基于时间管理新增了<strong>CronJob</strong>。</span></p>
<p><span style="font-size: 15px; color: rgba(255, 0, 0, 1)">◆ <strong>Service</strong><strong>:</strong>定义了一个服务的多个Pod逻辑合集和访问Pod的策略,实现服务发现和负载均衡。</span></p>
<p><span style="font-size: 15px">◆ <strong>HPA</strong><strong>:</strong>实现基于CPU使用率(或在使用自定义指标)的Pod自动伸缩的功能。</span></p>
<p><strong> </strong></p>
<h3><strong>3) </strong><strong>存储管理</strong></h3>
<p><span style="font-size: 15px">◆ <strong>Secret</strong><strong>:</strong>用来保存和传递密码、密钥、认证凭证这些敏感信息的对象。</span></p>
<p><span style="font-size: 15px; color: rgba(255, 0, 0, 1)">◆ <strong>ConfigMap</strong><strong>:</strong>将配置信息与镜像内容分离,以使容器化的应用程序具有可移植性。</span></p>
<p><span style="font-size: 15px; color: rgba(0, 0, 255, 1)">◆ <strong>Volume</strong><strong>:</strong>是Pod中能够被多个容器访问的共享目录。</span></p>
<p><span style="font-size: 15px">◆ <strong>PV</strong><strong>:</strong>持久化存储和与之相关联的持久化存储声明(<strong>PVC</strong>),使得K8s集群具备了存储的逻辑抽象能力。</span></p>
<p align="left"> </p>
<p align="left"> </p>
<hr>
<h1><span style="color: rgba(153, 51, 0, 1)"><strong>四、 </strong><strong>系统架构</strong></span></h1>
<p><img src="https://img2020.cnblogs.com/blog/1059616/202005/1059616-20200506155422738-253453922.png"></p>
<h3><strong>1) </strong><strong>集群高可用</strong></h3>
<p align="left"><span style="font-size: 15px">K8s作为容器应用的管理中心,通过对Pod的数量进行监控,并且根据主机或容器失效的状态将新的Pod调度到其他Node上,实现了应用层的高可用性。 </span></p>
<p align="left"><span style="font-size: 15px">针对K8s集群高可用性还应包含以下两个层面的考虑:<span style="color: rgba(255, 0, 0, 1)">Etcd 数据存储的高可用性(至少3台)</span>和Master组件的高可用性。</span></p>
<p align="left"><span style="font-size: 15px">这里我们采用 Hproxy + Keepalive 高可用方案,并且与 Etcd 服务、Master组件均部署到同一节点。</span></p>
<p> </p>
<h3><strong>2) </strong><strong>控制管理</strong></h3>
<p align="left"><span style="font-size: 15px">K8s集群的<span style="color: rgba(255, 0, 0, 1)">管理和控制</span>主要由Master节点负责,它来负责具体的执行过程,我们后面执行的所有命令基本都是在Master节点上运行的。</span></p>
<p align="left"><span style="font-size: 15px">Master节点通常会占据一个独立的服务器,其主要原因是它太重要了,是整个集群的“首脑”,如果宕机或者不可用,那么对集群内容器应用的管理都将失效。</span></p>
<p><span style="font-size: 15px"> </span></p>
<h3><strong>3) </strong><strong>工作负载</strong></h3>
<p align="left"><span style="font-size: 15px">K8s集群中的<span style="color: rgba(255, 0, 0, 1)">计算能力</span>由Node提供,最初Node称为服务节点Minion,后来改名为Node。</span></p>
<p align="left"><span style="font-size: 15px">K8s集群中的Node也就等同于Mesos集群中的Slave节点,是所有Pod运行所在的<span style="color: rgba(255, 0, 0, 1)">工作主机</span>,可以是物理机也可以是虚拟机</span>。</p>
<p> </p>
<h3><strong>4) </strong><strong>系统监控</strong></h3>
<p><span style="font-size: 15px">Prometheus(普罗米修斯)是一套开源的<span style="color: rgba(255, 0, 0, 1)">监控、报警、时间序列数据库的组合</span>。</span></p>
<p><span style="font-size: 15px">基本原理是通过HTTP协议周期性抓取被监控组件的状态,这样做的好处是任意组件只要提供HTTP接口就可以接入监控系统,不需要任何SDK或者其他的集成过程。</span></p>
<p><span style="font-size: 15px">这样做非常适合作为虚拟化环境监控系统,比如Docker、Kubernetes。</span></p>
<p><img src="https://img2020.cnblogs.com/blog/1059616/202005/1059616-20200506160038548-481015366.png"></p>
<p><span style="font-size: 15px">组件说明:</span></p>
<p><span style="font-size: 15px; color: rgba(0, 0, 255, 1)">■ <strong>Prometheus</strong><strong>:</strong>负责实现对K8s集群监控数据的获取,存储以及查询。</span></p>
<p><span style="font-size: 15px">■ <strong>PrometheusOperator</strong><strong>:</strong>为Prometheus实例的部署和管理提供了简单的监视定义。</span></p>
<p><span style="font-size: 15px; color: rgba(0, 0, 255, 1)">■ <strong>KubeStateMetrics</strong><strong>:</strong>是K8s集群资源使用情况的聚合器,收集数据给K8s集群内使用(如HPA)。</span></p>
<p><span style="font-size: 15px">■ <strong>AlertManager</strong><strong>:</strong>负责将告警信息重复数据删除,分组和路由到正确的接收者集成。</span></p>
<p><span style="font-size: 15px; color: rgba(0, 0, 255, 1)">■ <strong>NodeExporter</strong><strong>:</strong>用于采集集群中各个节点的资源使用情况。</span></p>
<p><span style="font-size: 15px; color: rgba(255, 0, 0, 1)">■ <strong>Grafana</strong><strong>:</strong>一个跨平台的开源的度量分析和可视化工具。</span></p>
<p> </p>
<h3><strong>5) </strong><strong>日志收集</strong></h3>
<p><span style="font-size: 15px">ELK分别指Elastic公司的Elasticsearch、Logstash、Kibana。在比较旧的ELK架构中,Logstash身兼日志的采集、过滤两职。</span></p>
<p><span style="font-size: 15px">但由于Logstash基于JVM,性能有一定限制,因此,目前业界更推荐使用Go语言开发Fliebeat代替Logstash的采集功能,Logstash只作为了日志过滤的中间件。</span></p>
<p> </p>
<p> </p>
<p><span style="font-size: 15px"><img src="https://img2020.cnblogs.com/blog/1059616/202005/1059616-20200506160304143-1290453471.png"></span></p>
<p><span style="font-size: 15px">组件说明:</span></p>
<p><span style="font-size: 15px">■ <strong>Filebeat</strong><strong>:</strong>一个轻量型的单一功能<span style="color: rgba(255, 0, 0, 1)">数据采集器</span>。</span></p>
<p><span style="font-size: 15px">■ <strong>Logstash</strong><strong>:</strong>能够同时从多个来源采集数据,<span style="color: rgba(255, 0, 0, 1)">转换数据</span>,将数据发送到诸如ES中。</span></p>
<p><span style="font-size: 15px">■ <strong>Elasticsearch</strong><strong>:</strong>一个实时、分布式、可扩展的<span style="color: rgba(255, 0, 0, 1)">搜索引擎</span>,通常用于索引和搜索大量<span style="color: rgba(255, 0, 0, 1)">日志数据</span>。</span></p>
<p><span style="font-size: 15px">■ <strong>Kibana</strong><strong>:</strong>可以让用户在 ES 中使用图形和图表对数据进行<span style="color: rgba(255, 0, 0, 1)">可视化</span>。</span></p>
<p> </p>
<h3><strong>6) </strong><strong>镜像仓库</strong></h3>
<p><span style="font-size: 15px">Harbor是一个开源<span style="color: rgba(255, 0, 0, 1)">镜像仓库,可通过基于角色的访问控制来保护镜像</span>,新版本的Harbor还增加了扫描镜像中的漏洞并将镜像签名为受信任。</span></p>
<p><span style="font-size: 15px">作为CNCF孵化项目,Harbor提供合规性,性能和互操作性,以帮助你跨Kubernetes和Docker等云原生计算平台持续,安全地管理镜像。</span></p>
<p><span style="font-size: 15px">Harbor组件均以Docker容器方式启动,因此,你可以将其部署在任何支持Docker的Linux发行版上。</span></p>
<p> <img src="https://img2020.cnblogs.com/blog/1059616/202005/1059616-20200506160406753-531423813.png"></p>
<p> </p>
<p> </p>
<hr>
<h1><span style="color: rgba(153, 51, 0, 1)"><strong>五、 </strong><strong>镜像制作</strong></span></h1>
<h3><strong>1) </strong><strong>镜像构建</strong></h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 适用java1.8服务</span>
FROM hub.jhmy.com/base/java:1.8
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 维护者</span>
<span style="color: rgba(0, 0, 0, 1)">MAINTAINER zhangfan
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 工作目录</span>
WORKDIR /<span style="color: rgba(0, 0, 0, 1)">root
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 复制文件到镜像</span>
COPY *<span style="color: rgba(0, 0, 0, 1)">.jar .
COPY lib</span>/ lib/<span style="color: rgba(0, 0, 0, 1)">
COPY hosts.bak .
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 查看当前目录</span>
RUN ls -<span style="color: rgba(0, 0, 0, 1)">l .
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 容器启动时运行命令</span>
CMD [<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">./run.sh</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">]</span><span style="color: rgba(0, 128, 0, 1)"><br></span></pre>
</div>
<p><span style="font-size: 15px">构建镜像示例:<span style="color: rgba(0, 0, 255, 1)"><strong>docker build -t hub.jhmy.com/test/jmnbservice .</strong></span></span></p>
<p> </p>
<h3><strong>2) </strong><strong>容器结构</strong></h3>
<p><span style="font-size: 15px">当容器启动时,一个新的可写层被加载到镜像的顶部。</span><br><span style="font-size: 15px">
这一层通常被称作“容器层”,其余层都称作“镜像层”。</span></p>
<p> <img src="https://img2020.cnblogs.com/blog/1059616/202005/1059616-20200506161706677-361388620.png"></p>
<p><span style="font-size: 15px">启动容器示例:<span style="color: rgba(0, 0, 255, 1)"><strong>docker run -dit --name=myapp hub.jhmy.com/test/jmnbservice</strong></span></span></p>
<p><span style="font-size: 15px">进入容器示例:<span style="color: rgba(0, 0, 255, 1)"><strong>docker exec -it jmnbservice bash</strong></span></span></p>
<p><span style="font-size: 15px"><span style="color: rgba(0, 0, 255, 1)"> </span><img src="https://img2020.cnblogs.com/blog/1059616/202005/1059616-20200506161639752-917578364.png"></span></p>
<p> </p>
<p> </p>
<hr>
<h1><span style="color: rgba(153, 51, 0, 1)"><strong>六、 </strong><strong>服务编排</strong></span></h1>
<h3><strong>1) </strong><strong>ConfigMap</strong><strong>资源定义</strong></h3>
<p><span style="font-size: 15px">主要定义配置文件内容。</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">apiVersion: v1
kind: ConfigMap
metadata:
name: jmnbservice
namespace: </span><span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)">
data:
application.properties: </span>|
server.port=1111<span style="color: rgba(0, 0, 0, 1)">
spring.server.port</span>=2222<span style="color: rgba(0, 0, 0, 1)">
spring.dubbo.port</span>=3333<span style="color: rgba(0, 0, 0, 1)">
logging.config</span>=classpath:logback-<span style="color: rgba(0, 0, 0, 1)">spring.xml
logback.logdir</span>=/home/jhmyPro/xsr/<span style="color: rgba(0, 0, 0, 1)">logs
logback.maxHistory</span>=7<span style="color: rgba(0, 0, 0, 1)">
logback.totalSizeCap</span>=<span style="color: rgba(0, 0, 0, 1)">10GB
logback.maxFileSize</span>=<span style="color: rgba(0, 0, 0, 1)">128MB
dubbo.registry.address</span>=zookeeper://10.11.12.13:2181<span style="color: rgba(0, 0, 0, 1)">
spring.application.name</span>=<span style="color: rgba(0, 0, 0, 1)">JmNbService
......<br><br></span></pre>
</div>
<p> </p>
<h3><strong>2) </strong><strong>Deployment</strong><strong>资源定义</strong></h3>
<p>需要定义Pod副本数、匹配标签、容器名称、对应镜像、监听端口、环境变量(java运行参数)、资源限制(cpu和memory)、挂载配置等。</p>
<div class="cnblogs_code">
<pre>apiVersion: apps/<span style="color: rgba(0, 0, 0, 1)">v1
kind: Deployment
metadata:
name: jmnbservice
namespace: </span><span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)">
spec:
replicas: </span>2<span style="color: rgba(0, 0, 0, 1)">
selector:
matchLabels:
app: jmnbservice
project: nb
template:
metadata:
labels:
app: jmnbservice
project: nb
annotations:
version: </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">20200321</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
spec:
containers:
</span>-<span style="color: rgba(0, 0, 0, 1)"> name: jmnbservice
image: hub.jhmy.com</span>/test/<span style="color: rgba(0, 0, 0, 1)">jmnbservice:latest
imagePullPolicy: Always
env:
</span>-<span style="color: rgba(0, 0, 0, 1)"> name: JVM_OPTS
value: </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">-Xms1024m -Xmx1024m</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
ports:
</span>-<span style="color: rgba(0, 0, 0, 1)"> name: spring
containerPort: </span>1111
-<span style="color: rgba(0, 0, 0, 1)"> name: server
containerPort: </span>2222
-<span style="color: rgba(0, 0, 0, 1)"> name: dubbo
containerPort: </span>3333<span style="color: rgba(0, 0, 0, 1)">
resources:
limits:
cpu: 200m
memory: 2Gi
requests:
cpu: 100m
memory: 1Gi
volumeMounts:
</span>-<span style="color: rgba(0, 0, 0, 1)"> name: config
mountPath: </span>/root/<span style="color: rgba(0, 0, 0, 1)">application.properties
subPath: application.properties
</span>-<span style="color: rgba(0, 0, 0, 1)"> name: html
mountPath: </span>/usr/local/nginx/html/<span style="color: rgba(0, 0, 0, 1)">clientexe
</span>-<span style="color: rgba(0, 0, 0, 1)"> name: log
mountPath: </span>/<span style="color: rgba(0, 0, 0, 1)">home
volumes:
</span>-<span style="color: rgba(0, 0, 0, 1)"> name: config
configMap:
name: jmnbservice
</span>-<span style="color: rgba(0, 0, 0, 1)"> name: html
persistentVolumeClaim:
claimName: nginxhtml
</span>-<span style="color: rgba(0, 0, 0, 1)"> name: log
hostPath:
path: </span>/<span style="color: rgba(0, 0, 0, 1)">home
type: DirectoryOrCreate</span></pre>
</div>
<p> </p>
<h3><strong>3) </strong><strong>Service</strong><strong>资源定义</strong></h3>
<p> 主要定义匹配Pod标签、暴露方式、以及暴露端口。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">apiVersion: v1
kind: Service
metadata:
name: jmnbservice
namespace: </span><span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)">
spec:
type: NodePort
selector:
app: jmnbservice
project: nb
ports:
</span>-<span style="color: rgba(0, 0, 0, 1)"> name: spring
port: </span>1111<span style="color: rgba(0, 0, 0, 1)">
nodePort: </span>30121
-<span style="color: rgba(0, 0, 0, 1)"> name: server
port: </span>2222<span style="color: rgba(0, 0, 0, 1)">
nodePort: </span>30122
-<span style="color: rgba(0, 0, 0, 1)"> name: dubbo
port: </span>3333<span style="color: rgba(0, 0, 0, 1)">
nodePort: </span>30123</pre>
</div>
<p> </p>
<p> </p>
<hr>
<h1><span style="color: rgba(153, 51, 0, 1)"><strong>七、 </strong><strong>持续部署</strong></span></h1>
<h3><strong>1) </strong><strong>部署应用</strong></h3>
<p align="left"><span style="font-size: 15px">部署应用之前,请确保相关编排文件均已开发完成。至此,我们可以执行<span style="color: rgba(255, 0, 0, 1)"> <strong>kubectl create/apply </strong></span>命令进行部署。</span></p>
<p align="left"><span style="font-size: 15px">推荐使用apply,这样可以避免重复部署时报错,而且有利于编排文件修改更新,即当你修改编排文件后,只需要再次执行apply命令即可完成更新。</span></p>
<p align="left"><span style="font-size: 15px">当然,如果你需要确保此次部署为唯一创建且信息完整,请使用create命令,并且你可以使用 <span style="color: rgba(255, 0, 0, 1)"><strong>kubectl delete </strong></span>命令删除资源。</span></p>
<p align="left"><span style="font-size: 15px"><span style="color: rgba(255, 0, 0, 1)">最后我们可以使用<strong> -f </strong>标签指定具体编排文件,也可以指定路径,批量执行</span>。</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> ls</span>
<span style="color: rgba(0, 0, 0, 1)">ConfigMap.yamlDeployment.yamlService.yaml
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> kubectl apply -f .</span>
configmap/<span style="color: rgba(0, 0, 0, 1)">jmdiservice created
deployment.apps</span>/<span style="color: rgba(0, 0, 0, 1)">jmdiservice created
service</span>/<span style="color: rgba(0, 0, 0, 1)">jmdiservice created
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> kubectl get cm; kubectl get deploy; kubectl get svc</span>
<span style="color: rgba(0, 0, 0, 1)">NAME DATA AGE
jmdiservice </span>1<span style="color: rgba(0, 0, 0, 1)"> 70s
NAME READY UP</span>-TO-<span style="color: rgba(0, 0, 0, 1)">DATE AVAILABLE AGE
jmdiservice </span>2/2 2 2<span style="color: rgba(0, 0, 0, 1)"> 70s
NAME TYPE CLUSTER</span>-IP EXTERNAL-<span style="color: rgba(0, 0, 0, 1)">IP PORT(S) AGE
jmdiservice NodePort </span>10.102.153.176 <none> 20036:30111/<span style="color: rgba(0, 0, 0, 1)">TCP 69s
kubernetes ClusterIP </span>10.96.0.1 <none> 443/TCP 5d16h</pre>
</div>
<p> </p>
<h3><strong>2) </strong><strong>滚动更新</strong></h3>
<p><span style="font-size: 15px">通常进行应用升级,都是镜像版本的升级,我们可以使用 <strong><span style="color: rgba(255, 0, 0, 1)">kubectl set image</span> </strong>命令设置新的镜像名称即可;</span></p>
<p><span style="font-size: 15px">如果需要更新具体资源字段,则可以使用 <strong><span style="color: rgba(255, 0, 0, 1)">kubectl patch</span> </strong>命令;当然,你也可以使用 <strong><span style="color: rgba(255, 0, 0, 1)">kubectl edit</span> </strong>命令编辑资源对象。</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> kubectl get deployment</span>
NAME READY UP-TO-<span style="color: rgba(0, 0, 0, 1)">DATE AVAILABLE AGE
jmdiservice </span>2/2 2 2<span style="color: rgba(0, 0, 0, 1)"> 4h54m
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> kubectl set image deployment/jmdiservice jmdiservice=hub.jhmy.com/project-test/jmdiservice:latest</span>
deployment.apps/<span style="color: rgba(0, 0, 0, 1)">jmdiservice image updated
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> kubectl patch deployment/jmdiservice --patch '{"spec": {"template": {"metadata": {"annotations":{"version": "20200506" }}}}}'</span>
deployment.apps/jmdiservice patched</pre>
</div>
<p> </p>
<h3><strong>3) </strong><strong>版本回滚</strong></h3>
<p><span style="font-size: 15px">我们使用上面更新应用时K8S都会记录下当前的配置文件,保存为一个revision (版本),这样就可以通过这个版本回滚到特定的时间。</span></p>
<p><span style="font-size: 15px">我们可以通过 <span style="color: rgba(255, 0, 0, 1)"><strong>kubectl rollout history </strong></span>命令查看历史记录,并通过 <strong><span style="color: rgba(255, 0, 0, 1)">kubectl rollout undo</span> </strong>撤销本次发布回滚到上一个部署版本,也可以使用--to-revision标签回滚到指定版本。</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> kubectl rollout history deployment/jmdiservice</span>
deployment.apps/<span style="color: rgba(0, 0, 0, 1)">jmdiservice
REVISIONCHANGE</span>-<span style="color: rgba(0, 0, 0, 1)">CAUSE
</span>1 <none>
2 <none>
3 <none><span style="color: rgba(0, 0, 0, 1)">
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> kubectl rollout undo --to-revision=2 deployment/jmdiservice</span>
deployment.apps/jmdiservice rolled back</pre>
</div>
<p> </p>
<h3><strong>4) </strong><strong>CICD</strong><strong>流程</strong></h3>
<p> <img src="https://img2020.cnblogs.com/blog/1059616/202005/1059616-20200506164127623-1490653987.png"></p>
<p> </p>
<p> </p>
<p> </p>
<hr>
<h1><span style="color: rgba(153, 51, 0, 1)"><strong>八、 </strong><strong>故障排查</strong></span></h1>
<h3><strong>1) </strong><strong>查看系统Event</strong></h3>
<p><span style="font-size: 15px">通过<span style="color: rgba(255, 0, 0, 1)"> <strong>kubectl describe pod </strong></span>命令,可以显示Pod创建时的配置定义、状态等信息,还可以显示与该Pod相关的最近的Event事件, 事件信息对于查错非常有用。</span></p>
<p> <img src="https://img2020.cnblogs.com/blog/1059616/202005/1059616-20200506163341109-981750425.png"></p>
<p> </p>
<h3><strong>2) </strong><strong>查看容器日志</strong></h3>
<p><span style="font-size: 15px">在需要排查容器内部应用程序生成的日志时, 我们可以使用 <span style="color: rgba(255, 0, 0, 1)"><strong>kubectl logs <pod_name> </strong></span>命令。</span></p>
<p> <img src="https://img2020.cnblogs.com/blog/1059616/202005/1059616-20200506163402499-154353064.png"></p>
<p> </p>
<h3><strong>3) </strong><strong>查看K8s服务日志</strong></h3>
<p><span style="font-size: 15px">K8s服务默认使用systemd系统管理,那么systemd的journal系统会接管服务程序的输出日志。</span></p>
<p><span style="font-size: 15px">我们可以<span style="color: rgba(255, 0, 0, 1)"> </span><strong><span style="color: rgba(255, 0, 0, 1)">tailf /var/log/messages</span> </strong>查看系统日志,也可以使用 <strong><span style="color: rgba(255, 0, 0, 1)">journalctl</span> </strong>工具来查看k8s组件的日志。</span></p>
<p> <img src="https://img2020.cnblogs.com/blog/1059616/202005/1059616-20200506163428608-777966006.png"></p>
<p> </p>
<h3><strong>4) </strong><strong>寻求帮助</strong></h3>
<p align="left"><span style="font-size: 15px">◎ Kubernetes官方网站任务详解:https://kubernetes.io/zh/docs/tasks/</span></p>
<p align="left"><span style="font-size: 15px">◎ Kubernetes GitHub库问题列表:https://github.com/kubernetes/kubernetes/issues</span></p>
<p align="left"><span style="font-size: 15px"> <img src="https://img2020.cnblogs.com/blog/1059616/202005/1059616-20200506163512671-189750593.png"></span></p>
<p> </p>
<p> </p>
<blockquote>
<p>作者:Leozhanggg</p>
<p>出处:https://www.cnblogs.com/leozhanggg/p/12837025.html</p>
<p>本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。</p>
</blockquote>
<p> </p><br><br>
来源:https://www.cnblogs.com/leozhanggg/p/12837025.html
頁:
[1]