一 为什么选择用kubeadm部署集群
方便快捷,易于维护。
二 集群机器规划
该环境是我们的开发环境,如果是生产环境部署,k8s的master节点正常部署2个以上做高可用
hostname | 机器IP | 系统 | 机器配置 | 用途 |
---|---|---|---|---|
dev-k8s-master | 192.168.90.220 | CentOS7.2 | 2核4G | k8s的master节点 |
dev-k8s-worker01 | 192.168.90.221 | CentOS7.2 | 4核16G | k8s的worker节点1 |
dev-k8s-worker02 | 192.168.90.222 | CentOS7.2 | 4核16G | k8s的worker节点2 |
dev-k8s-worker03 | 192.168.90.223 | CentOS7.2 | 4核16G | k8s的worker节点3 |
三 每台机器相同的操作
1 | # ntp时钟一致 |
更换yaml源1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16# 1.备份当前yum源(可选)
cd /etc/yum.repos.d/
cp /CentOS-Base.repo /CentOS-Base-repo.bak
# 2.使用wget下载阿里yum源repo文件
wget http://mirrors.aliyun.com/repo/Centos-7.repo
# 3.清理默认缓存包
yum clean all
# 4.把下载下来的阿里云repo文件设置成为默认源
mv Centos-7.repo CentOS-Base.repo
# 5.生成阿里云yum源缓存并更新yum源
yum makecache
# yum update
k8s的yum源配置1
2
3
4
5
6
7
8
9
10
11
12
13yum -y install yum-utils
cat << EOF > kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
yum-config-manager --add-repo kubernetes.repo
安装docker1
2
3
4yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 指定版本 先 docker-ce-cli 否则 docker-ce-cli 会是最新版本
yum install docker-ce-cli-18.09.9-3.el7
yum install docker-ce-18.09.9-3.el7
安装kube1
2
3
4
5
6
7# 安装指定版本
yum install -y kubelet-1.15.12
yum install -y kubectl-1.15.12 kubeadm-1.15.12
systemctl daemon-reload
systemctl enable docker
systemctl enable kubelet
如果是私有仓库并且无ssl则配置docker daemon1
2
3
4
5
6
7
8
9
10mkdir /etc/docker
cat << EOF > /etc/docker/daemon.json
{
"registry-mirrors": ["https://79txu54u.mirror.aliyuncs.com"],
"insecure-registries": ["192.168.90.244"]
}
EOF
systemctl start docker
四 k8s的master的初始化
1 | $ kubeadm init --image-repository registry.cn-hangzhou.aliyuncs.com/google_containers --kubernetes-version=v1.15.12 --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/16 |
证书说明
kubeadm证书只有一年,证书到期前通过以下操作更新证书1
2
3
4
5
6
7# 证书有效期查询 -- 在master节点
kubeadm alpha certs check-expiration
# 直接通过以下命令更新证书
kubeadm alpha certs renew all
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
安装flannel1
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
五 部署ingress-nginx
参考以下文档即可
https://kubernetes.io/zh/docs/concepts/services-networking/ingress/
六 删除或新增节点
三个worker节点都是通过这里所指的方式添加1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20kubectl delete node [节点名]
添加节点
1 在之前删除的节点执行以下命令
kubeadm reset
2 down掉之前的cni网络
ifconfig cni0 down
ip link delete cni0
3 到master节点生成token
kubeadm token create --print-join-command
4 copy token 在node节点执行
kubeadm join 192.168.90.220:6443 --token o2nsll.nnpibjw8tv0vems7 --discovery-token-ca-cert-hash sha256:9aa23632d2df872181f5df5497ff84233623f9688fb3456214bf6d24fd15e0ff
如果执行kubeadm join ...
时报以下错误则安装一下ipset,命令:yum install ipset
1
2
3
4
5
6
7[preflight] Running pre-flight checks
[WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd". Please follow the guide at https://kubernetes.io/docs/setup/cri/
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml'
error execution phase preflight: [preflight] Some fatal errors occurred:
[ERROR IPVSProxierCheck]: error getting ipset version, error: executable file not found in $PATH
[preflight] If you know what you are doing, you can make a check non-fatal with `--ignore-preflight-errors=...`