K8S集群搭建

K8S集群搭建

1. 基础环境

IP节点名称域名规格
10.1.2.111k8s-master-1master01.k8s.io4核8G,50G
10.1.2.112k8s-master-2master02.k8s.io4核8G,50G
10.1.2.113k8s-cluster-endpoint,没有具体节点,只vipmaster.k8s.io
10.1.2.180k8s-node-18核16G,50G
10.1.2.181k8s-node-28核16G,50G
10.1.2.182k8s-node-38核16G,50G
10.1.2.190k8s-harborharbor.k8s.io4核8G,100G
10.1.2.191k8s-nfs-servernfs.k8s.io4核8G,300G

集群架构图如下:
在这里插入图片描述
配置集群主要步骤包括:

  1. kubeadm做集群初始化

  2. 需要安装keepalived,配置master的vip

  3. 安装haproxy,配置master的ha

  4. 初始化master1节点

  5. 配置calico网络,配置网络之后才能进行节点加入,包括master节点和node节点都需要先配置网络

  6. 初始化master2节点

  7. 加入node节点

2. kubeadm构建集群

构建k8s集群

kubeadm init \
--apiserver-advertise-address=10.1.2.111 \
--control-plane-endpoint=k8s-cluster-endpoint \
--image-repository registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images \
--kubernetes-version v1.20.9 \
--service-cidr=10.96.0.0/16 \
--pod-network-cidr=192.168.0.0/16

上述

  • “–apiserver-advertise-address=10.1.2.111”改成nodeIP

  • “–pod-network-cidr=192.168.0.0/16”改的是pod的网段,外面访问不了,直接192就可以

  • “–service-cidr=10.96.0.0/16”改的是service的网段,是一个虚IP,因此

  • “–control-plane-endpoint=k8s-cluster-endpoint”,控制节点,实际上是master的管理域名,即hosts里面的那个域名,我这个地方填的是vip的域名

  • “–image-repository”,默认的k8s镜像获取地址

3. 安装keepalived

安装:

# 依赖
yum install -y conntrack-tools libseccomp libtool-ltdl
# 安装keepalived
yum install -y keepalived

master1的配置文件:

cat > /etc/keepalived/keepalived.conf <<EOF 
! Configuration File for keepalivedglobal_defs {router_id k8s
}vrrp_script check_haproxy {script "killall -0 haproxy"interval 3weight -2fall 10rise 2
}vrrp_instance VI_1 {state MASTER interface eth0 virtual_router_id 51priority 250advert_int 1authentication {auth_type PASSauth_pass ceb1b3ec013d66163d6ab}virtual_ipaddress {10.1.2.113}track_script {check_haproxy}}
EOF

master2的配置文件:

cat > /etc/keepalived/keepalived.conf <<EOF 
! Configuration File for keepalivedglobal_defs {router_id k8s
}vrrp_script check_haproxy {script "killall -0 haproxy"interval 3weight -2fall 10rise 2
}vrrp_instance VI_1 {state BACKUP interface eth0 virtual_router_id 51priority 200advert_int 1authentication {auth_type PASSauth_pass ceb1b3ec013d66163d6ab}virtual_ipaddress {10.1.2.113}track_script {check_haproxy}}
EOF
# 启动keepalived
systemctl start keepalived.service
# 设置开机启动
systemctl enable keepalived.service
# 查看启动状态
systemctl status keepalived.service
ip a s eth0

4. 安装haproxy

