Kubernetes(k8s)高可用搭建

目录

一、系统基础设置

1.1、关闭防火墙

sh 复制代码
systemctl stop firewalld
systemctldisable firewalld

1.2、关闭selinux

sh 复制代码
sed -i 's/enforcing/disabled/' /etc/selinux/config  # 永久
setenforce 0  # 临时

1.3、关闭swap

sh 复制代码
swapoff -a  # 临时
sed -ri 's/.*swap.*/#&/' /etc/fstab    # 永久

1.4、设置hostname

sh 复制代码
hostnamectl set-hostname zxhy-master 

cat >> /etc/hosts << EOF
192.168.0.15 zxhy-vip
192.168.0.14 zxhy-master
192.168.0.222 zxhy-slave1
192.168.0.77 zxhy-slave2
192.168.0.188 zxhy-slave3
192.168.0.193 zxhy-slave4
192.168.0.227 zxhy-slave5
EOF

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

sh 复制代码
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system  # 生效

1.6、 时间同步

sh 复制代码
yum install ntpdate -y
ntpdate time.windows.com

1.7、 安装iproute-tc

sh 复制代码
yum install iproute-tc -y

二、所有master节点部署keepalived

2.1 安装相关包和keepalived

sh 复制代码
yum install -y conntrack-tools libseccomp libtool-ltdl
yum install -y keepalived

2.2配置master节点

master1节点配置

sh 复制代码
cat > /etc/keepalived/keepalived.conf <<EOF 
! 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 eth0
    virtual_router_id 51
    priority 250
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass ceb1b3ec013d66163d6ab
    }
    virtual_ipaddress {
        192.168.0.15
    }
    track_script {
        check_haproxy
    }

}
EOF

master2节点配置

sh 复制代码
cat > /etc/keepalived/keepalived.conf <<EOF 
! 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 eth0
    virtual_router_id 51
    priority 250
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass ceb1b3ec013d66163d6ab
    }
    virtual_ipaddress {
        192.168.0.14
    }
    track_script {
        check_haproxy
    }

}
EOF

2.3 启动和检查

在三台master节点都执行

sh 复制代码
# 启动keepalived
$ systemctl start keepalived.service
设置开机启动
$ systemctl enable keepalived.service
# 查看启动状态
$ systemctl status keepalived.service

启动后查看master1的网卡信息

sh 复制代码
ip a s eth0

如果是云服务器搭建的话,记得云服务器管理平台上申请虚拟IP地址,然后绑定虚拟IP地址到三台主节点服务器上,然后添加相应的网络策略,否侧ping不通虚拟ip

三、 部署haproxy

3.1 安装

sh 复制代码
yum install -y haproxy

3.2 配置

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

sh 复制代码
cat > /etc/haproxy/haproxy.cfg << EOF
#---------------------------------------------------------------------
# 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      zxhy-nacos   192.168.0.14:6443 check
    server      zxhy-redis   192.168.0.77:6443 check
    server      zxhy-mysql   192.168.0.222: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
EOF

3.3 启动和检查

三台master都启动

sh 复制代码
# 设置开机启动
$ systemctl enable haproxy
# 开启haproxy
$ systemctl start haproxy
# 查看启动状态
$ systemctl status haproxy

四、 所有节点安装Docker/kubeadm/kubelet

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

4.1 安装Docker

sh 复制代码
$ 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-24.0.5.el7
$ systemctl enable docker && systemctl start docker
$ docker --version
Docker version 24.0.5, build e68fc7a
sh 复制代码
$ cat > /etc/docker/daemon.json << EOF
{
  "registry-mirrors": ["https://6f09bd673d1d4e8d98dab0ab278fc7c2.mirror.swr.myhuaweicloud.com"]
}
EOF

4.2 cri-dockerd安装

下载 cri-dockerd 安装包

sh 复制代码
cd  /opt
wget https://github.com/Mirantis/cri-dockerd/releases/download/v0.3.6/cri-dockerd-0.3.6.20231018204925.877dc6a4-0.el7.x86_64.rpm

安装服务

sh 复制代码
yum install -y cri-dockerd-0.3.6.20231018204925.877dc6a4-0.el7.x86_64.rpm
vim /usr/lib/systemd/system/cri-docker.service
#添加镜像源
--pod-infra-container-image=registry.aliyuncs.com/google_containers/pause:3.7

systemctl daemon-reload
vim /usr/lib/systemd/system/cri-docker.socket

查看服务启动状态

sh 复制代码
# 设置开机启动
$ systemctl enable cri-docker
# 开启cri-docker
$ systemctl start cri-docker
# 查看启动状态
$ systemctl status cri-docker

查看CRI服务是否被禁止

sh 复制代码
vi /etc/containerd/config.toml 
#如果disabled_plugins中包含cri,删除"cri"即可
#disabled_plugins = ["cri"]
disabled_plugins = []

重启容器运行时

sh 复制代码
systemctl restart containerd 

4.3 添加阿里云YUM软件源

sh 复制代码
$ 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

4.4 安装kubeadm,kubelet和kubectl

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

sh 复制代码
$ yum install -y kubelet-1.24.7 kubeadm-1.24.7 kubectl-1.24.7
$ systemctl enable kubelet

4.4 安装kubeadm-cni

network plugin is not ready: cni config uninitialized

五、部署Kubernetes Master

5.1 创建kubeadm配置文件

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

sh 复制代码
$ mkdir /usr/local/kubernetes/manifests -p

$ cd /usr/local/kubernetes/manifests/

$ vi kubeadm-config.yaml

