K8s环境安装

最近开始学习K8s,网上虽然有许多教程,但是在环境搭建过程之中还是会遇到许多问题,特此记录一下。

安装方式

本文安装方式采用yum。

  1. 使用 Kubeadmin 通过离线镜像安装
  2. 使用阿里云公有云平台安装K8S
  3. 通过yum官方仓库安装
  4. 通过二进制包的形式进行安装

官方文档

https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/install-kubeadm/

准备3台CentOs7

192.168.11.12
vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.11.12 k8s-master
192.168.11.13 k8s-node1
192.168.11.14 k8s-node2
vim /etc/hostname
k8s-master
reboot

192.168.11.13
vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.11.13 k8s-node1
192.168.11.12 k8s-master
192.168.11.14 k8s-node2
vim /etc/hostname
k8s-node1
reboot

192.168.11.14
vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.11.14 k8s-node2
192.168.11.12 k8s-master
192.168.11.13 k8s-node1
vim /etc/hostname
k8s-node2
reboot

# 确保3台机器的MAC地址不一样
sudo cat /sys/class/dmi/id/product_uuid

# 方便起见,关闭防火墙

# 确保3台机器已经安装过docker,本文docker版本 20.10.12

安装Docker

curl -fsSL get.docker.com -o get-docker.sh
sh get-docker.sh --mirror Aliyun
systemctl daemon-reload
systemctl restart docker
docker version

cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
exclude=kubelet kubeadm kubectl
EOF

安装

sudo setenforce 0
sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config

sudo yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes

systemctl enable kubelet && systemctl start kubelet
# 常用命令:systemctl restart kubelet | systemctl status kubelet

配置

cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
br_netfilter
EOF

# 网桥
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF

sudo sysctl --system

# 设置时区
timedatectl set-timezone Asia/Shanghai

# 关闭交换区
## 暂时关闭
swapoff -a
## 永久关闭
vi /etc/fstab
## 注释这行 
#/dev/mapper/centos-swap swap                    swap    defaults        0 0

# 设置cgroup
# vim /usr/lib/systemd/system/kubelet.service.d/10-kubeadm.conf
# --cgroup-driver=cgroupfs
# systemctl daemon-reload && systemctl restart kubelet

#= ============================================================================= =#
   以上步骤三个节点都需要执行,以下步骤master节点执行注释master节点下的命令,node节点同理。
#= ============================================================================= =#

# master节点
# init失败执行:kubeadm reset -f
# kubelet启动失败日志查看:journalctl -xeu kubelet
# init参数查看:kubeadm init --help
kubeadm init --apiserver-advertise-address=192.168.11.12 --pod-network-cidr=10.244.0.0/16 --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers

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

# github -> flannel-io/flannel/master/Documentation/kube-flannel.yml
kubectl apply -f kube-flannel.yml

# node节点;重新加入节点需要执行 kubeadm reset
kubeadm join 192.168.11.12:6443 --token 1l5lrm.y4c0sr6snms4etvu \
        --discovery-token-ca-cert-hash sha256:5bbcad888dbfae51b9f773cd46e8b68b9884d0ae2c109d9ba21ac660d38ca6f9
        
# 查看节点状态,刚join完,节点是not ready状态,需等待一会再查看就是ready状态。
kubectl get node

启动

# 机器重启后如果出现某个节点不是ready状态,使用下述命令查找原因
journalctl -f -u kubelet
systemctl status docker

# 启动命令
systemctl restart docker
systemctl enable kubelet && systemctl start kubelet

# 查看节点状态
kubectl get node
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