环境配置说明
-
服务器最低配置要求:2核4G
-
kubernetes version=v1.23.5
-
ubuntu v18.04.1 2核4G 主机:master 操作步骤:1~9
-
ubuntu v18.04.1 2核4G 节点:node 操作步骤:1~6 + 10
目录
-
1. apt-get 源添加 kubernetes
-
2. apt-get 源添加密钥
-
3. apt-get 源更新
-
4. apt-get 安装 kubeadm、kubectl、kubelet、kubernetes-cni、docker.io
-
5. 启动并设置开机自动启动
-
6. k8s 初始化前的镜像准备
-
7. 下载 flannel 网络插件配置文件 kube-flannel.yml
-
8. 初始化 master 主机
-
9. 安装部署 flannel 网络插件
-
10. node 接入 master 集群
1. apt-get 源添加 kubernetes
cat <<EOF > /etc/apt/sources.list.d/kubernetes.list
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF
2. apt-get 源添加密钥
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -
3. apt-get 源更新
apt-get update
4. apt-get 安装 kubeadm、kubectl、kubelet、kubernetes-cni、docker.io
apt-get install -y kubeadm kubectl kubelet kubernetes-cni docker.io
5. 启动并设置开机自动启动
systemctl enable docker && systemctl start docker
systemctl enable kubelet && systemctl start kubelet
6. k8s 初始化前的镜像准备
// 查看环境所需的镜像(本案例版本 v1.23.5)
kubeadm config images list
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.23.5
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.23.5
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.23.5
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.23.5
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.6
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.5.1-0
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.8.6
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.23.5 k8s.gcr.io/kube-apiserver:v1.23.5
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.23.5 k8s.gcr.io/kube-controller-manager:v1.23.5
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.23.5 k8s.gcr.io/kube-scheduler:v1.23.5
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.23.5 k8s.gcr.io/kube-proxy:v1.23.5
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.6 k8s.gcr.io/pause:3.6
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.5.1-0 k8s.gcr.io/etcd:3.5.1-0
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.8.6 k8s.gcr.io/coredns/coredns:v1.8.6
docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.23.5
docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.23.5
docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.23.5
docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.23.5
docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.6
docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.5.1-0
docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.8.6
7. 下载 flannel 网络插件配置文件 kube-flannel.yml
wget https://github.com/coreos/flannel/raw/master/Documentation/kube-flannel.yml
文件中默认网段 10.244.0.0/16 可自行编辑 net-conf.json 对象中的 Network 参数
修改后必须在 kubeadm init 时与 --pod-network-cidr=10.244.0.0/16 参数保持一致
8. 初始化 master 主机
vi /etc/docker/daemon.json
{
"exec-opts":["native.cgroupdriver=systemd"]
}
systemctl restart docker
// 敲黑板:初始化成功后记录 token 密钥 node 连接加入集群时需要
kubeadm init --kubernetes-version=v1.23.5 --apiserver-advertise-address=0.0.0.0 --pod-network-cidr=10.244.0.0/16 --service-cidr=10.1.0.0/16
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
-
--kubernetes-version (版本号)
-
--apiserver-advertise-address (监听IP地址)
-
--pod-network-cidr(pod IP 地址段)
-
--service-cidr(service IP 地址段)
kubeadm 命令参考:https://kubernetes.io/zh/docs/reference/setup-tools/kubeadm/kubeadm/
9. 安装部署 flannel 网络插件
kubectl apply -f kube-flannel.yml
10. node 接入 master 集群
// master 主机可以通过命令重新生成密钥:kubeadm token create --print-join-command
kubeadm join 172.21.0.6:6443 --token 8yw2zv.n64l7w57sjg8uyyh --discovery-token-ca-cert-hash sha256:cb2486da12731d9a158d2d4e604e34001f5f0bc22cc84884247d730c53be2264