Linux容器篇-使用kubeadm搭建一個kubernetes集群

kubernetes集群架構和組件

在這里插入圖片描述

master節點組件

  1. kube-apiserver:Kubernetes API,集群的統一入口,各組件的協調者,以RESTful API提供接口服務,所有對象資源的增刪改查和監聽操作都交給APIserver處理后再交給Etcd存儲。

  2. kube-controller-manager:處理集群中的常規后臺事務,一個資源對應一個控制器,Controller Manager就是負責管理這些控制器的。

  3. kube-scheduler:根據算法為新創建的Pode選擇一個Node節點。

Node節點組件

  1. kubelet:kubelet是Master在Node節點上的Agent,管理本機運行容器的生命周期,比如:創建容器,pod掛載數據卷,下載secret,獲取容器和節點狀態等工作。kubelet將每個Pod轉換為一組容器。

  2. kube-proxy:在Node節點上實現Pod網絡代理,維護網絡規則和四層負載均衡工作。

  3. 容器進行時:容器引擎,運行容器,例如Docker,containerd,podman等。

kubeadm搭建一個簡單的集群

硬件配置

學習環境:

  • master-2C/2G/20G

  • node-2C/2G/20G

測試環境:

  • master-4C/8G/50G

  • node-8C/16G/100G

生產環境:

  • master-8C/16G/500G

  • node-16C/32G/1T

環境準備

  • 操作系統:CentOS7.9-x86_64
  • Docker版本:26.1.4(CE)
  • kubernetes:1.28

服務器規劃

主機名ip
k8s-master192.168.3.10
k8s-node1192.168.3.11
k8s-node2192.168.3.12

操作系統初始化配置

#關閉防火墻
systemctl stop firewalld && systemctl disable firewalld#關閉selinux
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config#關閉swap分區
sed -ri 's@(.*swap.*)@#\1@g' /etc/fstab#修改主機名
hostnamectl set-hostname <hostname>#修改hosts文件(非必選配置)
cat >> /etc/hosts << EOF
192.168.3.10  k8s-master
192.168.3.11  k8s-node1
192.168.3.12  k8s-node2
EOF#開啟內核 ipv4 轉發需要執?如下命令加載  overlay、br_netfilter  模塊
cat > /etc/modules-load.d/k8s.conf <<EOF
overlay
br_netfilter
EOF
modprobe overlay 
modprobe br_netfilter#配置內核參數,將橋接的IPv4流量傳遞到iptables鏈
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl -p /etc/sysctl.d/k8s.conf#時間同步,統一配置阿里云時鐘服務器
server ntp.aliyun.com iburst 

安裝docker

配置docker鏡像源

# step 1: 安裝必要的一些系統工具
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
# Step 2: 添加軟件源信息
sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# Step 3
sudo sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo
# Step 4: 更新并安裝Docker-CE
sudo yum makecache fast
sudo yum -y install docker-ce

配置鏡像加速器

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{"registry-mirrors": ["https://5fid4glg.mirror.aliyuncs.com","https://docker.m.daocloud.io"],"exec-opts": ["native.cgroupdriver=systemd"]
}
EOF

注意:這里的鏡像源中,大部分鏡像都是兩年前的,眾所周知docker hub已經不對國內開放了,但是學習用也足夠了。

“exec-opts”: [“native.cgroupdriver=systemd”] 這個配置是官方推薦的一個配置,也可以不做修改。

“insecure-registries”: 證書驗證

配置完成之后,加載配置文件,重啟docker,設置開機自啟動

systemctl daemon-reload
systemctl enable docker --now

安裝cri-docker

wget https://github.com/Mirantis/cri-dockerd/releases/download/v0.3.14/cri-dockerd-0.3.14-3.el7.x86_64.rpm

如果由于網絡原因,無法下載,可以科學上網先下載再上傳到機器。

指定依賴鏡像地址:

在cri-docker.service配置文件中添加:–pod-infra-container-image=registry.aliyuncs.com/google_containers/pause:3.9

