使用kubeadm部署k8s多master集群(一)集群部署

一、部署方案选择

使用kubeadm安装多master集群有两种部署方案:
第一种使用kubeadm中的默认拓扑。使用kubeadm join --experimental-control-plane命令在master节点上自动创建本地etcd成员,加入到master集群。这种部署方式最为简单,缺点是有一定的耦合失败的风险。每个master节点运行kube-apiserver,kube-scheduler和kube-controller-manager的一个实例,每个master节点创建一个本地etcd成员,该etcd成员仅与本节点kube-apiserver通信,kube-controller-manager和kube-scheduler也一样。所以每掉一台服务器就会使etcd和apiserver的实例减少一台,必要时候需要按照集群的规模,扩大master实例的数量,减少风险。
第二种是使用外部etcd集群拓扑,etcd节点与master在不同节点上运行。需要额外为每个master创建独立的init配置文件,并在配置文件中指定外部的etcd集群。每个master节点运行一个kube-apiserver,kube-scheduler和kube-controller-manager实例,每个master都可以和etcd集群通信。好处自然是低耦合,大大的降低了风险。但配置上稍有一些麻烦,并且需要的服务器要多一些。

二、默认拓扑部署

1、使用ansiable分发配置,如:设置主机名、/etc/hosts解析、repo文件、dockerhub证书、服务器配置、安装docker等,不详述了就。(每台服务器)

设置主机名
分发配置主机文件/etc/hosts
分发repo文件
kube.repo

cat <<EOF > /etc/yum.repos.d/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

docker.repo
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo

yum clean
yum makecatch

安装依赖
yum install -y conntrack ipvsadm ipset jq sysstat curl iptables libseccomp
关闭防火墙、swap,重置iptables
关闭防火墙
$ systemctl stop firewalld && systemctl disable firewalld
重置iptables

iptables -F && iptables -X && iptables -F -t nat && iptables -X -t nat && iptables -P FORWARD ACCEPT
# 关闭swap
swapoff -a
sed -i '/swap/s/^\(.*\)$/#\1/g' /etc/fstab
# 关闭selinux
setenforce 0
# 关闭dnsmasq(否则可能导致docker容器无法解析域名)
service dnsmasq stop && systemctl disable dnsmasq

设置系统参数

cat > /etc/sysctl.d/kubernetes.conf <<EOF
net.bridge.bridge-nf-call-iptables=1
net.bridge.bridge-nf-call-ip6tables=1
net.ipv4.ip_forward=1
vm.swappiness=0
vm.overcommit_memory=1
vm.panic_on_oom=0
fs.inotify.max_user_watches=89100
EOF

sysctl -p /etc/sysctl.d/kubernetes.conf

#!/bin/bash
# 开启forward
# Docker从1.13版本开始调整了默认的防火墙规则
# 禁用了iptables filter表中FOWARD链
# 这样会引起Kubernetes集群中跨Node的Pod无法通信
 
iptables -P FORWARD ACCEPT
 
# 加载ipvs相关内核模块
# 如果重新开机,需要重新加载
modprobe ip_vs
modprobe ip_vs_rr
modprobe ip_vs_wrr
modprobe ip_vs_sh
modprobe nf_conntrack_ipv4
lsmod | grep ip_vs

2、安装docker(每台服务器)

# 安装docker
查看可以安装的版本
yum list docker-ce --showduplicates|sort -r
yum install -y docker-ce-18.06.1.ce-3

cat > /etc/docker/daemon.json <<EOF
{
    "graph": "/docker/data/path",
    "exec-opts": ["native.cgroupdriver=cgroupfs"],
    "registry-mirrors":["https://k9e55i4n.mirror.aliyuncs.com"]
}
EOF

systemctl start docker && systemctl enable docker

3、安装 kubeadm, kubelet 和 kubectl(每台服务器)

# 找到安装的版本号
yum list kubeadm --showduplicates | sort -r

#安装指定版本
yum install -y kubelet-1.14.5 kubeadm-1.14.5 kubectl-1.14.5

#查看安装情况
systemctl cat kubelet

4、配置高可用

参照LVS+keepalived部署高可用集群

