落雁 發表於 2021-9-2 08:47:00

Kubernetes环境搭建

<section id="nice" data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style="font-size: 16px; color: rgba(0, 0, 0, 1); padding: 0 10px; line-height: 1.6; word-spacing: 0; letter-spacing: 0; word-wrap: break-word; text-align: left; font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, &quot;PingFang SC&quot;, Cambria, Cochin, Georgia, Times, &quot;Times New Roman&quot;, serif"><h3 data-tool="mdnice编辑器" style="margin: 30px 0 15px; border-bottom: 2px solid rgba(0, 172, 193, 1); padding: 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 20px"><span class="prefix" style="font-weight: normal; background: rgba(0, 172, 193, 1); color: rgba(255, 255, 255, 1); padding: 3px 10px 0; border-top-right-radius: 3px; border-top-left-radius: 4px; margin-right: 2px; display: none"></span><span class="content" style="display: inline-block; font-weight: normal; background: rgba(0, 172, 193, 1); color: rgba(255, 255, 255, 1); padding: 3px 10px 0; border-top-right-radius: 3px; border-top-left-radius: 4px; margin-right: 2px">前言</span><span class="suffix" style="font-weight: normal; background: rgba(0, 172, 193, 1); color: rgba(255, 255, 255, 1); padding: 3px 10px 0; border-top-right-radius: 3px; border-top-left-radius: 4px; margin-right: 2px; display: none"></span></h3>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1)">本篇是Kubernetes第二篇,大家一定要把环境搭建起来,看是解决不了问题的,必须实战。</p>
<h5 data-tool="mdnice编辑器" style="margin: 30px 0 15px; padding: 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 16px"><span class="prefix" style="display: none"></span><span class="content">Kubernetes系列文章:</span><span class="suffix" style="display: none"></span></h5>
<ol data-tool="mdnice编辑器" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: decimal">
<li style="margin-bottom: 0.5em"><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500">Kubernetes介绍</section></li></ol>
<h3 data-tool="mdnice编辑器" style="margin: 30px 0 15px; border-bottom: 2px solid rgba(0, 172, 193, 1); padding: 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 20px"><span class="prefix" style="font-weight: normal; background: rgba(0, 172, 193, 1); color: rgba(255, 255, 255, 1); padding: 3px 10px 0; border-top-right-radius: 3px; border-top-left-radius: 4px; margin-right: 2px; display: none"></span><span class="content" style="display: inline-block; font-weight: normal; background: rgba(0, 172, 193, 1); color: rgba(255, 255, 255, 1); padding: 3px 10px 0; border-top-right-radius: 3px; border-top-left-radius: 4px; margin-right: 2px">Kubernetes搭建方式介绍和对比</span><span class="suffix" style="font-weight: normal; background: rgba(0, 172, 193, 1); color: rgba(255, 255, 255, 1); padding: 3px 10px 0; border-top-right-radius: 3px; border-top-left-radius: 4px; margin-right: 2px; display: none"></span></h3>
<h5 data-tool="mdnice编辑器" style="margin: 30px 0 15px; padding: 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 16px"><span class="prefix" style="display: none"></span><span class="content">Minikube</span><span class="suffix" style="display: none"></span></h5>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1)">Minikube是一种可以在本地轻松运行Kubernetes的工具。Minikube在笔记本电脑的VM中运行单节点Kubernetes集群,供希望尝试Kubernetes或日常开发的用户测试使用。特点是不能启动生产集群,没有高可用性的单节点机器。</p>
<h5 data-tool="mdnice编辑器" style="margin: 30px 0 15px; padding: 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 16px"><span class="prefix" style="display: none"></span><span class="content">Kubeadm</span><span class="suffix" style="display: none"></span></h5>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1)">Kubeadm是Kubernetes 1.4开始新增的特性,Kubeadm 是一个提供了 kubeadm init 和 kubeadm join 的工具, 作为创建 Kubernetes 集群的 “快捷途径” 的最佳实践。Kubeadm 通过执行必要的操作来启动和运行最小可用集群。 按照设计,它只关注启动引导,而非配置机器。同样的, 安装各种 “锦上添花” 的扩展,例如 Kubernetes Dashboard、 监控方案、以及特定云平台的扩展,都不在讨论范围内。Kubeadm是一种把 kubelet 直接运行在宿主机上,然后使用容器部署其他的 Kubernetes 组件的方案。</p>
<h5 data-tool="mdnice编辑器" style="margin: 30px 0 15px; padding: 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 16px"><span class="prefix" style="display: none"></span><span class="content">Kind</span><span class="suffix" style="display: none"></span></h5>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1)">Kind 是 Kubernetes in Docker 的简写,是一个使用 Docker 容器作为 Nodes,在本地创建和运行 Kubernetes 群集的工具。适用于在本机创建 Kubernetes 群集环境进行开发和测试。Kind 使用kubeadm创建和启动群集节点,使用 containerd 作为容器运行时。</p>
<h5 data-tool="mdnice编辑器" style="margin: 30px 0 15px; padding: 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 16px"><span class="prefix" style="display: none"></span><span class="content">二进制包</span><span class="suffix" style="display: none"></span></h5>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1)">二进制包就是在官网下载相关的组件的二进制包,相对于上面两种快速搭建集群的方式,其实就是相当于用程序脚本帮我们装好了集群,前两者属于自动部署,简化部署操作,自动部署屏蔽了很多细节,使得对各个模块感知很少,遇到问题很难排查,如果手动安装,对Kubernetes理解也会更全面。二进制包目前生产环境的主流搭建方式,已在生产环境验证,kubeadm也可以搭建生产环境。</p>
<h3 data-tool="mdnice编辑器" style="margin: 30px 0 15px; border-bottom: 2px solid rgba(0, 172, 193, 1); padding: 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 20px"><span class="prefix" style="font-weight: normal; background: rgba(0, 172, 193, 1); color: rgba(255, 255, 255, 1); padding: 3px 10px 0; border-top-right-radius: 3px; border-top-left-radius: 4px; margin-right: 2px; display: none"></span><span class="content" style="display: inline-block; font-weight: normal; background: rgba(0, 172, 193, 1); color: rgba(255, 255, 255, 1); padding: 3px 10px 0; border-top-right-radius: 3px; border-top-left-radius: 4px; margin-right: 2px">使用Kubeadm搭建Kubernetes学习环境</span><span class="suffix" style="font-weight: normal; background: rgba(0, 172, 193, 1); color: rgba(255, 255, 255, 1); padding: 3px 10px 0; border-top-right-radius: 3px; border-top-left-radius: 4px; margin-right: 2px; display: none"></span></h3>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1)">这里我简单说下我为啥选择Kubeadm作为自己搭建集群工具,Kind、Minikube实在是太简单,根本让我们感觉不到一点困难,Kubeadm可以让我们适当体验Kubernetes搭建过程,去理解一些组件之间的交互,二进制才是我们终极目标,我只是刚开始不想让大家被安装就抹灭了学习Kubernetes的兴趣。</p>
<h5 data-tool="mdnice编辑器" style="margin: 30px 0 15px; padding: 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 16px"><span class="prefix" style="display: none"></span><span class="content">配置说明</span><span class="suffix" style="display: none"></span></h5>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1)">在搭建K8S集群时,推荐在阿里云或腾讯云采购如下配置(当然也可以使用自己虚机):</p>
<ol data-tool="mdnice编辑器" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: decimal">
<li style="margin-bottom: 0.5em"><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500">至少2台2C2G的服务器,最好2C4G或者8G,2G的话内存单存Kubernetes就会占用一般,后面不太方便大家做实验,我采用的2台2C4G阿里云机器;</section></li><li style="margin-bottom: 0.5em"><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500">系统的话大家采用CentOS 7.X 或 CentOS 8.X的版本,这里我采用8.2的版本;</section></li></ol>
<h5 data-tool="mdnice编辑器" style="margin: 30px 0 15px; padding: 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 16px"><span class="prefix" style="display: none"></span><span class="content">搭建前准备工作</span><span class="suffix" style="display: none"></span></h5>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1)">注意:我这里采用的Kubernetes1.21.2和Docker 20.10.7的版本,大家注意下版本问题。</p>
<h5 data-tool="mdnice编辑器" style="margin: 30px 0 15px; padding: 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 16px"><span class="prefix" style="display: none"></span><span class="content">查看Linux版本</span><span class="suffix" style="display: none"></span></h5>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="word-wrap: break-word; margin: 0 2px; overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">#所有机器都进行检查</span><br>cat&nbsp;/etc/redhat-release<br></code></pre>
<h5 data-tool="mdnice编辑器" style="margin: 30px 0 15px; padding: 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 16px"><span class="prefix" style="display: none"></span><span class="content">修改hostname</span><span class="suffix" style="display: none"></span></h5>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="word-wrap: break-word; margin: 0 2px; overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">#这里强烈建议修改hostname,这样子在集群列表查询的时候很很明确</span><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">#修改hostname</span><br>hostnamectl&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">set</span>-hostname&nbsp;demo-master-1<br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">#检查是否修改成功</span><br>hostname<br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">#设置hostname解析&nbsp;类似windows的host解析</span><br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">echo</span>&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"127.0.0.1&nbsp;&nbsp;&nbsp;<span class="hljs-variable" style="color: rgba(152, 104, 1, 1); line-height: 26px">$(hostname)</span>"</span>&nbsp;&gt;&gt;&nbsp;/etc/hosts<br></code></pre>
<h5 data-tool="mdnice编辑器" style="margin: 30px 0 15px; padding: 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 16px"><span class="prefix" style="display: none"></span><span class="content">查看网络</span><span class="suffix" style="display: none"></span></h5>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="word-wrap: break-word; margin: 0 2px; overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">#查看内网ip地址&nbsp;找到eth0</span><br>ip&nbsp;add<br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">#使用ping命令保证每个节点都是IP地址必须可以互通</span><br>ping<br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">#关闭防火墙</span><br>systemctl&nbsp;stop&nbsp;firewalld<br>systemctl&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">disable</span>&nbsp;firewalld<br></code></pre>
<figure data-tool="mdnice编辑器" style="margin: 10px 0; display: flex; flex-direction: column; justify-content: center; align-items: center"><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/374ad558221e44c598e993092c698c37~tplv-k3u1fbpfcp-zoom-1.image"><figcaption style="margin-top: 5px; text-align: center; color: rgba(136, 136, 136, 1); font-size: 14px">image.png</figcaption></figure>
<h5 data-tool="mdnice编辑器" style="margin: 30px 0 15px; padding: 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 16px"><span class="prefix" style="display: none"></span><span class="content">Docker安装</span><span class="suffix" style="display: none"></span></h5>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1)">可以参考我的这篇博文;</p>
<h5 data-tool="mdnice编辑器" style="margin: 30px 0 15px; padding: 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 16px"><span class="prefix" style="display: none"></span><span class="content">安装 kubeadm、kubelet 和 kubectl</span><span class="suffix" style="display: none"></span></h5>
<ol data-tool="mdnice编辑器" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: decimal">
<li style="margin-bottom: 0.5em"><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500"><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1)">配置配置Kubernetes的yum源;</p>
<pre class="custom" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; -webkit-overflow-scrolling: touch; font-size: 14px; word-wrap: break-word; padding: 15px 4px 2px; margin: 0 2px; color: rgba(30, 107, 184, 1); background: rgba(250, 250, 250, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-radius: 5px">cat&nbsp;&lt;&lt;EOF&nbsp;&gt;&nbsp;/etc/yum.repos.d/kubernetes.repo<br><br>name=Kubernetes<br>baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64<br>enabled=1<br>gpgcheck=0<br>repo_gpgcheck=0<br>gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg<br>EOF<br></code></pre>
</section></li><li style="margin-bottom: 0.5em"><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500"><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1)">如果存在旧版本首先卸载旧版本,安装kubelet、kubeadm、kubectl,如果非Master可以不安装kubectl;</p>
<pre class="custom" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; -webkit-overflow-scrolling: touch; font-size: 14px; word-wrap: break-word; padding: 15px 4px 2px; margin: 0 2px; color: rgba(30, 107, 184, 1); background: rgba(250, 250, 250, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-radius: 5px"><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">#卸载Kubernetes</span><br>yum&nbsp;remove&nbsp;-y&nbsp;kubelet&nbsp;kubeadm&nbsp;kubectl<br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">#安装</span><br>yum&nbsp;install&nbsp;-y&nbsp;kubelet-1.21.2&nbsp;kubeadm-1.21.2&nbsp;kubectl-1.21.2<br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">#非master可以采用以下命令</span><br>yum&nbsp;install&nbsp;-y&nbsp;kubelet-1.21.2&nbsp;kubeadm-1.21.2<br></code></pre>
</section></li><li style="margin-bottom: 0.5em"><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500"><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1)">关闭Swap,这里简单聊下Kubernetes为什么会关闭Swap?Swap Space是磁盘上的一块区域,可以是一个分区,也可以是一个文件,或者是他们的组合。简单点说,当系统物理内存吃紧时,Linux会将内存中不常访问的数据保存到Swap上,这样系统就有更多的物理内存为各个进程服务,而当系统需要访问Swap上存储的内容时,再将Swap上的数据加载到内存中。Kubernetes关闭Swap一个是性能问题,开启Swap会严重影响性能(包括内存和I/O);另一个是管理问题,开启Swap后通过Cgroups设置的内存上限就会失效,如果不关闭Kubernetes运行会出现错误, 即使安装成功了,node重启后也会出现kubernetes server运行错误;</p>
<pre class="custom" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; -webkit-overflow-scrolling: touch; font-size: 14px; word-wrap: break-word; padding: 15px 4px 2px; margin: 0 2px; color: rgba(30, 107, 184, 1); background: rgba(250, 250, 250, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-radius: 5px">swapoff&nbsp;-a<br>yes&nbsp;|&nbsp;cp&nbsp;/etc/fstab&nbsp;/etc/fstab_bak<br>cat&nbsp;/etc/fstab_bak&nbsp;|grep&nbsp;-v&nbsp;swap&nbsp;&gt;&nbsp;/etc/fstab<br></code></pre>
</section></li><li style="margin-bottom: 0.5em"><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500"><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1)">关闭SeLinux,也简单聊一下为什么关闭SeLinux?SELinux(Secure Enhanced Linux)安全增强的Linux是美国国家安全局NSA针对计算机基础结构安全开发的一个全新的Linux安全策略机制。SELinux可以允许系统管理员更加灵活的来定义安全策略。SELinux是一个内核级别的安全机制,从Linux2.6内核之后就将SELinux集成在了内核当中,因为SELinux是内核级别的,所以我们对于其配置文件的修改都是需要重新启动操作系统才能生效的。现在主流发现的Linux版本里面都集成了SELinux机制,CentOS/RHEL都会默认开启SELinux机制。这里为什么建议大家关闭SeLinux,我相信不是专门的运维可能懂这个的人也不多,所以为了防止大家遇到一些奇怪的问题,就把它关闭吧;</p>
<pre class="custom" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; -webkit-overflow-scrolling: touch; font-size: 14px; word-wrap: break-word; padding: 15px 4px 2px; margin: 0 2px; color: rgba(30, 107, 184, 1); background: rgba(250, 250, 250, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-radius: 5px">setenforce&nbsp;0<br>sed&nbsp;-i&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"s/SELINUX=enforcing/SELINUX=disabled/g"</span>&nbsp;/etc/selinux/config<br></code></pre>
</section></li><li style="margin-bottom: 0.5em"><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500"><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1)">将Docker的Cgroup Driver 修改为systemd并且更改Docker为国内的镜像加速器,不然在为Kubernetes集群添加节点时会报错;</p>
</section></li></ol>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="word-wrap: break-word; margin: 0 2px; overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">#修改docker/daemon.json</span><br>cat&nbsp;&gt;&nbsp;/etc/docker/daemon.json&nbsp;&lt;&lt;EOF<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"exec-opts"</span>:&nbsp;[<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"native.cgroupdriver=systemd"</span>],<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"log-driver"</span>:&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"json-file"</span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"log-opts"</span>:&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"max-size"</span>:&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"100m"</span><br>&nbsp;&nbsp;&nbsp;&nbsp;},<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"storage-driver"</span>:&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"overlay2"</span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"registry-mirrors"</span>:[<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"https://kfwkfulq.mirror.aliyuncs.com"</span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"https://2lqq34jg.mirror.aliyuncs.com"</span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"https://pee6w651.mirror.aliyuncs.com"</span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"http://hub-mirror.c.163.com"</span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"https://docker.mirrors.ustc.edu.cn"</span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"https://registry.docker-cn.com"</span><br>&nbsp;&nbsp;&nbsp;&nbsp;]<br>}<br>EOF<br>mkdir&nbsp;-p&nbsp;/etc/systemd/system/docker.service.d<br></code></pre>
<ol start="6" data-tool="mdnice编辑器" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: decimal">
<li style="margin-bottom: 0.5em"><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500"><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1)">设置网络配置相关的参数;</p>
<pre class="custom" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; -webkit-overflow-scrolling: touch; font-size: 14px; word-wrap: break-word; padding: 15px 4px 2px; margin: 0 2px; color: rgba(30, 107, 184, 1); background: rgba(250, 250, 250, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-radius: 5px"><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">#增加配置</span><br>cat&nbsp;&lt;&lt;EOF&nbsp;&gt;&nbsp;/etc/sysctl.d/<br>k8s.confnet.ipv4.ip_forward=1<br>net.bridge.bridge-nf-call-ip6tables&nbsp;=&nbsp;1<br>net.bridge.bridge-nf-call-iptables&nbsp;=&nbsp;1<br>EOF<br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">#加载</span><br>sysctl&nbsp;--system<br></code></pre>
</section></li><li style="margin-bottom: 0.5em"><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500"><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1)">重启docker,并设置开机启动kubelet;</p>
<pre class="custom" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; -webkit-overflow-scrolling: touch; font-size: 14px; word-wrap: break-word; padding: 15px 4px 2px; margin: 0 2px; color: rgba(30, 107, 184, 1); background: rgba(250, 250, 250, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-radius: 5px"><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">#&nbsp;重启docker,并启动&nbsp;kubelet</span><br>systemctl&nbsp;daemon-reloadsy<br>stemctl&nbsp;restart&nbsp;docker<br>systemctl&nbsp;<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">enable</span>&nbsp;kubelet&nbsp;&amp;&amp;&nbsp;systemctl&nbsp;start&nbsp;kubelet<br></code></pre>
</section></li></ol>
<h5 data-tool="mdnice编辑器" style="margin: 30px 0 15px; padding: 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 16px"><span class="prefix" style="display: none"></span><span class="content">初始化Master</span><span class="suffix" style="display: none"></span></h5>
<ol data-tool="mdnice编辑器" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: decimal">
<li style="margin-bottom: 0.5em"><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500"><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1)">配置Host相关内容;</p>
<pre class="custom" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; -webkit-overflow-scrolling: touch; font-size: 14px; word-wrap: break-word; padding: 15px 4px 2px; margin: 0 2px; color: rgba(30, 107, 184, 1); background: rgba(250, 250, 250, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-radius: 5px"><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">#只在master节点执行</span><br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">export</span>&nbsp;MASTER_IP=172.21.122.230<br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">#替换为自己hostName</span><br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">export</span>&nbsp;APISERVER_NAME=demo-master-1<br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">#设置Kubernetes容器组所在的网段</span><br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">export</span>&nbsp;POD_SUBNET=10.100.0.0/16echo&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"<span class="hljs-variable" style="color: rgba(152, 104, 1, 1); line-height: 26px">${MASTER_IP}</span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-variable" style="color: rgba(152, 104, 1, 1); line-height: 26px">${APISERVER_NAME}</span>"</span>&nbsp;&gt;&gt;&nbsp;/etc/hosts<br></code></pre>
</section></li><li style="margin-bottom: 0.5em"><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500"><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1)">配置kubeadm-config.xml相关参数,可以参考该文章,具体学习下参数含义;</p>
<pre class="custom" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; -webkit-overflow-scrolling: touch; font-size: 14px; word-wrap: break-word; padding: 15px 4px 2px; margin: 0 2px; color: rgba(30, 107, 184, 1); background: rgba(250, 250, 250, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-radius: 5px"><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">#删除默认是版本</span><br>rm&nbsp;-f&nbsp;./kubeadm-config.yaml<br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">#编辑kubeadm-config</span><br>cat&nbsp;&lt;&lt;EOF&nbsp;&gt;&nbsp;./kubeadm-config.yaml<br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">#api的版本</span><br>apiVersion:&nbsp;kubeadm.k8s.io/v1beta2<br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">#配置的种类</span><br>kind:&nbsp;ClusterConfiguration<br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">#版本</span><br>kubernetesVersion:&nbsp;v1.21.2<br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">#镜像拉取地址</span><br>imageRepository:&nbsp;registry.aliyuncs.com/google_containers<br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">#api默认绑定的端口</span><br>controlPlaneEndpoint:&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"<span class="hljs-variable" style="color: rgba(152, 104, 1, 1); line-height: 26px">${APISERVER_NAME}</span>:6443"</span><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">#自定义的网络</span><br>networking:&nbsp;&nbsp;<br>&nbsp;serviceSubnet:&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"10.96.0.0/16"</span>&nbsp;&nbsp;<br>&nbsp;podSubnet:&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"<span class="hljs-variable" style="color: rgba(152, 104, 1, 1); line-height: 26px">${POD_SUBNET}</span>"</span>&nbsp;&nbsp;<br>&nbsp;dnsDomain:&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"cluster.local"</span><br>EOF<br></code></pre>
</section></li><li style="margin-bottom: 0.5em"><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500"><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1)">初始化初始化Master节点,成功以后大致信息如下图;</p>
<pre class="custom" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; -webkit-overflow-scrolling: touch; font-size: 14px; word-wrap: break-word; padding: 15px 4px 2px; margin: 0 2px; color: rgba(30, 107, 184, 1); background: rgba(250, 250, 250, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-radius: 5px">kubeadm&nbsp;init&nbsp;--config=kubeadm-config.yaml&nbsp;--upload-certs<br></code></pre>
<figure style="margin: 10px 0; display: flex; flex-direction: column; justify-content: center; align-items: center"><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/822780c70b2f455487bea25ab6a42b24~tplv-k3u1fbpfcp-zoom-1.image"><figcaption style="margin-top: 5px; text-align: center; color: rgba(136, 136, 136, 1); font-size: 14px">image.png</figcaption></figure>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1)">如果出现以下错误,说明安装过kubeadm,使用以下命令进行初始化:</p>
<pre class="custom" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; -webkit-overflow-scrolling: touch; font-size: 14px; word-wrap: break-word; padding: 15px 4px 2px; margin: 0 2px; color: rgba(30, 107, 184, 1); background: rgba(250, 250, 250, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-radius: 5px">kubeadm reset -f
</code></pre>
<figure style="margin: 10px 0; display: flex; flex-direction: column; justify-content: center; align-items: center"><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/7207e37d988c47a491a65274d336ad21~tplv-k3u1fbpfcp-zoom-1.image"><figcaption style="margin-top: 5px; text-align: center; color: rgba(136, 136, 136, 1); font-size: 14px">image.png</figcaption></figure>
</section></li><li style="margin-bottom: 0.5em"><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500"><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1)">配置kubectl;</p>
<pre class="custom" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; -webkit-overflow-scrolling: touch; font-size: 14px; word-wrap: break-word; padding: 15px 4px 2px; margin: 0 2px; color: rgba(30, 107, 184, 1); background: rgba(250, 250, 250, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-radius: 5px">rm&nbsp;-rf&nbsp;/root/.kube/<br>mkdir&nbsp;/root/.kube/<br>cp&nbsp;-i&nbsp;/etc/kubernetes/admin.conf&nbsp;/root/.kube/config<br></code></pre>
</section></li><li style="margin-bottom: 0.5em"><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500"><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1)">检查节点初始化的情况;</p>
<pre class="custom" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; -webkit-overflow-scrolling: touch; font-size: 14px; word-wrap: break-word; padding: 15px 4px 2px; margin: 0 2px; color: rgba(30, 107, 184, 1); background: rgba(250, 250, 250, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-radius: 5px"><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">#查看各个组件执行的情况</span><br>watch&nbsp;kubectl&nbsp;get&nbsp;pod&nbsp;-n&nbsp;kube-system&nbsp;-o&nbsp;wide<br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">#查看Master节点情况</span><br>kubectl&nbsp;get&nbsp;nodes&nbsp;-o&nbsp;wide<br></code></pre>
<figure style="margin: 10px 0; display: flex; flex-direction: column; justify-content: center; align-items: center"><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/f3d2edf09aec4134b7fb1dd29d2ccb9f~tplv-k3u1fbpfcp-zoom-1.image"><figcaption style="margin-top: 5px; text-align: center; color: rgba(136, 136, 136, 1); font-size: 14px">image.png</figcaption></figure>
<figure style="margin: 10px 0; display: flex; flex-direction: column; justify-content: center; align-items: center"><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/f6c4f3832de0476085e88abd659f052b~tplv-k3u1fbpfcp-zoom-1.image"><figcaption style="margin-top: 5px; text-align: center; color: rgba(136, 136, 136, 1); font-size: 14px">image.png</figcaption></figure>
</section></li></ol>
<h5 data-tool="mdnice编辑器" style="margin: 30px 0 15px; padding: 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 16px"><span class="prefix" style="display: none"></span><span class="content">安装网络插件</span><span class="suffix" style="display: none"></span></h5>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1)">这里我选择了Flannel作为我的网络插件;</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="word-wrap: break-word; margin: 0 2px; overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">export</span>&nbsp;POD_SUBNET=10.100.0.0/16<br>kubectl&nbsp;apply&nbsp;-f&nbsp;https://kuboard.cn/install-script/v1.21.x/calico-operator.yaml<br>wget&nbsp;https://kuboard.cn/install-script/flannel/flannel-v0.14.0.yaml<br>sed&nbsp;-i&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"s#10.244.0.0/16#<span class="hljs-variable" style="color: rgba(152, 104, 1, 1); line-height: 26px">${POD_SUBNET}</span>#"</span>&nbsp;flannel-v0.14.0.yaml<br>kubectl&nbsp;apply&nbsp;-f&nbsp;./flannel-v0.14.0.yaml<br></code></pre>
<h5 data-tool="mdnice编辑器" style="margin: 30px 0 15px; padding: 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 16px"><span class="prefix" style="display: none"></span><span class="content">初始化Work节点</span><span class="suffix" style="display: none"></span></h5>
<ol data-tool="mdnice编辑器" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: decimal">
<li style="margin-bottom: 0.5em"><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500"><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1)">在Master节点上获取join参数;</p>
<pre class="custom" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; -webkit-overflow-scrolling: touch; font-size: 14px; word-wrap: break-word; padding: 15px 4px 2px; margin: 0 2px; color: rgba(30, 107, 184, 1); background: rgba(250, 250, 250, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-radius: 5px">kubeadm&nbsp;token&nbsp;create&nbsp;--<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">print</span>-join-command<br></code></pre>
</section></li><li style="margin-bottom: 0.5em"><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500"><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1)">配置Work节点的host信息;</p>
<pre class="custom" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; -webkit-overflow-scrolling: touch; font-size: 14px; word-wrap: break-word; padding: 15px 4px 2px; margin: 0 2px; color: rgba(30, 107, 184, 1); background: rgba(250, 250, 250, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-radius: 5px"><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">#&nbsp;替换为master节点IP</span><br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">export</span>&nbsp;MASTER_IP=172.21.122.230<br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">#&nbsp;替换为Master节点hostName</span><br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">export</span>&nbsp;APISERVER_NAME=demo-master-1<br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">echo</span>&nbsp;<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"<span class="hljs-variable" style="color: rgba(152, 104, 1, 1); line-height: 26px">${MASTER_IP}</span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-variable" style="color: rgba(152, 104, 1, 1); line-height: 26px">${APISERVER_NAME}</span>"</span>&nbsp;&gt;&gt;&nbsp;/etc/hosts<br></code></pre>
</section></li><li style="margin-bottom: 0.5em"><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500"><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1)">初始化Work节点,执行Master节点上获取到的Join参数;</p>
<figure style="margin: 10px 0; display: flex; flex-direction: column; justify-content: center; align-items: center"><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/d3f8c05e19b84f99a83af2a7e35940b7~tplv-k3u1fbpfcp-zoom-1.image"><figcaption style="margin-top: 5px; text-align: center; color: rgba(136, 136, 136, 1); font-size: 14px">image.png</figcaption></figure>
</section></li><li style="margin-bottom: 0.5em"><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500"><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1)">在Master上检查初始化结果;</p>
<pre class="custom" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; -webkit-overflow-scrolling: touch; font-size: 14px; word-wrap: break-word; padding: 15px 4px 2px; margin: 0 2px; color: rgba(30, 107, 184, 1); background: rgba(250, 250, 250, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-radius: 5px">kubectl&nbsp;get&nbsp;nodes&nbsp;-o&nbsp;wide<br></code></pre>
<figure style="margin: 10px 0; display: flex; flex-direction: column; justify-content: center; align-items: center"><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/7f65d52058bc460d860baba2de6fde91~tplv-k3u1fbpfcp-zoom-1.image"><figcaption style="margin-top: 5px; text-align: center; color: rgba(136, 136, 136, 1); font-size: 14px">image.png</figcaption></figure>
</section></li></ol>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1)">恭喜你,至此安装完成!</p>
<h3 data-tool="mdnice编辑器" style="margin: 30px 0 15px; border-bottom: 2px solid rgba(0, 172, 193, 1); padding: 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 20px"><span class="prefix" style="font-weight: normal; background: rgba(0, 172, 193, 1); color: rgba(255, 255, 255, 1); padding: 3px 10px 0; border-top-right-radius: 3px; border-top-left-radius: 4px; margin-right: 2px; display: none"></span><span class="content" style="display: inline-block; font-weight: normal; background: rgba(0, 172, 193, 1); color: rgba(255, 255, 255, 1); padding: 3px 10px 0; border-top-right-radius: 3px; border-top-left-radius: 4px; margin-right: 2px">实战</span><span class="suffix" style="font-weight: normal; background: rgba(0, 172, 193, 1); color: rgba(255, 255, 255, 1); padding: 3px 10px 0; border-top-right-radius: 3px; border-top-left-radius: 4px; margin-right: 2px; display: none"></span></h3>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1)">这个案例就简单创建一个运行着Nginx的Pod,体验一把Kubernetes的使用,此外建议大家再开始学习阶段不要去使用一些图形化工具,先把一些命令整体去了解一下,以后在使用图形化界面。</p>
<ol data-tool="mdnice编辑器" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: decimal">
<li style="margin-bottom: 0.5em"><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500">创建一个nginx-deployment.yaml文件,这里大家可以使用vscode去编辑,然后同步到服务器上,应该vscode也可以直接同步到服务器上,具体怎么配置大家自行百度,编写如下内容:</section></li></ol>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="word-wrap: break-word; margin: 0 2px; overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">#api版本
apiVersion: apps/v1
#指定资源的角色
kind: Deployment
#元数据名称
metadata:
name: nginx-deployment
#指定该资源的内容详情
spec:
#选择器
selector:
    matchLabels:
      app: nginx
#Pod的定义
template:
    metadata:
      labels:
      app: nginx
    #指定该资源的内容
    spec:
      containers:
      #容器名称
      - name: nginx
      #镜像版本
      image: nginx:latest
      #资源的限制
      resources:
          limits:
            memory: "128Mi"
            cpu: "500m"
      #暴露的端口
      ports:
      - containerPort: 80
</code><p><code class="hljs" style="word-wrap: break-word; margin: 0 2px; overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"></code></p></pre><p></p>
<ol start="2" data-tool="mdnice编辑器" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: decimal">
<li style="margin-bottom: 0.5em"><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500"><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1)">在Master上运行该资源;</p>
<pre class="custom" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; -webkit-overflow-scrolling: touch; font-size: 14px; word-wrap: break-word; padding: 15px 4px 2px; margin: 0 2px; color: rgba(30, 107, 184, 1); background: rgba(250, 250, 250, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-radius: 5px">kubectl&nbsp;create&nbsp;-f&nbsp;nginx-deployment.yaml<br></code></pre>
</section></li><li style="margin-bottom: 0.5em"><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500"><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1)">检查资源是否创建成功,这里我通过标签选择器进行查看,所以跟-l的参数;</p>
<pre class="custom" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; -webkit-overflow-scrolling: touch; font-size: 14px; word-wrap: break-word; padding: 15px 4px 2px; margin: 0 2px; color: rgba(30, 107, 184, 1); background: rgba(250, 250, 250, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-radius: 5px">kubectl&nbsp;get&nbsp;pods&nbsp;-l&nbsp;app=nginx<br></code></pre>
<figure style="margin: 10px 0; display: flex; flex-direction: column; justify-content: center; align-items: center"><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/902670782fcc46349057c2bc2cb8aae9~tplv-k3u1fbpfcp-zoom-1.image"><figcaption style="margin-top: 5px; text-align: center; color: rgba(136, 136, 136, 1); font-size: 14px">image.png</figcaption></figure>
</section></li><li style="margin-bottom: 0.5em"><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500"><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1)">查看Pod的整个详情;</p>
<pre class="custom" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url(&quot;https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg&quot;) 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; -webkit-overflow-scrolling: touch; font-size: 14px; word-wrap: break-word; padding: 15px 4px 2px; margin: 0 2px; color: rgba(30, 107, 184, 1); background: rgba(250, 250, 250, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-radius: 5px"><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">#后面跟着具体的pod名称</span><br>kubectl&nbsp;describe&nbsp;pod&nbsp;nginx-deployment-7dc45fbd74-5bh2z<br></code></pre>
<figure style="margin: 10px 0; display: flex; flex-direction: column; justify-content: center; align-items: center"><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/3f1c7961098143b6bd28d3d2364afc5d~tplv-k3u1fbpfcp-zoom-1.image"><figcaption style="margin-top: 5px; text-align: center; color: rgba(136, 136, 136, 1); font-size: 14px">image.png</figcaption></figure>
</section></li></ol>
<h3 data-tool="mdnice编辑器" style="margin: 30px 0 15px; border-bottom: 2px solid rgba(0, 172, 193, 1); padding: 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 20px"><span class="prefix" style="font-weight: normal; background: rgba(0, 172, 193, 1); color: rgba(255, 255, 255, 1); padding: 3px 10px 0; border-top-right-radius: 3px; border-top-left-radius: 4px; margin-right: 2px; display: none"></span><span class="content" style="display: inline-block; font-weight: normal; background: rgba(0, 172, 193, 1); color: rgba(255, 255, 255, 1); padding: 3px 10px 0; border-top-right-radius: 3px; border-top-left-radius: 4px; margin-right: 2px">结束</span><span class="suffix" style="font-weight: normal; background: rgba(0, 172, 193, 1); color: rgba(255, 255, 255, 1); padding: 3px 10px 0; border-top-right-radius: 3px; border-top-left-radius: 4px; margin-right: 2px; display: none"></span></h3>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1)">欢迎大家点点关注,点点赞!
<img src="https://img2018.cnblogs.com/blog/1005447/201906/1005447-20190620221533393-1847070636.png"></p>
</section><br><br>
来源:https://www.cnblogs.com/wtzbk/p/15216488.html
頁: [1]
查看完整版本: Kubernetes环境搭建