# vi /usr/lib/systemd/system/cri-docker.service
...
[Service]
Type=notify
ExecStart=/usr/bin/cri-dockerd --container-runtime-endpoint fd:// --pod-infra-container-image=registry.aliyuncs.com/google_containers/pause:3.9
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always
...systemctl daemon-reload
systemctl enable cri-docker --now

安裝kubernetes組件

配置yum源

cat <<EOF | tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes-new/core/stable/v1.28/rpm/
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes-new/core/stable/v1.28/rpm/repodata/repomd.xml.key
EOF

安裝指定版本的kubeadm、kubelet、kubectl組件

kubeadm :初始化集群?具

kubelet :在集群中的每個節點上?來啟動 Pod 和容器等

kubectl :?來與集群通信的命令??具(管理節點安裝即可)

yum install kubelet-1.28.0 kubeadm-1.28.0 kubectl-1.28.0 -y

配置kubelet服務開機自啟動

systemctl enable kubelet --now

配置Master節點

在master節點執行

kubeadm init \
--apiserver-advertise-address="192.168.3.10" \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.28.0 \
--service-cidr=10.96.0.0/16 \
--pod-network-cidr=10.244.0.0/16 \
--cri-socket=unix:///var/run/cri-dockerd.sock
  • –apiserver-advertise-address 集群通告地址,即監聽地址
  • –image-repository registry.aliyuncs.com/google_containers 默認是k8s.gcr.io國內無法訪問,這里指定阿里云鏡像倉庫地址
  • –kubernetes-version v1.28.0 指定k8s版本,和機器安裝的版本保持一致
  • –service-cidr=10.96.0.0/16 集群內部虛擬網絡,Pod統一訪問入口
  • –pod-network-cidr=10.244.0.0/16 Pod網絡,和CNI網絡組件yanl中保持一致
  • –cri-socket=unix:///var/run/cri-dockerd.sock 指定接口
  • –ignore-preflight-errors=all 忽略警告

這里執行可能會很慢,因為需要從鏡像站拉取鏡像,如果感覺慢,也可以先拉取鏡像再初始化。

 kubeadm config images pull \--image-repository registry.aliyuncs.com/google_containers \--kubernetes-version v1.28.0 \--cri-socket=unix:///var/run/cri-dockerd.sock

全過程如下:

