叶辰飞 發表於 2021-6-27 17:10:00

入门Kubernetes - YAML文件

<h3>前言</h3>
<p> 前篇文章中简单了解到如何把.Net Core 程序部署到k8s中,过程中使用了多个*.yaml文件,那么这些文件的格式及含义、语法是如何的呢?</p>
<p> 接下来,进一步了解学习</p>
<h3>一、YAML介绍:</h3>
<p> <strong>1、简介:</strong></p>
<p>  YAML 语言(发音 /ˈjæməl/ )的设计目标,就是方便人类读写。它实质上是一种通用的数据串行化格式。YAML又被称为是 json 的超集,使用起来比 json 更方便</p>
<p> <strong>2、语法:</strong></p>
<ul>
<li>大小写敏感</li>
<li>使用缩进表示层级关系</li>
<li>缩进时不允许使用Tab键,只允许使用空格。</li>
<li>缩进的空格数目不重要,只要相同层级的元素左侧对齐即可</li>
<li>#表示注释,从该字符到行尾</li>
</ul>
<p> <strong>3、支持的数据结构 </strong> </p>
<ul>
<li><strong>对象</strong>:键值对的集合,又称为映射(mapping)/ 哈希(hashes) / 字典(dictionary)</li>
<li><strong>数组</strong>:一组按次序排列的值,又称为序列(sequence) / 列表(list)</li>
<li><strong>纯量(scalars)</strong>:单个的、不可再分的值,场景类型如下:
<ul>
<li>字符串</li>
<li>布尔值</li>
<li>整数</li>
<li>浮点数</li>
<li>Null</li>
<li>时间</li>
<li>日期</li>
</ul>
</li>
</ul>
<p>  <strong>对象</strong>:一组键值对,使用冒号结构表示,如下</p>
<div class="cnblogs_code" style="margin-left: 30px">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">yaml格式:</span>
<span style="color: rgba(0, 0, 0, 1)">api: v1

</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">对应json格式:</span>
{api:<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">v1</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)">#</span><span style="color: rgba(0, 128, 0, 1)">yaml格式:行内对象</span>
<span style="color: rgba(0, 0, 0, 1)">obj: { name: Steve, foo: bar }

</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">Json格式:</span>
{ obj: { name: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Steve</span><span style="color: rgba(128, 0, 0, 1)">'</span>, foo: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">bar</span><span style="color: rgba(128, 0, 0, 1)">'</span> } }</pre>
</div>
<p>  <strong>数组</strong>:一组连词线开头的行,构成一个数组,且支持多维数组</p>
<div class="cnblogs_code" style="margin-left: 30px">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">yaml格式:</span>
-<span style="color: rgba(0, 0, 0, 1)"> Cat
</span>-<span style="color: rgba(0, 0, 0, 1)"> Dog

</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">json格式:</span>
[<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Cat</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Dog</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)">#</span><span style="color: rgba(0, 128, 0, 1)">yaml格式:多维数组</span>
-
-<span style="color: rgba(0, 0, 0, 1)">Cat
</span>-<span style="color: rgba(0, 0, 0, 1)">Dog

</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">json格式:多维数组</span>
[[<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Cat</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Dog</span><span style="color: rgba(128, 0, 0, 1)">'</span>]]</pre>
</div>
<p>  <strong>纯量:</strong>最基本的、不可再分的值  </p>
<div class="cnblogs_code" style="margin-left: 30px">
<pre><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)">str: 字符串
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">字符串之中包含空格或特殊字符,需要放在引号之中。</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">单引号和双引号都可以使用,双引号不会对特殊字符转义</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">多行字符串可以使用|保留换行符,也可以使用&gt;折叠换行</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">+表示保留文字块末尾的换行,-表示删除字符串末尾的换行</span>

<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">数字类型:</span>
number: 10.01

<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)">isRun: true

</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">空类型:</span>
name: ~

<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">时间类型:时间采用 ISO8601 格式</span>
iso8601: 2021-06-27t15:00:00.00-05:00

<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">日期类型:采用复合 iso8601 格式的年、月、日表示</span>
date: 2021-06-27

<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">使用两个感叹号,强制转换数据类型。</span>
strnum: !!str 123<span style="color: rgba(0, 0, 0, 1)">
strbol: !!str true</span></pre>
</div>
<p><strong> 3、引用用法:</strong></p>
<p>  锚点<span style="background-color: rgba(204, 255, 255, 1)"><strong><span style="color: rgba(255, 153, 0, 1)"><code>&amp;</code></span></strong></span>和别名<span style="background-color: rgba(204, 255, 255, 1); color: rgba(255, 153, 0, 1)"><strong><code>*</code></strong></span>,可以用来引用。</p>
<p>  <strong><span style="background-color: rgba(204, 255, 255, 1); color: rgba(255, 153, 0, 1)"><code>&amp;</code></span></strong>用来建立锚点,<span style="color: rgba(255, 153, 0, 1); background-color: rgba(204, 255, 255, 1)"><strong><code>&lt;&lt;</code></strong></span>表示合并到当前数据,<strong><span style="background-color: rgba(204, 255, 255, 1); color: rgba(255, 153, 0, 1)"><code>*</code></span></strong>用来引用锚点  </p>
<div class="cnblogs_code" style="margin-left: 30px">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">yaml引用示例:</span>
defaults: &amp;<span style="color: rgba(0, 0, 0, 1)">defaults
adapter:postgres
host:   localhost

development:
database: myapp_development
</span>&lt;&lt;: *<span style="color: rgba(0, 0, 0, 1)">defaults

test:
database: myapp_test
</span>&lt;&lt;: *<span style="color: rgba(0, 0, 0, 1)">defaults

</span><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)">defaults:
adapter:postgres
host:   localhost

