环境准备
- 节点数量:2台虚拟机 centos7
- 硬件配置:master节点内存至少3G(2G后面在master节点初始化集群时会报错,内存不够),node节点可以2G,CPU至少2个,硬盘至少30G
- 网络要求:多个节点之间网络互通,每个节点能访问外网
集群规划
这两个名称是准备设置的主机名称
- k8s-master
- k8s-node1
设置主机名
hostnamectl set-hostname k8s-master
hostnamectl set-hostname k8s-node1
# 查看主机名
hostname
同步host文件
如果DNS不支持主机名称解析,还需要在每台机器的/etc/hosts文件添加主机名和ip的对应关系。所有机器都要同步。我自己只搞了2台虚拟机做集群。
cat >> /etc/hosts <<EOF
192.168.35.133 k8s-master
192.168.35.132 k8s-node1
EOF
关闭防火墙
所有机器都要关闭
systemctl stop firewalld && systemctl disable firewalld
关闭SELINUX
setenforce 0 && sed -i 's/SELINUX=enforcing/SELINUX=disable/g' /etc/selinux/config
关闭swap分区
每一台机器都要关闭。为什么要关?自己百度去
swapoff -a && sed -ri 's/ .*swap.*/#&/' /etc/fstab
同步时间
yum install ntpdate -y
ntpdate ntp1.aliyun.com;hwclock --systohc
安装containerd
每台机器都要安装
# 安装yum-config-manger依赖
yum install -y yum-utils device-mapper-persistent-data lvm2
# 添加containerd yum源
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 安装containerd
yum install -y containerd.io cri-tools
# 配置containerd
cat > /etc/containerd/config.toml <<EOF
disabled_plugins = ["restart"]
[plugins.linux]
shim_debug = true
[plugins.cri.registry.mirrors."docker.io"]
endpoint = ["https://frz7i079.mirror.aliyuncs.com"]
[plugins.cri]
sandbox_image="registry.aliyuncs.com/google_containers/pause:3.2"
EOF
# 启动containerd服务 并设置开机启动
systemctl enable containerd && systemctl start containerd && systemctl status containerd
# 配置 containerd 配置
cat > /etc/modules-load.d/containerd.conf << EOF
overlay
br_netfilter
EOF
# 配置k8s网络配置
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
# 加载overlay br_netfilter模块
modprobe overlay
modprobe br_netfilter
# 查看当前配置是否生效
sysctl -p /etc/sysctl.d/k8s.conf
添加源
每台机器都安装。你的cpu是x86的就选择x86的,反之选择arm的。
# 查看源
yum repolist
添加x86的源
cat <<EOF > kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gogkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
# 移动并修改
mv kubernetes.repo /etc/yum.repos.d
添加arm的源
cat << EOF > kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-aarch64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gogkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
# 移动并修改
mv kubernetes.repo /etc/yum.repos.d
安装K8s
每台机器都要安装。
# 安装最新版本
yum install -y kubelet kubeadm kubectl
# 指定版本安装
yum install -y kubelet-1.26.0 kubeadm-1.26.0 kubectl-1.26.0
# 启动kubelet
sudo systemctl enable kubelet && sudo systemctl start kubelet && systemctl status kubelet
初始化集群
只需要在master节点上初始化即可。
kubeadm init \
--apiserver-advertise-address=192.168.35.133 \
--image-repository registry.aliyuncs.com/google_containers \
--pod-network-cidr=10.244.0.0/16
- --apiserver-advertise-address:master节点的IP地址
- --image-repository:选择用于拉取控制平面镜像的容器仓库,默认的太慢了,换成阿里云的
- --pod-network-cidr:指明pod网络可以使用的IP地址段。设置后控制平面将会为每一个节点自动分派CIDRs
# master节点执行
mkdir -p $HOME/.kube
# master节点执行
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
# master节点执行
sudo chown $(id -u):$(id -g) $HOME/.kube/config
node加入集群
# 所有node节点执行
kubeadm join 192.168.35.133:6443 --token bzotcq.9uev0sf5a19pgilp \
--discovery-token-ca-cert-hash sha256:98183ddb45ca9f9ee224c3a77fac6b524d2bf1dd31f911f5d023639f80abd43f
master检查是否加入集群成功
kubectl get nodes
解决节点NotReady状态
虽然集群搭建起来,但是不可用,需用通过网络插件解决该问题 https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml 在master节点创建kube-flannel.yml,输入下面内容
vi 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
- apiGroups:
- networking.k8s.io
resources:
- clustercidrs
verbs:
- list
- watch
---
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",
"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.1.2
#image: docker.io/rancher/mirrored-flannelcni-flannel-cni-plugin:v1.1.2
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.22.0
#image: docker.io/rancher/mirrored-flannelcni-flannel:v0.22.0
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.22.0
#image: docker.io/rancher/mirrored-flannelcni-flannel:v0.22.0
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
kubectl apply -f kube-flannel.yml
如果node节点还是NotReady状态,可尝试重启服务,等待几分钟再次查询
# 重启kubelet
sudo systemctl restart kubelet