Kubernetes v1.28.0安装详解
一.环境初始化
要在所有节点执行命令进行配置
1、检查操作系统的版本
此部署环境为CentOS 7.9
[root@CentOS7 ~]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
[root@CentOS7 ~]#
2、主机名解析
为了方便集群节点间的互相调用,配置主机名解析
hostnamectl set-hostname k8s-master
hostnamectl set-hostname k8s-noed1
hostnamectl set-hostname k8s-noed2
cat >>/etc/hosts<<-'EOF'
172.16.0.20 k8s-master
172.16.0.21 k8s-node1
172.16.0.22 k8s-node2
EOF
3、时间同步
kubernetes要求集群节点时间必须精确一致,使用chronyd服务从阿里云同步时间
yum install chrony -y
sed -i '3,6 s/^/# /' /etc/chrony.conf
sed -i '/server 3.centos/a\server ntp.aliyun.com iburst' /etc/chrony.conf
systemctl restart chronyd
chronyc sources
4、禁用swap分区
swap分区:虚拟内存分区,它的作用是物理内存使用完,之后将磁盘空间虚拟成内存来使用,启用swap设备会对系统性能产生很大的影响,kubernetes集群要求所有节点禁用swap分区
临时关闭:swapoff -a
永久关闭:sed -i 's/^[^#].*swap/#&/' /etc/fstab
5、关闭selinux
selinux是linux系统下的一个安全服务,一般选择直接关闭
临时关闭:setenforce 0
永久关闭:sed -i 's/^SELINUX=enforcing$/SELINUX=disabled/' /etc/selinux/config
#: s替换
#: ^开头匹配
#: [^#]:匹配非#
#: #&:中的&代表匹配整行,整个意思就是行前面加上#号
#: g:全部(只匹配特定行不加)
6、禁用iptable和firewalld服务
kubernetes集群在运行的中会产生大量的iptables规则,为了不和系统规则混淆,直接关闭系统的规则
systemctl stop iptables
systemctl disable iptables
systemctl stop firewalld
systemctl disable firewalld
7、修改linux的内核参数
1).修改linux的内核参数,添加网桥过滤和地址转发功能
cat >>/etc/sysctl.d/kubernetes.conf<<-'EOF'
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
2).重新加载配置
sysctl -p
3). 加载网桥过滤模块
modprobe br_netfilter
4). 查看网桥过滤模块是否加载成功
lsmod | grep br_netfilter
8、安装ipvs
iipvs(IP Virtual Server) 实现了传输层负载均衡,ipvs 可以将基于 TCP 和 UDP 的服务请求转发到真实服务器上,并使真实服务器上的服务在单个 IP 地址上显示为虚拟服务。
1).安装ipset和ipvsadm
yum install ipset ipvsadm -y
2).添加需要加载的模块写入脚本文件
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_ipv4
EOF
3).为脚本添加执行权限
chmod +x /etc/sysconfig/modules/ipvs.modules
4).执行脚本文件
/bin/bash /etc/sysconfig/modules/ipvs.modules
5).查看对应的模块是否加载成功
lsmod | grep -e ip_vs -e nf_conntrack_ipv4
9、安装docker
1).安装依赖
yum install -y yum-utils device-mapper-persistent-data lvm2
2).添加yum源
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
3).安装docker
yum -y install docker-ce docker-ce-cli
4).启动并添加开机自启
systemctl start docker && systemctl enable docker
5).添加镜像加速
cat >/etc/docker/daemon.json<<-'EOF'
{
"registry-mirrors": [
"https://mirror.gcr.io",
"https://docker.registry.cyou",
"https://docker-cf.registry.cyou",
"https://dockercf.jsdelivr.fyi",
"https://docker.jsdelivr.fyi",
"https://dockertest.jsdelivr.fyi",
"https://mirror.aliyuncs.com",
"https://dockerproxy.com",
"https://mirror.baidubce.com",
"https://docker.m.daocloud.io",
"https://docker.nju.edu.cn",
"https://docker.mirrors.sjtug.sjtu.edu.cn"
]
}
EOF
6).重新加载配置文件并重启
systemctl daemon-reload
systemctl restart docker
7).查看源是否生效
docker info
10、安装containerd
1).安装containerd
yum install -y containerd.io
2).生成config.toml配置
containerd config default > /etc/containerd/config.toml
3).配置 systemd cgroup 驱动
#修改SystemdCgroup
sed -i 's/SystemdCgroup = false/SystemdCgroup = true/g' /etc/containerd/config.toml
#将sandbox_image下载地址替换为阿里云地址
[plugins."io.containerd.grpc.v1.cri"]
...
...
sandbox_image = "registry.aliyuncs.com/google_containers/pause:3.9"
4).重启containerd 并设置开机自启动
systemctl restart containerd && systemctl enable containerd
二、安装kubernetes
1、kubernetes配置阿里云yum源
所有节点执行
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
2、安装kubeadm、kubelet、kubectl
所有节点执行
yum install -y kubelet-1.28.0 kubeadm-1.28.0 kubectl-1.28.0 --disableexcludes=kubernetes
systemctl enable kubelet
3、初始化master节点
master节点执行
kubeadm init \
--apiserver-advertise-address=172.16.0.20 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.28.0 \
--service-cidr=10.96.0.0/12 \
--pod-network-cidr=10.244.0.0/16
4、初始化结果
出现一下提示说明初始化成功
Your Kubernetes control-plane has initialized successfully!
To start using your cluster, you need to run the following as a regular user:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
Alternatively, if you are the root user, you can run:
export KUBECONFIG=/etc/kubernetes/admin.conf
You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
https://kubernetes.io/docs/concepts/cluster-administration/addons/
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 172.16.0.20:6443 --token yo82fc.pzvoqvx6zo71snxp \
--discovery-token-ca-cert-hash sha256:7f63e081bc13138eb9e28a41581d1e8e33d113d20fd9a1e9a76bbd6a57feba40
5、按提示执行命令
master节点执行
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
export KUBECONFIG=/etc/kubernetes/admin.conf
6、添加从节点
node节点执行
kubeadm join 172.16.0.20:6443 --token yo82fc.pzvoqvx6zo71snxp \
--discovery-token-ca-cert-hash sha256:7f63e081bc13138eb9e28a41581d1e8e33d113d20fd9a1e9a76bbd6a57feba40
7、查看集群节点
kubectl get node
8、coredns服务出现 Pending
1).查看coredns服务的状态
kubectl get pod -A
2).查看pod的详细信息
kubectl describe pod coredns-66f779496c-2pfpg -n kube-system
3).查看node节点的污点
4).去除污点
kubectl taint nodes k8s-node1 node.kubernetes.io/not-ready-
kubectl taint nodes k8s-node2 node.kubernetes.io/not-ready-
5).再查看coredns服务的状态
三、安装网络组件calico
1、下载calico.yaml文件
wget --no-check-certificate https://projectcalico.docs.tigera.io/archive/v3.25/manifests/calico.yaml
2、修改配置文件
# 修改pod容器网络
sed -i 's/192.168.0.0/10.244.0.0/g' calico.yaml
#添加网卡信息
- name: IP_AUTODETECTION_METHOD
value: "interface=ens33"
3、部署calico
kubectl apply -f calico.yaml
4、查看calico的pod状态
kubectl get pod -A
5、查看集群状态
kubectl get node
四、集群简单测试
1、创建一个nginx服务
kubectl create deployment nginx --image=nginx:1.24-alpine
2、暴露端口
kubectl expose deploy nginx --port=80 --target-port=80 --type=NodePort
3、查看pod和svc服务
kubectl get pod,svc