development:
database: myapp_development
adapter:postgres
host:   localhost

test:
database: myapp_test
adapter:postgres
host:   localhost</span></pre>
</div>
<p>&nbsp; 以上是yaml的基础语法,接下来看下在k8s中怎么使用呢?</p>
<h3>二、YAML在k8s中的使用</h3>
<p> k8s有两种创建资源的方式:kubectl 命令和 yaml 配置文件。<br>  kubectl命令行:最为简单,一条命令就OK.<br>  yaml配置文件:提供了一种让你知其然更知其所以然的方式。<br> 优势如下:</p>
<p style="margin-left: 30px">完整性:配置文件描述了一个资源的完整状态,可以很清楚地知道一个资源的创建背后究竟做了哪些事;<br>灵活性:配置文件可以创建比命令行更复杂的结构;<br>可维护性:配置文件提供了创建资源对象的模板,能够重复使用;<br>可扩展性:适合跨环境、规模化的部署。</p>
<p> k8s(Kubernetes)中Pod、Deployment、ReplicaSet、Service之间关系如下:</p>
<p> <strong>Pod</strong>:来管理容器,每个 Pod 可以包含一个或多个紧密关联的容器</p>
<p> <strong>ReplicaSet</strong>:是rc的升级版,也是来管理pod,Kubernetes官方强烈建议避免直接使用ReplicaSet,而应该通过Deployment来创建RS和Pod。由于ReplicaSet是ReplicationController的代替物,因此用法基本相同,唯一的区别在于ReplicaSet支持集合式的selector。</p>
<p> <strong>Deployment</strong>:更加方便的管理Pod和Replica Set,提供发布更新维护监控等功能</p>
<p> <strong>Service</strong>:是在这一整套基础之上提供给外部的稳定的服务</p>
<p> <strong>1、Deployment配置模板</strong></p>
<div class="cnblogs_code" style="margin-left: 30px">
<pre>apiVersion: extensions/<span style="color: rgba(0, 0, 0, 1)">v1beta1
kind: Deployment
metadata: </span>&lt;Object&gt;<span style="color: rgba(0, 0, 0, 1)">
spec: </span>&lt;Object&gt;<span style="color: rgba(0, 0, 0, 1)">
minReadySeconds: </span>&lt;integer&gt; <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">设置pod准备就绪的最小秒数</span>
paused: &lt;boolean&gt; <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">表示部署已暂停并且deploy控制器不会处理该部署</span>
progressDeadlineSeconds: &lt;integer&gt;<span style="color: rgba(0, 0, 0, 1)">
strategy: </span>&lt;Object&gt; <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">将现有pod替换为新pod的部署策略</span>
    rollingUpdate: &lt;Object&gt; <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">滚动更新配置参数,仅当类型为RollingUpdate</span>
      maxSurge: &lt;string&gt; <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">滚动更新过程产生的最大pod数量,可以是个数,也可以是百分比</span>
      maxUnavailable: &lt;string&gt; <span style="color: rgba(0, 128, 0, 1)">#
