说明:此文章基于对k8s集群相关组件已有一定了解,此安装方式采用的是k8s官方安装工具kubeadm安装,并且安装机器基于红帽系系统centos 7 版本为例,本教程安装为docker的19.03.9,k8s的1.19.0版本

开始安装之前我们先来看一张经典的高可用k8s集群的部署架构图:

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA546W44CH5ZCO5aSn5Y-U,size_20,color_FFFFFF,t_70,g_se,x_16

我们知道k8s集群的统一的访问入口为apiserver,那么我们实现集群的高可用其实只需实现apiserver的高可用即可,apiserver就是一个遵循restful api规范的web服务,我们可以用经典的keepalived+haproxy实现apiserver的高可用,那么集群其他组件的高可用怎么实现呢?幸好官方已经帮我们解决了这个问题,当一台机器作为master加入集群,k8s会在这台机器初始化master所需的必须组件scheduler,controller-manager,他们默认只会访问本机的apiserver(当然你也可以修改配置使其访问上层keepalived的虚拟ip经过haproxy负载均衡访问集群的某一台apiserver),组件etcd也会加入已有的集群并且同步已有数据。

假设我们有四台机器host-01,host-02,host-03,host-04内网ip为192.168.0.1/24,192.168.0.2/24,192.168.0.3/24,192.168.0.4/24

规划为host-01和host-02为集群master机器,host-03和host-04为集群node机器

我们将keepalive和haproxy安装在node机器,每一台node机器都安装keepalived+haproxy,

keepalived可以检测haproxy的存活,可以保持在haproxy存活的机器提供一个基于现有网卡的虚拟ip(其实就是网卡下面创建的子网卡),我们访问虚拟ip的haproxy端口其实就访问到了主网卡那台机器的haproxy,haproxy配置集群所有的apiserver的负载均衡即可实现keepalived+haproxy的apiserver的高可用

注:Keepalived 和 HAproxy 没有安装在任何主节点上。但您也可以这样做,注意haproxy的端口和apiserver的端口冲突,并同时实现高可用。然而,配置两个专用于负载均衡不在集群中的特定节点(您可以按需增加更多此类节点)会更加安全。这两个节点上只安装 Keepalived 和 HAproxy,以避免与任何 Kubernetes 组件和服务发生潜在冲突

1.安装集群的前置条件参考上篇博文中的步骤1-步骤12,需要在集群中每台机器执行

https://blog.csdn.net/qq_36595568/article/details/124179609?spm=1001.2014.3001.5502

2.安装keepalive和haproxy,host-03和host-04 都需要执行

(1)yum install keepalived haproxy psmisc -y

(2)配置keepalive和haproxy

配置haproxy:

vi /etc/haproxy/haproxy.cfg

global
    log /dev/log  local0 warning
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon
   
   stats socket /var/lib/haproxy/stats
   
defaults
  log global
  option  httplog
  option  dontlognull
        timeout connect 5000
        timeout client 50000
        timeout server 50000
   
frontend kube-apiserver
  bind *:6443          #haproxy监听端口,注意避免和已有端口发生冲突
  mode tcp
  option tcplog
  default_backend kube-apiserver
   
backend kube-apiserver
    mode tcp
    option tcplog
    option tcp-check
    balance roundrobin
    default-server inter 10s downinter 5s rise 2 fall 2 slowstart 60s maxconn 250 maxqueue 256 weight 100
    server kube-apiserver-1 192.168.0.1:6443 check  #host-01 apiserver 地址
    server kube-apiserver-2 192.168.0.2:6443 check  ##host-02 apiserver 地址
    

(3)配置keepalive

vi  /etc/keepalived/keepalived.conf

global_defs {
  notification_email {
  }
  router_id LVS_DEVEL
  vrrp_skip_check_adv_addr
  vrrp_garp_interval 0
  vrrp_gna_interval 0
}
   
vrrp_script chk_haproxy {
  script "killall -0 haproxy"
  interval 2
  weight 2
}
   
vrrp_instance haproxy-vip {
  state BACKUP
  priority 100
  interface eth0                       # 主网卡名
  virtual_router_id 60
  advert_int 1
  authentication {
    auth_type PASS
    auth_pass 1111
  }
  unicast_src_ip 192.169.0.1      # 本机地址,host-02此处应为192.169.0.2
  unicast_peer {
    192.168.0.2         # 安装了keepalive的其他机器列表,host-02此处应为192.169.0.1
  }
   
  virtual_ipaddress {
    192.168.0.5/24                  # 指定虚拟ip,注意和避免和现有内网发生ip冲突
  }
   
  track_script {
    chk_haproxy
  }
}

(4)启动服务且开机自启

systemctl enable keepalived haproxy --now

3.安装高可用集群(单条命令方式和配置文件方式)

单条命令:

kubeadm init --control-plane-endpoint "192.168.0.5:6443" --kubernetes-version "1.19.1" --pod-network-cidr "10.244.0.0/16"  --service-cidr "10.96.0.0/12" --token "abcdef.0123456789abcdef" --token-ttl "0" --image-repository registry.aliyuncs.com/google_containers --upload-certs 

注:--control-plane-endpoint 为虚拟ip地址,注意pod网段和service网段和已有内网冲突,执行以上后会在当前机器创建apiserver,并且在通过负载均衡找到当前机器的apiserver