5、初始化第一个master0

systemctl daemon-reload
systemctl enable kubelet

# 使用kubeadm-config.yaml配置k8s1.14.5集群

# cat init.sh
LOAD_BALANCER_DNS="192.168.2.22"     **\\这个是上一步配置的VIP地址**
LOAD_BALANCER_PORT="6443"
# 生成kubeadm配置文件
cat > kubeadm-master.config <<EOF
apiVersion: kubeadm.k8s.io/v1beta1
kind: ClusterConfiguration
# kubernetes版本
kubernetesVersion: v1.14.5
# 使用国内阿里镜像
imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers

apiServer:
  certSANs:
  - "$LOAD_BALANCER_DNS"
controlPlaneEndpoint: "$LOAD_BALANCER_DNS:$LOAD_BALANCER_PORT"

networking:
  podSubnet: 10.244.0.0/16
EOF

#初始化k8s集群
kubeadm init --config=kubeadm-master.config

6、验证证书有效时间

cd /etc/kubernetes/pki
for crt in $(find /etc/kubernetes/pki/ -name "*.crt"); do openssl x509 -in $crt -noout -dates; done 

7、安装网络插件

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

8、拷贝master证书到其他master节点

# cat scp.sh
USER=root
CONTROL_PLANE_IPS="192.168.3.43 192.168.3.44"
for host in ${CONTROL_PLANE_IPS}; do
    scp /etc/kubernetes/pki/ca.crt "${USER}"@$host:
    scp /etc/kubernetes/pki/ca.key "${USER}"@$host:
    scp /etc/kubernetes/pki/sa.key "${USER}"@$host:
    scp /etc/kubernetes/pki/sa.pub "${USER}"@$host:
    scp /etc/kubernetes/pki/front-proxy-ca.crt "${USER}"@$host:
    scp /etc/kubernetes/pki/front-proxy-ca.key "${USER}"@$host:
    scp /etc/kubernetes/pki/etcd/ca.crt "${USER}"@$host:etcd-ca.crt
    scp /etc/kubernetes/pki/etcd/ca.key "${USER}"@$host:etcd-ca.key
    scp /etc/kubernetes/admin.conf "${USER}"@$host:
    ssh ${USER}@${host} 'mkdir -p /etc/kubernetes/pki/etcd'
    ssh ${USER}@${host} 'mv /${USER}/ca.crt /etc/kubernetes/pki/'
    ssh ${USER}@${host} 'mv /${USER}/ca.key /etc/kubernetes/pki/'
    ssh ${USER}@${host} 'mv /${USER}/sa.pub /etc/kubernetes/pki/'
    ssh ${USER}@${host} 'mv /${USER}/sa.key /etc/kubernetes/pki/'
    ssh ${USER}@${host} 'mv /${USER}/front-proxy-ca.crt /etc/kubernetes/pki/'
    ssh ${USER}@${host} 'mv /${USER}/front-proxy-ca.key /etc/kubernetes/pki/'
    ssh ${USER}@${host} 'mv /${USER}/etcd-ca.crt /etc/kubernetes/pki/etcd/ca.crt'
    ssh ${USER}@${host} 'mv /${USER}/etcd-ca.key /etc/kubernetes/pki/etcd/ca.key'
    ssh ${USER}@${host} 'mv /${USER}/admin.conf /etc/kubernetes/admin.conf'
done

9、加入集群

#master
kubeadm join 192.168.2.22:6443 --token qlrq5y.1yhm3rz9r7ynfqf1 --discovery-token-ca-cert-hash sha256:62579157003c3537deb44b30f652c500e7fa6505b5ef6826d796ba1245283899 --experimental-control-plane
#node
kubeadm join 192.168.2.22:6443 --token qlrq5y.1yhm3rz9r7ynfqf1 --discovery-token-ca-cert-hash sha256:62579157003c3537deb44b30f652c500e7fa6505b5ef6826d796ba1245283899

参考文档:
外部etcd集群拓扑部署
默认拓扑部署

posted @ 2020-07-31 21:58  名字很长容易被惦记  阅读(8982)  评论(0编辑  收藏  举报