</span>    type: &lt;string&gt; <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">部署类型,Recreate,RollingUpdate</span>
replicas: &lt;integer&gt; <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">pods的副本数量</span>
selector: &lt;Object&gt; <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">pod标签选择器,匹配pod标签,默认使用pods的标签</span>
    matchLabels: &lt;mapstring&gt;<span style="color: rgba(0, 0, 0, 1)">
      key1: value1
      key2: value2
    matchExpressions: </span>&lt;[]Object&gt;<span style="color: rgba(0, 0, 0, 1)">
      operator: </span>&lt;string&gt; -required- <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">设定标签键与一组值的关系,In, NotIn, Exists and DoesNotExist</span>
      key: &lt;string&gt; -required-<span style="color: rgba(0, 0, 0, 1)">
      values: </span>&lt;[]string&gt;<span style="color: rgba(0, 0, 0, 1)">   
revisionHistoryLimit: </span>&lt;integer&gt; <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">设置保留的历史版本个数,默认是10</span>
rollbackTo: &lt;Object&gt;<span style="color: rgba(0, 0, 0, 1)">
    revision: </span>&lt;integer&gt; <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">设置回滚的版本,设置为0则回滚到上一个版本</span>
template: &lt;Object&gt; -required-<span style="color: rgba(0, 0, 0, 1)">
    metadata:
    spec:
      containers: </span>&lt;[]Object&gt; <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">容器配置</span>
      - name: &lt;string&gt; -required- <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">容器名、DNS_LABEL</span>
      image: &lt;string&gt; <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">镜像</span>
      imagePullPolicy: &lt;string&gt; <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">镜像拉取策略,Always、Never、IfNotPresent</span>
      ports: &lt;[]Object&gt;
      - name: <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">定义端口名</span>
          containerPort: <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">容器暴露的端口</span>
          protocol: TCP <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">或UDP</span>
      volumeMounts: &lt;[]Object&gt;
      - name: &lt;string&gt; -required- <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">设置卷名称</span>
          mountPath: &lt;string&gt; -required- <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">设置需要挂载容器内的路径</span>
          readOnly: &lt;boolean&gt; <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">设置是否只读</span>
      livenessProbe: &lt;Object&gt; <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">就绪探测</span>
          <span style="color: rgba(0, 0, 255, 1)">exec</span><span style="color: rgba(0, 0, 0, 1)">:
            command: </span>&lt;[]string&gt;<span style="color: rgba(0, 0, 0, 1)">
          httpGet:
            port: </span>&lt;string&gt; -required-<span style="color: rgba(0, 0, 0, 1)">
            path: </span>&lt;string&gt;<span style="color: rgba(0, 0, 0, 1)">
            host: </span>&lt;string&gt;<span style="color: rgba(0, 0, 0, 1)">
            httpHeaders: </span>&lt;[]Object&gt;<span style="color: rgba(0, 0, 0, 1)">
            name: </span>&lt;string&gt; -required-<span style="color: rgba(0, 0, 0, 1)">
            value: </span>&lt;string&gt; -required-<span style="color: rgba(0, 0, 0, 1)">
            scheme: </span>&lt;string&gt;<span style="color: rgba(0, 0, 0, 1)">
          initialDelaySeconds: </span>&lt;integer&gt; <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">设置多少秒后开始探测</span>
          failureThreshold: &lt;integer&gt; <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">设置连续探测多少次失败后,标记为失败,默认三次</span>
          successThreshold: &lt;integer&gt; <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">设置失败后探测的最小连续成功次数,默认为1</span>
          timeoutSeconds: &lt;integer&gt; <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">设置探测超时的秒数,默认1s</span>
          periodSeconds: &lt;integer&gt; <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">设置执行探测的频率(以秒为单位),默认1s</span>
          tcpSocket: &lt;Object&gt; <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">TCPSocket指定涉及TCP端口的操作</span>
            port: &lt;string&gt; -required- <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">容器暴露的端口</span>
            host: &lt;string&gt; <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">默认pod的IP</span>
      readinessProbe: &lt;Object&gt; <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">同livenessProbe</span>
      resources: &lt;Object&gt; <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">资源配置</span>
          requests: &lt;mapstring&gt; <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">最小资源配置</span>
            memory: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">1024Mi</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
            cpu: </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">500m</span><span style="color: rgba(128, 0, 0, 1)">"</span> <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">500m代表0.5CPU</span>
          limits: &lt;mapstring&gt; <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)">            memory:
            cpu:         
      volumes: </span>&lt;[]Object&gt; <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">数据卷配置</span>
      - name: &lt;string&gt; -required- <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">设置卷名称,与volumeMounts名称对应</span>
      hostPath: &lt;Object&gt; <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">设置挂载宿主机路径</span>
          path: &lt;string&gt; -required-<span style="color: rgba(0, 0, 0, 1)">
          type: </span>&lt;string&gt; <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">类型:DirectoryOrCreate、Directory、FileOrCreate、File、Socket、CharDevice、BlockDevice</span>
      -<span style="color: rgba(0, 0, 0, 1)"> name: nfs
      nfs: </span>&lt;Object&gt; <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">设置NFS服务器</span>
          server: &lt;string&gt; -required- <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">设置NFS服务器地址</span>
          path: &lt;string&gt; -required- <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">设置NFS服务器路径</span>
          readOnly: &lt;boolean&gt; <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)"> name: configmap
      configMap:
          name: </span>&lt;string&gt; <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">configmap名称</span>
          defaultMode: &lt;integer&gt; <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">权限设置0~0777,默认0664</span>
          optional: &lt;boolean&gt; <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">指定是否必须定义configmap或其keys</span>
          items: &lt;[]Object&gt;
          - key: &lt;string&gt; -required-<span style="color: rgba(0, 0, 0, 1)">
            path: </span>&lt;string&gt; -required-<span style="color: rgba(0, 0, 0, 1)">
            mode: </span>&lt;integer&gt;<span style="color: rgba(0, 0, 0, 1)">
      restartPolicy: </span>&lt;string&gt; <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">重启策略,Always、OnFailure、Never</span>
      nodeName: &lt;string&gt;<span style="color: rgba(0, 0, 0, 1)">
      nodeSelector: </span>&lt;mapstring&gt;<span style="color: rgba(0, 0, 0, 1)">
      imagePullSecrets: </span>&lt;[]Object&gt;<span style="color: rgba(0, 0, 0, 1)">
      hostname: </span>&lt;string&gt;<span style="color: rgba(0, 0, 0, 1)">
      hostPID: </span>&lt;boolean&gt;<span style="color: rgba(0, 0, 0, 1)">
