首页 > 系统相关 >Ubuntu 20.04版本安装k8s控制节点

Ubuntu 20.04版本安装k8s控制节点

时间:2024-01-13 12:00:37浏览次数:42  
标签:kubectl master4 etc 1.28 Ubuntu v1.28 kubeadm k8s 20.04

一、环境配置

服务器配置:2核4G
IP:192.168.10.23
主机名:master4
将改主机加入此 集群
# 1.修改主机名
hostnamectl set-hostname master4 && bash

# 2.添加hosts
127.0.1.1 master4
192.168.10.20 master
192.168.10.21 master2
192.168.10.22 master3
192.168.10.23 master4
192.168.10.24 node1
192.168.10.25 node2

# 3.ssh信任
ssh-keygen -t rsa
ssh-copy-id master

# 4.关闭交换分区
swapoff -a  # 临时关闭
永久关闭为注销/etc/fstab中swap一行

# 5.修改机器内核参数
modprobe br_netfilter
echo "modprobe br_netfilter" >> /etc/profile

cat > /etc/sysctl.d/k8s.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF

sysctl -p /etc/sysctl.d/k8s.conf

# 6. 关闭防火墙
systemctl stop firewalld ; systemctl disable firewalld

# 7.关闭selinux,修改 x selinux  配置文件之后,重启:ubuntu如果没有安装不用管
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

# 8.配置阿里云源
# step 1: 安装必要的一些系统工具
sudo apt-get update
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
# step 2: 安装GPG证书
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
# Step 3: 写入软件源信息
sudo add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
# Step 4: 更新并安装Docker-CE
sudo apt-get -y update

# 9.配置kubernets源
# (1).更新 apt 包索引并安装使用 Kubernetes apt 仓库所需要的包
sudo apt-get update
# apt-transport-https 可能是一个虚拟包(dummy package);如果是的话,你可以跳过安装这个包
sudo apt-get install -y apt-transport-https ca-certificates curl gpg
# (2).下载用于 Kubernetes 软件包仓库的公共签名密钥。所有仓库都使用相同的签名密钥,因此你可以忽略URL中的版本:
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.28/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
# (3).添加 Kubernetes apt 仓库。 请注意,此仓库仅包含适用于 Kubernetes 1.28 的软件包; 对于其他 Kubernetes 次要版本,则需要更改 URL 中的 Kubernetes 次要版本以匹配你所需的次要版本,如果之前有会覆盖
echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.28/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list

# 10.时间同步并定时同步
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ntpdate time1.aliyun.com

二、基础软件包安装

sudo apt -y install ipvsadm ipset sysstat conntrack g++ openssl libssl-dev zlib zlib1g-dev libpcre3 libpcre3-dev build-essential

2.1 安装containerd

# 1.安装containerd服务
yum -y install containerd

# 2.生成containerd配置文件
mkdir -p /etc/containerd
containerd config default > /etc/containerd/config.toml

# 3.修改配置文件
vim /etc/containerd/config.toml
SystemdCgroup = true   # false改为true
sandbox_image = "registry.aliyuncs.com/google_containers/pause:3.9"   # 如果版本不清楚后面kubeadm config images list --config=kubeadm.yml时可以看了再修改

# 4.配置为开机启动
systemctl enable containerd --now

# 5.修改/etc/crictl.yaml 文件
cat > /etc/crictl.yaml <<EOF
runtime-endpoint: unix:///run/containerd/containerd.sock
image-endpoint: unix:///run/containerd/containerd.sock
timeout: 10
debug: false
EOF

systemctl restart containerd

# 6.配置镜像加速器
# 编辑 vim /etc/containerd/config.toml 文件,修改
config_path = "/etc/containerd/certs.d"

mkdir /etc/containerd/certs.d/docker.io/ -p
vim /etc/containerd/certs.d/docker.io/hosts.toml 
[host."https://pft7f97f.mirror.aliyuncs.com",host."https://registry.docker-cn.com",host."https://docker.mirrors.ustc.edu.cn"]
  capabilities = ["pull"]

systemctl restart containerd

2.2 安装k8s所需软件

