环境准备
集群规划
这两个名称是准备设置的主机名称
设置主机名
hostnamectl set-hostname k8s-master
hostnamectl set-hostname k8s-node1
# 查看主机名
hostname
同步host文件
如果DNS不支持主机名称解析,还需要在每台机器的/etc/hosts文件添加主机名和ip的对应关系。所有机器都要同步。我自己只搞了2台虚拟机做集群。
cat >> /etc/hosts <
关闭防火墙
所有机器都要关闭
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 < /etc/modules-load.d/containerd.conf << EOF
overlay
br_netfilter
EOF
# 配置k8s网络配置
cat > /etc/sysctl.d/k8s.conf <
添加源
每台机器都安装。你的cpu是x86的就选择x86的,反之选择arm的。
# 查看源
yum repolist
添加x86的源
cat < 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
# 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