status: </span>&lt;Object&gt;</pre>
</div>
<pre> nginx实例:</pre>
<div class="cnblogs_code" style="margin-left: 30px">
<pre>apiVersion: app/v1         <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 1.9.0 之前的版本使用 apps/v1beta2,可通过命令 kubectl api-versions 查看</span>
kind: Deployment           <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">指定创建资源的角色/类型</span>
metadata:                    <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">资源的元数据/属性</span>
name: nginx-deployment       <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">资源的名字,在同一个namespace中必须唯一</span>
namespace:xxxx             <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)">labels:
    app: demo                  </span><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)">spec:
replicas: </span>3              <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">副本数量3</span>
<span style="color: rgba(0, 0, 0, 1)">strategy:
    rollingUpdate:         </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">#由于replicas为3,则整个升级,pod个数在2-4个之间</span>
      maxSurge: 1           <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">滚动升级时会先启动1个pod</span>
      maxUnavailable: 1     <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">滚动升级时允许的最大Unavailable的pod个数</span>
selector:                 <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">定义标签选择器,部署需要管理的pod(带有该标签的的会被管理)需在pod 模板中定义</span>
<span style="color: rgba(0, 0, 0, 1)">    matchLabels:
      app: web</span>-<span style="color: rgba(0, 0, 0, 1)">server