# 安装 kubelet、kubeadm 和 kubectl,并锁定其版本:
sudo apt-get update -y
# 1.查看kubeadm、kubectl、kubelet有哪些版本
root@master4:/etc/containerd# apt-cache madison kubeadm kubelet kubectl
   kubeadm | 1.28.5-1.1 | https://pkgs.k8s.io/core:/stable:/v1.28/deb  Packages
   kubeadm | 1.28.4-1.1 | https://pkgs.k8s.io/core:/stable:/v1.28/deb  Packages
   kubeadm | 1.28.3-1.1 | https://pkgs.k8s.io/core:/stable:/v1.28/deb  Packages
   kubeadm | 1.28.2-1.1 | https://pkgs.k8s.io/core:/stable:/v1.28/deb  Packages
   kubeadm | 1.28.1-1.1 | https://pkgs.k8s.io/core:/stable:/v1.28/deb  Packages
   kubeadm | 1.28.0-1.1 | https://pkgs.k8s.io/core:/stable:/v1.28/deb  Packages
   kubelet | 1.28.5-1.1 | https://pkgs.k8s.io/core:/stable:/v1.28/deb  Packages
   kubelet | 1.28.4-1.1 | https://pkgs.k8s.io/core:/stable:/v1.28/deb  Packages
   kubelet | 1.28.3-1.1 | https://pkgs.k8s.io/core:/stable:/v1.28/deb  Packages
   kubelet | 1.28.2-1.1 | https://pkgs.k8s.io/core:/stable:/v1.28/deb  Packages
   kubelet | 1.28.1-1.1 | https://pkgs.k8s.io/core:/stable:/v1.28/deb  Packages
   kubelet | 1.28.0-1.1 | https://pkgs.k8s.io/core:/stable:/v1.28/deb  Packages
   kubectl | 1.28.5-1.1 | https://pkgs.k8s.io/core:/stable:/v1.28/deb  Packages
   kubectl | 1.28.4-1.1 | https://pkgs.k8s.io/core:/stable:/v1.28/deb  Packages
   kubectl | 1.28.3-1.1 | https://pkgs.k8s.io/core:/stable:/v1.28/deb  Packages
   kubectl | 1.28.2-1.1 | https://pkgs.k8s.io/core:/stable:/v1.28/deb  Packages
   kubectl | 1.28.1-1.1 | https://pkgs.k8s.io/core:/stable:/v1.28/deb  Packages
   kubectl | 1.28.0-1.1 | https://pkgs.k8s.io/core:/stable:/v1.28/deb  Packages

# 2.安装指定版本
root@master4:/etc/containerd# apt-get -y install kubelet=1.28.2-1.1 kubeadm=1.28.2-1.1 kubectl=1.28.2-1.1

# 阻止软件自动更新
apt-mark hold kubelet kubeadm kubectl

systemctl enable kubelet

2.3 安装配置nginx+keepalived

# 安装nginx、keepalived
apt-get install nginx keepalived

# 配置文件
[root@master service]# scp /etc/keepalived/keepalived.conf master4:/etc/keepalived/
keepalived.conf                                                                                                                  100%  806    85.4KB/s   00:00    
[root@master service]# scp /etc/keepalived/check_nginx.sh master4:/etc/keepalived/
check_nginx.sh 
[root@master service]# scp /etc/nginx/nginx.conf master4:/etc/nginx/
nginx.conf

