前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >k8s安装部署

k8s安装部署

原创
作者头像
炒香菇的书呆子
发布2023-11-02 23:59:22
4920
发布2023-11-02 23:59:22
举报

Kubernetes作为容器的编排平台,它是以集群的形式为业务提供服务。所以在日常的工作中,作为Kubernetes平台的维护者,会经常对集群进行管理。

这里,我将集群管理分为以下几种:
图片
图片

安装集群

前置说明

Kubernetes的集群安装分为:kubeadm安装和二进制安装。在这里,只会介绍kubeadm的安装。

安装说明:

集群节点:2个 IP信息: master:192.168.205.128 node:192.168.205.128 Kubernetes版本:v1.24.2 运行时:containerd 系统:centos 7.9 系统内核:3.10.0-1160

环境准备

这是安装的不是生产级别的集群,只是为了演示使用。

(1)在每个节点添加host信息

代码语言:txt
复制
<span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> cat >> /etc/hosts << EOF</span><br>192.168.205.128 kk-master<br>192.168.205.130 kk-node01<br>EOF<br>

(2)关闭防火墙和SELinux

代码语言:txt
复制
<span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> systemctl stop firewalld</span><br><span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> systemctl <span style="color: #a6e22e;line-height: 26px;">disable</span> firewalld</span><br><span style="color: #75715e;line-height: 26px;"><br>$</span><span style="line-height: 26px;"> setenforce 0</span><br><span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> cat /etc/selinux/config</span><br>SELINUX=disabled<br>

(3)优化内核参数

代码语言:txt
复制
<span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> cat > /etc/sysctl.d/k8s.conf << EOF</span><br>net.bridge.bridge-nf-call-ip6tables = 1<br>net.bridge.bridge-nf-call-iptables = 1<br>net.ipv4.ip_forward = 1<br>vm.swappiness=0<br>EOF<br>

执行以下命令使其生效:

代码语言:txt
复制
<span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> modprobe br_netfilter</span><br><span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> sysctl -p /etc/sysctl.d/k8s.conf</span><br>

(4)关闭swap空间

代码语言:txt
复制
<span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> swapoff -a</span><br>

注释/etc/fstab文件中swap挂载。

代码语言:txt
复制
<span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> cat /etc/fstab </span><br><span style="color: #75715e;line-height: 26px;"><br>#</span><br><span style="color: #75715e;line-height: 26px;">#</span><span style="line-height: 26px;"> /etc/fstab</span><br><span style="color: #75715e;line-height: 26px;">#</span><span style="line-height: 26px;"> Created by anaconda on Tue Apr 12 17:10:16 2022</span><br><span style="color: #75715e;line-height: 26px;">#</span><br><span style="color: #75715e;line-height: 26px;">#</span><span style="line-height: 26px;"> Accessible filesystems, by reference, are maintained under <span style="color: #a6e22e;line-height: 26px;">'/dev/disk'</span></span><br><span style="color: #75715e;line-height: 26px;">#</span><span style="line-height: 26px;"> See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) <span style="color: #f92672;font-weight: bold;line-height: 26px;">for</span> more info</span><br><span style="color: #75715e;line-height: 26px;">#</span><br>/dev/mapper/centos-root /                       xfs     defaults        0 0<br>UUID=bc73c871-006c-4e24-a7af-6beb9aac06a7 /boot                   xfs     defaults        0 0<br><span style="color: #75715e;line-height: 26px;">#</span><span style="line-height: 26px;"> /dev/mapper/centos-swap swap                    swap    defaults        0 0</span><br>

(5)安装ipvs软件包

代码语言:txt
复制
<span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> cat > /etc/sysconfig/modules/ipvs.modules <<EOF</span><br><span style="color: #75715e;line-height: 26px;">#</span><span style="line-height: 26px;">!/bin/bash</span><br>modprobe -- ip_vs<br>modprobe -- ip_vs_rr<br>modprobe -- ip_vs_wrr<br>modprobe -- ip_vs_sh<br>modprobe -- nf_conntrack_ipv4<br>EOF<br><span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep -e ip_vs -e nf_conntrack_ipv4</span><br><span style="color: #75715e;line-height: 26px;"><br>$</span><span style="line-height: 26px;"> yum install ipset ipvsadm -y</span><br>

(6)同步服务器时间

代码语言:txt
复制
<span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> yum install chrony -y</span><br><span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> systemctl <span style="color: #a6e22e;line-height: 26px;">enable</span> chronyd</span><br><span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> systemctl start chronyd</span><br><span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> chronyc sources</span><br>

(7)安装containerd

代码语言:txt
复制
<span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> yum install -y yum-utils \</span><br> device-mapper-persistent-data \<br> lvm2<br><span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> yum-config-manager \</span><br> --add-repo \<br> https://download.docker.com/linux/centos/docker-ce.repo<br><span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> yum list | grep containerd</span><br><span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> yum install containerd -y</span><br>

创建containerd配置文件。

代码语言:txt
复制
<span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> mkdir -p /etc/containerd</span><br><span style="color: #75715e;line-height: 26px;">#</span><span style="line-height: 26px;"> containerd config default > /etc/containerd/config.toml</span><br><span style="color: #75715e;line-height: 26px;">#</span><span style="line-height: 26px;"> 替换配置文件</span><br><span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> sed -i <span style="color: #a6e22e;line-height: 26px;">"s#k8s.gcr.io#registry.cn-hangzhou.aliyuncs.com/google_containers#g"</span>  /etc/containerd/config.toml</span><br><span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> sed -i <span style="color: #a6e22e;line-height: 26px;">'s#SystemdCgroup = false#SystemdCgroup = true#g'</span> /etc/containerd/config.toml</span><br><span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> sed -i <span style="color: #a6e22e;line-height: 26px;">"s#https://registry-1.docker.io#https://registry.cn-hangzhou.aliyuncs.com#g"</span>  /etc/containerd/config.toml</span><br>

启动containerd。

代码语言:txt
复制
<span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> systemctl daemon-reload</span><br><span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> systemctl <span style="color: #a6e22e;line-height: 26px;">enable</span> containerd</span><br><span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> systemctl restart containerd</span><br>

(8)安装Kubernetes组件

代码语言:txt
复制
<span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> cat <<EOF > /etc/yum.repos.d/kubernetes.repo</span><br>[kubernetes]<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> http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg<br>EOF<br>

安装指定版本的组件。

代码语言:txt
复制
<span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> yum install -y kubelet-1.24.2 kubeadm-1.24.2 kubectl-1.24.2</span><br>

设置运行时。

代码语言:txt
复制
<span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> crictl config runtime-endpoint /run/containerd/containerd.sock</span><br>

设置kubelet为自启动。

代码语言:txt
复制
<span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> systemctl daemon-reload</span><br><span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> systemctl <span style="color: #a6e22e;line-height: 26px;">enable</span> kubelet && systemctl start kubelet</span><br>

初始化集群

上面把基础环境准备好了,现在开始真正的进行集群初始化。

初始化master节点

然后接下来在 master 节点配置 kubeadm 初始化文件,可以通过如下命令导出默认的初始化配置:

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 安装集群
    • 前置说明
      • 环境准备
        • 初始化集群
          • 初始化master节点
      相关产品与服务
      容器服务
      腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
      http://www.vxiaotou.com