template:              </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">这里Pod的定义</span>
<span style="color: rgba(0, 0, 0, 1)">    metadata:
      labels:            </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">Pod的label</span>
      app: web-<span style="color: rgba(0, 0, 0, 1)">server
    spec:              </span><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)">      containers:
      </span>- name: nginx         <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">容器的名字</span>
      image: nginx:1.12.1   <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">容器的镜像地址 </span>
      command: [ <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">/bin/sh</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">-c</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">cat /etc/config/path/to/special-key</span><span style="color: rgba(128, 0, 0, 1)">"</span> ]    <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">启动命令   </span>
      args:                                                                <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">启动参数</span>
            - <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">-storage.local.retention=$(STORAGE_RETENTION)</span><span style="color: rgba(128, 0, 0, 1)">'</span>
            - <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">-storage.local.memory-chunks=$(STORAGE_MEMORY_CHUNKS)</span><span style="color: rgba(128, 0, 0, 1)">'</span>
            - <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">-config.file=/etc/prometheus/prometheus.yml</span><span style="color: rgba(128, 0, 0, 1)">'</span>
            - <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">-alertmanager.url=http://alertmanager:9093/alertmanager</span><span style="color: rgba(128, 0, 0, 1)">'</span>
            - <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">-web.external-url=$(EXTERNAL_URL)</span><span style="color: rgba(128, 0, 0, 1)">'</span>
    <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">如果command和args均没有写,那么用Docker默认的配置。</span>
    <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">如果command写了,但args没有写,那么Docker默认的配置会被忽略而且仅仅执行.yaml文件的command(不带任何参数的)。</span>
    <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">如果command没写,但args写了,那么Docker默认配置的ENTRYPOINT的命令行会被执行,但是调用的参数是.yaml中的args。</span>
    <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">如果如果command和args都写了,那么Docker默认的配置被忽略,使用.yaml的配置。</span>
<span style="color: rgba(0, 0, 0, 1)">      imagePullPolicy: IfNotPresent
      </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> IfNotPresent :默认值,本地有则使用本地镜像,不拉取,如果不存在则拉取</span>
      <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> Always:总是拉取</span>
      <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> Never:只使用本地镜像,从不拉取</span>
<span style="color: rgba(0, 0, 0, 1)">          livenessProbe:      
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">表示container是否处于live状态。如果LivenessProbe失败,LivenessProbe将会通知kubelet对应的container不健康了。<br>#随后kubelet将kill掉container,并根据RestarPolicy进行进一步的操作。默认情况下LivenessProbe在第一次检测之前初始化值为Success,如果container没有提供LivenessProbe,则也认为是Success;</span>
<span style="color: rgba(0, 0, 0, 1)">            httpGet:
            path: </span>/health <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">如果没有心跳检测接口就为/</span>
            port: 8080<span style="color: rgba(0, 0, 0, 1)">
            scheme: HTTP
            initialDelaySeconds: </span>60 <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">#启动后延时多久开始运行检测</span>
            timeoutSeconds: 5<span style="color: rgba(0, 0, 0, 1)">
            successThreshold: </span>1<span style="color: rgba(0, 0, 0, 1)">
            failureThreshold: </span>5<span style="color: rgba(0, 0, 0, 1)">
            readinessProbe:
          readinessProbe:
            httpGet:
            path: </span>/health <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">如果没有心跳检测接口就为/</span>
            port: 8080<span style="color: rgba(0, 0, 0, 1)">
            scheme: HTTP
            initialDelaySeconds: </span>30 <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">#启动后延时多久开始运行检测</span>
            timeoutSeconds: 5<span style="color: rgba(0, 0, 0, 1)">
            successThreshold: </span>1<span style="color: rgba(0, 0, 0, 1)">
            failureThreshold: </span>5<span style="color: rgba(0, 0, 0, 1)">
          resources:            </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">#CPU内存限制</span>
<span style="color: rgba(0, 0, 0, 1)">            requests:
            cpu: </span>2<span style="color: rgba(0, 0, 0, 1)">
            memory: 2048Mi
            limits:
            cpu: </span>2<span style="color: rgba(0, 0, 0, 1)">
            memory: 2048Mi
          env:                  </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">#通过环境变量的方式,直接传递pod=自定义Linux OS环境变量</span>
            - name: LOCAL_KEY   <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">本地Key</span>
<span style="color: rgba(0, 0, 0, 1)">            value: value
            </span>- name: CONFIG_MAP_KEY<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">局策略可使用configMap的配置Key,</span>
<span style="color: rgba(0, 0, 0, 1)">            valueFrom:
                configMapKeyRef:
                  name: special</span>-config   <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">configmap中找到name为special-config</span>
                  key: special.type      <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">找到name为special-config里data下的key</span>