[root@k8s-master ~]# kubeadm init \
> --apiserver-advertise-address="192.168.3.10" \
> --image-repository registry.aliyuncs.com/google_containers \
> --kubernetes-version v1.28.0 \
> --service-cidr=10.96.0.0/16 \
> --pod-network-cidr=10.244.0.0/16 \
> --cri-socket=unix:///var/run/cri-dockerd.sock
[init] Using Kubernetes version: v1.28.0
[preflight] Running pre-flight checks
[preflight] Pulling images required for setting up a Kubernetes cluster
[preflight] This might take a minute or two, depending on the speed of your internet connection
[preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [k8s-master kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 192.168.3.10]
[certs] Generating "apiserver-kubelet-client" certificate and key
[certs] Generating "front-proxy-ca" certificate and key
[certs] Generating "front-proxy-client" certificate and key
[certs] Generating "etcd/ca" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [k8s-master localhost] and IPs [192.168.3.10 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [k8s-master localhost] and IPs [192.168.3.10 127.0.0.1 ::1]
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "sa" key and public key
[kubeconfig] Using kubeconfig folder "/etc/kubernetes"
[kubeconfig] Writing "admin.conf" kubeconfig file
[kubeconfig] Writing "kubelet.conf" kubeconfig file
[kubeconfig] Writing "controller-manager.conf" kubeconfig file
[kubeconfig] Writing "scheduler.conf" kubeconfig file
[etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
[control-plane] Using manifest folder "/etc/kubernetes/manifests"
[control-plane] Creating static Pod manifest for "kube-apiserver"
[control-plane] Creating static Pod manifest for "kube-controller-manager"
[control-plane] Creating static Pod manifest for "kube-scheduler"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Starting the kubelet
[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
[apiclient] All control plane components are healthy after 10.504564 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config" in namespace kube-system with the configuration for the kubelets in the cluster
[upload-certs] Skipping phase. Please see --upload-certs
[mark-control-plane] Marking the node k8s-master as control-plane by adding the labels: [node-role.kubernetes.io/control-plane node.kubernetes.io/exclude-from-external-load-balancers]
[mark-control-plane] Marking the node k8s-master as control-plane by adding the taints [node-role.kubernetes.io/control-plane:NoSchedule]
[bootstrap-token] Using token: twzwgv.xqhb98gfu1edpm62
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstrap-token] Configured RBAC rules to allow Node Bootstrap tokens to get nodes
[bootstrap-token] Configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstrap-token] Configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstrap-token] Configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
[kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate and key
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxyYour 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/Then you can join any number of worker nodes by running the following on each as root:kubeadm join 192.168.3.10:6443 --token twzwgv.xqhb98gfu1edpm62 \--discovery-token-ca-cert-hash sha256:43eff3fcb345a6138ae9254d60b219cd04dd5e18cc2910d0eb52db209bb93b26 
[root@k8s-master ~]# 

拷? kubeconfig 配置?件

  mkdir -p $HOME/.kubesudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/configsudo chown $(id -u):$(id -g) $HOME/.kube/config

將Node節點加入集群

在node節點執行

kubeadm join 192.168.3.10:6443 --token twzwgv.xqhb98gfu1edpm62 \--discovery-token-ca-cert-hash sha256:43eff3fcb345a6138ae9254d60b219cd04dd5e18cc2910d0eb52db209bb93b26 \--cri-socket=unix:///var/run/cri-dockerd.sock

為了安全性,kubeadm生成的token,默認有效期為24小時,過期之后就無法使用了,需要重新生成加入節點命令:

kubeadm token create --print-join-command

部署容器網絡

提前下載好calico的鏡像文件,導入所有節點

ls *.tar | xargs -i docker load -i {}

在master節點使用yaml文件創建pod

kubectl create -f tigera-operator.yaml
kubectl create -f custom-resources.yaml

注意:按官方給的做法,是直接使用yaml文件然后在線下載,必須保證鏡像源的訪問速度才行。不然需要很久很久

在這里插入圖片描述

到這里,集群就算是搭建完成了

[root@localhost ~]# kubectl get pods -n calico-system
NAME                                       READY   STATUS    RESTARTS   AGE
calico-kube-controllers-85955d4f5b-rlrrg   1/1     Running   0          21s
calico-node-gvv4h                          1/1     Running   0          21s
calico-node-mhkxp                          0/1     Running   0          21s
calico-node-z9czg                          1/1     Running   0          21s
calico-typha-6dfcdf98b5-984zj              1/1     Running   0          22s
calico-typha-6dfcdf98b5-pvg5j              1/1     Running   0          18s
csi-node-driver-b5h5x                      2/2     Running   0          21s
csi-node-driver-htgqx                      2/2     Running   0          21s
csi-node-driver-js88m                      2/2     Running   0          21s
[root@localhost ~]# kubectl get nodes
NAME         STATUS   ROLES           AGE    VERSION
k8s-master   Ready    control-plane   4h4m   v1.28.0
k8s-node1    Ready    <none>          4h2m   v1.28.0
k8s-node2    Ready    <none>          4h2m   v1.28.0

Master節點命令自動補全

yum install bash-completion -y
echo 'source <(kubectl completion bash)' >>~/.bashrc

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/40398.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/40398.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/40398.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

學習Mybatis

Mybatis 第一節 引言 1. 什么是框架 框架是一個半成品&#xff0c;解決了軟件開發過程中的普遍性問題&#xff0c;簡化了開發步驟&#xff0c;提高了開發效率。 2. 什么是ORM ORM全稱為Object Relational Mapping&#xff0c;意為對象關系映射&#xff0c;主要實現了將程序…

usecallback()與usememo()

簡單的說 都是用來監聽數據變化 來進行控制渲染、減少不必要的渲染 、優化性能 usecallback()是用來監聽數據變化從而調用方法 usememo()是用來監聽數據變化從而改變數據 使用return返回變化的數據 當然return 也可以返回方法 所以usememo()可以代替usecallback() 下面詳解 …

常見的編碼技術簡介

常見的編碼技術簡介 文章目錄 常見的編碼技術簡介1. 字符編碼1.1 ASCII1.2 Unicode 2. 數據傳輸編碼2.1 Base系列編碼2.1.1 Base642.1.2 Base162.1.3 Base322.1.4 Base852.1.5 其他Base編碼 2.2 URL編碼2.3 JSON2.4 XML2.5 Protobuf (Protocol Buffers) 1. 字符編碼 1.1 ASCII…

AI是在幫助開發者還是取代他們?——探討AI在軟件開發中的角色與未來

引言 隨著人工智能技術的迅猛發展&#xff0c;AI工具在軟件開發中的應用越來越廣泛。有人認為AI可以顯著提升開發者的效率&#xff0c;而也有人擔心AI會取代開發者的工作。本文將從三個方面探討AI在軟件開發中的角色&#xff1a;AI工具現狀、AI對開發者的影響以及AI開發的未來…

學習springAOP

第三章 Spring AOP 第一節 AOP 簡介 1. 概念 AOP全稱為Aspect Oriented Programming&#xff0c;表示面向切面編程。何為切面呢&#xff1f; 由此可以得出&#xff0c;切面是一種將那些與業務無關&#xff0c;但業務模塊都需要使用的功能封裝起來的技術。這樣便于減少系統的…

昇思25天學習打卡營第4天|應用實踐

昇思25天學習打卡營第4天 文章目錄 昇思25天學習打卡營第4天基于 MindSpore 實現 BERT 對話情緒識別模型簡介環境配置數據集數據加載和數據預處理input_idsattention_mask 模型構建模型驗證模型推理自定義推理數據集 打卡記錄 基于 MindSpore 實現 BERT 對話情緒識別 模型簡介…

奧比中光astra_pro相機使用記錄

一、信息獲取 1、官網 用于了解產品信息 http://www.orbbec.com.cn/sys/37.html 2、開發者社區 咨詢問題下載開發部https://developer.orbbec.com.cn/ 二 、windowvs19 1、相機型號 orbbec_astro_pro 根據對應的型號找到需要的包工具 踩坑1&#xff0c;因為這個相機型號…

第20章 Mac+VSCode配置C++環境

1. 下載VSCode VSCode下載地址在mac終端里輸入xcode- select --install命令&#xff0c;根據提示安裝xcode工具。 2. 安裝插件&#xff08;4個&#xff09; 打開VScode&#xff0c;點擊應用右側菜單欄 C/C&#xff08;必裝&#xff09; Code Runner&#xff08;必裝&#xf…

UCOS-III 任務調度與就緒列表管理

01. 就緒優先級位圖 在實時操作系統中&#xff0c;任務調度的效率至關重要。UCOS-III通過就緒優先級位圖來快速查找最高優先級的就緒任務&#xff0c;從而實現高效調度。就緒優先級位圖是一個按位表示的結構&#xff0c;每個位代表一個優先級&#xff0c;當某個優先級上有任務就…

高效管理 TensorFlow 2 GPU 顯存的實用指南

前言 在使用 TensorFlow 2 進行訓練或預測時&#xff0c;合理管理 GPU 顯存至關重要。未能有效管理和釋放 GPU 顯存可能導致顯存泄漏&#xff0c;進而影響后續的計算任務。在這篇文章中&#xff0c;我們將探討幾種方法來有效釋放 GPU 顯存&#xff0c;包括常規方法和強制終止任…

【FFmpeg】avcodec_open2函數

目錄 1. avcodec_open21.1 編解碼器的預初始化&#xff08;ff_encode_preinit & ff_decode_preinit&#xff09;1.2 編解碼器的初始化&#xff08;init&#xff09;1.3 釋放編解碼器&#xff08;ff_codec_close&#xff09; FFmpeg相關記錄&#xff1a; 示例工程&#xff…

Windows編程之多線程事件對象(Event Object)用法詳解

目錄 一、前言 二、基礎用法 三、API詳解 1.創建事件對象 2控制事件狀態 3.等待事件對象&#xff1a; 四、實戰案例 1.案例描述 2.代碼設計 3.總設計代碼 4.運行結果 一、前言 事件對象&#xff08;Event Object&#xff09;是我們在大型項目中&#xff0c;進行多線…

競賽選題 醫學大數據分析 - 心血管疾病分析

文章目錄 1 前言1 課題背景2 數據處理3 數據可視化4 最后 1 前言 &#x1f525; 優質競賽項目系列&#xff0c;今天要分享的是 &#x1f6a9; 基于大數據的心血管疾病分析 該項目較為新穎&#xff0c;適合作為競賽課題方向&#xff0c;學長非常推薦&#xff01; &#x1f9…

AI繪畫Stable Diffusion 解鎖精美壁紙創作:利用SD與LLM定制你的專屬壁紙,AI副業變現指南!

大家好&#xff0c;我是畫畫的小強 今天給大家分享一下用AI繪畫Stable Diffusion 制作精美手機壁紙&#xff0c;這也可能是當前最快AIGC變現的一種途徑。雖然本文的主題為手機壁紙&#xff0c;當調整不同的比例的分辨率寬高比例&#xff0c;就可以直接復用到手機、電腦和平板、…

旋轉和鏡像的關系

旋轉矩陣行列式與 在E(3)三維空間中&#xff0c;旋轉矩陣的行列式可以用來判斷該旋轉是否包含鏡像變換。 行列式為正&#xff1a; 表示純旋轉&#xff0c;不包含鏡像。 旋轉矩陣保持向量的長度和角度不變&#xff0c;只是改變向量的方向。 行列式為負&#xff1a; 表示旋轉…

機器學習原理之 -- 支持向量機分類:由來及原理詳解

支持向量機&#xff08;Support Vector Machine, SVM&#xff09;是統計學習理論的一個重要成果&#xff0c;廣泛應用于分類和回歸問題。SVM以其高效的分類性能和良好的泛化能力在機器學習領域中占據重要地位。本文將詳細介紹支持向量機的由來、基本原理、構建過程及其優缺點。…

LVS負載均衡群集部署之——DR模式的介紹及搭建步驟

一、LVS-DR集群介紹1.1 LVS-DR 工作原理1.2 數據包流向分析1.3 LVS-DR 模式的特點1.4 LVS-DR中的ARP問題1.4.1 問題一1.4.2 問題二二、構建LVS-DR集群2.1 構建LVS-DR集群的步驟&#xff08;理論&#xff09;1.配置負載調度器&#xff08;192.168.80.30&#xff09;&#xff08;…

5分鐘教你用AI把老照片動起來,別再去花49塊9的冤枉錢了

文章目錄 需要的工具 最近&#xff0c;AI視頻在各大平臺上&#xff0c;又火了。 只是火的形式&#xff0c;變成了將老照片動起來&#xff0c;打情感牌&#xff0c;或者做很多經典電視劇的再整活。 直接把可靈的生成時間&#xff0c;從以前的4分鐘&#xff0c;生生的干成了20分鐘…

鴻蒙應用筆記

安裝就跳過了&#xff0c;一直點點就可以了 配置跳過&#xff0c;就自動下了點東西。 鴻蒙那個下載要12g個內存&#xff0c;大的有點嚇人。 里面跟idea沒區別 模擬器或者真機運行 真機要鴻蒙4.0&#xff0c;就可以實機調試 直接在手機里面跑&#xff0c;這個牛逼&#xf…

國標GB/T 28181詳解:國標GBT28181-2022 SIP服務器發起廣播的命令流程

目錄 一、定義 二、作用 1、實現信息的集中管理和分發 &#xff08;1&#xff09;信息集中 &#xff08;2&#xff09;信息分發 2、提高信息傳輸的可靠性和效率 &#xff08;1&#xff09;可靠性 &#xff08;2&#xff09;提高效率 3、支持多種設備和系統的互通 &am…