一、 环境和版本
1. 操作系统
干干静静的centos7系统,选取的是mini的iso最小化安装
# cat /etc/centos-release CentOS Linux release 7.3.1611 (Core) # cat /proc/version Linux version 3.10.0-514.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC) ) #1 SMP Tue Nov 22 16:42:41 UTC 2016
2. K8S版本
K8S使用的当前(现在是2021-04-07)最新版本:v1.20。官方教程:https://kubernetes.io/zh/docs/home/。安装方式采用kubeadm安装。
3. 集群环境
最少2个节点,其中1个节点作为master节点,其他节点作为node节点,本例中:
- 10.10.2.241,master节点
- 10.10.2.242,node节点
- 10.10.2.243,node节点
二、 安装
1. 设置host
# ssh root@10.10.2.241 # hostnamectl set-hostname k8s-1 # exit # ssh root@10.10.2.242 # hostnamectl set-hostname k8s-2 # exit # ssh root@10.10.2.242 # hostnamectl set-hostname k8s-3 # exit
2. 搭建master节点
2.1 关闭防火墙和selinux
# setenforce 0 # sed -i 's|SELINUX=\(.*\)|SELINUX=disabled|g' /etc/selinux/config
2.2 配置docker的yum源
# curl -o /etc/yum.repos.d/docker-ce.repo https://download.docker.com/linux/centos/docker-ce.repo
2.3 安装docker-ce
# yum install -y docker-ce
2.4 启动docker服务
# systemctl start docker # systemctl enable docker
2.5 关闭swap
# swapoff -a # vim /etc/fstab
将有swap那行注释掉
2.6 配置K8S的yum源
# vim /etc/yum.repos.d/kubernetes.repo
输入一下内容保存 [kubernetes]
name=kubernetes
baseurl=https://opentuna.cn/kubernetes/yum/repos/kubernetes-el7-$basearch
enabled=1
2.7 安装kubelet、kubeadm、kubectl
# yum install -y kubelet kubeadm kubectl --nogpgcheck
2.8 获取K8S所需的docker镜像
# docker pull registry.aliyuncs.com/google_containers/kube-apiserver:v1.20.5 # docker pull registry.aliyuncs.com/google_containers/kube-controller-manager:v1.20.5 # docker pull registry.aliyuncs.com/google_containers/kube-scheduler:v1.20.5 # docker pull registry.aliyuncs.com/google_containers/kube-proxy:v1.20.5 # docker pull registry.aliyuncs.com/google_containers/pause:3.2 # docker pull registry.aliyuncs.com/google_containers/etcd:3.4.13-0 # docker pull registry.aliyuncs.com/google_containers/coredns:1.7.0 # docker tag registry.aliyuncs.com/google_containers/kube-apiserver:v1.20.5 k8s.gcr.io/kube-apiserver:v1.20.5 # docker tag registry.aliyuncs.com/google_containers/kube-controller-manager:v1.20.5 k8s.gcr.io/kube-controller-manager:v1.20.5 # docker tag registry.aliyuncs.com/google_containers/kube-scheduler:v1.20.5 k8s.gcr.io/kube-scheduler:v1.20.5 # docker tag registry.aliyuncs.com/google_containers/kube-proxy:v1.20.5 k8s.gcr.io/kube-proxy:v1.20.5 # docker tag registry.aliyuncs.com/google_containers/pause:3.2 k8s.gcr.io/pause:3.2 # docker tag registry.aliyuncs.com/google_containers/etcd:3.4.13-0 k8s.gcr.io/etcd:3.4.13-0 # docker tag registry.aliyuncs.com/google_containers/coredns:1.7.0 k8s.gcr.io/coredns:1.7.0
2.9 初始化集群
# kubeadm init
注意执行成功后类似如下输出,复制下来
kubeadm join 10.10.2.241:6443 --token 2dhvds.4yd881szk64kqxoo --discovery-token-ca-cert-hash sha256:5435c5cbfbef0e00f5fd7b96c325771adc5a8f5591a7799939861bde6d0f557f
2.10 执行命令
# export KUBECONFIG=/etc/kubernetes/admin.conf
2.11 安装网络插件
# kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
3. 将node节点加入集群
执行2.9步输出内容
# ssh root@10.10.2.242 # kubeadm join 10.10.2.241:6443 --token 2dhvds.4yd881szk64kqxoo --discovery-token-ca-cert-hash sha256:5435c5cbfbef0e00f5fd7b96c325771adc5a8f5591a7799939861bde6d0f557f # exit # ssh root@10.10.2.243 # kubeadm join 10.10.2.241:6443 --token 2dhvds.4yd881szk64kqxoo --discovery-token-ca-cert-hash sha256:5435c5cbfbef0e00f5fd7b96c325771adc5a8f5591a7799939861bde6d0f557f # exit
4. 查看集群状态
# kubectl get nodes NAME STATUS ROLES AGE VERSION k8s-1 Ready control-plane,master 171m v1.20.5 k8s-2 Ready <none> 6m48s v1.20.5 k8s-3 Ready <none> 7m35s v1.20.5