# 安装haproxy
yum install -y haproxy
# 启动 haproxy
systemctl start haproxy
# 开启自启
systemctl enable haproxy
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 local2chroot      /var/lib/haproxypidfile     /var/run/haproxy.pidmaxconn     4000user        haproxygroup       haproxydaemon # turn on stats unix socketstats socket /var/lib/haproxy/stats
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------  
defaultsmode                    httplog                     globaloption                  httplogoption                  dontlognulloption http-server-closeoption forwardfor       except 127.0.0.0/8option                  redispatchretries                 3timeout http-request    10stimeout queue           1mtimeout connect         10stimeout client          1mtimeout server          1mtimeout http-keep-alive 10stimeout check           10smaxconn                 3000
#---------------------------------------------------------------------
# kubernetes apiserver frontend which proxys to the backends
#--------------------------------------------------------------------- 
frontend kubernetes-apiservermode                 tcpbind                 *:16443option               tcplogdefault_backend      kubernetes-apiserver    
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend kubernetes-apiservermode        tcpbalance     roundrobinserver      master01.k8s.io   10.1.2.111:6443 checkserver      master02.k8s.io   10.1.2.112:6443 check
#---------------------------------------------------------------------
# collection haproxy statistics message
#---------------------------------------------------------------------
listen statsbind                 *:1080stats auth           admin:awesomePasswordstats refresh        5sstats realm          HAProxy\ Statisticsstats uri            /admin?stats
EOF

5. 初始化master1节点

# 创建文件夹
mkdir /usr/local/kubernetes/manifests -p
# 到manifests目录
cd /usr/local/kubernetes/manifests/
# 新建yaml文件
vi kubeadm-config.yaml

kubeadm-config.yaml文件内容:

apiServer:certSANs:- guankong-ctrl-k8s01- guankong-ctrl-k8s02- k8s-cluster-endpoint- 10.1.2.111- 10.1.2.112- 10.1.2.113- 127.0.0.1extraArgs:authorization-mode: Node,RBACtimeoutForControlPlane: 4m0s
apiVersion: kubeadm.k8s.io/v1beta2
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
controlPlaneEndpoint: "master.k8s.io:16443"
controllerManager: {}
dns: type: CoreDNS
etcd:local:    dataDir: /var/lib/etcd
imageRepository: registry.aliyuncs.com/google_containers
kind: ClusterConfiguration
kubernetesVersion: v1.20.9
networking: dnsDomain: cluster.local  podSubnet: 10.244.0.0/16serviceSubnet: 10.1.0.0/16
scheduler: {}

然后执行master节点init:

# 查看kubelet发现不对,需要更改变为正确
swapoff -a
systemctl daemon-reload
kubeadm init --config kubeadm-config.yaml --ignore-preflight-errors=Swap

然后得到如下结果:

Your Kubernetes control-plane has initialized successfully!To start using your cluster, you need to run the following as a regular user:mkdir -p $HOME/.kubesudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/configsudo chown $(id -u):$(id -g) $HOME/.kube/configAlternatively, if you are the root user, you can run:export KUBECONFIG=/etc/kubernetes/admin.confYou should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:https://kubernetes.io/docs/concepts/cluster-administration/addons/You can now join any number of control-plane nodes by copying certificate authorities
and service account keys on each node and then running the following as root:kubeadm join master.k8s.io:16443 --token 8ya83c.0aviiwbjb1eorrp1 \--discovery-token-ca-cert-hash sha256:fded3b3f22275c13a0637cd7e2592f6ec7b5e9cc8d611c01a37d20a36c4748d0 \--control-plane Then you can join any number of worker nodes by running the following on each as root:kubeadm join master.k8s.io:16443 --token 8ya83c.0aviiwbjb1eorrp1 \--discovery-token-ca-cert-hash sha256:fded3b3f22275c13a0637cd7e2592f6ec7b5e9cc8d611c01a37d20a36c4748d0 

查看集群状态:

# 查看集群状态
kubectl get cs
# 查看pod
kubectl get pods -n kube-system

给节点添加label,用于亲和性调度

kubectl label node k8s-master-1 kubernetes.io/node=master1

6. 配置calico网络

配置了k8s的master1之后,需要添加k8s网络,不然节点加入时候无法互通。

配置calico网络:

mkdir /usr/local/kubernetes/manifests/calico -p
cd /usr/local/kubernetes/manifests/calico
wget https://docs.projectcalico.org/v3.14/manifests/calico.yaml --no-check-certificate

