量体截衣 發表於 2024-3-14 18:54:00

CentOS 7.9 环境下搭建k8s集群(一主两从)

<p></p><div class="toc"><div class="toc-container-header">目录</div><ul><li>一、硬件准备(虚拟主机)</li><li>二、环境准备<ul><li>1、所有机器关闭防火墙</li><li>2、所有机器关闭selinux</li><li>3、所有机器关闭swap</li><li>4、所有机器上添加主机名与ip的对应关系</li><li>5、在所有主机上将桥接的ipv4流量传递到iptables的链</li></ul></li><li>三、为所有节点安装docker</li><li>四、集群部署<ul><li>1、为所有节点修改仓库,安装kubeadm、kubelet、kubectl</li><li>2、修改docker的配置(所有节点)</li><li>3、部署master节点(主节点k8s-master)<ul><li>(1)、遇到报错:</li><li>(2)、解决办法:</li></ul></li><li>4、按照指示执行:</li><li>5、查看kubelet.service状态</li><li>6、查看节点状态为notready</li><li>7、安装网络插件,官方文档:https://github.com/flannel-io/flannel</li><li>8、添加node节点</li></ul></li></ul></div><p></p>
<h2 id="一硬件准备虚拟主机">一、硬件准备(虚拟主机)</h2>
<table>
<thead>
<tr>
<th style="text-align: center">角色</th>
<th style="text-align: center">主机名</th>
<th style="text-align: center">ip地址</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: center">master</td>
<td style="text-align: center">k8s-master</td>
<td style="text-align: center">192.168.112.10</td>
</tr>
<tr>
<td style="text-align: center">node</td>
<td style="text-align: center">k8s-node1</td>
<td style="text-align: center">192.168.112.20</td>
</tr>
<tr>
<td style="text-align: center">node</td>
<td style="text-align: center">k8s-node2</td>
<td style="text-align: center">192.168.112.30</td>
</tr>
</tbody>
</table>
<blockquote>
<p>CentOS Linux release 7.9.2009 (Core)</p>
<p>至少2核CPU、3GB以上内存</p>
<p>使用命令hostnamectl set-hostname临时修改主机名</p>
</blockquote>
<h2 id="二环境准备">二、环境准备</h2>
<h3 id="1所有机器关闭防火墙">1、所有机器关闭防火墙</h3>
<ul>
<li>
<pre><code class="language-bash">systemctl stop firewalld        #关闭
systemctl disable firewalld                #开机不自启
systemctl status firewalld                #查看状态
</code></pre>
<p><img src="https://img2023.cnblogs.com/blog/3332572/202403/3332572-20240314183155692-1685112634.png" alt="image-20240314104221008" loading="lazy"></p>
</li>
</ul>
<h3 id="2所有机器关闭selinux">2、所有机器关闭selinux</h3>
<ul>
<li>
<pre><code class="language-bash">sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0
</code></pre>
<p><img src="https://img2023.cnblogs.com/blog/3332572/202403/3332572-20240314183155258-1401573694.png" alt="image-20240314104836330" loading="lazy"></p>
</li>
</ul>
<h3 id="3所有机器关闭swap">3、所有机器关闭swap</h3>
<ul>
<li>
<pre><code class="language-bash">swapoff -a # 临时关闭
sed -ri 's/.*swap.*/#&amp;/' /etc/fstab#永久关闭
</code></pre>
<p><img src="https://img2023.cnblogs.com/blog/3332572/202403/3332572-20240314183154857-1074478114.png" alt="image-20240314105035956" loading="lazy"></p>
</li>
</ul>
<h3 id="4所有机器上添加主机名与ip的对应关系">4、所有机器上添加主机名与ip的对应关系</h3>
<ul>
<li>
<pre><code class="language-bash">vim /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.112.10 k8s-master
192.168.112.20 k8s-node1
192.168.112.30 k8s-node2
</code></pre>
<p><img src="https://img2023.cnblogs.com/blog/3332572/202403/3332572-20240314183154501-1297636540.png" alt="image-20240314105326686" loading="lazy"></p>
</li>
</ul>
<h3 id="5在所有主机上将桥接的ipv4流量传递到iptables的链">5、在所有主机上将桥接的ipv4流量传递到iptables的链</h3>
<ul>
<li>
<pre><code class="language-bash">cat &gt; /etc/sysctl.d/k8s.conf &lt;&lt; EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
</code></pre>
<p><img src="https://img2023.cnblogs.com/blog/3332572/202403/3332572-20240314183154133-608027249.png" alt="image-20240314105648252" loading="lazy"></p>
</li>
</ul>
<h2 id="三为所有节点安装docker">三、为所有节点安装docker</h2>
<pre><code class="language-bash">yum install wget.x86_64 -y
rm -rf /etc/yum.repos.d/*
wget -O /etc/yum.repos.d/centos7.repo http://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo
wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install docker-ce-20.10.11 -y
systemctl start docker
systemctl enable docker
</code></pre>
<p><img src="https://img2023.cnblogs.com/blog/3332572/202403/3332572-20240314183153731-1866037176.png" alt="image-20240314110214781" loading="lazy"></p>
<h2 id="四集群部署">四、集群部署</h2>
<h3 id="1为所有节点修改仓库安装kubeadmkubeletkubectl">1、为所有节点修改仓库,安装kubeadm、kubelet、kubectl</h3>
<pre><code class="language-bash">cat &lt;&lt;EOF &gt; /etc/yum.repos.d/kubernetes.repo

name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
yum install kubelet-1.22.2 kubeadm-1.22.2 kubectl-1.22.2 -y
systemctl enable kubelet &amp;&amp; systemctl start kubelet
</code></pre>
<h3 id="2修改docker的配置所有节点">2、修改docker的配置(所有节点)</h3>
<pre><code class="language-bash"># cat &gt; /etc/docker/daemon.json &lt;&lt;EOF
&gt; {
&gt;   "exec-opts": ["native.cgroupdriver=systemd"]
&gt; }
&gt; EOF
# systemctl daemon-reload
# systemctl restart docker.service
# systemctl restart kubelet.service
# systemctl status kubelet.service
</code></pre>
<blockquote>
<p>这里从节点的kubelet.service状态报code=exited, status=1/FAILURE是正常的</p>
</blockquote>
<h3 id="3部署master节点主节点k8s-master">3、部署master节点(主节点k8s-master)</h3>
<pre><code class="language-bash">kubeadm init \
--apiserver-advertise-address=192.168.112.10 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.22.2 \
--control-plane-endpoint k8s-master \
--service-cidr=172.16.0.0/16 \
--pod-network-cidr=10.244.0.0/16
</code></pre>
<blockquote>
<p>记得保存好这段命令是用于将一个工作节点(worker node)加入到已存在的 Kubernetes 集群中的过程。</p>
</blockquote>
<p><img src="https://img2023.cnblogs.com/blog/3332572/202403/3332572-20240314183153314-1021253735.png" alt="image-20240314174150506" loading="lazy"></p>
<h4 id="1遇到报错">(1)、遇到报错:</h4>
<pre><code class="language-bash">Here is one example how you may list all Kubernetes containers running in docker:
                - 'docker ps -a | grep kube | grep -v pause'
                Once you have found the failing container, you can inspect its logs with:
                - 'docker logs CONTAINERID'

error execution phase wait-control-plane: couldn't initialize a Kubernetes cluster
To see the stack trace of this error execute with --v=5 or higher
</code></pre>
<h4 id="2解决办法">(2)、解决办法:</h4>
<pre><code class="language-bash">rm -rf /etc/containerd/config.toml
systemctl restart containerd
</code></pre>
<h3 id="4按照指示执行">4、按照指示执行:</h3>
<pre><code class="language-bash"># mkdir -p $HOME/.kube
# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
# sudo chown $(id -u):$(id -g) $HOME/.kube/config
# export KUBECONFIG=/etc/kubernetes/admin.conf
</code></pre>
<h3 id="5查看kubeletservice状态">5、查看kubelet.service状态</h3>
<pre><code class="language-bash">systemctl status kubelet.service
</code></pre>
<p><img src="https://img2023.cnblogs.com/blog/3332572/202403/3332572-20240314183152960-1615503121.png" alt="image-20240314174723649" loading="lazy"></p>
<h3 id="6查看节点状态为notready">6、查看节点状态为notready</h3>
<pre><code class="language-bash"># kubectl get nodes
NAME         STATUS   ROLES    AGE   VERSION
k8s-master   NotReady   &lt;none&gt;   67s   v1.22.2
</code></pre>
<p><img src="https://img2023.cnblogs.com/blog/3332572/202403/3332572-20240314183152582-1298511207.png" alt="image-20240314123142381" loading="lazy"></p>
<h3 id="7安装网络插件官方文档httpsgithubcomflannel-ioflannel">7、安装网络插件,官方文档:https://github.com/flannel-io/flannel</h3>
<pre><code class="language-bash"># 最好手动提前拉取所需镜像
# docker pull quay.io/coreos/flannel:v0.14.0
# wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
# kubectl apply -f kube-flannel.yml
</code></pre>
<h3 id="8添加node节点">8、添加node节点</h3>
<pre><code class="language-bash"># 为node拉取网络插件镜像
# docker pull quay.io/coreos/flannel:v0.14.0
# docker pull quay.io/coreos/flannel:v0.14.0
# kubeadm join k8s-master:6443 --token byfq2h.myv4dj0yqmmjz6qx \
&gt;         --discovery-token-ca-cert-hash sha256:f6b364e22cd4e61897a9a58583ae072c5a3724ac14f44319b5f72021614eaadf
# kubeadm join k8s-master:6443 --token byfq2h.myv4dj0yqmmjz6qx \
&gt;         --discovery-token-ca-cert-hash sha256:f6b364e22cd4e61897a9a58583ae072c5a3724ac14f44319b5f72021614eaadf
</code></pre>
<p><img src="https://img2023.cnblogs.com/blog/3332572/202403/3332572-20240314183151757-2073402572.png" alt="image-20240314182912727" loading="lazy"></p>
<blockquote>
<p>至此一个简单的k8s集群安装完成</p>
</blockquote><br><br>
来源:https://www.cnblogs.com/misakivv/p/18073708
頁: [1]
查看完整版本: CentOS 7.9 环境下搭建k8s集群(一主两从)