目录
3.K8S master节点安装kubelet、kubeadm、kubectl
(1)主机
表1 主机
主机 | 架构 | 版本 | IP | 备注 |
master | K8S master节点 | 1.29.0 | 192.168.204.8 | |
node1 | K8S node节点 | 1.29.0 | 192.168.204.9 | |
node2 | K8S node节点 | 1.29.0 | 192.168.204.10 |
(1)查看内核
uname -rs
(2)主机名配置
hostnamectl set-hostname master && bash
(3)主机名与IP解析
cat >> /etc/hosts << EOF
192.168.204.8 master
192.168.204.9 node01
192.168.204.10 node02
EOF
(4)关闭防火墙与SELINUX
1)关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
2)关闭selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0
(5)时间同步配置
yum install chrony -y
systemctl start chronyd && systemctl enable chronyd && chronyc sources
① 安装
② 开机自启动服务
(6)配置内核路由转发及网桥过滤
1)配置
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
vm.swappiness=0
EOF
2)查看
sysctl --system
3)加载br_netfilter模块
modprobe br_netfilter
lsmod |grep br_netfilter
①配置
② ?查看
③ 加载br_netfilter模块
(7)配置ipvs转发
1)安装
yum -y install ipset ipvsadm
2)配置ipvsadm模块加载方式
# 添加需要加载的模块
mkdir -p /etc/sysconfig/ipvsadm
cat > /etc/sysconfig/ipvsadm/ipvs.modules <<EOF
#!/bin/bash
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack
EOF
3)授权、运行、检查是否加载
chmod 755 /etc/sysconfig/ipvsadm/ipvs.modules && bash /etc/sysconfig/ipvsadm/ipvs.modules && lsmod | grep -e ip_vs -e nf_conntrack
① 安装
② 配置
(8)关闭swap分区
sed -ri 's/.*swap.*/#&/' /etc/fstab
swapoff -a
grep swap /etc/fstab
(1)安装docker
① 阿里云镜像加速将XXXXXXXX改为自己的即可(?"https://XXXXXXXX.mirror.aliyuncs.com",)
1)安装
yum -y install wget && wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
yum -y install docker-ce
2)配置cgroup驱动及镜像下载加速器:
cat > /etc/docker/daemon.json << EOF
{
"exec-opts": ["native.cgroupdriver=systemd"],
"registry-mirrors": [
"https://XXXXXXXX.mirror.aliyuncs.com",
"https://registry.docker-cn.com",
"https://docker.mirrors.ustc.edu.cn",
"https://dockerhub.azk8s.cn",
"http://hub-mirror.c.163.com"
]
}
EOF
3)自启动
systemctl enable docker && systemctl start docker && systemctl status docker && docker info|grep systemd
② 安装docker
③ 配置镜像加速
④ 开机自启动服务
(2)安装ci-dockerd
1)下载安装最新版的cri-dockerd
wget https://github.com/Mirantis/cri-dockerd/releases/download/v0.3.8/cri-dockerd-0.3.8.amd64.tgz
tar xf cri-dockerd-0.3.8.amd64.tgz
mv cri-dockerd/cri-dockerd /usr/bin/
rm -rf cri-dockerd cri-dockerd-0.3.8.amd64.tgz
2)配置启动项
cat > /etc/systemd/system/cri-docker.service<<EOF
[Unit]
Description=CRI Interface for Docker Application Container Engine
Documentation=https://docs.mirantis.com
After=network-online.target firewalld.service docker.service
Wants=network-online.target
Requires=cri-docker.socket
[Service]
Type=notify
# ExecStart=/usr/bin/cri-dockerd --container-runtime-endpoint fd://
# 指定用作 Pod 的基础容器的容器镜像(“pause 镜像”)
ExecStart=/usr/bin/cri-dockerd --pod-infra-container-image=registry.k8s.io/pause:3.9 --container-runtime-endpoint fd://
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always
StartLimitBurst=3
StartLimitInterval=60s
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
TasksMax=infinity
Delegate=yes
KillMode=process
[Install]
WantedBy=multi-user.target
EOF
cat > /etc/systemd/system/cri-docker.socket <<EOF
[Unit]
Description=CRI Docker Socket for the API
PartOf=cri-docker.service
[Socket]
ListenStream=%t/cri-dockerd.sock
SocketMode=0660
SocketUser=root
SocketGroup=docker
[Install]
WantedBy=sockets.target
EOF
3)重新加载并设置自启动
systemctl daemon-reload
systemctl enable cri-docker && systemctl start cri-docker && systemctl status cri-docker
① 解压
② 移动
③ 配置启动项
④ 重启并设置开机自启服务
(3)安装kubelet、kubeadm、kubectl
1)配置k8s源
cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://pkgs.k8s.io/core:/stable:/v1.29/rpm/
enabled=1
gpgcheck=1
gpgkey=https://pkgs.k8s.io/core:/stable:/v1.29/rpm/repodata/repomd.xml.key
# exclude=kubelet kubeadm kubectl cri-tools kubernetes-cni
EOF
2)安装
yum install -y kubelet kubeadm kubectl
3)配置 cgroup 驱动与docker一致
cp /etc/sysconfig/kubelet{,.bak}
cat > /etc/sysconfig/kubelet <<EOF
KUBELET_EXTRA_ARGS="--cgroup-driver=systemd"
EOF
systemctl enable kubelet
4)安装自动补全工具(可选)
yum install bash-completion -y
source /usr/share/bash-completion/bash_completion
echo "source <(kubectl completion bash)" >> ~/.bashrc
source ~/.bashrc
5)查看配置镜像
kubeadm config images list
6)下载k8s配置镜像和Calico网络配置镜像
wget http://mirrors.oby.ink/docker-images/k8s-1.29.0.tar
wget http://mirrors.oby.ink/docker-images/k8s-calico-3.27.0.tar
7)导入k8s配置镜像和Calico网络配置镜像
docker load -i k8s-1.29.0.tar
docker load -i k8s-calico-3.27.0.tar
8)所有需要用到镜像:
docker images
①配置k8s源
② 安装
③ 配置 cgroup 驱动与docker一致
④ 安装自动补全工具
⑤ 申明环境变量
⑥ 查看配置镜像
⑦导入k8s配置镜像镜像
⑧导入Calico网络配置镜像
⑨查看镜像
(1)配置与安装
① 注意主机名修改为不同的名称
hostnamectl set-hostname node1 && bash
hostnamectl set-hostname node2 && bash
②查看内核
uname -rs
(2)测试时间同步
date
① master节点
② node1节点
③node2节点
(3)其他过程
与master节点相同,此处省略
(1)初始化
1)初始化集群
kubeadm init \
--apiserver-advertise-address 192.168.204.8 \
--kubernetes-version v1.29.0 \
--pod-network-cidr=10.244.0.0/16 \
--cri-socket=unix:///var/run/cri-dockerd.sock
(2) 配置
1) 创建配置目录
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
2)默认初始化生成token有效期是24小时,所以用自己的生成不过期的token,node节点加入需要用到
kubeadm token create --ttl 0 --print-join-command
3) 生成结果如下
kubeadm join 192.168.204.8:6443 --token 4n321n.czak0y1fhvbfzjcq --discovery-token-ca-cert-hash sha256:e375cebe793b83f64a68b5c0aab56ae578b0989af989897324241e22d7738fca
(1)加入集群
1)添加节点需要指定cri-dockerd接口–cri-socket ,这里是使用cri-dockerd
kubeadm join 192.168.204.8:6443 --token 4n321n.czak0y1fhvbfzjcq --discovery-token-ca-cert-hash sha256:e375cebe793b83f64a68b5c0aab56ae578b0989af989897324241e22d7738fca --cri-socket=unix:///var/run/cri-dockerd.sock
2)如果是containerd则使用–cri-socket unix:///run/containerd/containerd.sock
① node1 节点
② node2节点
(1)查看
1)查看node
kubectl get node
2)查看node详细信息
kubectl get node -o wide
master节点查看(状态为NotReady)
(1)下载Calico配置文件
wget https://github.com/projectcalico/calico/blob/v3.27.0/manifests/calico.yaml
(2)改里面定义Pod网络(CALICO_IPV4POOL_CIDR)
vim calico.yaml
① ?修改前:
②修改后:
与前面kubeadm init的 --pod-network-cidr指定的一样
(3)部署
kubectl apply -f calico.yaml
(4)查看
kubectl get pods -n kube-system
(4) 查看pod(状态已变更为Ready)
kubectl get node
(1)创建deploymenty资源,指定镜像为nginx,副本数量为2个,暴露端口80类型为NodePort
kubectl create deployment web -r 2 --image=nginx
kubectl expose deployment web --port=80 --type=NodePort
(2)观察资源生成情况
kubectl get deployment
kubectl get deployment -w
(3)查看pod
kubectl get pod
kubectl get pod -o wide
(4)查看service
kubectl get svc |grep web
32406端口是容器80映射到主机的端口
(5)网页浏览测试
http://192.168.204.9:32406/
http://192.168.204.10:32406/
node1节点
node2节点
(1)报错
error: error parsing calico.yaml: error converting YAML to JSON: yaml: line 204: did not find expected '-' indicator
(2)原因分析
语法错误,符号“-”缩进错误
(3)解决方法
修改配置文件。
修改前:
修改后:
成功:
(1)文档
K8S官网
docker安装
docker-ce镜像_docker-ce下载地址_docker-ce安装教程-阿里巴巴开源镜像站
cgroup驱动
(2)k8s(v1.24版本以前)
使用docker-shim调用流程:kubelet(客户端) -> docker shim -> dockerd -> containerd -> containerd-shim -> runc
(3)k8s(v1.24版本以后)
(4)解决方案
Kubernetes v1.24移除docker-shim的支持,而Docker Engine默认又不支持CRI标准,因此二者默认无法再直接集成。为此,Mirantis和Docker联合创建了cri-dockerd项目,用于为Docker Engine提供一个能够支持到CRI规范的桥梁,从而能够让Docker作为Kubernetes容器引擎。