Centos7.9使用kubeadm部署K8S 1.27.6叢集環境(內網透過代理部署)
在內網藉助代理伺服器,使用kubeadm部署一個k8s叢集,單master+2worker節點,K8S版本為1.7.6,使用containerd作為容器執行時。
1. 環境資訊
- 作業系統:CentOS 7.9.2009
- 記憶體: 8GB
- CPU: 4
- 網路: 節點透過代理進行訪問。
hostname | ip | 備註 |
---|---|---|
k8s-master1 | 10.210.10.201 | master |
k8s-node1 | 10.210.10.202 | worker |
k8s-node2 | 10.210.10.203 | worker |
2. 準備工作
2.1 配置網路代理
# 配置全域性代理
export proxy=http://10.0.112.18:808
export all_proxy=$proxy
export no_proxy=localhost,10.0.0.0/8,192.168.0.0/16,172.16.0.0/12
# 持久化配置可以將上面配置寫/etc/profile檔案
在k8s初始化的時候,容器拉取映象需要單獨配置代理:
# docker代理
[root@k8s-master1 ~]# mkdir -p /etc/systemd/system/docker.service.d
[root@k8s-master1 ~]# cat /etc/systemd/system/docker.service.d/http-proxy.conf
[Service]
Environment="HTTP_PROXY=http://10.0.112.18:808"
Environment="HTTPS_PROXY=http://10.0.112.18:808"
Environment="NO_RPOXY=localhost,127.0.0.1"
# containerd代理
[root@k8s-master1 ~]# mkdir -p /etc/systemd/system/containerd.service.d
[root@k8s-master1 ~]# cat /etc/systemd/system/containerd.service.d/http-proxy.conf
[Service]
Environment="HTTP_PROXY=http://10.0.112.18:808"
Environment="HTTPS_PROXY=http://10.0.112.18:808"
Environment="NO_RPOXY=localhost,127.0.0.1"
說明:
K8S 1.24版本後啟用了docker-shim容器執行時,本地安裝的k8s版本為1.27.6,選用containd作為容器執行,可以只配置containerd的代理。由於每個節點都要執行容器,所以代理需要在每個節點都需要配置。
2.2 linux基礎配置
以下操作在所有節點執行:
# 配置yum源
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
# 安裝常用軟體
yum install wget vim-enhanced net-tools
# 關閉防火牆
systemctl stop firewalld && systemctl disable firewalld
# 關閉 swap
swapoff -a && sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
# 關閉 selinux
setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
設定hosts:
# 設定主機名
hostnamectl set-hostname k8s-master1 # k8s-node1 / k8s-node2
hostname
# 配置 hosts
cat >> /etc/hosts << EOF
10.210.10.201 k8s-master1
10.210.10.202 k8s-node1
10.210.10.203 k8s-node2
EOF
由於環境在內網,沒有ntp伺服器。這裡手動修改時間,也可以配置內部的ntp伺服器。
# 設定時區
timedatectl set-timezone Asia/Shanghai
# 將系統時間改為utc時間(如果需要)。編輯下面檔案,寫入ZONE="Etc/UTC"
vi /etc/sysconfig/clock
# 建立軟連線
ln -sf /usr/share/zoneinfo/UTC /etc/localtime
# 設定系統時間為當前時間
date -s "2024-06-20 19:04:00"
# 同步硬體時間
hwclock --systohc
配置核心引數:
cat >/etc/modules-load.d/k8s.conf <<EOF
overlay
br_netfilter
EOF
modprobe overlay
modprobe br_netfilter
# 將橋接的IPv4流量傳遞到iptables的鏈
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward=1
EOF
sysctl --system # 生效
# 透過執行以下指令確認 br_netfilter 和 overlay 模組被載入
lsmod | egrep 'overlay|br_netfilter'
# 確認sysctl配置中被設定為 1
sysctl net.bridge.bridge-nf-call-iptables net.bridge.bridge-nf-call-ip6tables net.ipv4.ip_forward
2.3 安裝容器執行時
# 新增映象源
curl https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker-ce.repo
# 檢視docker-ce的版本列表
yum list docker-ce --showduplicates | sort -r
# 刪除 docker(如果有的話)
yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engin
# 安裝必備工具
yum install -y yum-utils device-mapper-persistent-data lvm2
# 安裝 docker和containerd
yum install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
# 生成並修改配置檔案
cp /etc/containerd/config.toml /etc/containerd/config.toml.bak
containerd config default > /etc/containerd/config.toml
#修改
sed -i "s#registry.k8s.io/pause#registry.cn-hangzhou.aliyuncs.com/google_containers/pause#g" /etc/containerd/config.toml
sed -i "s#SystemdCgroup = false#SystemdCgroup = true#g" /etc/containerd/config.toml
# 將 containerd 加入開機自啟
sudo systemctl enable --now containerd.service
# 啟動 docker
sudo systemctl start docker.service
# 將 docker 加入開機自啟
sudo systemctl enable docker.service
sudo systemctl enable docker.socket
sudo systemctl list-unit-files | grep docker
# 設定Docker加速
mkdir -p /etc/docker
cat > /etc/docker/daemon.json << EOF
{
"registry-mirrors": ["https://wnsrsn9i.mirror.aliyuncs.com"],
"exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
# 重啟配置生效
systemctl daemon-reload
systemctl restart docker
docker info
...
Registry Mirrors:
https://wnsrsn9i.mirror.aliyuncs.com/
...
2.4 安裝 kubeadm、kubelet 和 kubectl
# 新增映象源
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
EOF
# 檢視支援的版本
yum list kubelet --showduplicates | sort -r
# 安裝
yum install -y kubelet-1.18.0 kubeadm-1.18.0 kubectl-1.18.0
# 配置kubelet服務自啟動
systemctl enable kubelet
3. 部署k8s叢集
初始化master:
# 提前拉取映象
kubeadm config images pull --image-repository=registry.aliyuncs.com/google_containers
# 執行初始化命令,apiserver地址為master地址。pod網路設定為210.244.0.0/16
kubeadm init --image-repository=registry.aliyuncs.com/google_containers --kubernetes-version=v1.27.6 --apiserver-advertise-address=10.210.10.201 --pod-network-cidr=210.244.0.0/16
...
[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
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 10.210.10.201:6443 --token 3ravyv.7g7re8vu4xpuozmr \
--discovery-token-ca-cert-hash sha256:cc5e30bae2b696a9d9d4535a31ed7b0dc53abe905b2ca7234336e7090f5f317a
...
# 初始化成功後,按照提示執行如下命令
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
# 檢視節點列表,此時節點狀態為NotReady
kubectl get nodes
初始化worker:
kubeadm join 10.210.10.201:6443 --token 3ravyv.7g7re8vu4xpuozmr \
--discovery-token-ca-cert-hash sha256:cc5e30bae2b696a9d9d4535a31ed7b0dc53abe905b2ca7234336e7090f5f317a
master部署網路外掛:
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
kubectl get pods -n kube-system # 檢視執行狀態
如果無法下載,手動建立kube-flannel.yml,內容如下:
---
kind: Namespace
apiVersion: v1
metadata:
name: kube-flannel
labels:
k8s-app: flannel
pod-security.kubernetes.io/enforce: privileged
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
labels:
k8s-app: flannel
name: flannel
rules:
- apiGroups:
- ""
resources:
- pods
verbs:
- get
- apiGroups:
- ""
resources:
- nodes
verbs:
- get
- list
- watch
- apiGroups:
- ""
resources:
- nodes/status
verbs:
- patch
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
labels:
k8s-app: flannel
name: flannel
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: flannel
subjects:
- kind: ServiceAccount
name: flannel
namespace: kube-flannel
---
apiVersion: v1
kind: ServiceAccount
metadata:
labels:
k8s-app: flannel
name: flannel
namespace: kube-flannel
---
kind: ConfigMap
apiVersion: v1
metadata:
name: kube-flannel-cfg
namespace: kube-flannel
labels:
tier: node
k8s-app: flannel
app: flannel
data:
cni-conf.json: |
{
"name": "cbr0",
"cniVersion": "0.3.1",
"plugins": [
{
"type": "flannel",
"delegate": {
"hairpinMode": true,
"isDefaultGateway": true
}
},
{
"type": "portmap",
"capabilities": {
"portMappings": true
}
}
]
}
net-conf.json: |
{
"Network": "10.244.0.0/16",
"EnableNFTables": false,
"Backend": {
"Type": "vxlan"
}
}
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: kube-flannel-ds
namespace: kube-flannel
labels:
tier: node
app: flannel
k8s-app: flannel
spec:
selector:
matchLabels:
app: flannel
template:
metadata:
labels:
tier: node
app: flannel
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/os
operator: In
values:
- linux
hostNetwork: true
priorityClassName: system-node-critical
tolerations:
- operator: Exists
effect: NoSchedule
serviceAccountName: flannel
initContainers:
- name: install-cni-plugin
image: docker.io/flannel/flannel-cni-plugin:v1.4.1-flannel1
command:
- cp
args:
- -f
- /flannel
- /opt/cni/bin/flannel
volumeMounts:
- name: cni-plugin
mountPath: /opt/cni/bin
- name: install-cni
image: docker.io/flannel/flannel:v0.25.4
command:
- cp
args:
- -f
- /etc/kube-flannel/cni-conf.json
- /etc/cni/net.d/10-flannel.conflist
volumeMounts:
- name: cni
mountPath: /etc/cni/net.d
- name: flannel-cfg
mountPath: /etc/kube-flannel/
containers:
- name: kube-flannel
image: docker.io/flannel/flannel:v0.25.4
command:
- /opt/bin/flanneld
args:
- --ip-masq
- --kube-subnet-mgr
resources:
requests:
cpu: "100m"
memory: "50Mi"
securityContext:
privileged: false
capabilities:
add: ["NET_ADMIN", "NET_RAW"]
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: EVENT_QUEUE_DEPTH
value: "5000"
volumeMounts:
- name: run
mountPath: /run/flannel
- name: flannel-cfg
mountPath: /etc/kube-flannel/
- name: xtables-lock
mountPath: /run/xtables.lock
volumes:
- name: run
hostPath:
path: /run/flannel
- name: cni-plugin
hostPath:
path: /opt/cni/bin
- name: cni
hostPath:
path: /etc/cni/net.d
- name: flannel-cfg
configMap:
name: kube-flannel-cfg
- name: xtables-lock
hostPath:
path: /run/xtables.lock
type: FileOrCreate
說明:
flannel.yaml中的"Network"指定的ip地址與kubeadm init初始化時指定的pod網路保持一致。
部署flannel會拉取兩個映象,國內網路環境有時候無法順利拉取,可以從其他地方獲取後離線匯入當前環境:
[root@k8s-master1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
flannel/flannel v0.25.4 e6c43605b714 18 hours ago 81MB
flannel/flannel-cni-plugin v1.4.1-flannel1 1e3c860c213d 7 weeks ago 10.3MB
說明:
本次安裝容器同時安裝了docker和containerd,但是k8s底層對接的是containerd,kubeadm初始化時會走containerd拉取映象,所以網路代理要配置到containerd一側。另外containerd底層有名稱空間的概念,如果容器映象採用離線匯入的方式,需要指定名稱空間,參考命令
ctr -n=k8s.io images import xxx.tar