<span style="color: rgba(0, 0, 0, 1)">          ports:
            </span>-<span style="color: rgba(0, 0, 0, 1)"> name: http
            containerPort: </span>8080 <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">对service暴露端口</span>
          volumeMounts:   <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">挂载volumes中定义的磁盘</span>
          - name: log-<span style="color: rgba(0, 0, 0, 1)">cache
            mount: </span>/tmp/<span style="color: rgba(0, 0, 0, 1)">log
          </span>- name: sdb       <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">普通用法,该卷跟随容器销毁,挂载一个目录</span>
            mountPath: /data/<span style="color: rgba(0, 0, 0, 1)">media   
          </span>- name: nfs-client-root    <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">直接挂载硬盘方法,如挂载下面的nfs目录到/mnt/nfs</span>
            mountPath: /mnt/<span style="color: rgba(0, 0, 0, 1)">nfs
          </span>- name: example-volume-config<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">高级用法第1种,将ConfigMap的log-script,backup-script分别挂载到/etc/config目录下的一个相对路径path/to/...下,如果存在同名文件,直接覆盖。</span>
            mountPath: /etc/<span style="color: rgba(0, 0, 0, 1)">config      
          </span>- name: rbd-pvc                <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">高级用法第2中,挂载PVC(PresistentVolumeClaim)</span>

<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">使用volume将ConfigMap作为文件或目录直接挂载,其中每一个key-value键值对都会生成一个文件,key为文件名,value为内容,</span>
volumes:<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 定义磁盘给上面volumeMounts挂载</span>
- name: log-<span style="color: rgba(0, 0, 0, 1)">cache
    emptyDir: {}
</span>- name: sdb<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)">    hostPath:
      path: </span>/any/path/it/will/be/<span style="color: rgba(0, 0, 0, 1)">replaced
</span>- name: example-volume-config<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 供ConfigMap文件内容到指定路径使用</span>
<span style="color: rgba(0, 0, 0, 1)">    configMap:
      name: example</span>-volume-config<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">ConfigMap中名称</span>
<span style="color: rgba(0, 0, 0, 1)">      items:
      </span>- key: log-script         <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">ConfigMap中的Key</span>
      path: path/to/log-script<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">指定目录下的一个相对路径path/to/log-script</span>
      - key: backup-script      <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">ConfigMap中的Key</span>
      path: path/to/backup-script<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">指定目录下的一个相对路径path/to/backup-script</span>
- name: nfs-client-root         <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">供挂载NFS存储类型</span>
<span style="color: rgba(0, 0, 0, 1)">    nfs:
      server: </span>10.42.0.55          <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">NFS服务器地址</span>
      path: /opt/public         <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">showmount -e 看一下路径</span>
- name: rbd-pvc               <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">挂载PVC磁盘</span>
<span style="color: rgba(0, 0, 0, 1)">    persistentVolumeClaim:
      claimName: rbd</span>-pvc1         <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">挂载已经申请的pvc磁盘</span></pre>
</div>
<p> <strong>2、SERVICE 配置模板</strong></p>
<div class="cnblogs_code" style="margin-left: 30px">
<pre><span style="color: rgba(0, 0, 0, 1)">apiVersion: v1
kind: Service
matadata:                              </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">元数据</span>
name: string                           <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">service的名称</span>
namespace: string                      <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">命名空间</span>
labels:                              <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)"> name: string
annotations:                           </span><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)"> name: string
spec:                                    </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">详细描述</span>
selector: []                           <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">label selector配置,将选择具有label标签的Pod作为管理 范围</span>
type: string                           <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">service的类型,指定service的访问方式,默认为clusterIp</span>
clusterIP: string                      <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">虚拟服务地址</span>
sessionAffinity: string                <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">是否支持session</span>
ports:                                 <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">service需要暴露的端口列表</span>
- name: string                         <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">端口名称</span>
    protocol: string                     <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">端口协议,支持TCP和UDP,默认TCP</span>
    port: int                            <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">服务监听的端口号</span>
    targetPort: int                      <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">需要转发到后端Pod的端口号</span>
    nodePort: int                        <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">当type = NodePort时,指定映射到物理机的端口号</span>
