首页 > 其他分享 >K8S集群(高可用)搭建详细过程

K8S集群(高可用)搭建详细过程

时间:2022-12-21 15:31:15浏览次数:49  
标签:haproxy k8s kubernetes -- etc 集群 kubeadm K8S 搭建

多master(高可用)

K8S集群(高可用)搭建详细过程_Docker

1. 安装要求

在开始之前,部署Kubernetes集群机器需要满足以下几个条件:

  • 一台或多台机器,操作系统
  •  硬件配置:2GB或更多RAM,2个CPU或更多CPU,硬盘30GB或更多
  • 可以访问外网,需要拉取镜像,如果服务器不能上网,需要提前下载镜像并导入节点
  •  禁止swap分区

2. 准备环境(所有节点操作)

角色

IP

VIP(虚拟ip)

192.168.169.50

k8s-master1

192.168.169.51

k8s-master2

192.168.169.52

k8s-node1

192.168.169.53

k8s-node2

192.168.169.54

# 关闭防火墙

systemctl stop firewalld

systemctl disable firewalld

# 关闭selinux

sed -i 's/enforcing/disabled/' /etc/selinux/config  # 永久

setenforce 0  # 临时

# 关闭swap

swapoff -a  # 临时

sed -ri 's/.*swap.*/#&/' /etc/fstab    # 永久

# 根据规划设置主机名

hostnamectl set-hostname <hostname>

# 在master添加hosts

K8S集群(高可用)搭建详细过程_Docker_02

# 将桥接的IPv4流量传递到iptables的链

K8S集群(高可用)搭建详细过程_Docker_03

sysctl --system  # 生效 

# 时间同步

yum install ntpdate -y

ntpdate time.windows.com

3. 所有master节点部署keepalived

3.1 安装相关包和keepalived

yum install -y conntrack-tools libseccomp libtool-ltdl

yum install -y keepalived

3.2配置master节点

master1节点配置

vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived

 

global_defs {

   router_id k8s

}

 

vrrp_script check_haproxy {

    script "killall -0 haproxy"

    interval 3

    weight -2

    fall 10

    rise 2

}

 

vrrp_instance VI_1 {

    state MASTER

    interface ens33

    virtual_router_id 51

    priority 250

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass ceb1b3ec013d66163d6ab

    }

    virtual_ipaddress {

        192.168.169.50

    }

    track_script {

        check_haproxy

    } 

}

master2节点配置

vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived

 

global_defs {

   router_id k8s

}

 

vrrp_script check_haproxy {

    script "killall -0 haproxy"

    interval 3

    weight -2

    fall 10

    rise 2

}

 

vrrp_instance VI_1 {

    state BACKUP

    interface ens33

    virtual_router_id 51

    priority 200

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass ceb1b3ec013d66163d6ab

    }

    virtual_ipaddress {

        192.168.169.50

    }

    track_script {

        check_haproxy

    }

3.3 启动和检查

在两台master节点都执行 

# 启动keepalived

$ systemctl start keepalived.service

设置开机启动

$ systemctl enable keepalived.service

# 查看启动状态

$ systemctl status keepalived.service

启动后查看master1的网卡信息

ip a s ens33

4. 部署haproxy

4.1 安装

yum install -y haproxy

4.2 配置

两台master节点的配置均相同,配置中声明了后端代理的两个master节点服务器,指定了haproxy运行的端口为16443等,因此16443端口为集群的入口 

vi /etc/haproxy/haproxy.cfg

#---------------------------------------------------------------------

# Global settings

#---------------------------------------------------------------------

global

    # to have these messages end up in /var/log/haproxy.log you will

    # need to:

    # 1) configure syslog to accept network log events.  This is done

    #    by adding the '-r' option to the SYSLOGD_OPTIONS in

    #    /etc/sysconfig/syslog

    # 2) configure local2 events to go to the /var/log/haproxy.log

    #   file. A line like the following can be added to

    #   /etc/sysconfig/syslog

    #

    #    local2.*                       /var/log/haproxy.log

    #

    log         127.0.0.1 local2

   

    chroot      /var/lib/haproxy

    pidfile     /var/run/haproxy.pid

    maxconn     4000

    user        haproxy

    group       haproxy

    daemon

      

    # turn on stats unix socket

    stats socket /var/lib/haproxy/stats

#---------------------------------------------------------------------

# common defaults that all the 'listen' and 'backend' sections will

# use if not designated in their block

#--------------------------------------------------------------------- 

defaults

    mode                    http

    log                     global

    option                  httplog

    option                  dontlognull

    option http-server-close

    option forwardfor       except 127.0.0.0/8

    option                  redispatch

    retries                 3

    timeout http-request    10s

    timeout queue           1m

    timeout connect         10s

    timeout client          1m

    timeout server          1m

    timeout http-keep-alive 10s

    timeout check           10s

    maxconn                 3000

#---------------------------------------------------------------------

# kubernetes apiserver frontend which proxys to the backends

#---------------------------------------------------------------------

