kubeadm方式部署k8s集群
1 修改主机名(所有主机)
规划三台机器,如下:
ip | 主机名称 | 最低配置 | 职责 |
---|---|---|---|
192.168.1.200 | k8s-master | 4核8G | master节点 |
192.168.1.201 | k8s-node1 | 2核8G | node节点 |
192.168.1.202 | k8s-node2 | 2核8G | node节点 |
hostnamectl set-hostname k8s-master
hostnamectl set-hostname k8s-node1
hostnamectl set-hostname k8s-node2
修改host文件(三台机器一致,可配置一台后,使用scp命名复制到其它机器)
cat >> /etc/hosts << EOF
192.168.1.200 k8s-master
192.168.1.201 k8s-node1
192.168.1.202 k8s-node2
EOF
scp命名复制到其它机器
scp /etc/hosts root@192.168.1.201:/etc/hosts
scp /etc/hosts root@192.168.1.202:/etc/hosts
2.配置阿里云官方源(所有主机)
mkdir -p /etc/yum.repos.d/back
find /etc/yum.repos.d/ -type f -exec mv {} /etc/yum.repos.d/back/ \;
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
3.关闭防火墙(所有主机)
systemctl stop firewalld.service
systemctl disable firewalld.service
4.关闭交换分区和selinux(所有主机)
sed -i.bak '/swap/s/^/#/' /etc/fstab
sed -i 's/^ *SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
5.配置内核转发等相关参数(所有主机)
modprobe br_netfilter
cat <<EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
cat << EOF >> /etc/sysctl.conf
net.ipv4.ip_forward=1
EOF
chmod a+x /etc/rc.local
echo "source /etc/profile" >> /etc/rc.local
echo "modprobe br_netfilter" >> /etc/rc.local
echo "sysctl -p /etc/sysctl.d/k8s.conf" >> /etc/rc.local
echo "sysctl -p" >> //etc/rc.local
reboot
6.安装containerd(所有主机)
由于新的Kubernates [1.24.0以上] 建议使用contanerd, 而且kubernates如何使用containerd 不会像使用docker一样,要中间转几层,故其性能很好。大概CPU使用率减少60%,内存使用率能减少12%。
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
sudo yum install -y containerd.io containerd
sudo systemctl stop containerd.service
sudo containerd config default > /etc/containerd/config.toml
sudo sed -i "s#registry.k8s.io/pause#registry.cn-hangzhou.aliyuncs.com/google_containers/pause#g" /etc/containerd/config.toml
# 更改/etc/containerd/config.toml ,disabled_plugins 中删除 cri
vi /etc/containerd/config.toml
sudo sed -i "s#SystemdCgroup = false#SystemdCgroup = true#g" /etc/containerd/config.toml
sudo systemctl enable --now containerd.service
sudo systemctl status containerd.service
sudo modprobe br_netfilter
7.安装k8s(所有主机)
sudo yum install -y kubelet-1.28.0 kubeadm-1.28.0 kubectl-1.8.0 --disableexcludes=kubernetes --nogpgcheck
sudo systemctl daemon-reload
sudo systemctl restart kubelet
sudo systemctl enable kubelet
8.初始化k8s(master节点)
kubeadm init --kubernetes-version=1.28.0 --apiserver-advertise-address=192.168.1.200 --pod-network-cidr=10.244.0.0/16 --image-repository registry.aliyuncs.com/google_containers
对集群做config认证
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
Node节点加入集群
kubeadm join 192.168.1.200:6443 --token i2meul.73ipaykm3jc2k6vs --discovery-token-ca-cert-hash sha256:9e6d95ffc5ba78a5c71352ffcc9bca2330b17264ebc44facf6359b1ac153f33f
9安装flannel插件(CNI)
#创建文件夹
mkdir flannel && cd flannel
#下载文件
curl -O https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
# kube-flannel.yml里需要下载镜像,我这里提前先下载
docker pull quay.io/coreos/flannel:v0.14.0-rc1
#创建flannel网络插件
kubectl apply -f kube-flannel.yml
#过一会查看k8s集群节点,变成Ready状态了
kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-master Ready master 9m39s v1.19.3
在获取kube-flannel.yml文件时发现很慢,有时候还打不开,索性就记录一下吧,如果curl不到,也可以用下面的代码直接在本地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.2.0
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.3
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.3
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