---安装思路---
1.环境准备:最小化安装centos7,可上网
2.使用yum 下载依赖源到本地
3.下载K8S相关镜像资源
4.准备好yaml文件
5.脚本部署(或者手动命令)
---开始安装---
# 使用阿里云镜像源
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
# 创建本地仓库包
yum install --downloadonly --downloaddir=/root/k8sOfflineSetup/packages createrepo
# 实用工具
yum install --downloadonly --downloaddir=/root/k8sOfflineSetup/packages yum-utils nfs-utils wget
# docker 依赖包
yum install --downloadonly --downloaddir=/root/k8sOfflineSetup/packages device-mapper-persistent-data lvm2
# 添加阿里云Docker源(yum-config-manager命令需要安装yum-utils)
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
#下载docker部署包
yum install --downloadonly --downloaddir=/root/k8sOfflineSetup/packages docker-ce-19.03.5 docker-ce-cli-19.03.5 containerd.io
# 时间同步
yum install --downloadonly --downloaddir=/root/k8sOfflineSetup/packages chrony
# 配置K8S的yum源
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[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
#下载kubeadm相关组件
yum install --downloadonly --downloaddir=/root/k8sOfflineSetup/packages kubelet-1.20.4 kubeadm-1.20.4 kubectl-1.20.4
#查看最新的组件镜像版本,可参考
kubeadm config images list
#在有docker环境的服务器上下载K8S镜像,然后传输到模板机
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.20.4
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.20.4
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.20.4
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.20.4
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.2
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.4.13-0
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.7.0
# 重新tag镜像,该步骤是因为K8S配置清单中yaml用的是默认镜像地址
docker images \
| grep registry.cn-hangzhou.aliyuncs.com/google_containers \
| sed 's/registry.cn-hangzhou.aliyuncs.com\/google_containers/k8s.gcr.io/' \
| awk '{print "docker tag " $3 " " $1 ":" $2}' \
| sh
#导出镜像
docker save -o kube-apiserver-v1.20.4.tar k8s.gcr.io/kube-apiserver:v1.20.4
docker save -o kube-controller-manager-v1.20.4.tar k8s.gcr.io/kube-controller-manager:v1.20.4
docker save -o kube-scheduler-v1.20.4.tar k8s.gcr.io/kube-scheduler:v1.20.4
docker save -o kube-proxy-v1.20.4.tar k8s.gcr.io/kube-proxy:v1.20.4
docker save -o pause-3.2.tar k8s.gcr.io/pause:3.2
docker save -o etcd-3.4.13-0.tar k8s.gcr.io/etcd:3.4.13-0
docker save -o coredns-1.7.0.tar k8s.gcr.io/coredns:1.7.0
docker save -o ingress.tar quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.23.0
#calico相关镜像下载可通过官方yaml内提取:https://docs.projectcalico.org/manifests/calico.yaml
#默认下载calico镜像是最新的,我这里使用3.16.3,需要将yaml版本替换
docker save -o calico-node-v3.16.3.tar calico/node:v3.16.3
docker save -o calico-pod2daemon-flexvol-v3.16.3.tar calico/pod2daemon-flexvol:v3.16.3
docker save -o calico-cni-v3.16.3.tar calico/cni:v3.16.3
docker save -o calico-kube-controllers-v3.16.3.tar calico/kube-controllers:v3.16.3
v3.16.3,需要将如下删除掉
# 配置本地目录源
cd /etc/yum.repos.d/
mv CentOS-Base.repo CentOS-Base.repo.bak
vim /etc/yum.repos.d/CentOS-Media.repo
[c7-media]
name=CentOS-$releasever - Media
baseurl=file:///root/k8sOfflineSetup/packages/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
file:///root/k8sOfflineSetup/gpg/Docker.gpg
file:///root/k8sOfflineSetup/gpg/Aliyun-kubernetes-yum-key.gpg
file:///root/k8sOfflineSetup/gpg/Aliyun-kubernetes-rpm-package-key.gpg
# 设置环境
export HOSTNAME=K8S2
export APISERVER_NAME=apiserver.k8s.com
export MASTER_IP=192.168.50.133
export POD_SUBNET=10.11.10.0/16
echo "192.168.50.133 apiserver.k8s.com" >> /etc/hosts
# 安装并启动 docker
yum install -y docker-ce-19.03.5 docker-ce-cli-19.03.5 containerd.io
systemctl enable docker
systemctl start docker
# 安装kubelet、kubeadm、kubectl
yum install -y kubelet-1.20.4 kubeadm-1.20.4 kubectl-1.20.4
# 修改docker Cgroup Driver为systemd
sed -i "s#^ExecStart=/usr/bin/dockerd.*#ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --exec-opt native.cgroupdriver=systemd#g" /usr/lib/systemd/system/docker.service
# 重启 docker,并启动 kubelet
systemctl daemon-reload
systemctl restart docker
systemctl enable kubelet && systemctl start kubelet
kubectl version
# 配置kubeadm文件
cat <<EOF > kubeadm-config.yaml
apiVersion: kubeadm.k8s.io/v1beta2
kind: ClusterConfiguration
kubernetesVersion: v1.20.4
controlPlaneEndpoint: "${APISERVER_NAME}:6443"
networking:
serviceSubnet: "10.96.0.0/16"
podSubnet: "${POD_SUBNET}"
dnsDomain: "cluster.local"
EOF
# 安装K8S服务
kubeadm init --config=kubeadm-config.yaml --upload-certs
# 配置kubectl
mkdir /root/.kube/
cp -i /etc/kubernetes/admin.conf /root/.kube/config
# 安装calico服务
kubectl apply -f calico.yaml
# 安装ingress控制器
kubectl apply -f ingress.yaml
其他说明:docker gpg文件Index of linux/centos/ (docker.com)
aliyun kubernetes gpg Index of /kubernetes/yum/doc/ (aliyun.com)