frontend kubernetes-apiserver

    mode                 tcp

    bind                 *:16443

    option               tcplog

    default_backend      kubernetes-apiserver   

#---------------------------------------------------------------------

# round robin balancing between the various backends

#---------------------------------------------------------------------

backend kubernetes-apiserver

    mode        tcp

    balance     roundrobin

    server      k8s-master1   192.168.169.51:6443 check

    server      k8s-master2   192.168.169.52:6443 check

#---------------------------------------------------------------------

# collection haproxy statistics message

#---------------------------------------------------------------------

listen stats

    bind                 *:1080

    stats auth           admin:awesomePassword

    stats refresh        5s

    stats realm          HAProxy\ Statistics

    stats uri            /admin?stats

4.3 启动和检查

两台master都启动

# 设置开机启动

systemctl enable haproxy

# 开启haproxy

systemctl start haproxy

# 查看启动状态

$ systemctl status haproxy

haproxy 启动错误

​ALERT] 106/093950 (115136) : Starting proxy short_message: cannot bind socket

问题1,如果bind的是vip,则需要内核添加配置/etc/sysctl.conf ,sysctl -p

net.ipv4.ip_nonlocal_bind=1

问题2,haproxy连接策略问题

执行

setsebool -P haproxy_connect_any=1

​检查端口

netstat -lntup|grep haproxy

5. 所有节点安装Docker/kubeadm/kubelet

Kubernetes默认CRI(容器运行时)为Docker,因此先安装Docker。

5.1 安装Docker

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

$ yum -y install docker-ce-18.06.1.ce-3.el7

$ systemctl enable docker && systemctl start docker

$ docker --version

Docker version 18.06.1-ce, build e68fc7a

$ cat > /etc/docker/daemon.json << EOF

{

  "registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"]

}

EOF

5.2 添加阿里云YUM软件源

$ cat > /etc/yum.repos.d/kubernetes.repo << EOF

[kubernetes]

name=Kubernetes

baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64

enabled=1

gpgcheck=0

repo_gpgcheck=0

gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg

EOF

 

5.3 安装kubeadm,kubelet和kubectl 

由于版本更新频繁,这里指定版本号部署:

$ yum install -y kubelet-1.16.3 kubeadm-1.16.3 kubectl-1.16.3

$ systemctl enable kubelet

6. 部署Kubernetes Master

6.1 创建kubeadm配置文件

在具有vip的master上操作,这里为master1

$ mkdir /usr/local/kubernetes/manifests -p

$ cd /usr/local/kubernetes/manifests/

$ vi kubeadm-config.yaml

apiServer:

  certSANs:

    - k8s-master1

    - k8s-master2

    - k8s-vip

    - 192.168.169.50

    - 192.168.169.51

    - 192.168.169.52

    - 127.0.0.1

  extraArgs:

    authorization-mode: Node,RBAC

  timeoutForControlPlane: 4m0s

apiVersion: kubeadm.k8s.io/v1beta1

certificatesDir: /etc/kubernetes/pki

clusterName: kubernetes

controlPlaneEndpoint: "k8s-vip:16443"

controllerManager: {}

dns:

  type: CoreDNS

etcd:

  local:   

    dataDir: /var/lib/etcd

imageRepository: registry.aliyuncs.com/google_containers

kind: ClusterConfiguration

kubernetesVersion: v1.16.3

networking:

  dnsDomain: cluster.local 

  podSubnet: 10.244.0.0/16

  serviceSubnet: 10.1.0.0/16

scheduler: {}

6.2 在master1节点执行

$ kubeadm init --config kubeadm-config.yaml    (时间长。。。)

K8S集群(高可用)搭建详细过程_docker_04

**按照提示保存以下内容,一会要使用:

kubeadm join k8s-vip:16443 --token r7bcyr.5qsuutg6agg2y26r \

--discovery-token-ca-cert-hash sha256:0477cf0d297bfb040b45459354481dffffd756bb66cd6f09b527b109330b21e9

按照提示配置环境变量,使用kubectl工具:

bash

mkdir -p $HOME/.kube

cp -i /etc/kubernetes/admin.conf $HOME/.kube/config

chown $(id -u):$(id -g) $HOME/.kube/config

kubectl get nodes

K8S集群(高可用)搭建详细过程_Docker_05

kubectl get pods -n kube-system 

查看集群状态

kubectl get cs

kubectl get pods -n kube-system

7.安装集群网络

从官方地址获取到flannel的yaml,在master1上执行

wget -c ​https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml​

K8S集群(高可用)搭建详细过程_docker_06

安装flannel网络

kubectl apply -f kube-flannel.yml

K8S集群(高可用)搭建详细过程_Docker_07

检查

kubectl get pods -n kube-system

K8S集群(高可用)搭建详细过程_Docker_08

8、master2节点加入集群

8.1 复制密钥及相关文件

从master1复制密钥及相关文件到master2

ssh [email protected] mkdir -p /etc/kubernetes/pki/etcd

