安装K8s(1.24.0)
一、准备工作
两台节点(centos7):
使用containerd做容器引擎
主机名 | IP地址 |
---|---|
k8s-master-01 | 192.168.10.21 |
k8s-node-01 | 192.168.10.23 |
环境准备
sed -i 's/enforcing/disabled/g' /etc/selinux/config #永久关闭selinux
setenforce 0 #临时关闭selinux
systemctl disable firewalld --now #关闭防火墙
swapoff -a #临时关闭swap
vim /etc/fstab # 永久关闭swap
#设置主机名:
hostnamectl set-hostname <hostname>
#添加hosts:
cat >> /etc/hosts << EOF
192.168.10.21 k8s-master-01
192.168.10.23 k8s-node=01
EOF
#配置时间同步:
yum install ntpdate -y
ntpdate ntp.aliyun.com
#配置流量转发:
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 --system # 生效
升级内核:
#导入elrepo gpg key
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
#安装elrepo yum仓库
yum -y install https://www.elrepo.org/elrepo-release-7.0-4.el7.elrepo.noarch.rpm
#安装kernel-ml版本,ml为长期稳定版本,lt为长期维护版本
yum -y install kernel-ml.x86_64 --enablerepo="elrepo-kernel"
#设置grub2默认引导为0
grub2-set-default 0
#重新生成grub2引导文件
grub2-mkconfig -o /boot/grub2/grub.cfg
#重启
reboot
#检查
uname -r
#5.19.1-1.el7.elrepo.x86_64
#加载br_netfilter
modprobe br_netfilter
#安装配置ipset、ipvsadm
yum -y install ipset ipvsadm
cat >/etc/sysconfig/modules/ipvs.modules <<EOF
#!/bin/bash
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack
EOF
chmod +x /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules
#升级系统软件
yum update -y
二、安装containerd
1、配置先决条件【所有节点】
cat <<EOF | sudo tee /etc/modules-load.d/containerd.conf
overlay
br_netfilter
EOF
modprobe overlay
modprobe br_netfilter
# 设置必需的 sysctl 参数,这些参数在重新启动后仍然存在。
cat <<EOF | sudo tee /etc/sysctl.d/99-kubernetes-cri.conf
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
EOF
# Apply sysctl params without reboot
sudo sysctl --system
2、安装containerd【所有节点】
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
yum install -y containerd.io
mkdir -p /etc/containerd
containerd config default | sudo tee /etc/containerd/config.toml
systemctl enable containerd --now
3、修改配置文件【所有节点】
vi /etc/containerd/config.toml
[plugins."io.containerd.grpc.v1.cri"]
sandbox_image = "registry.aliyuncs.com/google_containers/pause:3.2"
...
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
SystemdCgroup = true
...
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]
endpoint = ["https://b9pmyelo.mirror.aliyuncs.com"]
systemctl restart containerd
三、安装kubeadm,kubelet和kubectl
添加阿里云YUM软件源,安装kubeadm,kubelet和kubectl
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=0
repo_gpgcheck=0
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 -y kubelet-1.24.0 kubeadm-1.24.0 kubectl-1.24.0
systemctl enable kubelet
##四、配置kubelet使用containerd
vi /etc/sysconfig/kubelet
KUBELET_EXTRA_ARGS=--container-runtime=remote --container-runtime-endpoint=unix:///run/containerd/containerd.sock --cgroup-driver=systemd
systemctl restart kubelet
五、 部署Kubernetes Master
在master节点:
kubeadm init \
--apiserver-advertise-address=192.168.10.21 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.24.0 \
--service-cidr=10.96.0.0/12 \
--pod-network-cidr=10.244.0.0/16 \
--ignore-preflight-errors=all
- –apiserver-advertise-address 集群通告地址
- –image-repository 由于默认拉取镜像地址k8s.gcr.io国内无法访问,这里指定阿里云镜像仓库地址
- –kubernetes-version K8s版本,与上面安装的一致
- –service-cidr 集群内部虚拟网络,Pod统一访问入口
- –pod-network-cidr Pod网络,,与下面部署的CNI网络组件yaml中保持一致
拷贝kubectl使用的连接k8s认证文件到默认路径:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
向集群添加新节点,执行在kubeadm init输出的kubeadm join命令:
kubeadm join 192.168.10.21:6443 --token 77vyru.ahbzvy00hl5o5plk \
--discovery-token-ca-cert-hash sha256:cfb22821069c326da1373c98f63210d52eaf124971dd9fbf9207dafb52b287ad
默认token有效期为24小时,当过期之后,该token就不可用了。这时就需要重新创建token,可以直接使用命令快捷生成:
kubeadm token create --print-join-command
查看集群节点
[root@k8s-master-01 ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-master-01 NotReady control-plane 10m v1.24.0
k8s-node-01 NotReady <none> 9m58s v1.24.0
#部署完CNI之后STATUS就会变成Ready
六、 部署容器网络(CNI)
yum -y install wget
#下载calico资源清单
wget https://docs.projectcalico.org/manifests/calico.yaml
#修改里面定义Pod网络(CALICO_IPV4POOL_CIDR),与前面kubeadm init指定的一样修改完后应用清单:
vi calico.yaml
···
kubectl apply -f calico.yaml
#等calico的pod起来
[root@k8s-master-01 ~]# kubectl get pod -n kube-system
NAME READY STATUS RESTARTS AGE
calico-kube-controllers-555bc4b957-bjdfk 1/1 Running 2 (7m26s ago) 31m
calico-node-gc9nw 1/1 Running 3 (6m12s ago) 59m
calico-node-n77bw 1/1 Running 3 (4m7s ago) 86m
coredns-74586cf9b6-8r9d6 1/1 Running 2 (7m21s ago) 149m
coredns-74586cf9b6-kz8cs 1/1 Running 2 (7m21s ago) 149m
etcd-k8s-master-01 1/1 Running 4 (4m7s ago) 150m
kube-apiserver-k8s-master-01 1/1 Running 4 (4m7s ago) 150m
kube-controller-manager-k8s-master-01 1/1 Running 4 (4m7s ago) 150m
kube-proxy-sw77j 1/1 Running 4 (4m7s ago) 149m
kube-proxy-vt2bm 1/1 Running 4 (6m12s ago) 149m
kube-scheduler-k8s-master-01 1/1 Running 4 (4m7s ago) 150m
metrics-server-7957fd9f5c-znk95 1/1 Running 1 (8m21s ago) 13m
#完结撒花!!!
测试k8s集群
- 验证Pod工作
- 验证Pod网络通信
- 验证DNS解析
在Kubernetes集群中创建一个pod,验证是否正常运行:
kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort
kubectl get pod,svc
访问地址:http://NodeIP:Port