1準(zhǔn)備三臺(tái)虛擬機(jī) (省略)
192.168.37.21 hostnamectl set-hostname k8s01
192.168.37.22 hostnamectl set-hostname k8s02
192.168.37.23 hostnamectl set-hostname k8s03
2 修改IP地址
vi /etc/sysconfig/network-script/ifcfg-enp0s3
3 重啟網(wǎng)絡(luò)
systemctl restart network
4 關(guān)閉swap
swapoff -a
sed -i '/swap/s/^/#/' /etc/fstab
5 修改/etc/hosts
cat <<EOF > /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.137.21 k8s01
192.168.137.22 k8s02
192.168.137.23 k8s03
EOF
6 安裝 containerd
yum install -y containerd.io cri-tools
7 修改containerd的配置文件/etc/containerd/config.toml
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
8 containerd 設(shè)置為開機(jī)自動(dòng)啟動(dòng)并啟動(dòng)
systemctl enable containerd
systemctl start containerd
9 修改參數(shù)
修改內(nèi)存參數(shù)及加載模塊
cat > /etc/modules-load.d/containerd.conf <<EOF
overlay
br_netfilter
EOF
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
modprobe overlay
modprobe br_netfilter
sysctl -p /etc/sysctl.d/k8s.conf
10設(shè)置yum源
cat > /etc/yum.repos.d/kubernetes.repo <<EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
11安裝 kubelet kubeadmin kubectl
yum install -y kubelet-1.23.1-0 kubeadm-1.23.1-0 kubectl-1.23.1-0
yum -y remove kubelet-1.24.0-0 kubeadm-1.24.0-0 kubectl-1.24.0-0
12重啟機(jī)器以及關(guān)閉防火墻
reboot
systemctl disable firewalld
systemctl stop firewalld
systemctl enable kubelet.service
13初始化集群
kubeadm init --image-repository registry.aliyuncs.com/google_containers --kubernetes-version=v1.23.1 --pod-network-cidr=10.244.0.0/16
14按照提示
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> ~/.bash_profile
export KUBECONFIG=/etc/kubernetes/admin.conf
15查看k8s01 node
kubectl get nodes -o wide
16將k8s02 k8s03 加入集群
kubeadm join 192.168.137.21:6443 --token t9xpdk.hp4m0g049ymcw6yo \
--discovery-token-ca-cert-hash sha256:d2ac77f602960b2e3170a02e549381f1d32511e7a2fb5a1e2f8a89de6fecca0
17安裝calico
mkdir -p /root/install/calico
cd /root/install/calico
wget https://docs.projectcalico.org/v3.16/manifests/calico.yaml --no-check-certificate
kubectl apply -f calico.yaml
18安裝dashboard
mkdir -p /root/install/dashboard
cd /root/install/dashboard
wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.5.1/aio/deploy/recommended.yaml
kubectl apply -f recommended.yml
kubectl delete service kubernetes-dashboard --namespace=kubernetes-dashboard
cat >dashbord-svc.yml <<EOF
kind: Service
apiVersion: v1
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
namespace: kubernetes-dashboard
spec:
type: NodePort
ports:
- port: 443
targetPort: 8443
selector:
k8s-app: kubernetes-dashboard
EOF
kubectl apply -f dashbord-svc.yml
#查看創(chuàng)建情況
kubectl get pods --all-namespaces
kubectl get svc --all-namespaces
cat > dashboard-adminuser.yaml << EOF
apiVersion: v1
kind: ServiceAccount
metadata:
name: admin-user
namespace: kubernetes-dashboard
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: admin-user
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: admin-user
namespace: kubernetes-dashboard
EOF
kubectl get secret -n kubernetes-dashboard |grep admin|awk '{print $1}'
kubectl describe secret admin-user-token-zzhgz -nkubernetes-dashboard|grep '^token'|awk '{print $2}'