Linux | centos 7 |
---|---|
Docker | 20.10 |
K8s | 1.23 |
1. 准备工作
1.1 安装k8s时,临时关闭swap ,如果不关闭在执行kubeadm部分命令会报错
swapoff -a
或直接注释swap(需要重启生效)
[root@wzlab /]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Tue Apr 19 11:43:17 2022
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos_hhdcloudrd6-root / xfs defaults 0 0
UUID=13a8fe45-33c8-4258-a434-133ce183d3c3 /boot xfs defaults 0 0
#/dev/mapper/centos_hhdcloudrd6-swap swap swap defaults 0 0
1.2 安装k8s时,可以临时关闭selinux,减少额外配置
setenforce 0
或修改 /etc/sysconfig/selinux 文件 后重启
[root@localhost /]# cat /etc/sysconfig/selinux
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
1.3 关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
1.4 启用 bridge-nf-call-iptables 预防网络问题
echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables
1.5 设置网桥参数
cat << EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
1.6 修改hosts文件 方便查看域名映射
[root@wzlab /]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.34.7 k8s-master
192.168.5.129 k8s-node1
192.168.34.8 k8s-node2
1.7 查看系统版本信息 修改hostname
[root@localhost /]# hostnamectl
Static hostname: localhost.localdomain
Icon name: computer-vm
Chassis: vm
Machine ID: 5c2c4826a7cd442a85c37d3b4dba39e0
Boot ID: 3f70bab69c37412da8eada29d50cc12c
Virtualization: vmware
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-1160.el7.x86_64
Architecture: x86-64
hostnamectl set-hostname k8s-node1
su root
1.8 查看cpu信息 k8s安装至少需要2核2G的环境,否则会安装失败
lscpu
2. 安装Docker
- 列出并排序您存储库中可用的版本。此示例按版本号(从高到低)对结果进行排序。
yum list docker-ce --showduplicates | sort -r
docker-ce.x86_64 3:18.09.1-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.0-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.1.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.0.ce-3.el7 docker-ce-stable
- 通过其完整的软件包名称安装特定版本,该软件包名称是软件包名称(docker-ce)加上版本字符串(第二列),从第一个冒号(:)一直到第一个连字符,并用连字符(-)分隔。例如:docker-ce-18.09.1
yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io
- 启动 Docker。
systemctl start docker
systemctl enable docker
- 通过运行 hello-world 镜像来验证是否正确安装了 Docker Engine-Community 。
docker run hello-world
3. 安装kubeadm kubelet kubectl
3.1 配置k8s下载资源配置文件
cat >> /etc/yum.repos.d/kubernetes.repo < EOF
[kubernetes]
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
3.2 安装 kubelet kubeadm kubectl
yum install -y --nogpgcheck kubelet-1.23.5 kubeadm-1.23.5 kubectl-1.23.5
- kubelet :运行在cluster,负责启动pod管理容器
- kubeadm :k8s快速构建工具,用于初始化cluster
- kubectl :k8s命令工具,部署和管理应用,维护组件
3.2.1 查看是否安装成功
kubelet --version
kubectl version
kubeadm version
3.3 启动kubelet
systemctl daemon-reload
systemctl start kubelet
systemctl enable kubelet
3.4 初始化master节点 其他node节点通过 kubeadm join 进来
kubeadm init --apiserver-advertise-address=192.168.137.142 --apiserver-bind-port=6443 --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12 --kubernetes-version=1.23.5 --image-repository registry.aliyuncs.com/google_containers
如果下载失败执行 kubeadm reset ,重新执行kubeadm init
之前有使用kubeadm安装过,需要提前把之前的kube文件删除掉
rm -rf $HOME/.kube
在master节点运行以下三行命令 执行完成后可以通过 kubeadm token list获取token
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
3.5 部署网络插件 kube-flannel.yml 并 应用获取运行中容器
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
kubectl apply -f kube-flannel.yml
3.6 查看kubelet日志
journalctl -xefu kubelet
4. 卸载k8s
yum -y remove kubelet kubeadm kubectl
sudo kubeadm reset -f
sudo rm -rvf $HOME/.kube
sudo rm -rvf ~/.kube/
sudo rm -rvf /etc/kubernetes/
sudo rm -rvf /etc/systemd/system/kubelet.service.d
sudo rm -rvf /etc/systemd/system/kubelet.service
sudo rm -rvf /usr/bin/kube*
sudo rm -rvf /etc/cni
sudo rm -rvf /opt/cni
sudo rm -rvf /var/lib/etcd
sudo rm -rvf /var/etcd
kubeadm init --apiserver-advertise-address=192.168.137.142 --apiserver-bind-port=6443 --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12 --kubernetes-version=1.23.5 --image-repository registry.aliyuncs.com/google_containers
网友评论