江西建設(shè)職業(yè)技術(shù)學(xué)院招生信息網(wǎng)站如何統(tǒng)計(jì)網(wǎng)站訪問量
這里寫目錄標(biāo)題
- Kubeadm - K8S1.20 - 高可用集群部署
- 一.環(huán)境準(zhǔn)備
- 1.系統(tǒng)設(shè)置
- 二.所有節(jié)點(diǎn)安裝docker
- 三.所有節(jié)點(diǎn)安裝kubeadm,kubelet和kubectl
- 1.定義kubernetes源
- 2.高可用組件安裝、配置
- 四.部署K8S集群
- 五.問題解決
- 1.加入集群的 Token 過期
- 2.master節(jié)點(diǎn) 無法部署非系統(tǒng)Pod
- 3.修改NodePort的默認(rèn)端口
- 4.外部 etcd 部署配置
Kubeadm - K8S1.20 - 高可用集群部署
一.環(huán)境準(zhǔn)備
1.系統(tǒng)設(shè)置
注意事項(xiàng):
master節(jié)點(diǎn)cpu核心數(shù)要求大于2
●最新的版本不一定好,但相對于舊版本,核心功能穩(wěn)定,但新增功能、接口相對不穩(wěn)
●學(xué)會(huì)一個(gè)版本的 高可用部署,其他版本操作都差不多
●宿主機(jī)盡量升級到CentOS 7.9
●內(nèi)核kernel升級到 4.19+ 這種穩(wěn)定的內(nèi)核
●部署k8s版本時(shí),盡量找 1.xx.5 這種大于5的小版本(這種一般是比較穩(wěn)定的版本)
//所有節(jié)點(diǎn),關(guān)閉防火墻規(guī)則,關(guān)閉selinux,關(guān)閉swap交換
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/enforcing/disabled/' /etc/selinux/config
iptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -X
swapoff -a
sed -ri 's/.*swap.*/#&/' /etc/fstab//修改主機(jī)名
hostnamectl set-hostname master01
hostnamectl set-hostname master02
hostnamectl set-hostname master03
hostnamectl set-hostname node01
hostnamectl set-hostname node02//所有節(jié)點(diǎn)修改hosts文件
vim /etc/hosts
192.168.82.100 master01
192.168.82.101 master02
192.168.82.102 master03
192.168.82.103 node01
192.168.82.104 node02//所有節(jié)點(diǎn)時(shí)間同步
yum -y install ntpdate
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
echo 'Asia/Shanghai' >/etc/timezone
ntpdate time2.aliyun.comsystemctl enable --now crondcrontab -e
*/30 * * * * /usr/sbin/ntpdate time2.aliyun.com//所有節(jié)點(diǎn)實(shí)現(xiàn)Linux的資源限制
vim /etc/security/limits.conf
* soft nofile 65536
* hard nofile 131072
* soft nproc 65535
* hard nproc 655350
* soft memlock unlimited
* hard memlock unlimited//所有節(jié)點(diǎn)升級內(nèi)核
wget http://193.49.22.109/elrepo/kernel/el7/x86_64/RPMS/kernel-ml-devel-4.19.12-1.el7.elrepo.x86_64.rpm -O /opt/kernel-ml-devel-4.19.12-1.el7.elrepo.x86_64.rpm
wget http://193.49.22.109/elrepo/kernel/el7/x86_64/RPMS/kernel-ml-4.19.12-1.el7.elrepo.x86_64.rpm -O /opt/kernel-ml-4.19.12-1.el7.elrepo.x86_64.rpmcd /opt/
yum localinstall -y kernel-ml*#更改內(nèi)核啟動(dòng)方式
grub2-set-default 0 && grub2-mkconfig -o /etc/grub2.cfg
grubby --args="user_namespace.enable=1" --update-kernel="$(grubby --default-kernel)"
grubby --default-kernel
reboot//調(diào)整內(nèi)核參數(shù)
cat > /etc/sysctl.d/k8s.conf <<EOF
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
fs.may_detach_mounts = 1
vm.overcommit_memory=1
vm.panic_on_oom=0
fs.inotify.max_user_watches=89100
fs.file-max=52706963
fs.nr_open=52706963
net.netfilter.nf_conntrack_max=2310720net.ipv4.tcp_keepalive_time = 600
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_keepalive_intvl =15
net.ipv4.tcp_max_tw_buckets = 36000
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_max_orphans = 327680
net.ipv4.tcp_orphan_retries = 3
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.ip_conntrack_max = 65536
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_timestamps = 0
net.core.somaxconn = 16384
EOF#生效參數(shù)
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
二.所有節(jié)點(diǎn)安裝docker
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install -y docker-ce docker-ce-cli containerd.iomkdir /etc/docker
cat > /etc/docker/daemon.json <<EOF
{"registry-mirrors": ["https://xxxxxxx.mirror.aliyuncs.com"],"exec-opts": ["native.cgroupdriver=systemd"],"log-driver": "json-file","log-opts": {"max-size": "500m", "max-file": "3"}
}
EOFsystemctl daemon-reload
systemctl restart docker.service
systemctl enable docker.service docker info | grep "Cgroup Driver"
Cgroup Driver: systemd
三.所有節(jié)點(diǎn)安裝kubeadm,kubelet和kubectl
1.定義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=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
EOFyum install -y kubelet-1.20.15 kubeadm-1.20.15 kubectl-1.20.15#配置Kubelet使用阿里云的pause鏡像
cat > /etc/sysconfig/kubelet <<EOF
KUBELET_EXTRA_ARGS="--cgroup-driver=systemd --pod-infra-container-image=registry.cn-hangzhou.aliyuncs.com/google_containers/pause-amd64:3.2"
EOF//開機(jī)自啟kubelet
systemctl enable --now kubelet
2.高可用組件安裝、配置
//所有 master 節(jié)點(diǎn)部署 Haproxy
yum -y install haproxy keepalivedcat > /etc/haproxy/haproxy.cfg << EOF
globallog 127.0.0.1 local0 infolog 127.0.0.1 local1 warningchroot /var/lib/haproxypidfile /var/run/haproxy.pidmaxconn 4000user haproxygroup haproxydaemonstats socket /var/lib/haproxy/statsdefaultsmode tcplog globaloption tcplogoption dontlognulloption redispatchretries 3timeout queue 1mtimeout connect 10stimeout client 1mtimeout server 1mtimeout check 10smaxconn 3000frontend monitor-inbind *:33305mode httpoption httplogmonitor-uri /monitorfrontend k8s-masterbind *:6444 mode tcpoption tcplogdefault_backend k8s-masterbackend k8s-mastermode tcpoption tcplogoption tcp-checkbalance roundrobinserver k8s-master1 192.168.82.100:6443 check inter 10000 fall 2 rise 2 weight 1server k8s-master2 192.168.82.101:6443 check inter 10000 fall 2 rise 2 weight 1server k8s-master3 192.168.82.102:6443 check inter 10000 fall 2 rise 2 weight 1
EOF
//所有 master 節(jié)點(diǎn)部署 keepalived
yum -y install keepalivedcd /etc/keepalived/
vim keepalived.conf
! Configuration File for keepalived
global_defs {router_id LVS_HA1 #路由標(biāo)識符,每個(gè)節(jié)點(diǎn)配置不同
}vrrp_script chk_haproxy {script "/etc/keepalived/check_haproxy.sh"interval 2weight 2
}vrrp_instance VI_1 {state MASTER #本機(jī)實(shí)例狀態(tài),MASTER/BACKUP,備機(jī)配置文件中設(shè)置BACKUPinterface ens33virtual_router_id 51priority 100 #本機(jī)初始權(quán)重,備機(jī)設(shè)置小于主機(jī)的值advert_int 1virtual_ipaddress {192.168.82.200 #設(shè)置VIP地址}track_script {chk_haproxy}
}vim check_haproxy.sh
#!/bin/bash
if ! killall -0 haproxy; thensystemctl stop keepalived
fisystemctl enable --now haproxy
systemctl enable --now keepalived
四.部署K8S集群
//在 master01 節(jié)點(diǎn)上設(shè)置集群初始化配置文件
kubeadm config print init-defaults > /opt/kubeadm-config.yamlcd /opt/
vim kubeadm-config.yaml
......
11 localAPIEndpoint:
12 advertiseAddress: 192.168.82.100 #指定當(dāng)前master節(jié)點(diǎn)的IP地址
13 bindPort: 6443 #注意haproxy中的后端端口21 apiServer:
22 certSANs: #在apiServer屬性下面添加一個(gè)certsSANs的列表,添加所有master節(jié)點(diǎn)的IP地址和集群VIP地址
23 - 192.168.82.200
24 - 192.168.82.100
25 - 192.168.82.101
26 - 192.168.82.10230 clusterName: kubernetes
31 controlPlaneEndpoint: "192.168.82.200:6444" #指定集群VIP地址,注意與haproxy的前端訪問端口一致
32 controllerManager: {}38 imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers #指定鏡像下載地址
39 kind: ClusterConfiguration
40 kubernetesVersion: v1.20.15 #指定kubernetes版本號
41 networking:
42 dnsDomain: cluster.local
43 podSubnet: "10.244.0.0/16" #指定pod網(wǎng)段,10.244.0.0/16用于匹配flannel默認(rèn)網(wǎng)段
44 serviceSubnet: 10.96.0.0/16 #指定service網(wǎng)段
45 scheduler: {}
#末尾再添加以下內(nèi)容
---
apiVersion: kubeproxy.config.k8s.io/v1alpha1
kind: KubeProxyConfiguration
mode: ipvs #把默認(rèn)的kube-proxy調(diào)度方式改為ipvs模式#更新集群初始化配置文件
kubeadm config migrate --old-config kubeadm-config.yaml --new-config new.yaml//所有節(jié)點(diǎn)拉取鏡像
#拷貝yaml配置文件給其他主機(jī),通過配置文件進(jìn)行拉取鏡像
for i in master02 master03 node01 node02; do scp /opt/new.yaml $i:/opt/; donekubeadm config images pull --config /opt/new.yaml//master01 節(jié)點(diǎn)進(jìn)行初始化
kubeadm init --config new.yaml --upload-certs | tee kubeadm-init.log
#提示:
.........
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 the control-plane node running the following command on each as root:
#master節(jié)點(diǎn)加入使用的命令,記錄!kubeadm join 192.168.82.200:6444 --token 7t2weq.bjbawausm0jaxury \--discovery-token-ca-cert-hash sha256:e76e4525ca29a9ccd5c24142a724bdb6ab86512420215242c4313fb830a4eb98 \--control-plane --certificate-key 0f2a7ff2c46ec172f834e237fcca8a02e7c29500746594c25d995b78c92dde96Please note that the certificate-key gives access to cluster sensitive data, keep it secret!
As a safeguard, uploaded-certs will be deleted in two hours; If necessary, you can use
"kubeadm init phase upload-certs --upload-certs" to reload certs afterward.Then you can join any number of worker nodes by running the following on each as root:
#node節(jié)點(diǎn)加入使用的命令。記錄!
kubeadm join 192.168.82.200:6444 --token 7t2weq.bjbawausm0jaxury \--discovery-token-ca-cert-hash sha256:e76e4525ca29a9ccd5c24142a724bdb6ab86512420215242c4313fb830a4eb98#若初始化失敗,進(jìn)行的操作
kubeadm reset -f
ipvsadm --clear
rm -rf ~/.kube
再次進(jìn)行初始化
//master01 節(jié)點(diǎn)進(jìn)行環(huán)境配置
#配置 kubectl
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config#修改controller-manager和scheduler配置文件
vim /etc/kubernetes/manifests/kube-scheduler.yaml
vim /etc/kubernetes/manifests/kube-controller-manager.yaml
......#- --port=0 #搜索port=0,把這一行注釋掉systemctl restart kubelet#部署網(wǎng)絡(luò)插件flannel
所有節(jié)點(diǎn)上傳 flannel 鏡像 flannel.tar 和網(wǎng)絡(luò)插件 cni-plugins-linux-amd64-v0.8.6.tgz 到 /opt 目錄,master節(jié)點(diǎn)上傳 kube-flannel.yml 文件
cd /opt
docker load < flannel.tarmv /opt/cni /opt/cni_bak
mkdir -p /opt/cni/bin
tar zxvf cni-plugins-linux-amd64-v0.8.6.tgz -C /opt/cni/binkubectl apply -f kube-flannel.yml //所有節(jié)點(diǎn)加入集群
#master 節(jié)點(diǎn)加入集群
kubeadm join 192.168.82.200:6444 --token 7t2weq.bjbawausm0jaxury \--discovery-token-ca-cert-hash sha256:e76e4525ca29a9ccd5c24142a724bdb6ab86512420215242c4313fb830a4eb98 \--control-plane --certificate-key 0f2a7ff2c46ec172f834e237fcca8a02e7c29500746594c25d995b78c92dde96mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config#node 節(jié)點(diǎn)加入集群
kubeadm join 192.168.82.200:6444 --token 7t2weq.bjbawausm0jaxury \--discovery-token-ca-cert-hash sha256:e76e4525ca29a9ccd5c24142a724bdb6ab86512420215242c4313fb830a4eb98
#在 master01 查看集群信息
kubectl get nodes
NAME STATUS ROLES AGE VERSION
master01 Ready control-plane,master 2h5m v1.20.15
master02 Ready control-plane,master 2h5m v1.20.15
master03 Ready control-plane,master 2h5m v1.20.15
node01 Ready <none> 2h5m v1.20.15
node02 Ready <none> 2h5m v1.20.15kubectl get pod -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-74ff55c5b-4fg44 1/1 Running 2 2h5m
coredns-74ff55c5b-jsdxz 1/1 Running 0 2h5m
etcd-master01 1/1 Running 1 2h5m
etcd-master02 1/1 Running 1 2h5m
etcd-master03 1/1 Running 1 2h5m
kube-apiserver-master01 1/1 Running 1 2h5m
kube-apiserver-master02 1/1 Running 1 2h5m
kube-apiserver-master03 1/1 Running 1 2h5m
kube-controller-manager-master01 1/1 Running 3 2h5m
kube-controller-manager-master02 1/1 Running 1 2h5m
kube-controller-manager-master03 1/1 Running 2 2h5m
kube-flannel-ds-8qtx6 1/1 Running 2 2h4m
kube-flannel-ds-lmzdz 1/1 Running 0 2h4m
kube-flannel-ds-nb9qx 1/1 Running 1 2h4m
kube-flannel-ds-t4l4x 1/1 Running 1 2h4m
kube-flannel-ds-v592x 1/1 Running 1 2h4m
kube-proxy-6gd5j 1/1 Running 1 2h5m
kube-proxy-f8k96 1/1 Running 3 2h5m
kube-proxy-h7nrf 1/1 Running 1 2h5m
kube-proxy-j96b6 1/1 Running 1 2h5m
kube-proxy-mgmx6 1/1 Running 0 2h5m
kube-scheduler-master01 1/1 Running 1 2h5m
kube-scheduler-master02 1/1 Running 2 2h5m
kube-scheduler-master03 1/1 Running 2 2h5m
五.問題解決
1.加入集群的 Token 過期
注意:Token值在集群初始化后,有效期為 24小時(shí) ,過了24小時(shí)過期。進(jìn)行重新生成Token,再次加入集群,新生成的Token為 2小時(shí)。1.1、生成Node節(jié)點(diǎn)加入集群的 Token
kubeadm token create --print-join-command
kubeadm join 192.168.82.200:16443 --token menw99.1hbsurvl5fiz119n --discovery-token-ca-cert-hash sha256:e76e4525ca29a9ccd5c24142a724bdb6ab865 12420215242c4313fb830a4eb981.2、生成Master節(jié)點(diǎn)加入集群的 --certificate-key
kubeadm init phase upload-certs --upload-certs
I1105 12:33:08.201601 93226 version.go:254] remote version is much newer: v1.22.3; falling back to: stable-1.20
[upload-certs] Storing the certificates in Secret "kubeadm-certs" in the "kube-system" Namespace
[upload-certs] Using certificate key:
38dba94af7a38700c3698b8acdf8e23f273be07877f5c86f4977dc023e333deb#master節(jié)點(diǎn)加入集群的命令
kubeadm join 192.168.82.200:16443 --token menw99.1hbsurvl5fiz119n --discovery-token-ca-cert-hash sha256:e76e4525ca29a9ccd5c24142a724bdb6ab86512420215242c4313fb830a4eb98 \--control-plane --certificate-key 38dba94af7a38700c3698b8acdf8e23f273be07877f5c86f4977dc023e333deb
2.master節(jié)點(diǎn) 無法部署非系統(tǒng)Pod
解析:主要是因?yàn)閙aster節(jié)點(diǎn)被加上污點(diǎn),污點(diǎn)是不允許部署非系統(tǒng) Pod,在 測試 環(huán)境,可以將污點(diǎn)去除,節(jié)省資源,可利用率。2.1、查看污點(diǎn)
kubectl describe node -l node-role.kubernetes.io/master= | grep Taints
Taints: node-role.kubernetes.io/master:NoSchedule
Taints: node-role.kubernetes.io/master:NoSchedule
Taints: node-role.kubernetes.io/master:NoSchedule2.2、取消污點(diǎn)
kubectl taint node -l node-role.kubernetes.io/master node-role.kubernetes.io/master:NoSchedule-
node/master01 untainted
node/master02 untainted
node/master03 untaintedkubectl describe node -l node-role.kubernetes.io/master= | grep Taints
Taints: <none>
Taints: <none>
Taints: <none>
3.修改NodePort的默認(rèn)端口
原理:默認(rèn)k8s的使用端口的范圍為30000左右,作為對外部提供的端口。我們也可以通過對配置文件的修改去指定默認(rèn)的對外端口的范圍。#報(bào)錯(cuò)
The Service "nginx-svc" is invalid: spec.ports[0].nodePort: Invalid value: 80: provided port is not in the valid range. The range of valid ports is 30000-32767[root@k8s-master1 ~]# vim /etc/kubernetes/manifests/kube-apiserver.yaml
- --service-cluster-ip-range=10.96.0.0/16
- --service-node-port-range=1-65535 #找到后進(jìn)行添加即可#無需重啟,k8s會(huì)自動(dòng)生效
4.外部 etcd 部署配置
kubeadm config print init-defaults > /opt/kubeadm-config.yamlcd /opt/
vim kubeadm-config.yaml
apiVersion: kubeadm.k8s.io/v1beta2
bootstrapTokens:
- groups:- system:bootstrappers:kubeadm:default-node-tokentoken: abcdef.0123456789abcdefttl: 24h0m0susages:- signing- authentication
kind: InitConfiguration
localAPIEndpoint:advertiseAddress: 192.168.82.104bindPort: 6443
nodeRegistration:criSocket: /var/run/dockershim.sockname: master01taints:- effect: NoSchedulekey: node-role.kubernetes.io/master
---
apiServer:certSANs:- 10.96.0.1- 127.0.0.1- localhost- kubernetes- kubernetes.default- kubernetes.default.svc- kubernetes.default.svc.cluster.local- 192.168.82.200- 192.168.82.100- 192.168.82.101- 192.168.82.102- master01- master02- master03timeoutForControlPlane: 4m0s
apiVersion: kubeadm.k8s.io/v1beta2
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
controlPlaneEndpoint: 192.168.82.200:16443
controllerManager: {}
dns:type: CoreDNS
etcd:external: #使用外部etcd的方式endpoints:- https://192.168.82.100:2379- https://192.168.82.101:2379- https://192.168.82.102:2379caFile: /opt/etcd/ssl/ca.pem #需要把etcd的證書都復(fù)制到所有master節(jié)點(diǎn)上certFile: /opt/etcd/ssl/server.pemkeyFile: /opt/etcd/ssl/server-key.pem
imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers
kind: ClusterConfiguration
kubernetesVersion: v1.20.15
networking:dnsDomain: cluster.localpodSubnet: "10.244.0.0/16"serviceSubnet: 10.96.0.0/16
scheduler: {}
---
apiVersion: kubeproxy.config.k8s.io/v1alpha1
kind: KubeProxyConfiguration
mode: ipvs