一.基础环境配置

此小节需要所有节点执行

#临时关闭分区
swapoff -a
#设置时区
timedatectl set-timezone Asia/Shanghai
#关闭防火墙
ufw disable
#默认ubunt默认是不安装selinux的,如果没有selinux命令和配置文件则说明没有安装selinux,则下面步骤就不用做了
sed -ri 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config 
setenforce 0
#安装网桥模块
apt-get install -y bridge-utils
#加载网桥模块
modprobe  br_netfilter
#添加配置文件 
tee /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

sysctl --system

 查看是否成功加载:

lsmod|grep br_net

若网桥模块加载成功如下:

二.docker安装 

此小节所有节点执行!!!

首先,添加镜像源:

apt-get update

apt upgrade
apt-get install ca-certificates curl gnupg lsb-release

curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

安装docker:

apt-get install docker-ce=5:20.10.15~3-0~ubuntu-jammy docker-ce-cli=5:20.10.15~3-0~ubuntu-jammy containerd.io

配置开机自启动:

system enable docker

开始配置镜像源:

mkdir /etc/docker
sudo tee /etc/docker/daemon.json <<EOF
{
  "exec-opts": ["native.cgroupdriver=systemd"], 
  "registry-mirrors": ["https://8i185852.mirror.aliyuncs.com"],         
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "storage-driver": "overlay2"
}
EOF

加载配置:

systemctl daemon-reload
systemctl restart docker

三. 安装k8s

3.1 安装k8s的前的准备

3.1需要所有节点执行!!!!

安装必要的包:

apt-get install -y   software-properties-common apt-transport-https 

配置安装镜像:

curl -s https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add -

sudo tee /etc/apt/sources.list.d/kubernetes.list <<EOF
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF

apt-get update

安装k8s: 

apt-get -y install kubelet=1.23.6-00 kubeadm=1.23.6-00 kubectl=1.23.6-00

apt-mark hold kubelet kubeadm kubectl 

systemctl start kubelet 
systemctl enable kubelet

3.2 主节点安装

3.2只需主节点执行!!!!

执行如下命令:

kubeadm config images pull

若能拉取镜像,则直接执行主节点初始化,否则继续执行步骤(1)(2): 

(1)查看k8s需要的镜像:

kubeadm config images list

(2)替换下列镜像为你需要的版本:

docker pull registry.aliyuncs.com/google_containers/kube-apiserver:v1.23.17
docker tag registry.aliyuncs.com/google_containers/kube-apiserver:v1.23.17 k8s.gcr.io/kube-apiserver:v1.23.17
 
docker pull registry.aliyuncs.com/google_containers/kube-controller-manager:v1.23.17
docker tag registry.aliyuncs.com/google_containers/kube-controller-manager:v1.23.17 k8s.gcr.io/kube-controller-manager:v1.23.17
 
docker pull registry.aliyuncs.com/google_containers/kube-scheduler:v1.23.17
docker tag registry.aliyuncs.com/google_containers/kube-scheduler:v1.23.17 k8s.gcr.io/kube-scheduler:v1.23.17
 
docker pull registry.aliyuncs.com/google_containers/kube-proxy:v1.23.17
docker tag registry.aliyuncs.com/google_containers/kube-proxy:v1.23.17 k8s.gcr.io/kube-proxy:v1.23.17
 
docker pull registry.aliyuncs.com/google_containers/pause:3.6
docker tag registry.aliyuncs.com/google_containers/pause:3.6 k8s.gcr.io/pause:3.6
 
 
docker pull registry.aliyuncs.com/google_containers/etcd:3.5.1-0
docker tag registry.aliyuncs.com/google_containers/etcd:3.5.1-0 k8s.gcr.io/etcd:3.5.1-0
 
docker pull registry.aliyuncs.com/google_containers/coredns:1.8.6
docker tag registry.aliyuncs.com/google_containers/coredns:1.8.6 k8s.gcr.io/coredns:1.8.6

主节点初始化,修改ip为自己的主节点ip:

 kubeadm init  --image-repository registry.aliyuncs.com/google_containers  --kubernetes-version v1.23.6  --service-cidr=10.96.0.0/12  --apiserver-advertise-address=你的ip

记下控制台输出的join命令行,初始化成功之后执行:

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

配置网络插件:

kubectl apply -f https://docs.projectcalico.org/v3.21/manifests/calico.yaml 

3.3 配置从节点

到这里基本安装成功了,只需要加入节点即可。

执行加入节点命令,每个人的命令不同带有独一份的token,所有从节点输入3.2中记下的join命令:

kubeadm join 你的主节点ip:6443 \ 
	--token 8507uc.o0knircuri8etnw2 \
	--discovery-token-ca-cert-hash \
	sha256:acc37967fb5b0acf39d7598f8a439cc7dc88f439a3f4d0c9cae88e7901b9d3f

等待几分钟,查看节点状态:

kubectl get nodes

若所有节点都是ready状态则安装成功。

Logo

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

更多推荐