一、實(shí)驗(yàn)節(jié)點(diǎn)規(guī)劃表??
角色 | 主機(jī)名 | IP地址 | 系統(tǒng)版本 | CPU/MEM |
---|---|---|---|---|
master | m1 | 192.168.200.61 | Rocky Linux 8.5 | 2C/2GB |
master | m2 | 192.168.200.62 | Rocky Linux 8.5 | 2C/2GB |
master | m3 | 192.168.200.63 | Rocky Linux 8.5 | 2C/2GB |
node | n1 | 192.168.200.64 | Rocky Linux 8.5 | 2C/4GB |
node | n2 | 192.168.200.65 | Rocky Linux 8.5 | 2C/4GB |
node | n3 | 192.168.200.66 | Rocky Linux 8.5 | 2C/4GB |
VIP | 192.168.200.68 |
二、實(shí)驗(yàn)版本說明??
!?。∽⒁猓篸ocker與K8S版本的兼容性問題?。?!
以下是本實(shí)驗(yàn)版本選型情況:
系統(tǒng)版本:Rocky Linux 8.5
Docker版本:20.10.14
Kubernetes版本:1.18.0
三、實(shí)驗(yàn)拓?fù)??
四、實(shí)驗(yàn)詳細(xì)操作步驟???
1. 安裝Rocky Linux開源企業(yè)操作系統(tǒng)
略。詳細(xì)安裝部署配置等內(nèi)容請參考本篇博文。
??參考鏈接:Rocky Linux 8.5版本全新圖文安裝教程并更換阿里鏡像源等配置操作
2. 所有主機(jī)系統(tǒng)初始化
系統(tǒng)版本查詢
[root@m1 ~]# cat /etc/redhat-release
Rocky Linux release 8.5 (Green Obsidian)
[root@m1 ~]# hostnamectl
Static hostname: m1
Icon name: computer-vm
Chassis: vm
Machine ID: f7786fc1a38647e78e4a0c25671b5fe1
Boot ID: dd692b53139a4eb0a07a7b9977e16dff
Virtualization: vmware
Operating System: Rocky Linux 8.5 (Green Obsidian)
CPE OS Name: cpe:/o:rocky:rocky:8:GA
Kernel: Linux 4.18.0-348.20.1.el8_5.x86_64
Architecture: x86-64
1、修改主機(jī)名
hostnamectl set-hostname <主機(jī)名稱>
2、添加主機(jī)名
cat >> /etc/hosts << EOF
192.168.200.68 master.k8s.io k8s-vip
192.168.200.61 master01.k8s.io m1
192.168.200.62 master02.k8s.io m2
192.168.200.63 master03.k8s.io m3
192.168.200.64 node01.k8s.io n1
192.168.200.65 node02.k8s.io n2
192.168.200.66 node03.k8s.io n3
EOF
3、關(guān)閉防火墻
systemctl stop firewalld && systemctl disable firewalld
systemctl status firewalld
4、關(guān)閉selinux
sed -i "s/SELINUX=enforcing/SELINUX=permissive/g" /etc/selinux/config
5、關(guān)閉swap分區(qū)
sed -ri 's/.*swap.*/#&/' /etc/fstab # 重啟生效
6、配置網(wǎng)卡聯(lián)網(wǎng)
cat /etc/sysconfig/network-scripts/ifcfg-ens160
?。?!修改網(wǎng)卡信息重啟網(wǎng)絡(luò)生效命令!??!
systemctl restart NetworkManager
nmcli connection up ens160
7、配置阿里云鏡像
鏈接:https://developer.aliyun.com/mirror/rockylinux?spm=a2c6h.13651102.0.0.79fa1b112sSnLq
cd /etc/yum.repos.d/ && mkdir bak && cp Rocky-* bak/
sed -e 's|^mirrorlist=|#mirrorlist=|g' \
-e 's|^#baseurl=http://dl.rockylinux.org/$contentdir|baseurl=https://mirrors.aliyun.com/rockylinux|g' \
-i.bak \
/etc/yum.repos.d/Rocky-*.repo
8、生成本地緩存
yum makecache 或 dnf makecache
9、更新YUM源軟件包
yum update -y
10、將橋接的 IPv4 流量傳遞到 iptables 的鏈
cat > /etc/sysctl.d/k8s.conf << EOF
#開啟網(wǎng)橋模式,可將網(wǎng)橋的流量傳遞給iptables鏈
net.bridge.bridge-nf-call-ip6tables=1
net.bridge.bridge-nf-call-iptables=1
#關(guān)閉ipv6協(xié)議
net.ipv6.conf.all.disable_ipv6=1
net.ipv4.ip_forward=1
EOF
sysctl --system
# 加載 ip_vs 模塊
for i in $(ls /usr/lib/modules/$(uname -r)/kernel/net/netfilter/ipvs|grep -o "^[^.]*");do echo $i; /sbin/modinfo -F filename $i >/dev/null 2>&1 && /sbin/modprobe $i;done
11、設(shè)置主機(jī)之間時(shí)間同步
參考鏈接:https://chegva.com/3265.html
yum install -y chrony
chronyc -a makestep
systemctl enable chronyd.service && systemctl restart chronyd.service && systemctl status chronyd.service
3. 所有master節(jié)點(diǎn)部署keepalived
keepalived是一個(gè)類似于layer3, 4 & 5交換機(jī)制的軟件,也就是平時(shí)說的第3層、第4層和第5層交換。Keepalived是自動(dòng)完成,不需人工干涉。
Keepalived的作用是檢測服務(wù)器的狀態(tài),如果有一臺服務(wù)器宕機(jī),或工作出現(xiàn)故障,Keepalived將檢測到,并將有故障的服務(wù)器從系統(tǒng)中剔除,同時(shí)使用其他服務(wù)器代替該服務(wù)器的工作,當(dāng)服務(wù)器工作正常后Keepalived自動(dòng)將服務(wù)器加入到服務(wù)器群中,這些工作全部自動(dòng)完成,不需要人工干涉,需要人工做的只是修復(fù)故障的服務(wù)器。
安裝相關(guān)依賴包和keepalived
yum install -y conntrack-tools libseccomp libtool-ltdl && yum install -y keepalived
配置keepalived.conf配置文件(master01、master02、master03配置文件幾乎相同)
# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER
interface ens160
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.200.68
}
}
啟動(dòng)keepalived服務(wù)
systemctl start keepalived.service && systemctl enable keepalived.service && systemctl status keepalived.service
查看并驗(yàn)證VIP地址
# 發(fā)現(xiàn)VIP在主機(jī)master01上。
[root@m1 ~]# ip -c a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:cd:b3:43 brd ff:ff:ff:ff:ff:ff
inet 192.168.200.61/24 brd 192.168.200.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever
inet 192.168.200.68/32 scope global ens160
valid_lft forever preferred_lft forever
#當(dāng)關(guān)閉master01節(jié)點(diǎn)的keepalived服務(wù),查看VIP是否在其他master節(jié)點(diǎn)上,如果發(fā)現(xiàn),則表示配置成功。
[root@m1 ~]# systemctl stop keepalived.service
[root@m2 ~]# ip -c a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:d2:4d:2e brd ff:ff:ff:ff:ff:ff
inet 192.168.200.62/24 brd 192.168.200.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever
inet 192.168.200.68/32 scope global ens160
valid_lft forever preferred_lft forever
# 當(dāng)重新啟動(dòng)master01節(jié)點(diǎn)的keepalived服務(wù),VIP重新漂移到master01節(jié)點(diǎn)上。因?yàn)樵O(shè)置了主機(jī)優(yōu)先級,所以漂移到master01節(jié)點(diǎn)上。
[root@m1 ~]# ip -c a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:cd:b3:43 brd ff:ff:ff:ff:ff:ff
inet 192.168.200.61/24 brd 192.168.200.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever
inet 192.168.200.68/32 scope global ens160
valid_lft forever preferred_lft forever
4. 所有master節(jié)點(diǎn)部署haproxy
安裝haproxy
yum install -y haproxy
配置haproxy.cfg配置文件
三臺master節(jié)點(diǎn)的配置均相同,配置中聲明了后端代理的三個(gè)master節(jié)點(diǎn)服務(wù)器,指定了haproxy運(yùn)行的端口為6443等,因此6443端口為集群的入口。
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 master01.k8s.io 192.168.200.61:6443 check
server master02.k8s.io 192.168.200.62:6443 check
server master03.k8s.io 192.168.200.63: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
啟動(dòng)haproxy服務(wù)
systemctl start haproxy && systemctl enable haproxy && systemctl status haproxy
驗(yàn)證haproxy服務(wù)
[root@m1 ~]# netstat -lntup|grep haproxy
tcp 0 0 0.0.0.0:1080 0.0.0.0:* LISTEN 4193/haproxy
tcp 0 0 0.0.0.0:16443 0.0.0.0:* LISTEN 4193/haproxy
udp 0 0 0.0.0.0:50288 0.0.0.0:* 4191/haproxy
[root@m2 ~]# netstat -lntup|grep haproxy
tcp 0 0 0.0.0.0:1080 0.0.0.0:* LISTEN 4100/haproxy
tcp 0 0 0.0.0.0:16443 0.0.0.0:* LISTEN 4100/haproxy
udp 0 0 0.0.0.0:39062 0.0.0.0:* 4098/haproxy
[root@m3 ~]# netstat -lntup|grep haproxy
tcp 0 0 0.0.0.0:1080 0.0.0.0:* LISTEN 4185/haproxy
tcp 0 0 0.0.0.0:16443 0.0.0.0:* LISTEN 4185/haproxy
udp 0 0 0.0.0.0:47738 0.0.0.0:* 4183/haproxy
5. 所有節(jié)點(diǎn)配置阿里云Docker,kubernetes鏡像
配置docker鏡像
# 安裝需要的依賴包
yum install -y yum-utils
# 設(shè)置阿里云docker鏡像
yum-config-manager \
--add-repo \
https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 生成本地緩存
yum makecache
# 配置阿里云docker鏡像加速
參考鏈接:https://blog.csdn.net/shida_csdn/article/details/104054041
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://xxxxxxxx.mirror.aliyuncs.com"],
"exec-opts": ["native.cgroupdriver=systemd"] # 修改 Docker Cgroup Driver 為 systemd
}
EOF
sudo systemctl daemon-reload
# 查看docker所有版本信息
yum list docker-ce --showduplicates | sort -r
[root@m1 ~]# yum list docker-ce --showduplicates | sort -r
Last metadata expiration check: 0:01:17 ago on Fri 01 Apr 2022 10:57:15 PM CST.
docker-ce.x86_64 3:20.10.9-3.el8 docker-ce-stable
docker-ce.x86_64 3:20.10.8-3.el8 docker-ce-stable
docker-ce.x86_64 3:20.10.7-3.el8 docker-ce-stable
docker-ce.x86_64 3:20.10.6-3.el8 docker-ce-stable
docker-ce.x86_64 3:20.10.5-3.el8 docker-ce-stable
docker-ce.x86_64 3:20.10.4-3.el8 docker-ce-stable
docker-ce.x86_64 3:20.10.3-3.el8 docker-ce-stable
docker-ce.x86_64 3:20.10.2-3.el8 docker-ce-stable
docker-ce.x86_64 3:20.10.14-3.el8 docker-ce-stable
docker-ce.x86_64 3:20.10.1-3.el8 docker-ce-stable
docker-ce.x86_64 3:20.10.13-3.el8 docker-ce-stable
docker-ce.x86_64 3:20.10.12-3.el8 docker-ce-stable
docker-ce.x86_64 3:20.10.11-3.el8 docker-ce-stable
docker-ce.x86_64 3:20.10.10-3.el8 docker-ce-stable
docker-ce.x86_64 3:20.10.0-3.el8 docker-ce-stable
docker-ce.x86_64 3:19.03.15-3.el8 docker-ce-stable
docker-ce.x86_64 3:19.03.14-3.el8 docker-ce-stable
docker-ce.x86_64 3:19.03.13-3.el8 docker-ce-stable
Available Packages
配置kubernetes鏡像
# 配置阿里云Kubernetes 鏡像
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=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
# 生成本地緩存
yum makecache -y
# 查看K8S所有版本信息
yum list kubelet --showduplicates | sort -r
[root@m1 ~]# yum list kubelet --showduplicates | sort -r
Last metadata expiration check: 0:00:37 ago on Fri 01 Apr 2022 11:03:29 PM CST.
kubelet.x86_64 1.18.9-0 kubernetes
kubelet.x86_64 1.18.8-0 kubernetes
kubelet.x86_64 1.18.6-0 kubernetes
kubelet.x86_64 1.18.5-0 kubernetes
kubelet.x86_64 1.18.4-1 kubernetes
kubelet.x86_64 1.18.4-0 kubernetes
kubelet.x86_64 1.18.3-0 kubernetes
kubelet.x86_64 1.18.2-0 kubernetes
kubelet.x86_64 1.18.20-0 kubernetes
kubelet.x86_64 1.18.19-0 kubernetes
kubelet.x86_64 1.18.18-0 kubernetes
kubelet.x86_64 1.18.17-0 kubernetes
kubelet.x86_64 1.18.16-0 kubernetes
kubelet.x86_64 1.18.15-0 kubernetes
kubelet.x86_64 1.18.14-0 kubernetes
kubelet.x86_64 1.18.13-0 kubernetes
kubelet.x86_64 1.18.12-0 kubernetes
kubelet.x86_64 1.18.1-0 kubernetes
kubelet.x86_64 1.18.10-0 kubernetes
kubelet.x86_64 1.18.0-0 kubernetes
Available Packages
[root@m1 ~]#
6. 所有節(jié)點(diǎn)安裝Docker,kubelet,kubeadm,kubectl等K8S組件
安裝docker
# 安裝docker docker-ce ee企業(yè)版
yum install -y docker-ce docker-ce-cli containerd.io
# 啟動(dòng)Docker
systemctl start docker && systemctl enable docker && systemctl status docker
# 查看docker版本信息
# docker version
Client: Docker Engine - Community
Version: 20.10.14
API version: 1.41
Go version: go1.16.15
Git commit: a224086
Built: Thu Mar 24 01:47:44 2022
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.14
API version: 1.41 (minimum version 1.12)
Go version: go1.16.15
Git commit: 87a90dc
Built: Thu Mar 24 01:46:10 2022
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.5.11
GitCommit: 3df54a852345ae127d1fa3092b95168e4a88e2f8
runc:
Version: 1.0.3
GitCommit: v1.0.3-0-gf46b6ba
docker-init:
Version: 0.19.0
GitCommit: de40ad0
安裝kubelet、kubeadm、kubectl
由于K8S版本更新頻繁,所以選擇K8S的一個(gè)版本進(jìn)行安裝。這里指定版本號部署:
yum install -y kubelet-1.18.0 kubeadm-1.18.0 kubectl-1.18.0
systemctl enable kubelet
7. 在master01節(jié)點(diǎn)上部署Kubernetes Master
在含有VIP的master節(jié)點(diǎn)上操作,kubeadm.conf為初始化的配置文件。
# 創(chuàng)建目錄并進(jìn)入此目錄
mkdir /usr/local/kubernetes/manifests -p && cd /usr/local/kubernetes/manifests/
# 生成kubeadm-config配置文件
kubeadm config print init-defaults > /usr/local/kubernetes/manifests/kubeadm-config.yaml
# 配置kubeadm-config配置文件
cat > kubeadm-config.yaml <<EOF
apiServer:
certSANs:
- m1
- m2
- m3
- n1
- n2
- n3
- master.k8s.io
- 192.168.200.68
- 192.168.200.61
- 192.168.200.62
- 192.168.200.63
- 192.168.200.64
- 192.168.200.65
- 192.168.200.66
- 127.0.0.1
extraArgs:
authorization-mode: Node,RBAC
timeoutForControlPlane: 4m0s
apiVersion: kubeadm.k8s.io/v1beta1
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.18.0
networking:
dnsDomain: cluster.local
podSubnet: 10.244.0.0/16
serviceSubnet: 10.1.0.0/16
scheduler: {}
EOF
在master1節(jié)點(diǎn)執(zhí)行初始化操作,此過程漫長,需要耐心等待。
kubeadm init --config kubeadm-config.yaml
?。?!記錄以下命令,后續(xù)需要使用?。。?/code>
?。。”4婧靡韵滦畔?,待用?。。?/code>
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/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
You 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 n5x0ko.y83svfv1nxhsqjz8 \
--discovery-token-ca-cert-hash sha256:c0b2d0ffa4adf3775f0583f13434f14480fba3aa2e14c2e261db8fb8824a1ebc \
--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 n5x0ko.y83svfv1nxhsqjz8 \
--discovery-token-ca-cert-hash sha256:c0b2d0ffa4adf3775f0583f13434f14480fba3aa2e14c2e261db8fb8824a1ebc
master01節(jié)點(diǎn)上加載環(huán)境變量
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
使用以下命令查詢相關(guān)信息
kubectl get nodes -o wide
kubeadm config images pull
docker images
docker ps
8. 在master01節(jié)點(diǎn)上部署集群網(wǎng)絡(luò)
在master01上新建flannel網(wǎng)絡(luò)
# 下載kube-flannel.yml
wget -c https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
# 運(yùn)行kube-flannel.yml
kubectl apply -f kube-flannel.yml
# 檢查運(yùn)行狀態(tài),狀態(tài)全部為Running
kubectl get pods -n kube-system
# 查看master01節(jié)點(diǎn)狀態(tài),狀態(tài)為Ready
kubectl get nodes
9. 將master02,master03節(jié)點(diǎn)加入K8S集群
?。?!復(fù)制密鑰及相關(guān)文件?。?!
# 從master0復(fù)制密鑰及相關(guān)文件到master02、master03
ssh root@192.168.200.62 mkdir -p /etc/kubernetes/pki/etcd
scp /etc/kubernetes/admin.conf root@192.168.200.62:/etc/kubernetes
scp /etc/kubernetes/pki/{ca.*,sa.*,front-proxy-ca.*} root@192.168.200.62:/etc/kubernetes/pki
scp /etc/kubernetes/pki/etcd/ca.* root@192.168.200.62:/etc/kubernetes/pki/etcd
ssh root@192.168.200.63 mkdir -p /etc/kubernetes/pki/etcd
scp /etc/kubernetes/admin.conf root@192.168.200.63:/etc/kubernetes
scp /etc/kubernetes/pki/{ca.*,sa.*,front-proxy-ca.*} root@192.168.200.63:/etc/kubernetes/pki
scp /etc/kubernetes/pki/etcd/ca.* root@192.168.200.63:/etc/kubernetes/pki/etcd
分別在master02和master03節(jié)點(diǎn)上執(zhí)行
執(zhí)行在master1上init后輸出的join命令,需要帶上參數(shù)
--control-plane
表示把master控制節(jié)點(diǎn)加入集群
kubeadm join master.k8s.io:16443 --token n5x0ko.y83svfv1nxhsqjz8 \
--discovery-token-ca-cert-hash sha256:c0b2d0ffa4adf3775f0583f13434f14480fba3aa2e14c2e261db8fb8824a1ebc \
--control-plane
This node has joined the cluster and a new control plane instance was created:
* Certificate signing request was sent to apiserver and approval was received.
* The Kubelet was informed of the new secure connection details.
* Control plane (master) label and taint were applied to the new node.
* The Kubernetes control plane instances scaled up.
* A new etcd member was added to the local/stacked etcd cluster.
To start administering your cluster from this node, you need to run the following as a regular user:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
Run 'kubectl get nodes' to see this node join the cluster.
檢查集群狀態(tài)
kubectl get node
kubectl get pods --all-namespaces
10. 將Node01,Node02,Node03三個(gè)工作節(jié)點(diǎn)加入到K8S集群
分別在node1和node02節(jié)點(diǎn)上執(zhí)行
向集群添加新節(jié)點(diǎn),執(zhí)行在kubeadm init輸出的kubeadm join命令:不需要添加
--control-plane
kubeadm join master.k8s.io:16443 --token n5x0ko.y83svfv1nxhsqjz8 \
--discovery-token-ca-cert-hash sha256:c0b2d0ffa4adf3775f0583f13434f14480fba3aa2e14c2e261db8fb8824a1ebc
在三個(gè)任意一個(gè)控制節(jié)點(diǎn)上檢查節(jié)點(diǎn)狀態(tài)信息
kubectl get nodes
kubectl get cs -o wide
kubectl get pods -o wide -n kube-system
kubectl get pods --all-namespaces
11. 創(chuàng)建Pod測試kubernetes集群可用性
在Kubernetes集群中創(chuàng)建一個(gè)pod,驗(yàn)證是否正常運(yùn)行:以下命令基于任意控制節(jié)點(diǎn)操作:
kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort
kubectl get pod,svc
# 查看Pod詳細(xì)信息
kubectl describe pod nginx-f89759699-gl7rc
訪問地址:http://NodeIP:Port
12. 安裝Kubernetes Dashboard
安裝Kubernetes Dashboard
# 下載recommended.yaml
wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.3/aio/deploy/recommended.yaml
# 執(zhí)行recommended.yaml
kubectl apply -f recommended.yaml
kubectl get pods -n kubernetes-dashboard
kubectl get svc -n kubernetes-dashboard
# kubectl patch svc kubernetes-dashboard -p '{"spec":{"type":"NodePort"}}' -n kubernetes-dashboard
service/kubernetes-dashboard patched
# 檢查Kubernetes Dashboard運(yùn)行情況,查看關(guān)于kubernetes-dashboard的所有信息
kubectl get all -n kubernetes-dashboard
訪問Kubernetes Dashboard
# K8S集群中任意一臺服務(wù)器地址+端口號
https://192.168.200.61:32252
采用Token認(rèn)證方式登錄Dashboard
[root@m1 ~]# kubectl create serviceaccount dashboard -n kubernetes-dashboard
[root@m1 ~]# kubectl create rolebinding def-ns-admin --clusterrole=admin --serviceaccount=default:def-ns-admin
[root@m1 ~]# kubectl create clusterrolebinding dashboard-cluster-admin --clusterrole=cluster-admin --serviceaccount=kubernetes-dashboard:dashboard
[root@m1 ~]# kubectl describe sa dashboard -n kubernetes-dashboard
Name: dashboard
Namespace: kubernetes-dashboard
Labels: <none>
Annotations: <none>
Image pull secrets: <none>
Mountable secrets: dashboard-token-pm2r4
Tokens: dashboard-token-pm2r4
Events: <none>
[root@m1 ~]# kubectl describe secret dashboard-token-pm2r4 -n kubernetes-dashboard
獲得token值后,填入登錄。
登錄成功,查看集群狀態(tài)
13. 模擬管理節(jié)點(diǎn)出故障(master01宕機(jī))
當(dāng)master01主機(jī)宕機(jī),nginx服務(wù)是否能夠正常工作,不受影響。查看VIP轉(zhuǎn)移至哪個(gè)master節(jié)點(diǎn)上。
master01關(guān)機(jī),模擬master出現(xiàn)故障。
繼續(xù)訪問nginx服務(wù),發(fā)現(xiàn)除master01節(jié)點(diǎn)無法正常訪問,其余節(jié)點(diǎn)都可以正常訪問,不受影響。實(shí)現(xiàn)了K8S集群的高可用性。
查看各個(gè)node、pod工作狀況。除master01節(jié)點(diǎn)出故障外,其余節(jié)點(diǎn)和容器均正常運(yùn)行。
發(fā)現(xiàn)VIP漂移到了master02節(jié)點(diǎn)上。實(shí)現(xiàn)了網(wǎng)絡(luò)的負(fù)載均衡。
重啟master01主機(jī),查看服務(wù)是否能夠正常運(yùn)行,VIP節(jié)點(diǎn)是否能夠重新漂移回去。
發(fā)現(xiàn)master02主機(jī)節(jié)點(diǎn)上的VIP已經(jīng)漂移。
五、 K9S安裝與體驗(yàn)(可選)?
k9s是一個(gè)基于終端的UI,用于與你的Kubernetes集群互動(dòng)。這個(gè)項(xiàng)目的目的是使其更容易導(dǎo)航、觀察和管理你在kubernetes集群部署的應(yīng)用程序。k9s持續(xù)觀察Kubernetes的變化,并提供后續(xù)的命令來與你觀察到的資源進(jìn)行互動(dòng)。
下載K9S
[root@m1 ~]# curl -sS https://webinstall.dev/k9s | bash
Thanks for using webi to install 'k9s@stable' on 'Linux/x86_64'.
Have a problem? Experience a bug? Please let us know:
https://github.com/webinstall/webi-installers/issues
Lovin' it? Say thanks with a Star on GitHub:
https://github.com/webinstall/webi-installers
Downloading k9s from
https://github.com/derailed/k9s/releases/download/v0.25.18/k9s_Linux_x86_64.tar.gz
Saved as /root/Downloads/webi/k9s/0.25.18/k9s_Linux_x86_64.tar.gz
Extracting /root/Downloads/webi/k9s/0.25.18/k9s_Linux_x86_64.tar.gz
Installing to /root/.local/opt/k9s-v0.25.18/bin/k9s
Installed 'k9s v0.25.18' as /root/.local/bin/k9s
You need to update your PATH to use k9s:
export PATH="/root/.local/bin:$PATH"
[root@m1 ~]# export PATH="/root/.local/bin:$PATH"
查看K9S版本信息
[root@m1 ~]# k9s version
____ __.________
| |/ _/ __ \______
| < \____ / ___/
| | \ / /\___ \
|____|__ \ /____//____ >
\/ \/
Version: v0.25.18
Commit: 6085039f83cd5e8528c898cc1538f5b3287ce117
Date: 2021-12-28T16:53:21Z
[root@m1 ~]#
[root@m1 ~]# k9s info
____ __.________
| |/ _/ __ \______
| < \____ / ___/
| | \ / /\___ \
|____|__ \ /____//____ >
\/ \/
Configuration: /root/.config/k9s/config.yml
Logs: /tmp/k9s-root.log
Screen Dumps: /tmp/k9s-screens-root
[root@m1 ~]#
查看K9S幫助命令文章來源:http://www.zghlxwxcb.cn/news/detail-484057.html
[root@m1 ~]# k9s help
K9s is a CLI to view and manage your Kubernetes clusters.
Usage:
k9s [flags]
k9s [command]
Available Commands:
completion generate the autocompletion script for the specified shell
help Help about any command
info Print configuration info
version Print version/build info
Flags:
-A, --all-namespaces Launch K9s in all namespaces
--as string Username to impersonate for the operation
--as-group stringArray Group to impersonate for the operation
--certificate-authority string Path to a cert file for the certificate authority
--client-certificate string Path to a client certificate file for TLS
--client-key string Path to a client key file for TLS
--cluster string The name of the kubeconfig cluster to use
-c, --command string Overrides the default resource to load when the application launches
--context string The name of the kubeconfig context to use
--crumbsless Turn K9s crumbs off
--headless Turn K9s header off
-h, --help help for k9s
--insecure-skip-tls-verify If true, the server's caCertFile will not be checked for validity
--kubeconfig string Path to the kubeconfig file to use for CLI requests
--logFile string Specify the log file (default "/tmp/k9s-root.log")
-l, --logLevel string Specify a log level (info, warn, debug, trace, error) (default "info")
--logoless Turn K9s logo off
-n, --namespace string If present, the namespace scope for this CLI request
--readonly Sets readOnly mode by overriding readOnly configuration setting
-r, --refresh int Specify the default refresh rate as an integer (sec) (default 2)
--request-timeout string The length of time to wait before giving up on a single server request
--screen-dump-dir string Sets a path to a dir for a screen dumps
--token string Bearer token for authentication to the API server
--user string The name of the kubeconfig user to use
--write Sets write mode by overriding the readOnly configuration setting
Use "k9s [command] --help" for more information about a command.
[root@m1 ~]#
文章來源地址http://www.zghlxwxcb.cn/news/detail-484057.html
到了這里,關(guān)于【K8S&RockyLinux】基于開源操作系統(tǒng)搭建K8S高可用集群(詳細(xì)版)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!