环境
- 系统 : entOS Linux release 7.9.2009 (Core
- IP:192.168.44.177
硬件要求:控制平面最少需要 2c2g
安装前环境准备
如果是集群部署还需要配置时间同步
-
关闭防火墙
systemctl disable firewalld
-
关闭selinux
setenforce 0 sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
-
关闭swap分区
如果没有开启swap分区可以跳过这步
swapoff -a
删除掉 /etc/fstab 有关swap分区的信息
-
设置主机名
hostnamectl set-hostname k8s-master su # 可以直接切换到 新的主机名
开始安装容器运行时(containerd)
安装和配置先决条件
- 转发 IPv4 并让 iptables 看到桥接流量
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF
sudo modprobe overlay
sudo modprobe br_netfilter
# 设置所需的 sysctl 参数,参数在重新启动后保持不变
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
EOF
# 应用 sysctl 参数而不重新启动
sudo sysctl --system
通过运行以下指令确认 br_netfilter 和 overlay 模块被加载:
lsmod | grep br_netfilter
lsmod | grep overlay
通过运行以下指令确认 net.bridge.bridge-nf-call-iptables、net.bridge.bridge-nf-call-ip6tables 和 net.ipv4.ip_forward 系统变量在你的 sysctl 配置中被设置为 1:
sysctl net.bridge.bridge-nf-call-iptables net.bridge.bridge-nf-call-ip6tables net.ipv4.ip_forward
开始安装containerd容器运行时
https://github.com/containerd/containerd/blob/main/docs/getting-started.md#advanced-topics 官网安装指南
通常,您还必须 从其官方网站安装runc
和CNI
插件。
- 安装containerd
下载安装包解压安装
安装包路径:https://github.com/containerd/containerd/releases
没有科学上网的话很慢,可以到我的网盘获取:链接:https://pan.baidu.com/s/1QakzbfaHM4bbyZ2MjRdIhQ
提取码:afh0
wget https://github.com/containerd/containerd/releases/download/v1.7.15/containerd-1.7.15-linux-amd64.tar.gz
tar Cxzvf /usr/local containerd-1.7.15-linux-amd64.tar.gz
使用systemd启动containerd
获取containerd.service文件
https://raw.githubusercontent.com/containerd/containerd/main/containerd.service
将containerd.service文件移动到 /usr/local/lib/systemd/system/ 目录下
如果/usr/local/lib/systemd/system/ 目录不存在则创建
mkdir -p /usr/local/lib/systemd/system/
mv containerd.service /usr/local/lib/systemd/system/
重新加载systemd服务
containerd 的默认socket文件路径为
/run/containerd/containerd.sock
高版本的k8s现在默认都使用 containerd作为 容器运行时,包括现在使用的k8s1.28
systemctl daemon-reload
systemctl enable --now containerd
- 安装runc
下载地址:https://github.com/opencontainers/runc/releases
wget https://github.com/opencontainers/runc/releases/download/v1.1.12/runc.amd64
install -m 755 runc.amd64 /usr/local/sbin/runc
- 安装CNI插件
下载地址: https://github.com/containernetworking/plugins/releases
wget https://github.com/containernetworking/plugins/releases/download/v1.4.1/cni-plugins-linux-amd64-v1.4.1.tgz
mkdir -p /opt/cni/bin
tar Cxzvf /opt/cni/bin cni-plugins-linux-amd64-v1.4.1.tgz
- 修改containerd cgroup为 systemd
containerd 使用位于 的配置文件
/etc/containerd/config.toml
来指定守护进程级别选项。
使用二进制安装目录默认是没有,是需要自己手动创建。
默认配置可以通过 生成containerd config default > /etc/containerd/config.toml
mkdir /etc/containerd/
containerd config default > /etc/containerd/config.toml
[root@k8s-master ~]# vim /etc/containerd/config.toml
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
BinaryName = ""
CriuImagePath = ""
CriuPath = ""
CriuWorkPath = ""
IoGid = 0
IoUid = 0
NoNewKeyring = false
NoPivotRoot = false
Root = ""
ShimCgroup = ""
SystemdCgroup = true # 将flase 修改为 true
- 设置sandbox_image 镜像
默认配置为:sandbox_image = “registry.k8s.io/pause:3.8”
必须修改为你后面拉取pause镜像一致的版本和地址,后面将使用阿里云的镜像,所以这里可以提前写好。
[root@k8s-master ~]# vim /etc/containerd/config.toml
sandbox_image = "registry.aliyuncs.com/google_containers/pause:3.9"
-
重启containerd,使用上面两个修改生效
systemctl restart containerd
安装k8s
这里使用阿里云的yum源进行安装目录最新的版本为1.28
- 配置yum源
cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
-
安装
yum -y install kubeadm kubectl kubelet --disableexcludes=kubernetes
-
启动kubelet
systemctl enable kubelet --now
-
使用 kubeadm 创建集群
4.1. 修改初始集群默认配置文件kubeadm config print init-defaults > init-defaults.yaml
[root@k8s-master ~]# vim init-defaults.yaml
apiVersion: kubeadm.k8s.io/v1beta3
bootstrapTokens:
- groups:
- system:bootstrappers:kubeadm:default-node-token
token: abcdef.0123456789abcdef
ttl: 24h0m0s
usages:
- signing
- authentication
kind: InitConfiguration
localAPIEndpoint:
advertiseAddress: 192.168.44.177 # 集群宣告地址,k8smaster节点地址
bindPort: 6443
nodeRegistration:
criSocket: unix:///var/run/containerd/containerd.sock # 容器运行时 sock文件地址,上面有提到
imagePullPolicy: IfNotPresent
name: k8s-master # 节点名称
taints: null
---
apiServer:
timeoutForControlPlane: 4m0s
apiVersion: kubeadm.k8s.io/v1beta3
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
controllerManager: {}
dns: {}
etcd:
local:
dataDir: /var/lib/etcd
imageRepository: registry.aliyuncs.com/google_containers # 镜像拉取地址
kind: ClusterConfiguration
kubernetesVersion: 1.28.0
networking:
dnsDomain: cluster.local
serviceSubnet: 10.96.0.0/12
podSubnet: 10.244.0.0/16 # pod地址,这里配置的地址范围和后面的网络插件的地址范围是一致的,注意一下。
scheduler: {}
4.2. 使用初始化配置文件拉取镜像
kubeadm config images list --config=init-defaults.yaml # 查看需要哪些镜像
kubeadm config images pull --config=init-defaults.yaml # 拉取镜像
[root@k8s-master ~]# kubeadm config images pull --config=init-defaults.yaml
[config/images] Pulled registry.aliyuncs.com/google_containers/kube-apiserver:v1.28.0
[config/images] Pulled registry.aliyuncs.com/google_containers/kube-controller-manager:v1.28.0
[config/images] Pulled registry.aliyuncs.com/google_containers/kube-scheduler:v1.28.0
[config/images] Pulled registry.aliyuncs.com/google_containers/kube-proxy:v1.28.0
[config/images] Pulled registry.aliyuncs.com/google_containers/pause:3.9 # 回顾一下,这个pause镜像,在配置containerd使用的sandbox有使用到
[config/images] Pulled registry.aliyuncs.com/google_containers/etcd:3.5.9-0
[config/images] Pulled registry.aliyuncs.com/google_containers/coredns:v1.10.1
4.3. 初始化集群
kubeadm init --config=init-defaults.yaml
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 192.168.92.40:6443 --token abcdef.0123456789abcdef \
--discovery-token-ca-cert-hash sha256:b31f38202281a96d1f721899dc56e95f770c7bbfea75b2b1076f1fed602e1318
4.4. 将kubectl证书添加到环境变量,否则你将无法使用kubectl命令
如果你是普通用户执行如下命令:
最好是添加到环境变量文件中,以免重启变量失效
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
如果你是root用户执行如下
export KUBECONFIG=/etc/kubernetes/admin.conf
- 安装网络插件
网络插件很多,作用都是为了让Pod之间可以互相通信,这里选择简单的三层网络
flannel
kubectl apply -f https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml
扩展:
1) coredns依赖网络插件,如果没有网路插件coredns是无法启动
2)如果没有安装网络插件 node 的状态为noReady
到此k8s单节点就部署完成。
运行应用
由于k8s控制平面一般是不能调度Pod的,是因为master节点上被打上了污点,所以一般的Pod无法调度到k8s master节点。
查看节点上的污点
[root@k8s-master ~]# kubectl get node
NAME STATUS ROLES AGE VERSION
k8s-master Ready control-plane 16m v1.28.2
[root@k8s-master ~]# kubectl describe node k8s-master | grep -i taint
Taints: node-role.kubernetes.io/control-plane:NoSchedule
去除污点
[root@k8s-master ~]# kubectl taint node k8s-master node-role.kubernetes.io/control-plane:NoSchedule-
node/k8s-master untainted
运行应用
运行一个nginx,并将端口映射到主机端口
kubectl run nginx --image=nginx --labels="app=nginx" --port=80
kubectl expose pod nginx --port=80 --target-port=80 --labels="app=nginx" --type=NodePort
[root@k8s-master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 4m48s
[root@k8s-master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 36m
nginx NodePort 10.97.129.0 <none> 80:31493/TCP 62s
service类型为 NodePort 将service端口随机映射到 所有主机上,默认范围在 30000-32767,以上是映射到了 31493端口上