status:                              <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">当spce.type=LoadBalancer时,设置外部负载均衡器的地址</span>
    loadBalancer:                        <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">外部负载均衡器</span>
      ingress:                           <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">外部负载均衡器</span>
      ip: string                     <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">外部负载均衡器的Ip地址值</span>
      hostname: string               <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">外部负载均衡器的主机名</span></pre>
</div>
<p style="margin-left: 30px">Service节点说明:</p>
<p style="margin-left: 30px">  <img src="https://img2020.cnblogs.com/blog/374428/202106/374428-20210627160308947-947135813.png"></p>
<p> <strong>3、Pod配置模板:</strong></p>
<div class="cnblogs_code" style="margin-left: 30px">
<pre>apiVersion: v1                  <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">必选,版本号,例如v1,版本号必须可以用 kubectl api-versions 查询到 .</span>
kind: Pod                      <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">必选,Pod</span>
metadata:                      <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">必选,元数据</span>
name: string                  <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">必选,Pod名称</span>
namespace: string           <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">必选,Pod所属的命名空间,默认为"default"</span>
labels:                      <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">自定义标签</span>
    - name: string                 <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">自定义标签名字</span>
annotations:                       <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)"> name: string
spec:                            </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">必选,Pod中容器的详细定义</span>
containers:                       <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">必选,Pod中容器列表</span>
- name: string                        <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">必选,容器名称,需符合RFC 1035规范</span>
    image: string                     <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">必选,容器的镜像名称</span>
    imagePullPolicy: [ Always|Never|IfNotPresent ]<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">获取镜像的策略 Alawys表示下载镜像 IfnotPresent表示优先使用本地镜像,否则下载镜像,Nerver表示仅使用本地镜像</span>
    command:              <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">容器的启动命令列表,如不指定,使用打包时使用的启动命令</span>
    args:                      <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">容器的启动命令参数列表</span>
    workingDir: string                  <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">容器的工作目录</span>
    volumeMounts:               <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">挂载到容器内部的存储卷配置</span>
    - name: string                 <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">引用pod定义的共享存储卷的名称,需用volumes[]部分定义的的卷名</span>
      mountPath: string               <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">存储卷在容器内mount的绝对路径,应少于512字符</span>
      readOnly: boolean               <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">是否为只读模式</span>
    ports:                     <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">需要暴露的端口库号列表</span>
    - name: string                 <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">端口的名称</span>
      containerPort: int                <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">容器需要监听的端口号</span>
      hostPort: int                  <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">容器所在主机需要监听的端口号,默认与Container相同</span>
      protocol: string                  <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">端口协议,支持TCP和UDP,默认TCP</span>
    env:                        <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">容器运行前需设置的环境变量列表</span>
    - name: string                    <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">环境变量名称</span>
      value: string                 <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">环境变量的值</span>
    resources:                        <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">资源限制和请求的设置</span>
      limits:                       <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">资源限制的设置</span>
      cpu: string                 <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">Cpu的限制,单位为core数,将用于docker run --cpu-shares参数</span>
      memory: string                  <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">内存限制,单位可以为Mib/Gib,将用于docker run --memory参数</span>
      requests:                         <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">资源请求的设置</span>
      cpu: string                 <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">Cpu请求,容器启动的初始可用数量</span>
      memory: string                  <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">内存请求,容器启动的初始可用数量</span>
    livenessProbe:                    <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">对Pod内各容器健康检查的设置,当探测无响应几次后将自动重启该容器,检查方法有exec、httpGet和tcpSocket,对一个容器只需设置其中一种方法即可</span>
      <span style="color: rgba(0, 0, 255, 1)">exec</span>:                   <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">对Pod容器内检查方式设置为exec方式</span>
      command:                <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">exec方式需要制定的命令或脚本</span>
      httpGet:                <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">对Pod内个容器健康检查方法设置为HttpGet,需要制定Path、port</span>
<span style="color: rgba(0, 0, 0, 1)">      path: string
      port: number
      host: string
      scheme: string
      HttpHeaders:
      </span>-<span style="color: rgba(0, 0, 0, 1)"> name: string
          value: string
      tcpSocket:            </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">对Pod内个容器健康检查方式设置为tcpSocket方式</span>
<span style="color: rgba(0, 0, 0, 1)">         port: number
       initialDelaySeconds: 0       </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">容器启动完成后首次探测的时间,单位为秒</span>
       timeoutSeconds: 0          <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">对容器健康检查探测等待响应的超时时间,单位秒,默认1秒</span>
       periodSeconds: 0         <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">对容器监控检查的定期探测时间设置,单位秒,默认10秒一次</span>
