kubeadm 安裝叢集
準備工作
角色 | IP | 元件 |
---|---|---|
k8s-master | 192.168.1.20 | kube-apiserver,kube-controller-manager,kube-scheduler,docker, etcd |
k8s-node01 | 192.168.1.18 | kubelet,kube-proxy,docker, etcd |
k8s-node02 | 192.168.1.19 | kubelet,kube-proxy,docker, etcd |
- docker版本:docker-ce 20.10.9
- kubernetes版本:v1.21.4
1、關閉防火牆
- systemctl stop firewalld
- systemctl disable firewalld
2、關閉Selinux
- setenforce 0
- sed -i 's/enforcing/disabled/g' /etc/selinux/config
3、關閉swap
- sudo sed -i '/swap/s/^/#/' /etc/fstab #登出掉 含有 swap 的行 /swap/: 匹配包含 swap 的行。s/^/#/: 在每行的開頭(^ 表示行的開頭)新增 # 號
4、修改主機名
- hostnamectl set-hostname k8s-master
- hostnamectl set-hostname k8s-node01
- hostnamectl set-hostname k8s-node02
5、時間同步
yum install ntpdate -y
ntpdate ntp1.aliyun.com
6、定時任務
crontab -e
*/30 * * * * /usr/sbin/ntpdate-u ntp1.aliyun.com >> /var/log/ntpdate.log 2>&1
7、核心引數調整
modprobe br_netfilter
cat > /etc/sysctl.d/kubernetes.conf <<EOF
net.bridge.bridge-nf-call-iptables=1
net.bridge.bridge-nf-call-ip6tables=1
net.ipv4.ip_forward=1
net.ipv4.tcp_tw_recycle=0 # 表示開啟TCP連線中TIME-WAIT sockets的快速回收,預設為0,表示關閉
vm.swappiness=0 # 禁止使用 swap 空間,只有當系統 OOM 時才允許使用它
vm.overcommit_memory=1 # 不檢查實體記憶體是否夠用
fs.inotify.max_user_instances=8192 # 開啟 OOM
vm.panic_on_oom=0
fs.inotify.max_user_watches=1048576
fs.file-max=52706963
fs.nr_open=52706963
net.ipv6.conf.all.disable_ipv6=1
EOF
sysctl -p /etc/sysctl.d/kubernetes.conf
8、開啟 ipvs
lsmod|grep ip_vs
for i in $(ls /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
ls /lib/modules/$(uname -r)/kernel/net/netfilter/ipvs|grep -o "^[^.]*" >> /etc/modules
9、安裝 ipvsadm
yum install ipvsadm ipset -y
docker安裝
centos
# 1、下載阿里 的docker-ce映象源倉庫檔案:
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo #將repo檔案下載到指定的位置
# 2、更新yum源:
yum makecache fast
# 3、檢視docker-ce版本:
yum list docker-ce --showduplicates
# 4、新建docker配置檔案daemon.json(作用是配置docker國內的映象下載的網站、阿里雲加速器):
mkdir /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
"exec-opts": ["native.cgroupdriver=systemd"],
"registry-mirrors":["https://gwsg6nw9.mirror.aliyuncs.com"]
}
EOF
# 5、安裝docker指定版本:
yum install -y --setopt=obsoletes=0 docker-ce-20.10.9-3.el7 #--setopt=obsoletes=0關閉安裝最新的包 指定安裝版本
# 6、啟動docker:
systemctl start docker.service
# 7. 修改cgroup驅動為systemd,適配k8s預設選項
cat > /etc/docker/daemon.json <<EOF
{
"exec-opts": ["native.cgroupdriver=systemd"],
"registry-mirrors":["https://xxxxxxx.aliyuncs.com"], #阿里雲映象加速,這裡需要從阿里雲平臺獲取
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2",
"storage-opts": [
"overlay2.override_kernel_check=true"
]
}
EOF
# 8、檢視docker版本:
docker version
ubuntu
# 1. 安裝GPG證書
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
# 2. 寫入軟體源資訊
add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
# 3. 更新
apt update
# 4. 查詢版本
apt-cache madison docker-ce
#docker-ce | 5:20.10.9~3-0~ubuntu-focal | https://mirrors.aliyun.com/docker-ce/linux/ubuntu focal/stable amd64 Packages
# 5. 安裝
apt install docker-ce=5:20.10.9~3-0~ubuntu-focal -y
# 6. 驗證
docker version
# 7. 修改cgroup驅動為systemd,適配k8s預設選項
cat > /etc/docker/daemon.json <<EOF
{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2",
"storage-opts": [
"overlay2.override_kernel_check=true"
]
}
EOF
systemctl restart docker
kubeadm
簡介
作用:將kuberenets大部分元件都容器化,透過StaticPod方式執行,並自動化了大部分的叢集配置及認證等工作,簡單幾步即可搭建一個可用k8ss的叢集。
-
kubeadm init:master 節點建立
- 檢查當前機器是否合規
- 自動生成叢集執行所需的各類證書及各類配置,並將master節點資訊儲存在名為cluster-info的ConfigMap中
- 透過Static Pod方式,執行API server、controller manager 、scheduler及etcd元件。
- 生成Token以便其他節點加入叢集
-
kubeadm join:node 節點加入叢集
-
節點透過token訪問kube-apiserver,獲取cluster-info中資訊,主要是apiserver的授權資訊(節點信任叢集)。
-
透過授權資訊,kubelet可執行TLS bootstrapping,與apiserver真正建立互信任關係(叢集信任節點)。
安裝元件(kubelet kubeadm kubectl)
centos
# 1、新建repo檔案:
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=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
EOF
# 2、安裝指定的版本元件:
### master ###
yum install -y --setopt=obsoletes=0 kubelet-1.21.4-0 kubeadm-1.21.4-0 kubectl-1.21.4-0
### node ###
yum install -y --setopt=obsoletes=0 kubelet-1.21.4-0 kubeadm-1.21.4-0
ubuntu
# 1. 安裝GPG證書
curl -fsSL https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -
# 2. 寫入軟體源資訊
cat > /etc/apt/sources.list.d/kubernetes.list <<EOF
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF
# 3. 更新
apt update
# 4. 查詢版本
apt-cache madison kubeadm
#kubeadm | 1.21.4-00 | https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial/main amd64 Packages
# 5. 安裝
#### master ####
apt-get install -y kubeadm=1.21.4-00 kubelet=1.21.4-00 kubectl=1.21.4-00
#### node ####
apt-get install -y kubeadm=1.21.4-00 kubelet=1.21.4-00
叢集的安裝
master節點
- 初始化
kubeadm init \
--apiserver-advertise-address=192.168.1.20 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.21.4 \
--service-cidr=10.96.0.0/12 \
--pod-network-cidr=10.244.0.0/16 \
--ignore-preflight-errors=all
# 對應引數含義:
# 節點ip
# 映象倉庫
# 版本
# service網段 預設
# pod網段 預設
# 忽略檢查
- 輸出結果:
[kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate and key
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy
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/
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 192.168.1.20:6443 --token mutg2q.34gp9f77hfm3mzqt \
--discovery-token-ca-cert-hash sha256:5aafdb66ba0d96855c0ec8a59577d4e7b3ba81e2f58069b9b407a0059998372e
- 根據提示,建立 kubectl 認證檔案:
# 即使是root使用者,也採用預設檔案方式
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
- kubectl 命令補齊:
echo "source <(kubectl completion bash)" >> ~/.bashrc
# 立即生效
source <(kubectl completion bash)
Node 節點
- 加入叢集:
- 在node01和node02節點執行
- 根據上面的輸出,執行加入命令
# 在node上執行(ip不用改)
kubeadm join 192.168.1.20:6443 --token mutg2q.34gp9f77hfm3mzqt \
--discovery-token-ca-cert-hash sha256:5aafdb66ba0d96855c0ec8a59577d4e7b3ba81e2f58069b9b407a0059998372e
# 注意:如果某個節點報錯:(這是kubelet報錯導致無法啟動)
報錯1)
Failed to run kubelet" err="failed to run Kubelet: misconfiguration: kubelet cgroup driver: \"systemd\" is different from docker cgroup driver: \"cgroupfs\"
# 在報錯的節點上檢視:
docker info | grep Cgroup
Cgroup Driver: cgroupfs #不是systemd ,因為有些系統不支援cgroupfs ,所以要改為systemd
Cgroup Version: 1
# 修改Cgroup Driver:
# 在/etc/docker/daemon.json中新增如下內容(如果沒有建立)- 可看 上面的安裝docker第7步:
{
"exec-opts":["native.cgroupdriver=systemd"]
}
為什麼要修改為使用systemd?
Kubernetes 推薦使用 systemd 來代替 cgroupf
因為systemd是Kubernetes自帶的cgroup管理器, 負責為每個程序分配cgroups,
但docker的cgroup driver預設是cgroupfs,這樣就同時執行有兩個cgroup控制管理器,
當資源有壓力的情況時,有可能出現不穩定的情況
報錯2):
error execution phase preflight: [preflight] Some fatal errors occurred:
[ERROR FileAvailable--etc-kubernetes-kubelet.conf]: /etc/kubernetes/kubelet.conf already exists
[ERROR Port-10250]: Port 10250 is in use
[ERROR FileAvailable--etc-kubernetes-pki-ca.crt]: /etc/kubernetes/pki/ca.crt already exists
[preflight] If you know what you are doing, you can make a check non-fatal with `--ignore-preflight-errors=...`
- 解決:(注意是在node節點上執行)
如果你正在重新初始化一個 Kubernetes 叢集,可以使用 kubeadm reset 命令來清理先前的設定。
sudo kubeadm reset
以下這個情況是在token過期時操作或後續新增節點時,需要重新生成token。
- token 過期: kubeadm join 加入叢集時,需要2個引數,--token與--discovery-token-ca-cert-hash。其中,token有限期一般是24小時,如果超過時間要新增節點,就需要重新生成token。
# 查詢token(在master上操作)
kubeadm token list
TOKEN TTL EXPIRES USAGES DESCRIPTION EXTRA GROUPS
mutg2q.34gp9f77hfm3mzqt 23h 2024-07-03T22:43:22+08:00 authentication,signing The default bootstrap token generated by 'kubeadm init'. system:bootstrappers:kubeadm:default-node-token
# 建立 token
kubeadm token create
token:l0r2mh.0z5aojdur71posxt
# discovery-token-ca-cert-hash token的hash值
openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'
5aafdb66ba0d96855c0ec8a59577d4e7b3ba81e2f58069b9b407a0059998372e
# 新節點加入(比如新增節點或者原先節點未加入)
kubeadm join 192.168.80.20:6443 --token l0r2mh.0z5aojdur71posxt \
--discovery-token-ca-cert-hash sha256:5aafdb66ba0d96855c0ec8a59577d4e7b3ba81e2f58069b9b407a0059998372e
網路外掛安裝
- 未安裝網路外掛,節點沒有 Ready
kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-master NotReady control-plane,master 41m v1.21.4
k8s-node01 NotReady <none> 35m v1.21.4
k8s-node02 NotReady <none> 8m21s v1.21.4
- 安裝 flannel:
- 如果無法拉取映象:
參考:docker配置代理篇的老方法配置 proxy.conf
# 下載網路外掛yaml檔案
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
# 確保網路配置與 `--pod-network-cidr=10.244.0.0/16` 一致
vi kube-flannel.yml
net-conf.json: |
{
"Network": "10.244.0.0/16",
# 應用網路外掛
kubectl apply -f kube-flannel.yml
# 檢視網路外掛pod
- 預設每個節點都會跑一個flannel 的pod、才能讓叢集正常
kubectl get pod -n kube-flannel
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
kube-flannel-ds-nkjbh 1/1 Running 0 103s 192.168.1.18 k8s-node01 <none> <none>
kube-flannel-ds-qs9sz 1/1 Running 0 103s 192.168.1.20 k8s-master <none> <none>
kube-flannel-ds-xsg6c 1/1 Running 0 103s 192.168.1.19 k8s-node02 <none> <none>
# 檢視節點狀態
kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-master Ready control-plane,master 43m v1.21.4
k8s-node01 Ready <none> 33m v1.21.4
k8s-node02 Ready <none> 32m v1.21.4
ipvs
- 開啟 kube-proxy 協議為 ipvs(master上操作)
kubectl edit configmap kube-proxy -n kube-system
mode: "ipvs"
叢集狀態
- 叢集狀態異常:
$ kubectl get cs
NAME STATUS MESSAGE ERROR
scheduler Unhealthy Get "http://127.0.0.1:10251/healthz": dial tcp 127.0.0.1:10251: connect: connection refused
controller-manager Unhealthy Get "http://127.0.0.1:10252/healthz": dial tcp 127.0.0.1:10252: connect: connection refused
etcd-0 Healthy {"health":"true"}
- 原因:使用了非安全埠。按如下方法修改(master上操作)
$ vi /etc/kubernetes/manifests/kube-scheduler.yaml
...
spec:
containers:
- command:
- kube-scheduler
- --kubeconfig=/etc/kubernetes/scheduler.conf
- --leader-elect=true
#- --port=0 # 註釋掉
image: k8s.gcr.io/kube-scheduler:v1.18.6
$ vi /etc/kubernetes/manifests/kube-controller-manager.yaml
...
spec:
containers:
- command:
- kube-controller-manager
- --node-cidr-mask-size=24
#- --port=0 # 註釋掉
- --requestheader-client-ca-file=/etc/kubernetes/pki/front-proxy-ca.crt
# 重啟kubelet
$ systemctl restart kubelet
# 再次查詢狀態
$ kubectl get cs
NAME STATUS MESSAGE ERROR
scheduler Healthy ok
controller-manager Healthy ok
etcd-0 Healthy {"health":"true"}