scp /etc/kubernetes/admin.conf [email protected]:/etc/kubernetes  

scp /etc/kubernetes/pki/{ca.*,sa.*,front-proxy-ca.*} [email protected]:/etc/kubernetes/pki  

scp /etc/kubernetes/pki/etcd/ca.* [email protected]:/etc/kubernetes/pki/etcd

8.2 master2加入集群

执行在master1上init后输出的join命令,需要带上参数`--control-plane`表示把master控制节点加入集群

kubeadm join k8s-vip:16443 --token r7bcyr.5qsuutg6agg2y26r   --discovery-token-ca-cert-hash sha256:0477cf0d297bfb040b45459354481dffffd756bb66cd6f09b527b109330b21e9  --control-plane

检查状态

kubectl get node

K8S集群(高可用)搭建详细过程_Docker_09

kubectl get pods --all-namespaces

K8S集群(高可用)搭建详细过程_Docker_10

9. 加入Kubernetes Node

在node1/2上执行

向集群添加新节点,执行在kubeadm init输出的kubeadm join命令:

kubeadm join k8s-vip:16443 --token r7bcyr.5qsuutg6agg2y26r \

    --discovery-token-ca-cert-hash sha256:0477cf0d297bfb040b45459354481dffffd756bb66cd6f09b527b109330b21e9

**集群网络重新安装,因为添加了新的node节点**

检查状态

kubectl get node

K8S集群(高可用)搭建详细过程_docker_11

kubectl get pods --all-namespaces

K8S集群(高可用)搭建详细过程_Docker_12

10. 测试kubernetes集群

在Kubernetes集群中创建一个pod,验证是否正常运行:

$ kubectl create deployment nginx --image=nginx

$ kubectl expose deployment nginx --port=80 --type=NodePort

K8S集群(高可用)搭建详细过程_Docker_13

$ kubectl get pod,svc

K8S集群(高可用)搭建详细过程_Docker_14

访问地址:http://NodeIP:Port   

仪表盘:

kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-beta4/aio/deploy/recommended.yaml


标签:haproxy,k8s,kubernetes,--,etc,集群,kubeadm,K8S,搭建
From: https://blog.51cto.com/u_14572091/5959943

相关文章

  • 使用Keepalived工具实现集群节点的高可用
    GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源。GreatSQL是MySQL的国产分支版本,使用上与MySQL一致。作者:蟹黄瓜子文章来源:社区投稿1.前言在集......
  • kafka集群配置文件修改
    broker.id=2listeners=PLAINTEXT://192.168.49.222:9092#每个节点的host不同,默认是注释的log.dirs=/opt/module/kafka/datas#自定义路径zookeeper.connect=192.168......
  • Web集群案例实战 -- Nginx 反向代理 -- 案例实战
    Nginx反向代理--案例实战​​前言​​前言本环境是基于Centos7.8系统构建Nginx学习环境具体构建,请参考​​Nginx-1.18.0环境部署​​环境准备rolehostipnginx-vers......
  • Web集群案例实战 -- Nginx 负载均衡 -- 案例实战
    Nginx负载均衡--案例实战​​前言​​前言本环境是基于Centos7.8系统构建Nginx学习环境具体构建,请参考​​Nginx-1.18.0环境部署​​环境准备rolehostipnginx-vers......
  • Web集群案例实战 -- Nginx 负载均衡 之 客户端访问日志优化
    Nginx负载均衡之客户端访问日志优化​​前言​​前言本环境是基于Centos7.8系统构建Nginx学习环境具体构建,请参考​​Nginx-1.18.0环境部署​​Nginx在做反向代理......
  • Linux服务搭建--DHCP
    1.什么是DHCP服务?DHCP(动态主机配置协议):是局域网常见的网络协议,它主要是通过客户端发送广播数据包给整个物理网段内的所有主机,若局域网内存在DHCP服务器,该服务器收到广播包,才......
  • MySQL高可用复制管理工具 —— Orchestrator简介及基本搭建
    1、背景 Orchestrator(orch):go编写的MySQL高可用性和复制拓扑管理工具,支持复制拓扑结构的调整,自动故障转移和手动主从切换等。后端数据库用MySQL或SQLite存储元数据,并提供W......
  • Docker高级篇:实战Redis集群!从3主3从变为4主4从
    通过前面两篇,我们学会了三主三从的Redis集群搭建及主从容错切换迁移,随着业务增加,可能会有主从扩容的,所以,本文我们来实战主从扩容PS本系列:《Docker学习系列》教程已经发布......
  • Spark远程集群实现
    内容简介本篇文章主要讲述了Springboot项目和Spark相结合的方法。在Spark的学习过程中,我们大多数提交作业的方式都是使用Spark的命令来提交jar包并运行,但是本次相反,我们......
  • tomcat优化-有改protocol 和 缓存 集群方案
     tomcat优化在线上环境中我们是采用了tomcat作为Web服务器,它的处理性能直接关系到用户体验,在平时的工作和学习中,归纳出以下七种调优经验。1.服务器资源  服务器所能提......