配置文件:

# vim kubeadm-config.yaml        #内容如下
apiVersion: kubeadm.k8s.io/v1beta2
bootstrapTokens:
- groups:
  - system:bootstrappers:kubeadm:default-node-token
  token: abcdef.0123456789abcdef
  ttl: 24h0m0s
  usages:
  - signing
  - authentication
kind: InitConfiguration
localAPIEndpoint:
  advertiseAddress: 192.168.0.1   #本机IP
  bindPort: 6443
nodeRegistration:
  criSocket: /var/run/dockershim.sock
  name: host-01                  #本主机名
  taints:
  - effect: NoSchedule
    key: node-role.kubernetes.io/master
---
apiServer:
  timeoutForControlPlane: 4m0s
apiVersion: kubeadm.k8s.io/v1beta2
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
controlPlaneEndpoint: "192.168.0.5:6443"    #虚拟IP和haproxy端口
controllerManager: {}
dns:
  type: CoreDNS
etcd:
  local:
    dataDir: /var/lib/etcd       #etcd数据目录
imageRepository: registry.aliyuncs.com/google_containers    #镜像仓库源
kind: ClusterConfiguration
kubernetesVersion: v1.19.1    # k8s版本
networking:
  dnsDomain: cluster.local
  podSubnet: "10.244.0.0/16"     #pod网段地址,注意和已有内网冲突
  serviceSubnet: "10.96.0.0/12"  # service网段地址,注意和已有内网冲突
scheduler: {}

--- #此部分为可选内容,如果开启ipvs内核路由转发,可以添加如下
apiVersion: kubeproxy.config.k8s.io/v1alpha1
kind: KubeProxyConfiguration
featureGates:
  SupportIPVSProxyMode: true
mode: ipvs

执行

kubeadm init --config kubeadm-config.yaml --upload-certs

执行成功会出现如下示例:

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA546W44CH5ZCO5aSn5Y-U,size_20,color_FFFFFF,t_70,g_se,x_16

当出现如图结果,说明初始化成功。

1:此处命令用于配置kubectl;

2:此处命令用于添加master节点,去host-02执行;

3:此处命令用于添加node节点,去host-03和host04执行;

添加完其他master以及node,host-01执行kubectl get nodes以及kubectl get pods -n kube-system -o wide node ,所有node 处于notready 以及coredns pod 处于pending状态,因为没有安装网络插件

4. 安装网络插件,此处我们选择flannel插件

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

注:如果初始化集群pod网段更换,注意修改kube-flannel.yml中pod中pod网段地址

host-01执行kubectl get nodes以及kubectl get pods -n kube-system -o wide node当所有node状态为ready以及coredns pod状态为running 后,恭喜你,高可用集群安装成功啦 

将host-01的kubectl配置文件/root/.kube/config  scp至其他节点的/root/.kube/config,其他节点也可用kubectl工具操作集群了

后期加入master以及node步骤如下:

加入host-06 192.168.0.6作为node:

  在host-01执行

kubeadm token create --print-join-command

得到kubeadm join输出:

kubeadm join 192.168.0.5:6443 --token 5jdu6p.drdy94q6gvavolzv     --discovery-token-ca-cert-hash sha256:ae9eb4b1aac10c11a01c753ba1d7d197113c8fdc4e7b088ede19931738d85af4 

在host-06执行即可,前提完成了步骤1-12的前置步骤

加入host-06 192.168.0.6作为master:

(1)在host-01执行

kubeadm init phase upload-certs --upload-certs

得到Using certificate key的输出

906c53cfbd44f91c5e77efae9c6d1c13d19dfc40a09e6d75f5a146a0fd50d154

拼接kubeadm join的输出,如下

kubeadm join 192.168.0.5:6443 --token apzooq.r9zivb2vaakn72k7 --discovery-token-ca-cert-hash sha256:255fc6a65316054aa5f1bfd92467ebce52f557983f5e901663a3a460af2c60b5 --control-plane --certificate-key 911682ede59903ec2f7968e64110dfaea58e0c0abbab01e01ab5dc45f17d1d7b

在host-06执行即可,前提完成了步骤1-12的前置步骤

(2)修改host-03 host-04的haproxy配置文件增加host-06的负载均衡

global
    log /dev/log  local0 warning
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon
   
   stats socket /var/lib/haproxy/stats
   
defaults
  log global
  option  httplog
  option  dontlognull
        timeout connect 5000
        timeout client 50000
        timeout server 50000
   
frontend kube-apiserver
  bind *:6443          #haproxy监听端口,注意避免和已有端口发生冲突
  mode tcp
  option tcplog
  default_backend kube-apiserver
   
backend kube-apiserver
    mode tcp
    option tcplog
    option tcp-check
    balance roundrobin
    default-server inter 10s downinter 5s rise 2 fall 2 slowstart 60s maxconn 250 maxqueue 256 weight 100
    server kube-apiserver-1 192.168.0.1:6443 check  #host-01 apiserver 地址
    server kube-apiserver-2 192.168.0.2:6443 check  ##host-02 apiserver 地址
    server kube-apiserver-3 192.168.0.6:6443 check  ##host-06 apiserver 地址
    

(3)重启host-03 host-04 haproxy服务

systemctl restart haproxy

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