1. 准备工作
首先,关闭所有节点的selinux
和iptables
防火墙,关闭swap分区
# 禁用swap分区:
swapoff -a
# 注释掉/etc/fstab文件中的swap定义
配置网桥
vim /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
sysctl --system
systemctl enable --now docker
配置docker
更改cGroupdriver
,添加内容
vim /etc/docker/daemon.json
"exec-opts": ["native.cgroupdriver=systemd"],
重新加载docker
systemctl daemon-reload
systemctl restart docker
2. 安装配置kubeadm
在所有节点安装
- 安装kubeadm
若没有离线安装包,推荐使用阿里云资源,新建一个yum源文件
vim /etc/yum.repos.d/k8s.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=0
yum install -y kubelet kubeadm kubectl
systemctl enable --now kubelet
若有离线安装包,解压之后进入解压目录
yum install -y *
systemctl enable --now kubelet
在控制节点配置
- 查看默认配置信息
kubeadm config print init-defaults
- 拉取所需镜像
若没有离线镜像,还是使用阿里云镜像拉取,阿里云若部分镜像没有,可以使用docker
官方镜像,推送至私有仓库。
# 列出所需镜像
kubeadm config images list --image-repository registry.aliyuncs.com/google_containers
# 拉取镜像
kubeadm config images pull --image-repository registry.aliyuncs.com/google_containers
若有离线镜像包,可推送至私有仓库然后部署
# 离线镜像
#docker load -i k8s-1.21.3.tar
# 阿里云镜像
#docker images | grep ^registry.aliyuncs.com |awk '{ print $1":"$2 }'|awk -F "/" '{system("docker tag "$0" reg.westos.org/k8s/"$3" ") }'
docker images | grep ^reg.westos.org/k8s | awk '{system("docker push "$1"")}'
- 配置环境变量与kubectl补齐
# 环境变量
export KUBECONFIG=/etc/kubernetes/admin.conf
echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> ~/.bash_profile
# 补齐
echo "source <(kubectl completion bash)" >> ~/.bashrc
- 初始化集群
命令后接私有仓库地址,也可以直接通过阿里云镜像初始化。
kubeadm init --pod-network-cidr=10.244.0.0/16 --image-repository reg.westos.org/k8s
3. flannel 网络组件
在控制节点安装flannel
组件,若无flannel
镜像可直接通过github
拉取
https://github.com/coreos/flannel
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
若有flannel
镜像可先上传至私有仓库默认项目中,自行编写kube-flannel.yml
vim kube-flannel.yml
---
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
name: psp.flannel.unprivileged
annotations:
seccomp.security.alpha.kubernetes.io/allowedProfileNames: docker/default
seccomp.security.alpha.kubernetes.io/defaultProfileName: docker/default
apparmor.security.beta.kubernetes.io/allowedProfileNames: runtime/default
apparmor.security.beta.kubernetes.io/defaultProfileName: runtime/default
spec:
privileged: false
volumes:
- configMap
- secret
- emptyDir
- hostPath
allowedHostPaths:
- pathPrefix: "/etc/cni/net.d"
- pathPrefix: "/etc/kube-flannel"
- pathPrefix: "/run/flannel"
readOnlyRootFilesystem: false
# Users and groups
runAsUser:
rule: RunAsAny
supplementalGroups:
rule: RunAsAny
fsGroup:
rule: RunAsAny
# Privilege Escalation
allowPrivilegeEscalation: false
defaultAllowPrivilegeEscalation: false
# Capabilities
allowedCapabilities: ['NET_ADMIN', 'NET_RAW']
defaultAddCapabilities: []
requiredDropCapabilities: []
# Host namespaces
hostPID: false
hostIPC: false
hostNetwork: true
hostPorts:
- min: 0
max: 65535
# SELinux
seLinux:
# SELinux is unused in CaaSP
rule: 'RunAsAny'
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: flannel
rules:
- apiGroups: ['extensions']
resources: ['podsecuritypolicies']
verbs: ['use']
resourceNames: ['psp.flannel.unprivileged']
- apiGroups:
- ""
resources:
- pods
verbs:
- get
- apiGroups:
- ""
resources:
- nodes
verbs:
- list
- watch
- apiGroups:
- ""
resources:
- nodes/status
verbs:
- patch
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: flannel
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: flannel
subjects:
- kind: ServiceAccount
name: flannel
namespace: kube-system
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: flannel
namespace: kube-system
---
kind: ConfigMap
apiVersion: v1
metadata:
name: kube-flannel-cfg
namespace: kube-system
labels:
tier: node
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-system
labels:
tier: node
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
image: flannel:v0.14.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: flannel:v0.14.0
command:
- /opt/bin/flanneld
args:
- --ip-masq
- --kube-subnet-mgr
resources:
requests:
cpu: "100m"
memory: "50Mi"
limits:
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
volumeMounts:
- name: run
mountPath: /run/flannel
- name: flannel-cfg
mountPath: /etc/kube-flannel/
volumes:
- name: run
hostPath:
path: /run/flannel
- name: cni
hostPath:
path: /etc/cni/net.d
- name: flannel-cfg
configMap:
name: kube-flannel-cfg
应用配置文件
kubectl apply -f kube-flannel.yml
4 . 节点扩容
在受控端根据初始化集群返回的token
加入集群
- 受控节点1:
kubeadm join 172.25.52.2:6443 --token owgjeq.leu9qteaclc7mofd --discovery-token-ca-cert-hash sha256:56ef627275f401f47970bd71242ee8fa8288736d68105edad7a9c289220a17ed
- 受控节点2:
kubeadm join 172.25.52.2:6443 --token owgjeq.leu9qteaclc7mofd --discovery-token-ca-cert-hash sha256:56ef627275f401f47970bd71242ee8fa8288736d68105edad7a9c289220a17ed
- 查看集群节点工作状态
在控制端
kubectl get node
kubectl get pod -n kube-system