# 在master4的nginx.conf中添加一条启用stream模块
include /etc/nginx/modules-enabled/*.conf;

systemctl enable nginx && systemctl start nginx
systemctl enable keepalived && systemctl start keepalived

2.4 加入到集群中作为控制节点

# 2.将master节点证书拷贝到master4节点,master4节点建立目录
mkdir -p /etc/kubernetes/pki/etcd/

scp /etc/kubernetes/pki/ca.* master4:/etc/kubernetes/pki/
scp /etc/kubernetes/pki/sa.* master4:/etc/kubernetes/pki/
scp /etc/kubernetes/pki/front-proxy-ca.* master4:/etc/kubernetes/pki/
scp /etc/kubernetes/pki/etcd/ca.* master4:/etc/kubernetes/pki/etcd/

# 将kubeadm.yaml文件传输到master4上下载kubernetes所需镜像
root@master4:~# kubeadm config images list --config=kubeadm.yaml
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.28.2
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.28.2
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.28.2
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.28.2
registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.9
registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.5.9-0
registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:v1.10.1

root@master4:~# kubeadm config images pull--config=kubeadm.yaml
# 在master节点生成token
[root@master k8s]# kubeadm token create --print-join-command
kubeadm join 192.168.10.19:16443 --token 03b1fu.tyfrsq69b28jcduj --discovery-token-ca-cert-hash sha256:3d2052ebcdc58cce07aeb55f9e5987d8d406e3b0d0370299283cdb4fdc216eeb

# 将master4加入集群中
kubeadm join 192.168.10.19:16443 --token 03b1fu.tyfrsq69b28jcduj --discovery-token-ca-cert-hash sha256:3d2052ebcdc58cce07aeb55f9e5987d8d406e3b0d0370299283cdb4fdc216eeb --control-plane

# master4上执行
    mkdir -p $HOME/.kube
    sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
    sudo chown $(id -u):$(id -g) $HOME/.kube/config

# 查看nodes
root@master4:/etc/nginx# kubectl get nodes
NAME      STATUS   ROLES           AGE     VERSION
master    Ready    control-plane   2d11h   v1.28.5
master2   Ready    control-plane   2d11h   v1.28.5
master4   Ready    control-plane   4m3s    v1.28.2
node1     Ready    worker          2d10h   v1.28.5
node2     Ready    worker          30h     v1.28.5

 三、版本升级:从1.28.2升级到1.28.5

# 1.将master4设置为维护模式
kubectl cordon master4

# 2.驱逐节点上数据
kubectl  drain master4 --ignore-daemonsets --force --delete-empty-data

# 3.查看可升级的版本
apt update
apt-cache madison kubeadm

# 4.升级kubeadm,用最新的补丁版本号替换 1.28.x-* 中的 x
apt-mark unhold kubeadm && \
apt-get update && apt-get install -y kubeadm='1.28.x-*' && \
apt-mark hold kubeadm

# 查看版本号
kubectl version

# 5.验证版本升级计划
root@master4:/etc/apt/keyrings# kubeadm upgrade plan
[upgrade/config] Making sure the configuration is correct:
[upgrade/config] Reading configuration from the cluster...
[upgrade/config] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -o yaml'
[preflight] Running pre-flight checks.
[upgrade] Running cluster health checks
[upgrade] Fetching available versions to upgrade to
[upgrade/versions] Cluster version: v1.28.5
[upgrade/versions] kubeadm version: v1.28.5
I0113 11:32:45.140007  382936 version.go:256] remote version is much newer: v1.29.0; falling back to: stable-1.28
[upgrade/versions] Target version: v1.28.5
[upgrade/versions] Latest version in the v1.28 series: v1.28.5


# 6.选择要升级到的目标版本,运行合适的命令
root@master4:/etc/apt/keyrings# kubeadm upgrade apply v1.28.5
成功命令:
[upgrade/successful] SUCCESS! Your cluster was upgraded to "v1.28.5". Enjoy!

# 7.升级 kubelet 和 kubectl
apt-mark unhold kubeadm kubectl
apt-get update && apt-get install -y kubelet='1.28.5-*' kubectl='1.28.5-*'

验证版本:
kubectl version
kubelet --version

apt-mark hold kubelet kubectl

# 7.重启服务
sudo systemctl daemon-reload
sudo systemctl restart kubelet

# 8.解除保护,将节点加入集群
root@master4:/etc/apt/keyrings# kubectl uncordon master4
node/master4 uncordoned
root@master4:/etc/apt/keyrings# kubectl get nodes
NAME      STATUS   ROLES           AGE     VERSION
master    Ready    control-plane   2d22h   v1.28.5
master2   Ready    control-plane   2d22h   v1.28.5
master4   Ready    control-plane   10h     v1.28.5
node1     Ready    worker          2d20h   v1.28.5
node2     Ready    worker          41h     v1.28.5

 

标签:kubectl,master4,etc,1.28,Ubuntu,v1.28,kubeadm,k8s,20.04
From: https://www.cnblogs.com/yangmeichong/p/17961376

相关文章

  • 01 Ubuntu 系统的安装
    软件版本:vitis2021.1(vivado2021.1)操作系统:Ubuntu18.04硬件平台:适用XILINXA7/K7/Z7/ZU/KU系列FPGA登录"米联客"FPGA社区-www.uisrc.com视频课程、答疑解惑!1.1概述    基于XILINXXDMA的开发应用环境需要用到以下软件或者开发包:ubuntu、qt。环境的搭建对于后续的开发至......
  • 如何通过kube-apiserver访问K8s集群中的App
    本文分享自华为云社区《通过kube-apiserver访问K8s集群中的App》,作者:tsjsdbd。K8s集群中的App(或者svc),通常使用ClusterIP,NodePort,Loadbalancer这些方式访问,但是你也可以通过Kube-apiserver(管理面)来访问App。在《跟唐老师学习云网络 -Kubernetes网络实现》里面,提到K8s集群里面的容......
  • Ubuntu下压缩、解压常用命令
    .tar文件#仅打包,并非压缩tar-xvfFileName.tar#解包tar-cvfFileName.tarDirName#将DirName和其下所有文件(夹)打包.gz文件#.gzgunzipFileName.gz#解压1gzip-dFileName.gz#解压2gzipFileName#压缩,只能压缩文件.tar.gz文件、.tgz文......
  • Ubuntu系统安装mysql
    1.查看有没有安装mysql dpkg-l|grepmysql2.更新ubantu包索引sudoaptupdate 3.安装mysqlsudoapt-getinstallmysql-server 4.初始化环境sudomysql_secure_installation  5.查看mysql服务状态systemctlstatusm......
  • ubuntu 安装 PHP7.4
    1、添加PHP7.4的PPA存储库。执行以下命令以添加该存储库:sudoaptupdatesudoaptinstallsoftware-properties-commonsudoadd-apt-repositoryppa:ondrej/php2、安装aptinstallphp7.4php7.4-cliphp7.4-fpmphp7.4-mysqlphp7.4-curlphp7.4-gdphp7.4-zipphp7......
  • 服务器tomcat安装及配置教程(Ubuntu)
    一、从官网获取Tomcat。Tomcat官网地址是:https://tomcat.apache.org/,我用的Tomcat9(Tomcat10以上变化较大,根据自己的jdk环境选择,我的jdk依然用的jdk8)点开进入下载页面,选择「BinaryDistributions」中的「Core」,直接用第一个zip的即可。鼠标移到链接上,右键「复制链接地址」。然......
  • 5.k8s Service四层负载:服务端口暴露
    题目一:暴露服务service设置配置环境:[candidate@node-1]$kubectlconfiguse-contextk8sTask请重新配置现有的deploymentfront-end以及添加名为http的端口规范来公开现有容器nginx的端口80/tcp。创建一个名为front-end-svc的新service,以公开容器端口http。......
  • 4.k8s-配置网络策略 NetworkPolicy
    一、基本了解官方文档:https://kubernetes.io/zh-cn/docs/concepts/services-networking/network-policies/基本了解:1.网络策略通过网络插件来实现,创建一个NetworkPolicy资源对象而没有控制器来使它生效的话,是没有任何作用的,而我们搭建K8s集群时安装的calico网络组件就支持网......
  • 基于k8s Deployment的弹性扩缩容及滚动发布机制详解
    k8s第一个重要设计思想:控制器模式。k8s里第一个控制器模式的完整实现:Deployment。它实现了k8s一大重要功能:Pod的“水平扩展/收缩”(horizontalscalingout/in)。该功能从PaaS时代开始就是一个平台级项目必备编排能力。若你更新了Deployment的Pod模板(如修改容器的镜像),则Deployment......
  • 【K8S系列】快速初始化⼀个最⼩集群
      序言走得最慢的人,只要不丧失目标,也比漫无目的地徘徊的人走得快。文章标记颜色说明:黄色:重要标题红色:用来标记结论绿色:用来标记一级重要蓝色:用来标记二级重要希望这篇文章能让你不仅有一定的收获,而且可以愉快的学习,如果有什么建议,都可以留言和我交流写在前......