1.设置hostname
hostnamectl set-hostname k8s-master // master节点
hostnamectl set-hostname k8s-node1 // node1节点
hostnamectl set-hostname k8s-node2 // node2节点
2.设置hosts
后续可以使用别名直接访问别的节点
cat >> /etc/hosts << EOF
192.168.100.40 k8s-master
192.168.100.41 k8s-node1
192.168.100.42 k8s-node2
EOF
3.禁用swap
以前的版本swap可能会影响性能,当前版本未知
sudo swapoff -a
sudo sed -i '/swap/ s/^\(.*\)$/#\1/g' /etc/fstab
4.修改内核参数
-- 启用内核模块
sudo tee /etc/modules-load.d/containerd.conf <<EOF
overlay
br_netfilter
EOF
-- 启用内核模块
sudo modprobe overlay
sudo modprobe br_netfilter
4.sysctl配置
sudo tee /etc/sysctl.d/kubernetes.conf<<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
5.重新加载 sysctl
如果提示找不到sysctl 执行
lsmod |grep conntrack
modprobe ip_conntrack
在执行
sysctl -p
6.安装基础软件
sudo apt install -y curl gnupg2 software-properties-common apt-transport-https ca-certificates
7.添加docker源
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmour -o /etc/apt/trusted.gpg.d/docker.gpg
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
8.安装containerd
sudo apt update
sudo apt install -y containerd.io
9.配置 containerd 用systemdcgroup启动.
-- 生成默认的containerd配置
containerd config default > /etc/containerd/config.toml
-- 修改containerd的配置 Cgroup配置为Systemd
sudo sed -i 's/SystemdCgroup \= false/SystemdCgroup \= true/g' /etc/containerd/config.toml
修改
sandbox_image = "registry.aliyuncs.com/google_containers/pause:3.9"
10.重启containerd使之生效
sudo systemctl restart containerd
sudo systemctl enable containerd
11.设置crictl
-- 生成crictl配置 主要是启用containerd所需要的修改
cat > /etc/crictl.yaml << EOF
runtime-endpoint: unix:///var/run/containerd/containerd.sock
image-endpoint: unix:///var/run/containerd/containerd.sock
timeout: 10
debug: false
EOF
12.添加Kubernetes 阿里巴巴源
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add -
-- 这个源使用的系统代号是xenial,但不影响使用
sudo apt-add-repository "deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main"
13.安装k8s命令行工具
-- 查看当前最新版本 决定安装哪个版本
apt-cache madison kubeadm|head
-- 当前最新版本是1.26.3
apt install -y kubelet=1.26.3-00 kubeadm=1.26.3-00 kubectl=1.26.3-00
14.查看kubeadm启动k8s所需镜像的信息
-- 注意修改版本号
kubeadm config images list \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version=v1.26.3
15.使用阿里镜像站下载所需的镜像
-- 注意修改版本号
kubeadm config images pull \
--kubernetes-version=v1.26.3 \
--image-repository registry.aliyuncs.com/google_containers
16.查看已下载镜像
crictl images
17.生成kubeadm默认配置 (master节点执行)
kubeadm config print init-defaults > kubeadm.yaml
18.参考配置文件(master节点执行)
apiVersion: kubeadm.k8s.io/v1beta3
bootstrapTokens:
- groups:
- system:bootstrappers:kubeadm:default-node-token
token: abcdef.0123456789abcdef
ttl: 24h0m0s
usages:
- signing
- authentication
kind: InitConfiguration
localAPIEndpoint:
##修改masterip
advertiseAddress: 192.168.100.40
bindPort: 6443
nodeRegistration:
criSocket: unix:///var/run/containerd/containerd.sock
imagePullPolicy: IfNotPresent
##修改主机名
name: k8s-master
taints: null
---
apiServer:
timeoutForControlPlane: 4m0s
apiVersion: kubeadm.k8s.io/v1beta3
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
controllerManager: {}
dns: {}
etcd:
local:
dataDir: /var/lib/etcd
### 国内镜像
imageRepository: registry.aliyuncs.com/google_containers
kind: ClusterConfiguration
kubernetesVersion: 1.26.3
networking:
dnsDomain: cluster.local
serviceSubnet: 10.96.0.0/12
## 添加ip段
podSubnet: 10.244.0.0/16
scheduler: {}
19.初始化master(master节点执行)
kubeadm init --config=kubeadm.yaml --upload-certs | tee kubeadm-init.log
20.重置matser(master节点执行)
如果上一步因为配置出错 可以重置初始化 防止重复init因为资源重复报错
Kubeadm reset
21.本地化配置(master节点执行)
-- 生成一个用户配置
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
22.Node节点配置
-- 查看containerd的images相关配置
scp /etc/containerd/config.toml [email protected]:/etc/containerd/config.toml
scp /etc/containerd/config.toml [email protected]:/etc/containerd/config.toml
systemctl daemon-reload && systemctl restart containerd
23.Node节点初始化(node节点执行)
-- master节点执行 生成一个在node节点执行用于初始化node节点的k8s服务的命令
kubeadm token create --print-join-command
-- 将生成的命令在node1 和node2 执行 这个命令是有时效的 过期了在master重新生成
kubeadm join 192.168.100.40:6443 --token 53sidx.h2k9bfmfribezd50 --discovery-token-ca-cert-hash sha256:c839ac722c960a8da2796a94a3c97920c5a7b0433cf03dc672af863c2c6176f7
24.安装CNI (master节点执行)
-- 当前最新版本为3.25.0
curl https://raw.githubusercontent.com/projectcalico/calico/v3.25.0/manifests/calico.yaml -o calico.yaml
kubectl apply -f calico.yaml
安装 dashboard
下载:
curl https://raw.githubusercontent.com/kubernetes/dashboard/v2.7.0/aio/deploy/recommended.yaml -o dashboard.yaml
修改token 时长
args:
- --auto-generate-certificates
- --namespace=kubernetes-dashboard
- -token-ttl=43200
修改访问方式
kind: Service
apiVersion: v1
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
namespace: kubernetes-dashboard
#spec:
# ports:
# - port: 443
# targetPort: 8443
spec:
type: NodePort # 改成NodePort
ports:
- port: 443
targetPort: 8443
nodePort: 32000 # 指定nodePort端口
selector:
k8s-app: kubernetes-dashboard
保存后安装
kubectl apply -f dashboard.yaml
创建管理员sa&&dashboard所需token
kubectl create serviceaccount kube-dashboard-admin-sa -n kube-system kubectl create clusterrolebinding kube-dashboard-admin-sa \ --clusterrole=cluster-admin --serviceaccount=kube-system:kube-dashboard-admin-sa
创建集群管理员登录dashboard所需token:
kubectl create token kube-dashboard-admin-sa -n kube-system --duration=87600h
安装监控
curl https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml -o metrics-server.yaml
vim metrics-server.yaml
- args:
- --cert-dir=/tmp
- --secure-port=4443
- --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname
- --kubelet-use-node-status-port
- --kubelet-insecure-tls 添加这个
image换成国内阿里 registry.aliyuncs.com/google_containers/metrics-server:v0.6.3
kubectl apply -f metrics-server.yaml
标签:kubernetes,k8s1.26,部署,containerd,--,dashboard,kubeadm,k8s From: https://www.cnblogs.com/wangbj-93/p/17493099.html