Kubernetes(簡稱為:k8s)是Google在2014年6月開源的一個容器叢集管理系統,使用Go語言開發,用於管理雲平臺中多個主機上的容器化的應用,Kubernetes的目標是讓部署容器化的應用簡單並且高效,Kubernetes提供了資源排程、部署管理、服務發現、擴容縮容、監控,維護等一整套功能,努力成為跨主機叢集的自動部署、擴充套件以及執行應用程式容器的平臺。 它支援一系列容器工具, 包括Docker、Containerd等。
1.1 主機規劃
主機IP地址 | 主機名 | 主機配置 | 主機角色 | 軟體列表 |
---|---|---|---|---|
192.168.10.12 | k8s-master1 | 2C4G | master | kube-apiserver、kube-controller-manager、kube-scheduler、etcd、kubelet、kube-proxy、Containerd、runc |
192.168.10.13 | k8s-master2 | 2C4G | master | kube-apiserver、kube-controller-manager、kube-scheduler、etcd、kubelet、kube-proxy、Containerd、runc |
192.168.10.14 | k8s-master3 | 2C4G | master | kube-apiserver、kube-controller-manager、kube-scheduler、etcd、kubelet、kube-proxy、Containerd、runc |
192.168.10.15 | k8s-worker1 | 2C4G | worker | kubelet、kube-proxy、Containerd、runc |
192.168.10.10 | ha1 | 1C2G | LB | haproxy、keepalived |
192.168.10.11 | ha2 | 1C2G | LB | haproxy、keepalived |
192.168.10.100 | / | / | VIP(虛擬IP) |
1.2 軟體版本
軟體名稱 | 版本 | 備註 |
---|---|---|
CentOS7 | kernel版本:5.17 | |
kubernetes | v1.21.10 | |
etcd | v3.5.2 | 最新版本 |
calico | v3.19.4 | |
coredns | v1.8.4 | |
containerd | 1.6.1 | |
runc | 1.1.0 | |
haproxy | 5.18 | YUM源預設 |
keepalived | 3.5 | YUM源預設 |
1.3 網路分配
網路名稱 | 網段 | 備註 |
---|---|---|
Node網路 | 192.168.10.0/24 | |
Service網路 | 10.96.0.0/16 | |
Pod網路 | 10.244.0.0/16 |
2.1主機準備
2.1.1 主機名設定
hostnamectl set-hostname xxx
關於主機名參見1.1小節主機規劃表
2.1.2 主機與IP地址解析
cat >> /etc/hosts << EOF
192.168.10.10 ha1
192.168.10.11 ha2
192.168.10.12 k8s-master1
192.168.10.13 k8s-master2
192.168.10.14 k8s-master3
192.168.10.15 k8s-worker1
EOF
2.1.3 主機安全設定
2.1.3.1 關閉防火牆
systemctl stop firewalld
systmctl disable firewalld
firewall-cmd --state
2.1.3.2 關閉selinux
setenforce 0
sed -ri 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
sestatus
2.1.4 交換分割槽設定
swapoff -a
sed -ri 's/.*swap.*/#&/' /etc/fstab
echo "vm.swappiness=0" >> /etc/sysctl.conf
sysctl -p
2.1.5 主機系統時間同步
安裝軟體
yum -y install ntpdate
制定時間同步計劃任務
crontab -e
0 */1 * * * ntpdate time1.aliyun.com
2.1.6 主機系統最佳化
limit最佳化
ulimit -SHn 65535
cat <> /etc/security/limits.conf
- soft nofile 655360
- hard nofile 131072
- soft nproc 655350
- hard nproc 655350
- soft memlock unlimited
- hard memlock unlimited
EOF### 2.1.7 ipvs管理工具安裝及模組載入
為叢集節點安裝,負載均衡節點不用安裝
yum -y install ipvsadm ipset sysstat conntrack libseccomp
所有節點配置ipvs模組,在核心4.19+版本nf_conntrack_ipv4已經改為nf_conntrack, 4.18以下使用nf_conntrack_ipv4即可:
modprobe -- ip_vs modprobe -- ip_vs_rr modprobe -- ip_vs_wrr modprobe -- ip_vs_sh modprobe -- nf_conntrack
建立 /etc/modules-load.d/ipvs.conf 並加入以下內容:
cat >/etc/modules-load.d/ipvs.conf <<EOF 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 ip_tables ip_set xt_set ipt_set ipt_rpfilter ipt_REJECT ipip EOF
2.1.8 載入containerd相關核心模組
臨時載入模組
modprobe overlay
modprobe br_netfilter
永久性載入模組
cat > /etc/modules-load.d/containerd.conf << EOF
overlay
br_netfilter
EOF
設定為開機啟動
systemctl enable –now systemd-modules-load.service
2.1.9 Linux核心升級
> 在所有節點中安裝,需要重新作業系統更換核心。
[root@localhost ~]# yum -y install perl
[root@localhost ~]# rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
[root@localhost ~]# yum -y install https://www.elrepo.org/elrepo-release-7.0-4.el7.elrepo.noarch.rpm
[root@localhost ~]# yum --enablerepo="elrepo-kernel" -y install kernel-ml.x86_64
[root@localhost ~]# grub2-set-default 0
[root@localhost ~]# grub2-mkconfig -o /boot/grub2/grub.cfg
2.1.10 Linux核心最佳化
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 = 131072
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_timestamps = 0
net.core.somaxconn = 16384
EOF
sysctl --system
所有節點配置完核心後,重啟伺服器,保證重啟後核心依舊載入
reboot -h now
重啟後檢視ipvs模組載入情況:
lsmod | grep –color=auto -e ip_vs -e nf_conntrack
重啟後檢視containerd相關模組載入情況:
lsmod | egrep ‘br_netfilter | overlay’
2.1.11 其它工具安裝(選裝)
yum install wget jq psmisc vim net-tools telnet yum-utils device-mapper-persistent-data lvm2 git lrzsz -y
2.2 負載均衡器準備
2.2.1 安裝haproxy與keepalived
yum -y install haproxy keepalived
2.2.2 HAProxy配置
cat >/etc/haproxy/haproxy.cfg<<"EOF"
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:6443
bind 127.0.0.1:6443
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-master1 192.168.10.12:6443 check
server k8s-master2 192.168.10.13:6443 check
server k8s-master3 192.168.10.14:6443 check
EOF
2.2.3 KeepAlived
主從配置不一致,需要注意。
ha1: cat >/etc/keepalived/keepalived.conf<<"EOF" ! 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.10.10 virtual_router_id 51 priority 100 advert_int 2 authentication { auth_type PASS auth_pass K8SHA_KA_AUTH } virtual_ipaddress { 192.168.10.100 } track_script { chk_apiserver } } EOF
ha2:
cat >/etc/keepalived/keepalived.conf<<”EOF”
! 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.10.11
virtual_router_id 51
priority 99
advert_int 2
authentication {
auth_type PASS
auth_pass K8SHA_KA_AUTH
}
virtual_ipaddress {
192.168.10.100
}
track_script {
chk_apiserver
}
}
EOF
### 2.2.4 健康檢查指令碼
> ha1及ha2均要配置
cat > /etc/keepalived/check_apiserver.sh <<”EOF”
#!/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
EOF
chmod +x /etc/keepalived/check_apiserver.sh
### 2.2.5 啟動服務並驗證
systemctl daemon-reload
systemctl enable –now haproxy
systemctl enable –now keepalived
ip address show
## 2.3 配置免密登入
> 在k8s-master1上操作
ssh-keygen
ssh-copy-id root@k8s-master1
ssh-copy-id root@k8s-master2
ssh-copy-id root@k8s-master3
ssh-copy-id root@k8s-worker1
ssh root@k8s-master1
## 2.4 部署ETCD叢集
> 在k8s-master1上操作。
### 2.4.1 建立工作目錄
mkdir -p /data/k8s-work
### 2.4.2 獲取cfssl工具
cd /data/k8s-work
wget pkg.cfssl.org/R1.2/cfssl_linux-amd...
wget pkg.cfssl.org/R1.2/cfssljson_linux...
wget pkg.cfssl.org/R1.2/cfssl-certinfo_...
說明:
cfssl是使用go編寫,由CloudFlare開源的一款PKI/TLS工具。主要程式有:
- cfssl,是CFSSL的命令列工具
- cfssljson用來從cfssl程式獲取JSON輸出,並將證照,金鑰,CSR和bundle寫入檔案中。
chmod +x cfssl*
mv cfssl_linux-amd64 /usr/local/bin/cfssl
mv cfssljson_linux-amd64 /usr/local/bin/cfssljson
mv cfssl-certinfo_linux-amd64 /usr/local/bin/cfssl-certinfo
Version: 1.2.0
Revision: dev
Runtime: go1.6
### 2.4.3 建立CA證照
#### 2.4.3.1 配置ca證照請求檔案
cat > ca-csr.json <<”EOF”
{
“CN”: “kubernetes”,
“key”: {
“algo”: “rsa”,
“size”: 2048
},
“names”: [
{
“C”: “CN”,
“ST”: “Beijing”,
“L”: “Beijing”,
“O”: “kubemsb”,
“OU”: “CN”
}
],
“ca”: {
“expiry”: “87600h”
}
}
EOF
#### 2.4.3.2 建立ca證照
cfssl gencert -initca ca-csr.json | cfssljson -bare ca
#### 2.4.3.3 配置ca證照策略
cfssl print-defaults config > ca-config.json
cat > ca-config.json <<”EOF”
{
“signing”: {
“default”: {
“expiry”: “87600h”
},
“profiles”: {
“kubernetes”: {
“usages”: [
“signing”,
“key encipherment”,
“server auth”,
“client auth”
],
“expiry”: “87600h”
}
}
}
}
EOF
server auth 表示client可以對使用該ca對server提供的證照進行驗證
client auth 表示server可以使用該ca對client提供的證照進行驗證
### 2.4.4 建立etcd證照
#### 2.4.4.1 配置etcd請求檔案
cat > etcd-csr.json <<”EOF”
{
“CN”: “etcd”,
“hosts”: [
“127.0.0.1”,
“192.168.10.12”,
“192.168.10.13”,
“192.168.10.14”
],
“key”: {
“algo”: “rsa”,
“size”: 2048
},
“names”: [{
“C”: “CN”,
“ST”: “Beijing”,
“L”: “Beijing”,
“O”: “kubemsb”,
“OU”: “CN”
}]
}
EOF
#### 2.4.4.2 生成etcd證照
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes etcd-csr.json | cfssljson -bare etcd
# ls
輸出
ca-config.json ca.csr ca-csr.json ca-key.pem ca.pem etcd.csr etcd-csr.json etcd-key.pem etcd.pem
### 2.4.5 部署etcd叢集
#### 2.4.5.1 下載etcd軟體包
![image-20220319090935574](file://D:/BaiduNetdiskDownload/%E4%BA%91%E5%8E%9F%E7%94%9F%E5%A4%A7%E7%A4%BC%E5%8C%85/2.kubernetes%E4%BB%8E%E5%85%A5%E9%97%A8%E5%88%B0%E7%B2%BE%E9%80%9A/%E4%BA%8C%E8%BF%9B%E5%88%B6%E9%83%A8%E7%BD%B2Kubernetes%E9%AB%98%E5%8F%AF%E7%94%A8%E9%9B%86%E7%BE%A4%EF%BC%88Runtime%20Containerd%EF%BC%89/02_%E4%BA%8C%E8%BF%9B%E5%88%B6%E9%83%A8%E7%BD%B2Kubernetes%E9%AB%98%E5%8F%AF%E7%94%A8%E9%9B%86%E7%BE%A4%EF%BC%88Runtime%20Containerd%EF%BC%89/01_%E7%AC%94%E8%AE%B0/Kubernetes%E9%AB%98%E5%8F%AF%E7%94%A8%E9%9B%86%E7%BE%A4%E4%BA%8C%E8%BF%9B%E5%88%B6%E9%83%A8%E7%BD%B2%EF%BC%88Runtime%20Containerd%EF%BC%89.assets/image-20220319090935574.png?lastModify=1663139889)
![image-20220319091008943](file://D:/BaiduNetdiskDownload/%E4%BA%91%E5%8E%9F%E7%94%9F%E5%A4%A7%E7%A4%BC%E5%8C%85/2.kubernetes%E4%BB%8E%E5%85%A5%E9%97%A8%E5%88%B0%E7%B2%BE%E9%80%9A/%E4%BA%8C%E8%BF%9B%E5%88%B6%E9%83%A8%E7%BD%B2Kubernetes%E9%AB%98%E5%8F%AF%E7%94%A8%E9%9B%86%E7%BE%A4%EF%BC%88Runtime%20Containerd%EF%BC%89/02_%E4%BA%8C%E8%BF%9B%E5%88%B6%E9%83%A8%E7%BD%B2Kubernetes%E9%AB%98%E5%8F%AF%E7%94%A8%E9%9B%86%E7%BE%A4%EF%BC%88Runtime%20Containerd%EF%BC%89/01_%E7%AC%94%E8%AE%B0/Kubernetes%E9%AB%98%E5%8F%AF%E7%94%A8%E9%9B%86%E7%BE%A4%E4%BA%8C%E8%BF%9B%E5%88%B6%E9%83%A8%E7%BD%B2%EF%BC%88Runtime%20Containerd%EF%BC%89.assets/image-20220319091008943.png?lastModify=1663139889)
![image-20220319091037753](file://D:/BaiduNetdiskDownload/%E4%BA%91%E5%8E%9F%E7%94%9F%E5%A4%A7%E7%A4%BC%E5%8C%85/2.kubernetes%E4%BB%8E%E5%85%A5%E9%97%A8%E5%88%B0%E7%B2%BE%E9%80%9A/%E4%BA%8C%E8%BF%9B%E5%88%B6%E9%83%A8%E7%BD%B2Kubernetes%E9%AB%98%E5%8F%AF%E7%94%A8%E9%9B%86%E7%BE%A4%EF%BC%88Runtime%20Containerd%EF%BC%89/02_%E4%BA%8C%E8%BF%9B%E5%88%B6%E9%83%A8%E7%BD%B2Kubernetes%E9%AB%98%E5%8F%AF%E7%94%A8%E9%9B%86%E7%BE%A4%EF%BC%88Runtime%20Containerd%EF%BC%89/01_%E7%AC%94%E8%AE%B0/Kubernetes%E9%AB%98%E5%8F%AF%E7%94%A8%E9%9B%86%E7%BE%A4%E4%BA%8C%E8%BF%9B%E5%88%B6%E9%83%A8%E7%BD%B2%EF%BC%88Runtime%20Containerd%EF%BC%89.assets/image-20220319091037753.png?lastModify=1663139889)
wget https://github.com/etcd-io/etcd/releases/download/v3.5.2/etcd-v3.5.2-linux-amd64.tar.gz
#### 2.4.5.2 安裝etcd軟體
tar -xvf etcd-v3.5.2-linux-amd64.tar.gz
cp -p etcd-v3.5.2-linux-amd64/etcd* /usr/local/bin/
#### 2.4.5.3 分發etcd軟體
scp etcd-v3.5.2-linux-amd64/etcd* k8s-master2:/usr/local/bin/
scp etcd-v3.5.2-linux-amd64/etcd* k8s-master3:/usr/local/bin/
#### 2.4.5.4 建立配置檔案
mkdir /etc/etcd
cat > /etc/etcd/etcd.conf <<”EOF”
#[Member]
ETCD_NAME=”etcd1”
ETCD_DATA_DIR=”/var/lib/etcd/default.etcd”
ETCD_LISTEN_PEER_URLS=”https://192.168.10.12:2380"
ETCD_LISTEN_CLIENT_URLS=”https://192.168.10.12:2379,http://127.0.0.1:2379"
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS=”https://192.168.10.12:2380"
ETCD_ADVERTISE_CLIENT_URLS=”https://192.168.10.12:2379"
ETCD_INITIAL_CLUSTER=”etcd1=https://192.168.10.12:2380,etcd2=https://192.168.10.13:2380,etcd3=https://192.168.10.14:2380"
ETCD_INITIAL_CLUSTER_TOKEN=”etcd-cluster”
ETCD_INITIAL_CLUSTER_STATE=”new”
EOF
```
說明:
ETCD_NAME:節點名稱,叢集中唯一
ETCD_DATA_DIR:資料目錄
ETCD_LISTEN_PEER_URLS:叢集通訊監聽地址
ETCD_LISTEN_CLIENT_URLS:客戶端訪問監聽地址
ETCD_INITIAL_ADVERTISE_PEER_URLS:叢集通告地址
ETCD_ADVERTISE_CLIENT_URLS:客戶端通告地址
ETCD_INITIAL_CLUSTER:叢集節點地址
ETCD_INITIAL_CLUSTER_TOKEN:叢集Token
ETCD_INITIAL_CLUSTER_STATE:加入叢集的當前狀態,new是新叢集,existing表示加入已有叢集
2.4.5.5 建立服務配置檔案
mkdir -p /etc/etcd/ssl
mkdir -p /var/lib/etcd/default.etcd
cd /data/k8s-work
cp ca.pem /etc/etcd/ssl
cp etcd.pem /etc/etcd/ssl
cat > /etc/systemd/system/etcd.service <<”EOF”
[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
Type=notify
EnvironmentFile=-/etc/etcd/etcd.conf
WorkingDirectory=/var/lib/etcd/
ExecStart=/usr/local/bin/etcd
–cert-file=/etc/etcd/ssl/etcd.pem
–key-file=/etc/etcd/ssl/etcd-key.pem
–trusted-ca-file=/etc/etcd/ssl/ca.pem
–peer-cert-file=/etc/etcd/ssl/etcd.pem
–peer-key-file=/etc/etcd/ssl/etcd-key.pem
–peer-trusted-ca-file=/etc/etcd/ssl/ca.pem
–peer-client-cert-auth
–client-cert-auth
Restart=on-failure
RestartSec=5
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
2.4.5.6 同步etcd配置到叢集其它master節點
建立目錄
mkdir -p /etc/etcd
mkdir -p /etc/etcd/ssl
mkdir -p /var/lib/etcd/default.etcd
服務配置檔案,需要修改etcd節點名稱及IP地址
for i in k8s-master2 k8s-master3
do
scp /etc/etcd/etcd.conf $i:/etc/etcd/
done
k8s-master2:
cat /etc/etcd/etcd.conf
#[Member]
ETCD_NAME=”etcd2”
ETCD_DATA_DIR=”/var/lib/etcd/default.etcd”
ETCD_LISTEN_PEER_URLS=”https://192.168.10.13:2380"
ETCD_LISTEN_CLIENT_URLS=”https://192.168.10.13:2379,http://127.0.0.1:2379"
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS=”https://192.168.10.13:2380"
ETCD_ADVERTISE_CLIENT_URLS=”https://192.168.10.13:2379"
ETCD_INITIAL_CLUSTER=”etcd1=https://192.168.10.12:2380,etcd2=https://192.168.10.13:2380,etcd3=https://192.168.10.14:2380"
ETCD_INITIAL_CLUSTER_TOKEN=”etcd-cluster”
ETCD_INITIAL_CLUSTER_STATE=”new”
k8s-master3:
cat /etc/etcd/etcd.conf
#[Member]
ETCD_NAME=”etcd3”
ETCD_DATA_DIR=”/var/lib/etcd/default.etcd”
ETCD_LISTEN_PEER_URLS=”https://192.168.10.14:2380"
ETCD_LISTEN_CLIENT_URLS=”https://192.168.10.14:2379,http://127.0.0.1:2379"
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS=”https://192.168.10.14:2380"
ETCD_ADVERTISE_CLIENT_URLS=”https://192.168.10.14:2379"
ETCD_INITIAL_CLUSTER=”etcd1=https://192.168.10.12:2380,etcd2=https://192.168.10.13:2380,etcd3=https://192.168.10.14:2380"
ETCD_INITIAL_CLUSTER_TOKEN=”etcd-cluster”
ETCD_INITIAL_CLUSTER_STATE=”new”
證照檔案
for i in k8s-master2 k8s-master3
do
scp /etc/etcd/ssl/* $i:/etc/etcd/ssl
done
服務啟動配置檔案
for i in k8s-master2 k8s-master3
do
scp /etc/systemd/system/etcd.service $i:/etc/systemd/system/
done
2.4.5.7 啟動etcd叢集
systemctl daemon-reload
systemctl enable –now etcd.service
systemctl status etcd
2.4.5.8 驗證叢集狀態
ETCDCTL_API=3 /usr/local/bin/etcdctl –write-out=table –cacert=/etc/etcd/ssl/ca.pem –cert=/etc/etcd/ssl/etcd.pem –key=/etc/etcd/ssl/etcd-key.pem –endpoints=https://192.168.10.12:2379,https://192.168.10.13:2379,https://192.168.10.14:2379 endpoint health
+—————————-+——–+————-+——-+
| ENDPOINT | HEALTH | TOOK | ERROR |
+—————————-+——–+————-+——-+
| 192.168.10.14:2379 | true | 10.393062ms | |
| 192.168.10.12:2379 | true | 15.70437ms | |
| 192.168.10.13:2379 | true | 15.871684ms | |
+—————————-+——–+————-+——-+
檢查ETCD資料庫效能
ETCDCTL_API=3 /usr/local/bin/etcdctl –write-out=table –cacert=/etc/etcd/ssl/ca.pem –cert=/etc/etcd/ssl/etcd.pem –key=/etc/etcd/ssl/etcd-key.pem –endpoints=https://192.168.10.12:2379,https://192.168.10.13:2379,https://192.168.10.14:2379 check perf
59 / 60 Boooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooom ! 98.33%
PASS: Throughput is 151 writes/s
PASS: Slowest request took 0.066478s
PASS: Stddev is 0.002354s
PASS
ETCDCTL_API=3 /usr/local/bin/etcdctl –write-out=table –cacert=/etc/etcd/ssl/ca.pem –cert=/etc/etcd/ssl/etcd.pem –key=/etc/etcd/ssl/etcd-key.pem –endpoints=https://192.168.10.12:2379,https://192.168.10.13:2379,https://192.168.10.14:2379 member list
+——————+———+——-+—————————-+—————————-+————+
| ID | STATUS | NAME | PEER ADDRS | CLIENT ADDRS | IS LEARNER |
+——————+———+——-+—————————-+—————————-+————+
| 9b449b0ff1d4c375 | started | etcd1 | 192.168.10.12:2380 | 192.168.10.12:2379 | false |
| d1fbb74bc6a61e5c | started | etcd2 | 192.168.10.13:2380 | 192.168.10.13:2379 | false |
| f60b205fb02fe23c | started | etcd3 | 192.168.10.14:2380 | 192.168.10.14:2379 | false |
+——————+———+——-+—————————-+—————————-+————+
ETCDCTL_API=3 /usr/local/bin/etcdctl –write-out=table –cacert=/etc/etcd/ssl/ca.pem –cert=/etc/etcd/ssl/etcd.pem –key=/etc/etcd/ssl/etcd-key.pem –endpoints=https://192.168.10.12:2379,https://192.168.10.13:2379,https://192.168.10.14:2379 endpoint status
+—————————-+——————+———+———+———–+————+———–+————+——————–+——–+
| ENDPOINT | ID | VERSION | DB SIZE | IS LEADER | IS LEARNER | RAFT TERM | RAFT INDEX | RAFT APPLIED INDEX | ERRORS |
+—————————-+——————+———+———+———–+————+———–+————+——————–+——–+
| 192.168.10.12:2379 | 9b449b0ff1d4c375 | 3.5.2 | 24 MB | true | false | 2 | 403774 | 403774 | |
| 192.168.10.13:2379 | d1fbb74bc6a61e5c | 3.5.2 | 24 MB | false | false | 2 | 403774 | 403774 | |
| 192.168.10.14:2379 | f60b205fb02fe23c | 3.5.2 | 24 MB | false | false | 2 | 403774 | 403774 | |
+—————————-+——————+———+———+———–+————+———–+————+——————–+——–+
2.5 Kubernetes叢集部署
2.5.1 Kubernetes軟體包下載
wget dl.k8s.io/v1.21.10/kubernetes-serv...
2.5.2 Kubernetes軟體包安裝
tar -xvf kubernetes-server-linux-amd64.tar.gz
cd kubernetes/server/bin/
cp kube-apiserver kube-controller-manager kube-scheduler kubectl /usr/local/bin/
2.5.3 Kubernetes軟體分發
scp kube-apiserver kube-controller-manager kube-scheduler kubectl k8s-master2:/usr/local/bin/
scp kube-apiserver kube-controller-manager kube-scheduler kubectl k8s-master3:/usr/local/bin/
scp kubelet kube-proxy k8s-master1:/usr/local/bin
scp kubelet kube-proxy k8s-master2:/usr/local/bin
scp kubelet kube-proxy k8s-master3:/usr/local/bin
scp kubelet kube-proxy k8s-worker1:/usr/local/bin
2.5.4 在叢集節點上建立目錄
所有節點
mkdir -p /etc/kubernetes/
mkdir -p /etc/kubernetes/ssl
mkdir -p /var/log/kubernetes
2.5.5 部署api-server
2.5.5.1 建立apiserver證照請求檔案
cat > kube-apiserver-csr.json << “EOF”
{
“CN”: “kubernetes”,
“hosts”: [
“127.0.0.1”,
“192.168.10.12”,
“192.168.10.13”,
“192.168.10.14”,
“192.168.10.15”,
“192.168.10.16”,
“192.168.10.17”,
“192.168.10.18”,
“192.168.10.19”,
“192.168.10.20”,
“192.168.10.100”,
“10.96.0.1”,
“kubernetes”,
“kubernetes.default”,
“kubernetes.default.svc”,
“kubernetes.default.svc.cluster”,
“kubernetes.default.svc.cluster.local”
],
“key”: {
“algo”: “rsa”,
“size”: 2048
},
“names”: [
{
“C”: “CN”,
“ST”: “Beijing”,
“L”: “Beijing”,
“O”: “kubemsb”,
“OU”: “CN”
}
]
}
EOF
說明:
如果 hosts 欄位不為空則需要指定授權使用該證照的 IP(含VIP) 或域名列表。由於該證照被 叢集使用,需要將節點的IP都填上,為了方便後期擴容可以多寫幾個預留的IP。
同時還需要填寫 service 網路的首個IP(一般是 kube-apiserver 指定的 service-cluster-ip-range 網段的第一個IP,如 10.96.0.1)。
2.5.5.2 生成apiserver證照及token檔案
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes kube-apiserver-csr.json | cfssljson -bare kube-apiserver
cat > token.csv << EOF
$(head -c 16 /dev/urandom | od -An -t x | tr -d ‘ ‘),kubelet-bootstrap,10001,”system:kubelet-bootstrap”
EOF
說明:
建立TLS機制所需TOKEN
TLS Bootstraping:Master apiserver啟用TLS認證後,Node節點kubelet和kube-proxy與kube-apiserver進行通訊,必須使用CA簽發的有效證照才可以,當Node節點很多時,這種客戶端證照頒發需要大量工作,同樣也會增加叢集擴充套件複雜度。為了簡化流程,Kubernetes引入了TLS bootstraping機制來自動頒發客戶端證照,kubelet會以一個低許可權使用者自動向apiserver申請證照,kubelet的證照由apiserver動態簽署。所以強烈建議在Node上使用這種方式,目前主要用於kubelet,kube-proxy還是由我們統一頒發一個證照。
2.5.5.3 建立apiserver服務配置檔案
cat > /etc/kubernetes/kube-apiserver.conf << “EOF”
KUBE_APISERVER_OPTS=”–enable-admission-plugins=NamespaceLifecycle,NodeRestriction,LimitRanger,ServiceAccount,DefaultStorageClass,ResourceQuota
–anonymous-auth=false
–bind-address=192.168.10.12
–secure-port=6443
–advertise-address=192.168.10.12
–insecure-port=0
–authorization-mode=Node,RBAC
–runtime-config=api/all=true
–enable-bootstrap-token-auth
–service-cluster-ip-range=10.96.0.0/16
–token-auth-file=/etc/kubernetes/token.csv
–service-node-port-range=30000-32767
–tls-cert-file=/etc/kubernetes/ssl/kube-apiserver.pem
–tls-private-key-file=/etc/kubernetes/ssl/kube-apiserver-key.pem
–client-ca-file=/etc/kubernetes/ssl/ca.pem
–kubelet-client-certificate=/etc/kubernetes/ssl/kube-apiserver.pem
–kubelet-client-key=/etc/kubernetes/ssl/kube-apiserver-key.pem
–service-account-key-file=/etc/kubernetes/ssl/ca-key.pem
–service-account-signing-key-file=/etc/kubernetes/ssl/ca-key.pem
–service-account-issuer=api
–etcd-cafile=/etc/etcd/ssl/ca.pem
–etcd-certfile=/etc/etcd/ssl/etcd.pem
–etcd-keyfile=/etc/etcd/ssl/etcd-key.pem
–etcd-servers=https://192.168.10.12:2379,https://192.168.10.13:2379,https://192.168.10.14:2379
–enable-swagger-ui=true
–allow-privileged=true
–apiserver-count=3
–audit-log-maxage=30
–audit-log-maxbackup=3
–audit-log-maxsize=100
–audit-log-path=/var/log/kube-apiserver-audit.log
–event-ttl=1h
–alsologtostderr=true
–logtostderr=false
–log-dir=/var/log/kubernetes
–v=4”
EOF
2.5.5.4 建立apiserver服務管理配置檔案
cat > /etc/systemd/system/kube-apiserver.service << “EOF”
[Unit]
Description=Kubernetes API Server
Documentation=github.com/kubernetes/kubernetes
After=etcd.service
Wants=etcd.service
[Service]
EnvironmentFile=-/etc/kubernetes/kube-apiserver.conf
ExecStart=/usr/local/bin/kube-apiserver $KUBE_APISERVER_OPTS
Restart=on-failure
RestartSec=5
Type=notify
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
2.5.5.5 同步檔案到叢集master節點
cp ca*.pem /etc/kubernetes/ssl/
cp kube-apiserver*.pem /etc/kubernetes/ssl/
cp token.csv /etc/kubernetes/
scp /etc/kubernetes/token.csv k8s-master2:/etc/kubernetes
scp /etc/kubernetes/token.csv k8s-master3:/etc/kubernetes
scp /etc/kubernetes/ssl/kube-apiserver.pem k8s-master2:/etc/kubernetes/ssl
scp /etc/kubernetes/ssl/kube-apiserver.pem k8s-master3:/etc/kubernetes/ssl
scp /etc/kubernetes/ssl/ca.pem k8s-master2:/etc/kubernetes/ssl
scp /etc/kubernetes/ssl/ca.pem k8s-master3:/etc/kubernetes/ssl
scp /etc/kubernetes/kube-apiserver.conf k8s-master2:/etc/kubernetes/kube-apiserver.conf
KUBE_APISERVER_OPTS=”–enable-admission-plugins=NamespaceLifecycle,NodeRestriction,LimitRanger,ServiceAccount,DefaultStorageClass,ResourceQuota
–anonymous-auth=false
–bind-address=192.168.10.13
–secure-port=6443
–advertise-address=192.168.10.13
–insecure-port=0
–authorization-mode=Node,RBAC
–runtime-config=api/all=true
–enable-bootstrap-token-auth
–service-cluster-ip-range=10.96.0.0/16
–token-auth-file=/etc/kubernetes/token.csv
–service-node-port-range=30000-32767
–tls-cert-file=/etc/kubernetes/ssl/kube-apiserver.pem
–tls-private-key-file=/etc/kubernetes/ssl/kube-apiserver-key.pem
–client-ca-file=/etc/kubernetes/ssl/ca.pem
–kubelet-client-certificate=/etc/kubernetes/ssl/kube-apiserver.pem
–kubelet-client-key=/etc/kubernetes/ssl/kube-apiserver-key.pem
–service-account-key-file=/etc/kubernetes/ssl/ca-key.pem
–service-account-signing-key-file=/etc/kubernetes/ssl/ca-key.pem
–service-account-issuer=api
–etcd-cafile=/etc/etcd/ssl/ca.pem
–etcd-certfile=/etc/etcd/ssl/etcd.pem
–etcd-keyfile=/etc/etcd/ssl/etcd-key.pem
–etcd-servers=https://192.168.10.12:2379,https://192.168.10.13:2379,https://192.168.10.14:2379
–enable-swagger-ui=true
–allow-privileged=true
–apiserver-count=3
–audit-log-maxage=30
–audit-log-maxbackup=3
–audit-log-maxsize=100
–audit-log-path=/var/log/kube-apiserver-audit.log
–event-ttl=1h
–alsologtostderr=true
–logtostderr=false
–log-dir=/var/log/kubernetes
–v=4”
cp /etc/kubernetes/kube-apiserver.conf k8s-master3:/etc/kubernetes/kube-apiserver.conf
KUBE_APISERVER_OPTS=”–enable-admission-plugins=NamespaceLifecycle,NodeRestriction,LimitRanger,ServiceAccount,DefaultStorageClass,ResourceQuota
–anonymous-auth=false
–bind-address=192.168.10.14
–secure-port=6443
–advertise-address=192.168.10.14
–insecure-port=0
–authorization-mode=Node,RBAC
–runtime-config=api/all=true
–enable-bootstrap-token-auth
–service-cluster-ip-range=10.96.0.0/16
–token-auth-file=/etc/kubernetes/token.csv
–service-node-port-range=30000-32767
–tls-cert-file=/etc/kubernetes/ssl/kube-apiserver.pem
–tls-private-key-file=/etc/kubernetes/ssl/kube-apiserver-key.pem
–client-ca-file=/etc/kubernetes/ssl/ca.pem
–kubelet-client-certificate=/etc/kubernetes/ssl/kube-apiserver.pem
–kubelet-client-key=/etc/kubernetes/ssl/kube-apiserver-key.pem
–service-account-key-file=/etc/kubernetes/ssl/ca-key.pem
–service-account-signing-key-file=/etc/kubernetes/ssl/ca-key.pem
–service-account-issuer=api
–etcd-cafile=/etc/etcd/ssl/ca.pem
–etcd-certfile=/etc/etcd/ssl/etcd.pem
–etcd-keyfile=/etc/etcd/ssl/etcd-key.pem
–etcd-servers=https://192.168.10.12:2379,https://192.168.10.13:2379,https://192.168.10.14:2379
–enable-swagger-ui=true
–allow-privileged=true
–apiserver-count=3
–audit-log-maxage=30
–audit-log-maxbackup=3
–audit-log-maxsize=100
–audit-log-path=/var/log/kube-apiserver-audit.log
–event-ttl=1h
–alsologtostderr=true
–logtostderr=false
–log-dir=/var/log/kubernetes
–v=4”
scp /etc/systemd/system/kube-apiserver.service k8s-master2:/etc/systemd/system/kube-apiserver.service
scp /etc/systemd/system/kube-apiserver.service k8s-master3:/etc/systemd/system/kube-apiserver.service
2.5.5.6 啟動apiserver服務
systemctl daemon-reload
systemctl enable –now kube-apiserver
systemctl status kube-apiserver
curl –insecure 192.168.10.12:6443/
curl –insecure 192.168.10.13:6443/
curl –insecure 192.168.10.14:6443/
curl –insecure 192.168.10.100:6443/
2.5.6 部署kubectl
2.5.6.1 建立kubectl證照請求檔案
cat > admin-csr.json << “EOF”
{
“CN”: “admin”,
“hosts”: [],
“key”: {
“algo”: “rsa”,
“size”: 2048
},
“names”: [
{
“C”: “CN”,
“ST”: “Beijing”,
“L”: “Beijing”,
“O”: “system:masters”,
“OU”: “system”
}
]
}
EOF
說明:
後續 kube-apiserver 使用 RBAC 對客戶端(如 kubelet、kube-proxy、Pod)請求進行授權;
kube-apiserver 預定義了一些 RBAC 使用的 RoleBindings,如 cluster-admin 將 Group system:masters 與 Role cluster-admin 繫結,該 Role 授予了呼叫kube-apiserver 的所有 API的許可權;
O指定該證照的 Group 為 system:masters,kubelet 使用該證照訪問 kube-apiserver 時 ,由於證照被 CA 簽名,所以認證透過,同時由於證照使用者組為經過預授權的 system:masters,所以被授予訪問所有 API 的許可權;
注:
這個admin 證照,是將來生成管理員用的kubeconfig 配置檔案用的,現在我們一般建議使用RBAC 來對kubernetes 進行角色許可權控制, kubernetes 將證照中的CN 欄位 作為User, O 欄位作為 Group;
“O”: “system:masters”, 必須是system:masters,否則後面kubectl create clusterrolebinding報錯。
2.5.6.2 生成證照檔案
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes admin-csr.json | cfssljson -bare admin
2.5.6.3 複製檔案到指定目錄
cp admin*.pem /etc/kubernetes/ssl/
2.5.6.4 生成kubeconfig配置檔案
kube.config 為 kubectl 的配置檔案,包含訪問 apiserver 的所有資訊,如 apiserver 地址、CA 證照和自身使用的證照
kubectl config set-cluster kubernetes –certificate-authority=ca.pem –embed-certs=true –server=192.168.10.100:6443 –kubeconfig=kube.config
kubectl config set-credentials admin –client-certificate=admin.pem –client-key=admin-key.pem –embed-certs=true –kubeconfig=kube.config
kubectl config set-context kubernetes –cluster=kubernetes –user=admin –kubeconfig=kube.config
kubectl config use-context kubernetes –kubeconfig=kube.config
2.5.6.5 準備kubectl配置檔案並進行角色繫結
mkdir ~/.kube
cp kube.config ~/.kube/config
kubectl create clusterrolebinding kube-apiserver:kubelet-apis –clusterrole=system:kubelet-api-admin –user kubernetes –kubeconfig=/root/.kube/config
2.5.6.6 檢視叢集狀態
export KUBECONFIG=$HOME/.kube/config
檢視叢集資訊
kubectl cluster-info
檢視叢集元件狀態
kubectl get componentstatuses
檢視名稱空間中資源物件
kubectl get all –all-namespaces
2.5.6.7 同步kubectl配置檔案到叢集其它master節點
k8s-master2:
mkdir /root/.kube
k8s-master3:
mkdir /root/.kube
scp /root/.kube/config k8s-master2:/root/.kube/config
scp /root/.kube/config k8s-master3:/root/.kube/config
2.5.6.8 配置kubectl命令補全(可選)
yum install -y bash-completion
source /usr/share/bash-completion/bash_completion
source <(kubectl completion bash)
kubectl completion bash > ~/.kube/completion.bash.inc
source ‘/root/.kube/completion.bash.inc’
source $HOME/.bash_profile
2.5.7 部署kube-controller-manager
2.5.7.1 建立kube-controller-manager證照請求檔案
cat > kube-controller-manager-csr.json << “EOF”
{
“CN”: “system:kube-controller-manager”,
“key”: {
“algo”: “rsa”,
“size”: 2048
},
“hosts”: [
“127.0.0.1”,
“192.168.10.12”,
“192.168.10.13”,
“192.168.10.14”
],
“names”: [
{
“C”: “CN”,
“ST”: “Beijing”,
“L”: “Beijing”,
“O”: “system:kube-controller-manager”,
“OU”: “system”
}
]
}
EOF
說明:
hosts 列表包含所有 kube-controller-manager 節點 IP;
CN 為 system:kube-controller-manager;
O 為 system:kube-controller-manager,kubernetes 內建的 ClusterRoleBindings system:kube-controller-manager 賦予 kube-controller-manager 工作所需的許可權
2.5.7.2 建立kube-controller-manager證照檔案
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes kube-controller-manager-csr.json | cfssljson -bare kube-controller-manager
kube-controller-manager.csr
kube-controller-manager-csr.json
kube-controller-manager-key.pem
kube-controller-manager.pem
2.5.7.3 建立kube-controller-manager的kube-controller-manager.kubeconfig
kubectl config set-cluster kubernetes –certificate-authority=ca.pem –embed-certs=true –server=192.168.10.100:6443 –kubeconfig=kube-controller-manager.kubeconfig
kubectl config set-credentials system:kube-controller-manager –client-certificate=kube-controller-manager.pem –client-key=kube-controller-manager-key.pem –embed-certs=true –kubeconfig=kube-controller-manager.kubeconfig
kubectl config set-context system:kube-controller-manager –cluster=kubernetes –user=system:kube-controller-manager –kubeconfig=kube-controller-manager.kubeconfig
kubectl config use-context system:kube-controller-manager –kubeconfig=kube-controller-manager.kubeconfig
2.5.7.4 建立kube-controller-manager配置檔案
cat > kube-controller-manager.conf << “EOF”
KUBE_CONTROLLER_MANAGER_OPTS=”–port=10252
–secure-port=10257
–bind-address=127.0.0.1
–kubeconfig=/etc/kubernetes/kube-controller-manager.kubeconfig
–service-cluster-ip-range=10.96.0.0/16
–cluster-name=kubernetes
–cluster-signing-cert-file=/etc/kubernetes/ssl/ca.pem
–cluster-signing-key-file=/etc/kubernetes/ssl/ca-key.pem
–allocate-node-cidrs=true
–cluster-cidr=10.244.0.0/16
–experimental-cluster-signing-duration=87600h
–root-ca-file=/etc/kubernetes/ssl/ca.pem
–service-account-private-key-file=/etc/kubernetes/ssl/ca-key.pem
–leader-elect=true
–feature-gates=RotateKubeletServerCertificate=true
–controllers=*,bootstrapsigner,tokencleaner
–horizontal-pod-autoscaler-use-rest-clients=true
–horizontal-pod-autoscaler-sync-period=10s
–tls-cert-file=/etc/kubernetes/ssl/kube-controller-manager.pem
–tls-private-key-file=/etc/kubernetes/ssl/kube-controller-manager-key.pem
–use-service-account-credentials=true
–alsologtostderr=true
–logtostderr=false
–log-dir=/var/log/kubernetes
–v=2”
EOF
2.5.7.5 建立服務啟動檔案
cat > kube-controller-manager.service << “EOF”
[Unit]
Description=Kubernetes Controller Manager
Documentation=github.com/kubernetes/kubernetes
[Service]
EnvironmentFile=-/etc/kubernetes/kube-controller-manager.conf
ExecStart=/usr/local/bin/kube-controller-manager $KUBE_CONTROLLER_MANAGER_OPTS
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF
2.5.7.6 同步檔案到叢集master節點
cp kube-controller-manager*.pem /etc/kubernetes/ssl/
cp kube-controller-manager.kubeconfig /etc/kubernetes/
cp kube-controller-manager.conf /etc/kubernetes/
cp kube-controller-manager.service /usr/lib/systemd/system/
scp kube-controller-manager.pem k8s-master2:/etc/kubernetes/ssl/
scp kube-controller-manager.pem k8s-master3:/etc/kubernetes/ssl/
scp kube-controller-manager.kubeconfig kube-controller-manager.conf k8s-master2:/etc/kubernetes/
scp kube-controller-manager.kubeconfig kube-controller-manager.conf k8s-master3:/etc/kubernetes/
scp kube-controller-manager.service k8s-master2:/usr/lib/systemd/system/
scp kube-controller-manager.service k8s-master3:/usr/lib/systemd/system/
#檢視證照
openssl x509 -in /etc/kubernetes/ssl/kube-controller-manager.pem -noout -text
2.5.7.7 啟動服務
systemctl daemon-reload
systemctl enable –now kube-controller-manager
systemctl status kube-controller-manager
kubectl get componentstatuses
2.5.8 部署kube-scheduler
2.5.8.1 建立kube-scheduler證照請求檔案
cat > kube-scheduler-csr.json << “EOF”
{
“CN”: “system:kube-scheduler”,
“hosts”: [
“127.0.0.1”,
“192.168.10.12”,
“192.168.10.13”,
“192.168.10.14”
],
“key”: {
“algo”: “rsa”,
“size”: 2048
},
“names”: [
{
“C”: “CN”,
“ST”: “Beijing”,
“L”: “Beijing”,
“O”: “system:kube-scheduler”,
“OU”: “system”
}
]
}
EOF
2.5.8.2 生成kube-scheduler證照
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes kube-scheduler-csr.json | cfssljson -bare kube-scheduler
kube-scheduler.csr
kube-scheduler-csr.json
kube-scheduler-key.pem
kube-scheduler.pem
2.5.8.3 建立kube-scheduler的kubeconfig
kubectl config set-cluster kubernetes –certificate-authority=ca.pem –embed-certs=true –server=192.168.10.100:6443 –kubeconfig=kube-scheduler.kubeconfig
kubectl config set-credentials system:kube-scheduler –client-certificate=kube-scheduler.pem –client-key=kube-scheduler-key.pem –embed-certs=true –kubeconfig=kube-scheduler.kubeconfig
kubectl config set-context system:kube-scheduler –cluster=kubernetes –user=system:kube-scheduler –kubeconfig=kube-scheduler.kubeconfig
kubectl config use-context system:kube-scheduler –kubeconfig=kube-scheduler.kubeconfig
2.5.8.4 建立服務配置檔案
cat > kube-scheduler.conf << “EOF”
KUBE_SCHEDULER_OPTS=”–address=127.0.0.1
–kubeconfig=/etc/kubernetes/kube-scheduler.kubeconfig
–leader-elect=true
–alsologtostderr=true
–logtostderr=false
–log-dir=/var/log/kubernetes
–v=2”
EOF
2.5.8.5建立服務啟動配置檔案
cat > kube-scheduler.service << “EOF”
[Unit]
Description=Kubernetes Scheduler
Documentation=github.com/kubernetes/kubernetes
[Service]
EnvironmentFile=-/etc/kubernetes/kube-scheduler.conf
ExecStart=/usr/local/bin/kube-scheduler $KUBE_SCHEDULER_OPTS
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF
2.5.8.6 同步檔案至叢集master節點
cp kube-scheduler*.pem /etc/kubernetes/ssl/
cp kube-scheduler.kubeconfig /etc/kubernetes/
cp kube-scheduler.conf /etc/kubernetes/
cp kube-scheduler.service /usr/lib/systemd/system/
scp kube-scheduler.pem k8s-master2:/etc/kubernetes/ssl/
scp kube-scheduler.pem k8s-master3:/etc/kubernetes/ssl/
scp kube-scheduler.kubeconfig kube-scheduler.conf k8s-master2:/etc/kubernetes/
scp kube-scheduler.kubeconfig kube-scheduler.conf k8s-master3:/etc/kubernetes/
scp kube-scheduler.service k8s-master2:/usr/lib/systemd/system/
scp kube-scheduler.service k8s-master3:/usr/lib/systemd/system/
2.5.8.7 啟動服務
systemctl daemon-reload
systemctl enable –now kube-scheduler
systemctl status kube-scheduler
2.5.9 工作節點(worker node)部署
2.5.9.1 Containerd安裝及配置
2.5.9.1.1 獲取軟體包
wget github.com/containerd/containerd/r...
2.5.9.1.2 安裝containerd
tar -xf cri-containerd-cni-1.6.1-linux-amd64.tar.gz -C /
預設解壓後會有如下目錄:
etc
opt
usr
會把對應的目解壓到/下對應目錄中,這樣就省去複製檔案步驟。
2.5.9.1.3 生成配置檔案並修改
mkdir /etc/containerd
containerd config default >/etc/containerd/config.toml
config.toml
下面的配置檔案中已修改,可不執行,僅修改預設時執行。
sed -i ‘s@systemd_cgroup = false@systemd_cgroup = true@’ /etc/containerd/config.toml
下面的配置檔案中已修改,可不執行,僅修改預設時執行。
sed -i ‘s@k8s.gcr.io/pause:3.6@registry.aliyuncs.com/google_containers/pause:3.6@’ /etc/containerd/config.toml
root = “/var/lib/containerd”
state = “/run/containerd”
oom_score = -999
[grpc]
address = “/run/containerd/containerd.sock”
uid = 0
gid = 0
max_recv_message_size = 16777216
max_send_message_size = 16777216
[debug]
address = “”
uid = 0
gid = 0
level = “”
[metrics]
address = “”
grpc_histogram = false
[cgroup]
path = “”
[plugins]
[plugins.cgroups]
no_prometheus = false
[plugins.cri]
stream_server_address = “127.0.0.1”
stream_server_port = “0”
enable_selinux = false
sandbox_image = “registry.aliyuncs.com/google_containers/pause:3.6”
stats_collect_period = 10
systemd_cgroup = true
enable_tls_streaming = false
max_container_log_line_size = 16384
[plugins.cri.containerd]
snapshotter = “overlayfs”
no_pivot = false
[plugins.cri.containerd.default_runtime]
runtime_type = “io.containerd.runtime.v1.linux”
runtime_engine = “”
runtime_root = “”
[plugins.cri.containerd.untrusted_workload_runtime]
runtime_type = “”
runtime_engine = “”
runtime_root = “”
[plugins.cri.cni]
bin_dir = “/opt/cni/bin”
conf_dir = “/etc/cni/net.d”
conf_template = “/etc/cni/net.d/10-default.conf”
[plugins.cri.registry]
[plugins.cri.registry.mirrors]
[plugins.cri.registry.mirrors.”docker.io”]
endpoint = [
“https://docker.mirrors.ustc.edu.cn",
“http://hub-mirror.c.163.com"
]
[plugins.cri.registry.mirrors.”gcr.io”]
endpoint = [
“https://gcr.mirrors.ustc.edu.cn"
]
[plugins.cri.registry.mirrors.”k8s.gcr.io”]
endpoint = [
“https://gcr.mirrors.ustc.edu.cn/google-containers/"
]
[plugins.cri.registry.mirrors.”quay.io”]
endpoint = [
“https://quay.mirrors.ustc.edu.cn"
]
[plugins.cri.registry.mirrors.”harbor.kubemsb.com”]
endpoint = [
“http://harbor.kubemsb.com"
]
[plugins.cri.x509_key_pair_streaming]
tls_cert_file = “”
tls_key_file = “”
[plugins.diff-service]
default = [“walking”]
[plugins.linux]
shim = “containerd-shim”
runtime = “runc”
runtime_root = “”
no_shim = false
shim_debug = false
[plugins.opt]
path = “/opt/containerd”
[plugins.restart]
interval = “10s”
[plugins.scheduler]
pause_threshold = 0.02
deletion_threshold = 0
mutation_threshold = 100
schedule_delay = “0s”
startup_delay = “100ms”
EOF
2.5.9.1.4 安裝runc
由於上述軟體包中包含的runc對系統依賴過多,所以建議單獨下載安裝。
預設runc執行時提示:runc: symbol lookup error: runc: undefined symbol: seccomp_notify_respond
wget github.com/opencontainers/runc/rel...
chmod +x runc.amd64
替換掉原軟體包中的runc
mv runc.amd64 /usr/local/sbin/runc
runc version 1.1.0
commit: v1.1.0-0-g067aaf85
spec: 1.0.2-dev
go: go1.17.6
libseccomp: 2.5.3
systemctl enable containerd
systemctl start containerd
2.5.9.2 部署kubelet
在k8s-master1上操作
2.5.9.2.1 建立kubelet-bootstrap.kubeconfig
BOOTSTRAP_TOKEN=$(awk -F “,” ‘{print $1}’ /etc/kubernetes/token.csv)
kubectl config set-cluster kubernetes –certificate-authority=ca.pem –embed-certs=true –server=192.168.10.100:6443 –kubeconfig=kubelet-bootstrap.kubeconfig
kubectl config set-credentials kubelet-bootstrap –token=${BOOTSTRAP_TOKEN} –kubeconfig=kubelet-bootstrap.kubeconfig
kubectl config set-context default –cluster=kubernetes –user=kubelet-bootstrap –kubeconfig=kubelet-bootstrap.kubeconfig
kubectl config use-context default –kubeconfig=kubelet-bootstrap.kubeconfig
kubectl create clusterrolebinding cluster-system-anonymous –clusterrole=cluster-admin –user=kubelet-bootstrap
kubectl create clusterrolebinding kubelet-bootstrap –clusterrole=system:node-bootstrapper –user=kubelet-bootstrap –kubeconfig=kubelet-bootstrap.kubeconfig
kubectl describe clusterrolebinding cluster-system-anonymous
kubectl describe clusterrolebinding kubelet-bootstrap
2.5.9.2.2 建立kubelet配置檔案
cat > kubelet.json << “EOF”
{
“kind”: “KubeletConfiguration”,
“apiVersion”: “kubelet.config.k8s.io/v1beta1”,
“authentication”: {
“x509”: {
“clientCAFile”: “/etc/kubernetes/ssl/ca.pem”
},
“webhook”: {
“enabled”: true,
“cacheTTL”: “2m0s”
},
“anonymous”: {
“enabled”: false
}
},
“authorization”: {
“mode”: “Webhook”,
“webhook”: {
“cacheAuthorizedTTL”: “5m0s”,
“cacheUnauthorizedTTL”: “30s”
}
},
“address”: “192.168.10.12”,
“port”: 10250,
“readOnlyPort”: 10255,
“cgroupDriver”: “systemd”,
“hairpinMode”: “promiscuous-bridge”,
“serializeImagePulls”: false,
“clusterDomain”: “cluster.local.”,
“clusterDNS”: [“10.96.0.2”]
}
EOF
2.5.9.2.3 建立kubelet服務啟動管理檔案
cat > kubelet.service << “EOF”
[Unit]
Description=Kubernetes Kubelet
Documentation=github.com/kubernetes/kubernetes
After=docker.service
Requires=docker.service
[Service]
WorkingDirectory=/var/lib/kubelet
ExecStart=/usr/local/bin/kubelet
–bootstrap-kubeconfig=/etc/kubernetes/kubelet-bootstrap.kubeconfig
–cert-dir=/etc/kubernetes/ssl
–kubeconfig=/etc/kubernetes/kubelet.kubeconfig
–config=/etc/kubernetes/kubelet.json
–cni-bin-dir=/opt/cni/bin
–cni-conf-dir=/etc/cni/net.d
–container-runtime=remote
–container-runtime-endpoint=unix:///run/containerd/containerd.sock
–network-plugin=cni
–rotate-certificates
–pod-infra-container-image=registry.aliyuncs.com/google_containers/pause:3.2
–root-dir=/etc/cni/net.d
–alsologtostderr=true
–logtostderr=false
–log-dir=/var/log/kubernetes
–v=2
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF
2.5.9.2.4 同步檔案到叢集節點
cp kubelet-bootstrap.kubeconfig /etc/kubernetes/
cp kubelet.json /etc/kubernetes/
cp kubelet.service /usr/lib/systemd/system/
for i in k8s-master2 k8s-master3 k8s-worker1;do scp kubelet-bootstrap.kubeconfig kubelet.json $i:/etc/kubernetes/;done
for i in k8s-master2 k8s-master3 k8s-worker1;do scp ca.pem $i:/etc/kubernetes/ssl/;done
for i in k8s-master2 k8s-master3 k8s-worker1;do scp kubelet.service $i:/usr/lib/systemd/system/;done
說明:
kubelet.json中address需要修改為當前主機IP地址。
2.5.9.2.5 建立目錄及啟動服務
mkdir -p /var/lib/kubelet
mkdir -p /var/log/kubernetes
systemctl daemon-reload
systemctl enable –now kubelet
systemctl status kubelet
NAME STATUS ROLES AGE VERSION
k8s-master1 NotReady 2m55s v1.21.10
k8s-master2 NotReady 45s v1.21.10
k8s-master3 NotReady 39s v1.21.10
k8s-worker1 NotReady 5m1s v1.21.10
NAME AGE SIGNERNAME REQUESTOR CONDITION
csr-b949p 7m55s kubernetes.io/kube-apiserver-client-kubelet kubelet-bootstrap Approved,Issued
csr-c9hs4 3m34s kubernetes.io/kube-apiserver-client-kubelet kubelet-bootstrap Approved,Issued
csr-r8vhp 5m50s kubernetes.io/kube-apiserver-client-kubelet kubelet-bootstrap Approved,Issued
csr-zb4sr 3m40s kubernetes.io/kube-apiserver-client-kubelet kubelet-bootstrap Approved,Issued
說明:
確認kubelet服務啟動成功後,接著到master上Approve一下bootstrap請求。
2.5.9.3 部署kube-proxy
2.5.9.3.1 建立kube-proxy證照請求檔案
cat > kube-proxy-csr.json << “EOF”
{
“CN”: “system:kube-proxy”,
“key”: {
“algo”: “rsa”,
“size”: 2048
},
“names”: [
{
“C”: “CN”,
“ST”: “Beijing”,
“L”: “Beijing”,
“O”: “kubemsb”,
“OU”: “CN”
}
]
}
EOF
2.5.9.3.2 生成證照
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes kube-proxy-csr.json | cfssljson -bare kube-proxy
kube-proxy.csr kube-proxy-csr.json kube-proxy-key.pem kube-proxy.pem
2.5.9.3.3 建立kubeconfig檔案
kubectl config set-cluster kubernetes –certificate-authority=ca.pem –embed-certs=true –server=192.168.10.100:6443 –kubeconfig=kube-proxy.kubeconfig
kubectl config set-credentials kube-proxy –client-certificate=kube-proxy.pem –client-key=kube-proxy-key.pem –embed-certs=true –kubeconfig=kube-proxy.kubeconfig
kubectl config set-context default –cluster=kubernetes –user=kube-proxy –kubeconfig=kube-proxy.kubeconfig
kubectl config use-context default –kubeconfig=kube-proxy.kubeconfig
2.5.9.3.4 建立服務配置檔案
cat > kube-proxy.yaml << “EOF”
apiVersion: kubeproxy.config.k8s.io/v1alpha1
bindAddress: 192.168.10.12
clientConnection:
kubeconfig: /etc/kubernetes/kube-proxy.kubeconfig
clusterCIDR: 10.244.0.0/16
healthzBindAddress: 192.168.10.12:10256
kind: KubeProxyConfiguration
metricsBindAddress: 192.168.10.12:10249
mode: “ipvs”
EOF
2.5.9.3.5 建立服務啟動管理檔案
cat > kube-proxy.service << “EOF”
[Unit]
Description=Kubernetes Kube-Proxy Server
Documentation=github.com/kubernetes/kubernetes
After=network.target
[Service]
WorkingDirectory=/var/lib/kube-proxy
ExecStart=/usr/local/bin/kube-proxy
–config=/etc/kubernetes/kube-proxy.yaml
–alsologtostderr=true
–logtostderr=false
–log-dir=/var/log/kubernetes
–v=2
Restart=on-failure
RestartSec=5
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
2.5.9.3.6 同步檔案到叢集工作節點主機
cp kube-proxy*.pem /etc/kubernetes/ssl/
cp kube-proxy.kubeconfig kube-proxy.yaml /etc/kubernetes/
cp kube-proxy.service /usr/lib/systemd/system/
for i in k8s-master2 k8s-master3 k8s-worker1;do scp kube-proxy.kubeconfig kube-proxy.yaml $i:/etc/kubernetes/;done
for i in k8s-master2 k8s-master3 k8s-worker1;do scp kube-proxy.service $i:/usr/lib/systemd/system/;done
說明:
修改kube-proxy.yaml中IP地址為當前主機IP.
2.5.9.3.7 服務啟動
mkdir -p /var/lib/kube-proxy
systemctl daemon-reload
systemctl enable –now kube-proxy
systemctl status kube-proxy
2.5.10 網路元件部署 Calico
2.5.10.1 下載
wget docs.projectcalico.org/v3.19/manif...
2.5.10.2 修改檔案
3683 - name: CALICO_IPV4POOL_CIDR
3684 value: “10.244.0.0/16”
2.5.10.3 應用檔案
kubectl apply -f calico.yaml
2.5.10.4 驗證應用結果
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system calico-kube-controllers-7cc8dd57d9-tf2m5 1/1 Running 0 72s
kube-system calico-node-llw5w 1/1 Running 0 72s
kube-system calico-node-mhh6g 1/1 Running 0 72s
kube-system calico-node-twj99 1/1 Running 0 72s
kube-system calico-node-zh6xl 1/1 Running 0 72s
NAME STATUS ROLES AGE VERSION
k8s-master1 Ready 55m v1.21.10
k8s-master2 Ready 53m v1.21.10
k8s-master3 Ready 53m v1.21.10
k8s-worker1 Ready 57m v1.21.10
2.5.10 部署CoreDNS
cat > coredns.yaml << “EOF”
apiVersion: v1
kind: ServiceAccount
metadata:
name: coredns
namespace: kube-system
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: system:coredns
rules:
- apiGroups:
- “”
resources: - endpoints
- services
- pods
- namespaces
verbs: - list
- watch
- apiGroups:
- discovery.k8s.io
resources: - endpointslices
verbs: - list
- watch
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
annotations:
rbac.authorization.kubernetes.io/autoupdate: “true”
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: system:coredns
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: system:coredns
subjects:
- kind: ServiceAccount
name: coredns
namespace: kube-system
apiVersion: v1
kind: ConfigMap
metadata:
name: coredns
namespace: kube-system
data:
Corefile: |
.:53 {
errors
health {
lameduck 5s
}
ready
kubernetes cluster.local in-addr.arpa ip6.arpa {
fallthrough in-addr.arpa ip6.arpa
}
prometheus :9153
forward . /etc/resolv.conf {
max_concurrent 1000
}
cache 30
loop
reload
loadbalance
}
apiVersion: apps/v1
kind: Deployment
metadata:
name: coredns
namespace: kube-system
labels:
k8s-app: kube-dns
kubernetes.io/name: “CoreDNS”
spec:
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 1
selector:
matchLabels:
k8s-app: kube-dns
template:
metadata:
labels:
k8s-app: kube-dns
spec:
priorityClassName: system-cluster-critical
serviceAccountName: coredns
tolerations:
- key: “CriticalAddonsOnly”
operator: “Exists”
nodeSelector:
kubernetes.io/os: linux
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution: - weight: 100
podAffinityTerm:
labelSelector:
matchExpressions: - key: k8s-app
operator: In
values: [“kube-dns”]
topologyKey: kubernetes.io/hostname
containers: - name: coredns
image: coredns/coredns:1.8.4
imagePullPolicy: IfNotPresent
resources:
limits:
memory: 170Mi
requests:
cpu: 100m
memory: 70Mi
args: [ “-conf”, “/etc/coredns/Corefile” ]
volumeMounts: - name: config-volume
mountPath: /etc/coredns
readOnly: true
ports: - containerPort: 53
name: dns
protocol: UDP - containerPort: 53
name: dns-tcp
protocol: TCP - containerPort: 9153
name: metrics
protocol: TCP
securityContext:
allowPrivilegeEscalation: false
capabilities:
add: - NET_BIND_SERVICE
drop: - all
readOnlyRootFilesystem: true
livenessProbe:
httpGet:
path: /health
port: 8080
scheme: HTTP
initialDelaySeconds: 60
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 5
readinessProbe:
httpGet:
path: /ready
port: 8181
scheme: HTTP
dnsPolicy: Default
volumes: - name: config-volume
configMap:
name: coredns
items: - key: Corefile
path: Corefile
apiVersion: v1
kind: Service
metadata:
name: kube-dns
namespace: kube-system
annotations:
prometheus.io/port: “9153”
prometheus.io/scrape: “true”
labels:
k8s-app: kube-dns
kubernetes.io/cluster-service: “true”
kubernetes.io/name: “CoreDNS”
spec:
selector:
k8s-app: kube-dns
clusterIP: 10.96.0.2
ports:
- name: dns
port: 53
protocol: UDP - name: dns-tcp
port: 53
protocol: TCP - name: metrics
port: 9153
protocol: TCP
EOF
kubectl apply -f coredns.yaml
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system calico-kube-controllers-7cc8dd57d9-tf2m5 1/1 Running 0 4m7s
kube-system calico-node-llw5w 1/1 Running 0 4m7s
kube-system calico-node-mhh6g 1/1 Running 0 4m7s
kube-system calico-node-twj99 1/1 Running 0 4m7s
kube-system calico-node-zh6xl 1/1 Running 0 4m7s
kube-system coredns-675db8b7cc-ncnf6 1/1 Running 0 26s
2.5.11 部署應用驗證
cat > nginx.yaml << “EOF”
apiVersion: v1
kind: ReplicationController
metadata:
name: nginx-web
spec:
replicas: 2
selector:
name: nginx
template:
metadata:
labels:
name: nginx
spec:
containers:
- name: nginx
image: nginx:1.19.6
ports: - containerPort: 80
apiVersion: v1
kind: Service
metadata:
name: nginx-service-nodeport
spec:
ports:
- port: 80
targetPort: 80
nodePort: 30001
protocol: TCP
type: NodePort
selector:
name: nginx
EOF
kubectl apply -f nginx.yaml
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-web-qzvw4 1/1 Running 0 58s 10.244.194.65 k8s-worker1
nginx-web-spw5t 1/1 Running 0 58s 10.244.224.1 k8s-master2
NAME READY STATUS RESTARTS AGE
pod/nginx-web-qzvw4 1/1 Running 0 2m2s
pod/nginx-web-spw5t 1/1 Running 0 2m2s
NAME DESIRED CURRENT READY AGE
replicationcontroller/nginx-web 2 2 2 2m2s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 443/TCP 3h37m
service/nginx-service-nodeport NodePort 10.96.165.114 80:30001/TCP 2m2s
本作品採用《CC 協議》,轉載必須註明作者和本文連結