apiVersion: kubeadm.k8s.io/v1beta3
kind: InitConfiguration
bootstrapTokens:
- groups:
  - system:bootstrappers:kubeadm:default-node-token
  token: abcdef.0123456789abcdef
  ttl: 24h0m0s
  usages:
  - signing
  - authentication
localAPIEndpoint:
  advertiseAddress: 192.168.0.14
  bindPort: 6443
nodeRegistration:
  criSocket: unix:///var/run/cri-dockerd.sock
  imagePullPolicy: IfNotPresent
  name: zxhy-nacos
  taints: null
---
apiServer:
  certSANs:
    - zxhy-nacos
    - zxhy-redis
    - zxhy-mysql
    - zxhy-vip
    - 192.168.0.14
    - 192.168.0.222
    - 192.168.0.77
    - 192.168.0.15
    - 127.0.0.1
  extraArgs:
    authorization-mode: Node,RBAC
  timeoutForControlPlane: 4m0s
apiVersion: kubeadm.k8s.io/v1beta3
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
controlPlaneEndpoint: "zxhy-vip:16443"
controllerManager: {}
dns: {}
etcd:
  local:    
    dataDir: /var/lib/etcd
imageRepository: registry.aliyuncs.com/google_containers
kind: ClusterConfiguration
kubernetesVersion: v1.24.7
networking: 
  dnsDomain: cluster.local  
  podSubnet: 10.244.0.0/16
  serviceSubnet: 10.1.0.0/16
scheduler: {}

5.2 在master1节点执行

sh 复制代码
$ kubeadm init --config kubeadm-config.yaml

export KUBECONFIG=/etc/kubernetes/admin.conf

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

bash 复制代码
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
$ kubectl get nodes
$ kubectl get pods -n kube-system

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

bash 复制代码
kubeadm join zxhy-vip:16443 --token gp4qgj.3x8wal0o2gmbcpis --discovery-token-ca-cert-hash sha256:af5fe3bb4f2ada51967c34053e94ed4c703287e3e26487d6d8dbe450a2550013 --cri-socket=unix:///var/run/cri-dockerd.sock
#如果忘记复制,也可以利用这个命令重新生成下加入命令
kubeadm token create --print-join-command

六、安装集群网络

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

bash 复制代码
mkdir /usr/local/kubernetes/manifests/flannel
cd /usr/local/kubernetes/manifests/flannel
wget -c https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

安装flannel网络

bash 复制代码
kubectl apply -f kube-flannel.yml 

检查

bash 复制代码
kubectl get pods -n kube-system

七、master2节点加入集群

7.1 复制密钥及相关文件

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

bash 复制代码
# ssh root@192.168.0.222 mkdir -p /etc/kubernetes/pki/etcd

# scp /etc/kubernetes/admin.conf root@192.168.0.222:/etc/kubernetes
   
# scp /etc/kubernetes/pki/{ca.*,sa.*,front-proxy-ca.*} root@192.168.0.222:/etc/kubernetes/pki
   
# scp /etc/kubernetes/pki/etcd/ca.* root@192.168.0.222:/etc/kubernetes/pki/etcd

7.2 master2加入集群

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

kubeadm join zxhy-vip:16443 --token gp4qgj.3x8wal0o2gmbcpis --discovery-token-ca-cert-hash sha256:af5fe3bb4f2ada51967c34053e94ed4c703287e3e26487d6d8dbe450a2550013 --control-plane --cri-socket=unix:///var/run/cri-dockerd.sock

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

bash 复制代码
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
$ kubectl get nodes
$ kubectl get pods -n kube-system

7.3master3加入集群

跟节点2同样的操作

7.4检查状态

kubectl get node

kubectl get pods --all-namespaces

八、加入Kubernetes Node

8.1在node1、node2、node3上执行join命令

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

kubeadm join zxhy-vip:16443 --token gp4qgj.3x8wal0o2gmbcpis --discovery-token-ca-cert-hash sha256:af5fe3bb4f2ada51967c34053e94ed4c703287e3e26487d6d8dbe450a2550013 --cri-socket=unix:///var/run/cri-dockerd.sock

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

所有节点加入完成后安装flannel网络

bash 复制代码
#进入flannel网络
cd /usr/local/kubernetes/manifests/flannel
#删除之前的网络
kubectl delete -f kube-flannel.yml 
#重新初始化的网络
kubectl apply -f kube-flannel.yml 

8.3检查状态

kubectl get node

kubectl get pods --all-namespaces

九、测试kubernetes集群

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

$ kubectl create deployment nginx --image=nginx
$ kubectl expose deployment nginx --port=80 --type=NodePort
$ kubectl get pod,svc
相关推荐
南猿北者5 小时前
Docker Volume
运维·docker·容器
网络研究院7 小时前
Am I Isolated:一款安全态势基准测试工具
容器·工具·基准测试·安全态势
颜淡慕潇11 小时前
【K8S问题系列 | 9】如何监控集群CPU使用率并设置告警?
后端·云原生·容器·kubernetes·问题解决
运维&陈同学11 小时前
【模块一】kubernetes容器编排进阶实战之k8s基础概念
运维·docker·云原生·容器·kubernetes·云计算
信子姚青11 小时前
K8s使用nfs
容器
葡萄皮Apple11 小时前
[CKS] K8S RuntimeClass SetUp
java·容器·kubernetes
mit6.82412 小时前
[Docker#4] 镜像仓库 | 部分常用命令
linux·运维·docker·容器·架构
诡异森林。14 小时前
Docker--Docker是什么和对Docker的了解
运维·docker·容器
老大白菜14 小时前
goframe开发一个企业网站 验证码17
运维·docker·容器·golang·goframe
研究司马懿16 小时前
【Golang】Go语言环境安装
开发语言·后端·云原生·golang·二开