kubeadm部署高可用k8s集群
一、基础环境
所有节点hosts文件
$ cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.50.111 k8s-master01
192.168.50.112 k8s-master02
192.168.50.113 k8s-master03
192.168.50.114 k8s-node01
192.168.50.115 k8s-node02
192.168.50.120 k8s-master-lb
所有节点yum源
$ curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
$ yum install -y yum-utils device-mapper-persistent-data lvm2
$ cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[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
$ sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
所有节点安装必备工具
$ yum install wget jq psmisc vim net-tools telnet yum-utils device-mapper-persistent-data lvm2 git -y
关闭所有节点防火墙、selinux、dnsmasq、swap
$ systemctl disable --now firewalld
$ systemctl disable --now dnsmasq
$ systemctl disable --now NetworkManager
$ setenforce 0
$ sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
$ sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/sysconfig/selinux
$ swapoff -a && sysctl -w vm.swappiness=0
$ sed -ri '/^[^#]*swap/s@^@#@' /etc/fstab
所有节点安装ntpdate
$ rpm -ivh http://mirrors.wlnmp.com/centos/wlnmp-release-centos.noarch.rpm
$ yum install ntpdate -y
所有节点配置时间同步
$ ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
$ echo 'Asia/Shanghai' > /etc/timezone
$ crontab -e
*/5 * * * * ntpdate time2.aliyun.com
所有节点配置limit
$ ulimit -SHn 65535
$ vi /etc/security/limits.conf
# 在末尾加入
* soft nofile 655360
* hard nofile 131072
* soft nproc 655360
* hard nproc 655360
* soft memlock unlimited
* hard memlock unlimited
master01节点免密登录其他节点,安装过程中生成配置文件和证书均在master01上操作,集群管理也在master01上操作
$ ssh-keygen -t rsa
$ for i in k8s-master01 k8s-master02 k8s-master03 k8s-node01 k8s-node02;do ssh-copy-id -i .ssh/id_rsa.pub $i;done
所有节点升级系统并重启
$ yum update -y --exclude=kernel* && reboot
二、内核配置
所有节点升级内核
# 载入ELRepo仓库的公共密钥
$ rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
# 安装ELRepo仓库的yum源
$ rpm -Uvh https://www.elrepo.org/elrepo-release-7.el7.elrepo.noarch.rpm
# 查看可用的系统内核包
$ yum --disablerepo="*" --enablerepo="elrepo-kernel" list available
# 移除旧内核相关包
$ yum remove kernel-devel kernel-tools-libs kernel-tools kernel-headers -y
# 安装稳定版内核
$ yum --enablerepo=elrepo-kernel install kernel-lt-devel kernel-lt -y
# 安装内核相关软件
$ yum --disablerepo=\* --enablerepo=elrepo-kernel install -y kernel-lt-devel kernel-lt-tools kernel-lt-tools-libs kernel-lt-tools-libs-devel kernel-lt-headers
# 查看所有可用内核
$ awk -F\' '$1=="menuentry " {print i++ " : " $2}' /etc/grub2.cfg
0 : CentOS Linux (5.4.161-1.el7.elrepo.x86_64) 7 (Core)
1 : CentOS Linux (3.10.0-1160.45.1.el7.x86_64) 7 (Core)
2 : CentOS Linux (3.10.0-1160.el7.x86_64) 7 (Core)
3 : CentOS Linux (0-rescue-d24f071efb984be9bf29fb1a1248b9d0) 7 (Core)
# 设置 GRUB 默认的内核版本
$ grub2-set-default 0
# 重启
$ reboot
# 移除旧内核
$ yum remove kernel-3.* -y
# 生成 grub 配置文件
$ grub2-mkconfig -o /boot/grub2/grub.cfg
所有节点安装ipvsadm
$ yum install ipvsadm ipset sysstat conntrack libseccomp -y
所有节点配置ipvs模块
vim /etc/modules-load.d/ipvs.conf
# 加入以下内容
ip_vs
ip_vs_lc
ip_vs_wlc
ip_vs_rr
ip_vs_wrr
ip_vs_lblc
ip_vs_lblcr
ip_vs_dh
ip_vs_sh
ip_vs_fo
ip_vs_nq
ip_vs_sed
ip_vs_ftp
ip_vs_sh
nf_conntrack_ipv4
ip_tables
ip_set
xt_set
ipt_set
ipt_rpfilter
ipt_REJECT
ipip
加载内核配置
$ systemctl enable --now systemd-modules-load
开启一些k8s集群中必须的内核参数,所有节点配置k8s内核
$ cat <<EOF > /etc/sysctl.d/k8s.conf
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=2310720
net.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
$ sysctl --system
重启
$ reboot
三、基本组件安装
所有节点安装Docker-ce 19.03
# docker yum源
$ yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# 安装Docker-ce 19.03
$ yum install docker-ce-19.03.* -y
新版kubelet建议使用systemed,将所有节点docker的CgroupDriver改成systemd
$ mkdir /etc/docker
$ cat > /etc/docker/daemon.json <<EOF
{
"exec-opts":["native.cgroupdriver=systemd"]
}
EOF
所有节点设置开机自启动Docker
$ systemctl daemon-reload && systemctl enable --now docker
安装k8s组件
所有节点安装最新版kubeadm
$ yum install kubeadm -y
默认配置的pause镜像使用gcr.io仓库,国内可能无法访问,所以这里配置Kubelet使用阿里云的pause镜像:
$ cat >/etc/sysconfig/kubelet<<EOF
KUBELET_EXTRA_ARGS="--pod-infra-container-image=registry.cn-hangzhou.aliyuncs.com/google_containers/pause-amd64:3.2"
EOF
设置Kubelet开机自启动 ( 如果启动失败无须管理,初始化成功以后即可启动 )
$ systemctl daemon-reload
$ systemctl enable --now kubelet
四、高可用组件安装
所有Master节点通过yum安装HAProxy和KeepAlived:
$ yum install keepalived haproxy -y
所有Master节点配置HAProxy(详细配置参考HAProxy文档,所有Master节点的HAProxy配置相同):
$ vim /etc/haproxy/haproxy.cfg
global
maxconn 2000
ulimit-n 16384
log 127.0.0.1 local0 err
stats timeout 30s
defaults
log global
mode http
option httplog
timeout connect 5000
timeout client 50000
timeout server 50000
timeout http-request 15s
timeout http-keep-alive 15s
frontend monitor-in
bind *:33305
mode http
option httplog
monitor-uri /monitor
frontend k8s-master
bind 0.0.0.0:16443
bind 127.0.0.1:16443
mode tcp
option tcplog
tcp-request inspect-delay 5s
default_backend k8s-master
backend k8s-master
mode tcp
option tcplog
option tcp-check
balance roundrobin
default-server inter 10s downinter 5s rise 2 fall 2 slowstart 60s maxconn 250 maxqueue 256 weight 100
server k8s-master01 192.168.50.111:6443 check
server k8s-master02 192.168.50.112:6443 check
server k8s-master03 192.168.50.113:6443 check
所有Master节点配置KeepAlived,配置不一样,注意区分
注意每个节点的IP和网卡(interface参数)
Master01节点的配置:
$ vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
script_user root
enable_script_security
}
vrrp_script chk_apiserver {
script "/etc/keepalived/check_apiserver.sh"
interval 5
weight -5
fall 2
rise 1
}
vrrp_instance VI_1 {
state MASTER
interface ens33
mcast_src_ip 192.168.50.111
virtual_router_id 51
priority 101
advert_int 2
authentication {
auth_type PASS
auth_pass K8SHA_KA_AUTH
}
virtual_ipaddress {
192.168.50.120
}
track_script {
chk_apiserver
}
}
Master02节点的配置:
$ vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
script_user root
enable_script_security
}
vrrp_script chk_apiserver {
script "/etc/keepalived/check_apiserver.sh"
interval 5
weight -5
fall 2
rise 1
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
mcast_src_ip 192.168.50.112
virtual_router_id 51
priority 100
advert_int 2
authentication {
auth_type PASS
auth_pass K8SHA_KA_AUTH
}
virtual_ipaddress {
192.168.50.120
}
track_script {
chk_apiserver
}
}
Master03节点的配置:
$ vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
script_user root
enable_script_security
}
vrrp_script chk_apiserver {
script "/etc/keepalived/check_apiserver.sh"
interval 5
weight -5
fall 2
rise 1
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
mcast_src_ip 192.168.50.113
virtual_router_id 51
priority 100
advert_int 2
authentication {
auth_type PASS
auth_pass K8SHA_KA_AUTH
}
virtual_ipaddress {
192.168.50.120
}
track_script {
chk_apiserver
}
}
所有Master节点配置KeepAlived健康检查文件:
$ vim /etc/keepalived/check_apiserver.sh
#!/bin/bash
err=0
for k in $(seq 1 3)
do
check_code=$(pgrep haproxy)
if [[ $check_code == "" ]]; then
err=$(expr $err + 1)
sleep 1
continue
else
err=0
break
fi
done
if [[ $err != "0" ]]; then
echo "systemctl stop keepalived"
/usr/bin/systemctl stop keepalived
exit 1
else
exit 0
fi
$ chmod +x /etc/keepalived/check_apiserver.sh
$ systemctl daemon-reload
$ systemctl enable --now haproxy
$ systemctl enable --now keepalived
测试VIP
$ ping 192.168.50.120
PING 192.168.50.120 (192.168.50.120) 56(84) bytes of data.
64 bytes from 192.168.50.120: icmp_seq=1 ttl=64 time=0.048 ms
64 bytes from 192.168.50.120: icmp_seq=2 ttl=64 time=0.030 ms
64 bytes from 192.168.50.120: icmp_seq=3 ttl=64 time=0.027 ms
64 bytes from 192.168.50.120: icmp_seq=4 ttl=64 time=0.029 ms
^C
--- 192.168.50.120 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3056ms
rtt min/avg/max/mdev = 0.027/0.033/0.048/0.010 ms
五、初始化集群
Master01节点创建kubeadm-config.yaml配置文件如下:
apiVersion: kubeadm.k8s.io/v1beta2
bootstrapTokens:
- groups:
- system:bootstrappers:kubeadm:default-node-token
token: 7t2weq.bjbawausm0jaxury
ttl: 24h0m0s
usages:
- signing
- authentication
kind: InitConfiguration
localAPIEndpoint:
advertiseAddress: 192.168.50.111
bindPort: 6443
nodeRegistration:
criSocket: /var/run/dockershim.sock
name: k8s-master01
taints:
- effect: NoSchedule
key: node-role.kubernetes.io/master
---
apiServer:
certSANs:
- 192.168.50.120
timeoutForControlPlane: 4m0s
apiVersion: kubeadm.k8s.io/v1beta2
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
controlPlaneEndpoint: 192.168.50.120:16443
controllerManager: {}
dns:
type: CoreDNS
etcd:
local:
dataDir: /var/lib/etcd
imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers
kind: ClusterConfiguration
kubernetesVersion: v1.22.4
networking:
dnsDomain: cluster.local
podSubnet: 172.168.0.0/16
serviceSubnet: 10.96.0.0/12
scheduler: {}
更新kubeadm-config.yaml为new.yaml
$ kubeadm config migrate --old-config kubeadm-config.yaml --new-config new.yaml
将new.yaml文件复制到其他master节点, 之后所有master节点提前下载镜像
$ kubeadm config images pull --config /root/new.yaml
Master01节点初始化,初始化以后会在/etc/kubernetes目录下生成对应的证书和配置文件,之后其他Master节点加入Master01即可:
$ kubeadm init --config /root/new.yaml --upload-certs
如果初始化失败,重置后再次初始化,命令如下:
$ kubeadm reset -f ; ipvsadm --clear ; rm -rf ~/.kube
初始化成功以后,会产生Token值,用于其他节点加入时使用,因此要记录下初始化成功生成的token值(令牌值):
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
Alternatively, if you are the root user, you can run:
export KUBECONFIG=/etc/kubernetes/admin.conf
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 the control-plane node running the following command on each as root:
kubeadm join 192.168.50.120:16443 --token 7t2weq.bjbawausm0jaxury \
--discovery-token-ca-cert-hash sha256:d66dbd1904da037374b33cfc72b256d0f05ecbb4f67aecb87162338aebfe7e81 \
--control-plane --certificate-key 644418bcf3a48ad62fc7c8da7b9c349e6fee8dd82dd9919818f61b576cb9f5ba
Please 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:
kubeadm join 192.168.50.120:16443 --token 7t2weq.bjbawausm0jaxury \
--discovery-token-ca-cert-hash sha256:d66dbd1904da037374b33cfc72b256d0f05ecbb4f67aecb87162338aebfe7e81
Master01节点配置环境变量,用于访问kubernetes集群:
$ cat <<EOF >> /root/.bashrc
export KUBECONFIG=/etc/kubernetes/admin.conf
EOF
$ source /root/.bashrc
查看节点状态:
[root@k8s-master01 ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-master01 NotReady control-plane,master 48m v1.22.4
采用初始化安装方式,所有的系统组件均以容器的方式运行并且在kube-system命名空间内,此时可以查看Pod状态:
[root@k8s-master01 ~]# kubectl get pods -n kube-system -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
coredns-7d89d9b6b8-gc5mp 0/1 Pending 0 50m <none> <none> <none> <none>
coredns-7d89d9b6b8-zg2vd 0/1 Pending 0 50m <none> <none> <none> <none>
etcd-k8s-master01 1/1 Running 0 50m 192.168.50.111 k8s-master01 <none> <none>
kube-apiserver-k8s-master01 1/1 Running 0 50m 192.168.50.111 k8s-master01 <none> <none>
kube-controller-manager-k8s-master01 1/1 Running 0 50m 192.168.50.111 k8s-master01 <none> <none>
kube-proxy-bv7tf 1/1 Running 0 50m 192.168.50.111 k8s-master01 <none> <none>
kube-scheduler-k8s-master01 1/1 Running 0 50m 192.168.50.111 k8s-master01 <none> <none>
六、高可用Master
初始化其他master加入集群
kubeadm join 192.168.50.120:16443 --token 7t2weq.bjbawausm0jaxury \
--discovery-token-ca-cert-hash sha256:d66dbd1904da037374b33cfc72b256d0f05ecbb4f67aecb87162338aebfe7e81 \
--control-plane --certificate-key 644418bcf3a48ad62fc7c8da7b9c349e6fee8dd82dd9919818f61b576cb9f5ba
七、添加Node节点
kubeadm join 192.168.50.120:16443 --token 7t2weq.bjbawausm0jaxury \
--discovery-token-ca-cert-hash sha256:d66dbd1904da037374b33cfc72b256d0f05ecbb4f67aecb87162338aebfe7e81
八、Token过期处理
Token过期后生成新的Token:
$ kubeadm token create --print-join-command
kubeadm join 192.168.50.120:16443 --token 0vh88n.sc2lem9mg3vpn1zv --discovery-token-ca-cert-hash sha256:d66dbd1904da037374b33cfc72b256d0f05ecbb4f67aecb87162338aebfe7e81
Master需要生成 --certificate-key
$ kubeadm init phase upload-certs --upload-certs
[upload-certs] Storing the certificates in Secret "kubeadm-certs" in the "kube-system" Namespace
[upload-certs] Using certificate key:
89397dbe2eccfe45c28f863126375ae455e2c63349eaa2d977daef8798b5adb2
Master加入集群:
$ kubeadm join 192.168.50.120:16443 --token 0vh88n.sc2lem9mg3vpn1zv --discovery-token-ca-cert-hash sha256:d66dbd1904da037374b33cfc72b256d0f05ecbb4f67aecb87162338aebfe7e81 \
--control-plane --certificate-key 89397dbe2eccfe45c28f863126375ae455e2c63349eaa2d977daef8798b5adb2
Node加入集群:
$ kubeadm join 192.168.50.120:16443 --token 0vh88n.sc2lem9mg3vpn1zv --discovery-token-ca-cert-hash sha256:d66dbd1904da037374b33cfc72b256d0f05ecbb4f67aecb87162338aebfe7e81
查看新生成的Token:
[root@k8s-master01 ~]# kubectl get secret -n kube-system
NAME TYPE DATA AGE
bootstrap-token-0vh88n bootstrap.kubernetes.io/token 6 26m
# 可以看到token的前缀为0vh88n
[root@k8s-master01 ~]# kubectl get secret -n kube-system bootstrap-token-0vh88n -oyaml
apiVersion: v1
data:
auth-extra-groups: c3lzdGVtOmJvb3RzdHJhcHBlcnM6a3ViZWFkbTpkZWZhdWx0LW5vZGUtdG9rZW4=
expiration: MjAyMS0xMi0wM1QwNzo1MDoyM1o=
token-id: MHZoODhu
token-secret: c2MybGVtOW1nM3ZwbjF6dg==
usage-bootstrap-authentication: dHJ1ZQ==
usage-bootstrap-signing: dHJ1ZQ==
kind: Secret
metadata:
creationTimestamp: "2021-12-02T07:50:23Z"
name: bootstrap-token-0vh88n
namespace: kube-system
resourceVersion: "9974"
uid: 6861903a-d089-471a-b597-58ddc7018f0e
type: bootstrap.kubernetes.io/token
# 查看过期时间
[root@k8s-master01 ~]# echo "MjAyMS0xMi0wM1QwNzo1MDoyM1o=" | base64 -d
2021-12-03T07:50:23Z
九、Calico组件安装
以下操作只在Master01执行
$ git clone https://github.com/dotbalo/k8s-ha-install.git
$ cd /root/k8s-ha-install && git checkout manual-installation-v1.20.x && cd calico/
修改calico-etcd.yaml的以下位置
sed -i 's#etcd_endpoints: "http://<ETCD_IP>:<ETCD_PORT>"#etcd_endpoints: "https://192.168.50.111:2379,https://192.168.50.112:2379,https://192.168.50.113:2379"#g' calico-etcd.yaml
ETCD_CA=`cat /etc/kubernetes/pki/etcd/ca.crt | base64 | tr -d '\n'`
ETCD_CERT=`cat /etc/kubernetes/pki/etcd/server.crt | base64 | tr -d '\n'`
ETCD_KEY=`cat /etc/kubernetes/pki/etcd/server.key | base64 | tr -d '\n'`
sed -i "s@# etcd-key: null@etcd-key: ${ETCD_KEY}@g; s@# etcd-cert: null@etcd-cert: ${ETCD_CERT}@g; s@# etcd-ca: null@etcd-ca: ${ETCD_CA}@g" calico-etcd.yaml
sed -i 's#etcd_ca: ""#etcd_ca: "/calico-secrets/etcd-ca"#g; s#etcd_cert: ""#etcd_cert: "/calico-secrets/etcd-cert"#g; s#etcd_key: "" #etcd_key: "/calico-secrets/etcd-key" #g' calico-etcd.yaml
POD_SUBNET=`cat /etc/kubernetes/manifests/kube-controller-manager.yaml | grep cluster-cidr= | awk -F= '{print $NF}'`
sed -i 's@# - name: CALICO_IPV4POOL_CIDR@- name: CALICO_IPV4POOL_CIDR@g; s@# value: "192.168.0.0/16"@ value: '"${POD_SUBNET}"'@g' calico-etcd.yaml
创建Calico
$ kubectl apply -f calico-etcd.yaml
查看容器状态
[root@k8s-master01 ~]# kubectl get po -n kube-system
NAME READY STATUS RESTARTS AGE
calico-kube-controllers-cdd5755b9-r4f6s 1/1 Running 0 16m
calico-node-fc5m8 1/1 Running 0 16m
calico-node-fc7mv 1/1 Running 0 16m
calico-node-h8dm9 1/1 Running 0 16m
calico-node-hmgrj 1/1 Running 0 16m
calico-node-lnmc5 1/1 Running 0 16m
coredns-7d89d9b6b8-gc5mp 1/1 Running 0 3h3m
coredns-7d89d9b6b8-zg2vd 1/1 Running 0 3h3m
etcd-k8s-master01 1/1 Running 0 3h3m
etcd-k8s-master02 1/1 Running 0 107m
etcd-k8s-master03 1/1 Running 0 36m
kube-apiserver-k8s-master01 1/1 Running 0 3h3m
kube-apiserver-k8s-master02 1/1 Running 0 107m
kube-apiserver-k8s-master03 1/1 Running 0 36m
kube-controller-manager-k8s-master01 1/1 Running 1 (107m ago) 3h3m
kube-controller-manager-k8s-master02 1/1 Running 0 107m
kube-controller-manager-k8s-master03 1/1 Running 0 36m
kube-proxy-5x5wg 1/1 Running 0 107m
kube-proxy-bv7tf 1/1 Running 0 3h3m
kube-proxy-c2w92 1/1 Running 0 29m
kube-proxy-lrpft 1/1 Running 0 36m
kube-proxy-w75rs 1/1 Running 0 30m
kube-scheduler-k8s-master01 1/1 Running 1 (107m ago) 3h3m
kube-scheduler-k8s-master02 1/1 Running 0 107m
kube-scheduler-k8s-master03 1/1 Running 0 36m
查看集群状态
[root@k8s-master01 ~]# kubectl get node
NAME STATUS ROLES AGE VERSION
k8s-master01 Ready control-plane,master 3h5m v1.22.4
k8s-master02 Ready control-plane,master 109m v1.22.4
k8s-master03 Ready control-plane,master 38m v1.22.4
k8s-node01 Ready <none> 32m v1.22.4
k8s-node02 Ready <none> 32m v1.22.4
十、Metrics Server部署
在新版的Kubernetes中系统资源的采集均使用Metrics-server,可以通过Metrics采集节点和Pod的内存、磁盘、CPU和网络的使用率。
将Master01节点的front-proxy-ca.crt复制到所有Node节点
$ scp /etc/kubernetes/pki/front-proxy-ca.crt k8s-node01:/etc/kubernetes/pki/front-proxy-ca.crt
$ scp /etc/kubernetes/pki/front-proxy-ca.crt k8s-node(其他节点自行拷贝):/etc/kubernetes/pki/front-proxy-ca.crt
安装metrics server
$ cd /root/k8s-ha-install/metrics-server-0.4.x-kubeadm/
[root@k8s-master01 metrics-server-0.4.x-kubeadm]# kubectl create -f comp.yaml
等待kube-system命令空间下的Pod全部启动后,查看状态
[root@k8s-master01 metrics-server-0.4.x-kubeadm]# kubectl top node
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
k8s-master01 162m 8% 1176Mi 63%
k8s-master02 168m 8% 1182Mi 63%
k8s-master03 146m 7% 1078Mi 57%
k8s-node01 71m 3% 794Mi 42%
k8s-node02 81m 4% 851Mi 45%
十一、Dashboard部署
$ cd /root/k8s-ha-install/dashboard/
[root@k8s-master01 dashboard]# kubectl create -f .
更改dashboard的svc为NodePort:
$ kubectl edit svc kubernetes-dashboard -n kubernetes-dashboard
查看端口号:
$ kubectl get svc kubernetes-dashboard -n kubernetes-dashboard
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes-dashboard NodePort 10.108.28.61 <none> 443:30488/TCP 11m
# 可以看到端口号为30488
在谷歌浏览器(Chrome)启动文件中加入启动参数,用于解决无法访问Dashboard的问题,参考图:
--test-type --ignore-certificate-errors
访问Dashboard:https://192.168.50.120:30488(请更改30488为自己的端口),选择登录方式为令牌(即token方式)
查看token值:
$ kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep admin-user | awk '{print $1}')
Name: admin-user-token-hqqcm
Namespace: kube-system
Labels: <none>
Annotations: kubernetes.io/service-account.name: admin-user
kubernetes.io/service-account.uid: 53d47e5e-58e3-4e60-a3d2-4064737c97ea
Type: kubernetes.io/service-account-token
Data
====
ca.crt: 1099 bytes
namespace: 11 bytes
token: eyJhbGciOiJSUzI1NiIsImtpZCI6IlE4czB3aDNuWjNrREFGdlZoODRUR3Z1OWVMbmZNalhuY0s5ZGpUVExPa2sifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJhZG1pbi11c2VyLXRva2VuLWhxcWNtIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6ImFkbWluLXVzZXIiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiI1M2Q0N2U1ZS01OGUzLTRlNjAtYTNkMi00MDY0NzM3Yzk3ZWEiLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6a3ViZS1zeXN0ZW06YWRtaW4tdXNlciJ9.UuwWzbLJNGne6WF2nWNDt2eKn1YwVIQFuFkbZlxl0rIJkqySaJg7OfP9WSrKa8_FvPx_DTTzXdJPLZKN5qkuV4MhQVig9ktt52MeYvIK8i_KVoqRBzMF5t2_IoWENnNWP9sX30iqCo6-FtFqHUQoO6pjq1C4LIsx_rWQIfrwf6lGfuaWoh23J60dhPYUDq9FsIau9trQNHOuOl8bUnduDYm9jl2CFl7Xy13a-U3emGgtXU4N9JaLBJsOkAaYDrpPCPtJeBx5NaWgOiOD2T9zeUfjfeWiODr1mS1YLA_ZVzgYPeivlBMNUbPUl_oRfu5jVZ1sBhewZqzxX9YRnzyGhw
k8s-dashboard官方
https://github.com/kubernetes/dashboard
十二、一些必须更改的配置
将kube-proxy改为ipvs模式,因为在初始化集群的时候注释了ipvs配置,所以需要改一下:
在Master01节点执行
$ kubectl edit cm kube-proxy -n kube-system
mode: "ipvs"
更新kube-proxy的pod:
$ kubectl patch daemonset kube-proxy -p "{\"spec\":{\"template\":{\"metadata\":{\"annotations\":{\"date\":\"`date +'%s'`\"}}}}}" -n kube-system
验证kube-proxy模式
[root@k8s-master01 ~]# curl 127.0.0.1:10249/proxyMode
ipvs
十三、注意事项
注意:kubeadm安装的集群,证书有效期默认是一年。master节点的kube-apiserver、kube-scheduler、kube-controller-manager、etcd都是以容器运行的。可以通过kubectl get po -n kube-system
查看。
启动和二进制不同的是,kubelet的配置文件在/etc/sysconfig/kubelet 和 /var/lib/kubelet/config.yaml ,其他组件的配置文件在/etc/kubernetes/manifests 目录下,比如 kube-apiserver.yaml,该yaml文件更改后,kubelet会自动刷新配置,也就是会重启pod。不能再次创建该文件
kubeadm安装后,master节点默认不允许部署pod,可以通过以下方式打开:
查看Taints:
kubectl describe node -l node-role.kubernetes.io/master= | grep Taints
删除Taint:
kubectl taint node -l node-role.kubernetes.io/master node-role.kubernetes.io/master:NoSchedule-