修改calico.yaml:
修改pod网段,查看pod cidr:

kubectl get nodes -o jsonpath='{.items[*].spec.podCIDR}'
# 加到calico的网段上
# 把calico.yaml里pod所在网段改成kubeadm init时选项--pod-network-cidr所指定的网段,
# 直接用vim编辑打开此文件查找192,按如下标记进行修改:
# no effect. This should fall within `--cluster-cidr`.
# - name: CALICO_IPV4POOL_CIDR
#   value: "192.168.0.0/16"
# Disable file logging so `kubectl logs` works.
- name: CALICO_DISABLE_FILE_LOGGING
value: "true"# 把两个#及#后面的空格去掉,并把192.168.0.0/16改成10.244.0.0/16
# no effect. This should fall within `--cluster-cidr`.
- name: CALICO_IPV4POOL_CIDR
value: "10.244.0.0/16"
# Disable file logging so `kubectl logs` works.
- name: CALICO_DISABLE_FILE_LOGGING
value: "true"

安装calico网络:

kubectl apply -f calico.yaml

等拉完镜像了,可以查看到节点已经准备完毕

 [root@guankong-ctrl-k8s01 calico]#  kubectl get nodes
NAME                  STATUS   ROLES                  AGE   VERSION
k8s-master-1  Ready    control-plane,master   35m   v1.20.9

7. 初始化master2节点

复制master1上的文件到master2中:

ssh root@55.241.105.95 mkdir -p /etc/kubernetes/pki/etcdscp /etc/kubernetes/admin.conf root@55.241.105.95:/etc/kubernetesscp /etc/kubernetes/pki/{ca.*,sa.*,front-proxy-ca.*} root@55.241.105.95:/etc/kubernetes/pkiscp /etc/kubernetes/pki/etcd/ca.* root@55.241.105.95:/etc/kubernetes/pki/etcd

然后把上述的,返回的内容加到集群中来:

  kubeadm join master.k8s.io:16443 --token 8ya83c.0aviiwbjb1eorrp1 \--discovery-token-ca-cert-hash sha256:fded3b3f22275c13a0637cd7e2592f6ec7b5e9cc8d611c01a37d20a36c4748d0 \--control-plane 

8. 添加node节点

kubeadm join master.k8s.io:16443 --token 8ya83c.0aviiwbjb1eorrp1 \--discovery-token-ca-cert-hash sha256:fded3b3f22275c13a0637cd7e2592f6ec7b5e9cc8d611c01a37d20a36c4748d0 

至此,k8s集群搭建完毕,可以通过命令查看k8s的运行状态:

[root@guankong-ctrl-k8s02 ~]# kubectl get nodes
NAME                  STATUS   ROLES                  AGE    VERSION
k8s-master-1    Ready    <none>                 111d   v1.20.9
k8s-master-2    Ready    <none>                 111d   v1.20.9
k8s-node-1    Ready    <none>                 111d   v1.20.9
k8s-node-2   Ready    control-plane,master   111d   v1.20.9
k8s-node-3   Ready    control-plane,master   110d   v1.20.9

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/2293.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

基于深度学习的高精度足球检测识别系统(PyTorch+Pyside6+YOLOv5模型)

摘要&#xff1a;基于深度学习的高精度足球检测识别系统可用于日常生活中或野外来检测与定位足球目标&#xff0c;利用深度学习算法可实现图片、视频、摄像头等方式的足球目标检测识别&#xff0c;另外支持结果可视化与图片或视频检测结果的导出。本系统采用YOLOv5目标检测模型…

Unity | HDRP高清渲染管线学习笔记:材质系统Lit着色器

目录 一、Lit着色器 1. Surface Options 2. Surface Inputs&#xff08;表面输入&#xff09; 3. Transparency Inputs 二、HDRP渲染优先级 目录 一、Lit着色器 1. Surface Options 2. Surface Inputs&#xff08;表面输入&#xff09; 3. Transparency Inputs 4. Em…

