Centos7.9使用kubeadm部署K8S 1.27.6集群环境(内网通过代理部署)
在内网借助代理服务器,使用kubeadm部署一个k8s集群,单master+2worker节点,K8S版本为1.7.6,使用containerd作为容器运行时。
Centos7.9使用kubeadm部署K8S 1.27.6集群环境(内网通过代理部署)
在内网借助代理服务器,使用kubeadm部署一个k8s集群,单master+2worker节点,K8S版本为1.27.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.27.6 kubeadm-1.27.6 kubectl-1.27.6
# 配置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
更多推荐
所有评论(0)