<span style="color: rgba(0, 0, 0, 1)">       successThreshold: 0
       failureThreshold: 0
       securityContext:
         privileged: false
    restartPolicy: <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">Pod的重启策略,Always表示一旦不管以何种方式终止运行,kubelet都将重启,OnFailure表示只有Pod以非0退出码退出才重启,Nerver表示不再重启该Pod</span>
    nodeSelector: obeject         <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">设置NodeSelector表示将该Pod调度到包含这个label的node上,以key:value的格式指定</span>
    imagePullSecrets:       <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">Pull镜像时使用的secret名称,以key:secretkey格式指定</span>
      -<span style="color: rgba(0, 0, 0, 1)"> name: string
    hostNetwork: false         </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">是否使用主机网络模式,默认为false,如果设置为true,表示使用宿主机网络</span>
<span style="color: rgba(0, 0, 0, 1)">   
volumes:                </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">在该pod上定义共享存储卷列表</span>
    - name: string            <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">共享存储卷名称 (volumes类型有很多种)</span>
      emptyDir: {}              <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">类型为emtyDir的存储卷,与Pod同生命周期的一个临时目录。为空值</span>
      hostPath: string            <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">类型为hostPath的存储卷,表示挂载Pod所在宿主机的目录</span>
      path: string              <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">Pod所在宿主机的目录,将被用于同期中mount的目录</span>
      secret:                 <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">类型为secret的存储卷,挂载集群与定义的secre对象到容器内部</span>
<span style="color: rgba(0, 0, 0, 1)">      scretname: string
      items:   
      </span>-<span style="color: rgba(0, 0, 0, 1)"> key: string
          path: string
      configMap:                      </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">类型为configMap的存储卷,挂载预定义的configMap对象到容器内部</span>
<span style="color: rgba(0, 0, 0, 1)">      name: string
      items:
      </span>-<span style="color: rgba(0, 0, 0, 1)"> key: string
          path: string
      nfs:                           </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">类型为NFS的存储卷</span>
      server: 192.168.66.50      <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">nfs服务器ip或是域名 </span>
      path: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">/test</span><span style="color: rgba(128, 0, 0, 1)">"</span>                <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">nfs服务器共享的目录</span>
      persistentVolumeClaim:          <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">类型为persistentVolumeClaim的存储卷</span>
      claimName: test-pvc         <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">名字一定要正确,使用的是kind为PersistentVolumeClaim中的name</span></pre>
</div>
<h4><span style="font-size: 1em"> 4、快速生成YAML文件</span></h4>
<p><span style="font-size: 1em">  a) 命令:</span></p>
<div class="cnblogs_code" style="margin-left: 30px">
<pre>kubectl run --image=nginx my-deploy -o yaml --dry-run &gt;my-<span style="color: rgba(0, 0, 0, 1)">deploy.yaml
kubectl create </span>-f deploy-nginx.yaml -o yaml --dry-run &gt;my-<span style="color: rgba(0, 0, 0, 1)">deploy.yaml
kubectl create </span>-f deploy-nginx.yaml -o json --dry-run &gt;my-deploy.json<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 指定输出json格式</span>
<span style="color: rgba(0, 0, 0, 1)">
– image </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 指定模板镜像</span>
my-deploy <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 运行标签名称</span>
–dry-run <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 只测试运行,不会实际运行pod</span>
-o yaml <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 指定输出格式</span></pre>
</div>
<p> 2、查询Pod容器的字段资源内部文档</p>
<div class="cnblogs_code" style="margin-left: 30px">
<pre>kubectl explain pods<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)">kubectl explain pods.spec
kubectl explain pods.spec.containers</span></pre>
</div>
<h4><span style="font-size: 1em">总结</span></h4>
<p><span style="font-size: 1em"> 本篇对yaml文件语法了解,以及对k8s中常用创建Deployment、Service、Pod的yaml文件中各个节点的含义用法了解。接下来对k8s中各种资源进一步了解、学习。并实际操作应用。</span>  </p><br><br>
来源:https://www.cnblogs.com/cwsheng/p/14940571.html
頁: [1]
查看完整版本: 入门Kubernetes - YAML文件