【C/C++】拷贝构造函数的调用 使用方法

创作不易&#xff0c;本篇文章如果帮助到了你&#xff0c;还请点赞 关注支持一下♡>&#x16966;<)!! 主页专栏有更多知识&#xff0c;如有疑问欢迎大家指正讨论&#xff0c;共同进步&#xff01; &#x1f525;c系列专栏&#xff1a;C/C零基础到精通 &#x1f525; 给大…

【shell】expect命令详解:用expect实现自动化交互式操作

文章目录 一. 运用场景二. 语法说明三. 例子1. scp文件传输自动化2. ssh远程登录3. 切到root用户4. 创建ssh key5. ssh到一个节点创建用户 一. 运用场景 expect主要应用于自动化交互式操作的场景&#xff0c;借助Expect处理交互的命令&#xff0c;可以将交互过程如&#xff1a…

037:mapboxGL输入经纬度,地址坐标转换,弹出位置点地址信息

第037个 点击查看专栏目录 本示例的目的是介绍演示如何在vue+mapbox中输入经纬度,地址坐标转换,弹出位置点地址信息. 直接复制下面的 vue+mapbox源代码,操作2分钟即可运行实现效果 文章目录 示例效果配置方式示例源代码(共158行)相关API参考:专栏目标示例效果 配置方式…

LeetCode 2. 两数相加

文章目录 1. 题目描述2. 解题代码 1. 题目描述 链接&#xff1a;https://leetcode.cn/problems/add-two-numbers/ 2. 解题代码 public ListNode AddTwoNumber(ListNode l1, ListNode l2) {ListNode head new ListNode();ListNode cur head;int carry 0;while (l1 ! null…

Node.js HTTP 模块的内存泄露问题

很久没有逛社区了&#xff0c;晚上回来看了一下最近的情况&#xff0c;突然看到一个内存泄露问题&#xff0c;作为一个 APM 开发者&#xff0c;自然想分析其中的原因。 问题 下面介绍一下具体的问题。看一下 demo。 const http require(http)async function main () {let i…

企业级微服务架构实战项目--xx优选-用户登录

一 用户登录的触发页面 1.登录常量 2.登录地址 3.配置域名 4.启动程序 触发连接小程序后端的登录接口 小程序controller的登录方法

网络空间安全数学基础考试要点

网络空间安全数学基础 阶的计算不要求那个公式&#xff0c;但是Order几次方要求 考试会考原根 Legendre必考 多项式计算必考 扩域多项式计算 同态不考 域元素表示 本元多项式不考 1.整除 3 ≡ \equiv ≡ 4 mod 7不对吧3 ≡ \equiv ≡ 3 mod 74 ≡ \equiv ≡ 4 &#xff08;m…

vscode环境部署

目录 编译cpp 编译qt 借用插件 手撸&#xff08;建议&#xff0c;避免很多未知错误&#xff09; 踩过的坑 编译cpp vscode安装2个插件&#xff0c;extension pack自动包了下面3个通过命令窗口code .打开代码目录&#xff0c;或者添加cl.exe路径到path&#xff0c;以及c需…

津津乐道设计模式 - 桥接模式详解

&#x1f604; 19年之后由于某些原因断更了三年&#xff0c;23年重新扬帆起航&#xff0c;推出更多优质博文&#xff0c;希望大家多多支持&#xff5e; &#x1f337; 古之立大事者&#xff0c;不惟有超世之才&#xff0c;亦必有坚忍不拔之志 &#x1f390; 个人CSND主页——Mi…

clop勒索软件攻击活动频发,西门子能源中招

自6月初被通报利用MOVEit Transfer服务器中的零日漏洞窃取加密组织数据后&#xff0c;clop勒索软件攻击活动频繁&#xff0c;全球陆续发生了多起clop软件攻击事件。本周&#xff0c;Clop团伙在其数据泄露网站上列出了西门子能源公司的信息&#xff0c;表示该公司的数据被泄露。…