目录
1 环境配置要求:
apt使用阿里云的源
设置主机名及解析,设定时钟同步,关闭swap和防火墙,开启IPv4转发。
更改机器名
hostnamectl set-hostname master
2.关闭swap和防火墙
swapoff -a sed -i '/swap/s/^/#/' /etc/fstab
3.开启IPv4转发
每台机器都要设置
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf net.bridge.bridge-nf-call-iptables = 1 net.bridge.bridge-nf-call-ip6tables = 1 net.ipv4.ip_forward = 1 EOF sysctl --system sudo modprobe br_netfilter echo '1' | sudo tee /proc/sys/net/bridge/bridge-nf-call-iptables sudo nano /etc/sysctl.conf 在文件的末尾添加以下两行配置 net.bridge.bridge-nf-call-iptables = 1 net.ipv4.ip_forward = 1 sudo sysctl -p sudo nano /etc/modules-load.d/br_netfilter.conf 在文件中添加以下内容: br_netfilter
2安装containerd
curl -# -O https://mirrors.aliyun.com/docker-ce/linux/ubuntu/dists/jammy/pool/stable/amd64/containerd.io_1.6.26-1_amd64.deb dpkg -i containerd.io_1.6.26-1_amd64.deb #导出默认配置 containerd config default > /etc/containerd/config.toml #修改containerd使用SystemdCgroup SystemdCgroup = true #配置containerd使用国内mirror站点上的pause镜像及指定版本 sandbox_image = "registry.aliyuncs.com/google_containers/pause:3.9" #启动containerd服务 systemctl enable containerd systemctl restart containerd
3配置crictl客户端
vim /etc/crictl.yaml #输入 runtime-endpoint: unix:///run/containerd/containerd.sock image-endpoint: unix:///run/containerd/containerd.sock timeout: 10 debug: true
4安装kubuadm、kubelet、kubectl
apt update && apt install -y apt-transport-https curl curl -fsSL https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add - cat <<EOF >/etc/apt/sources.list.d/kubernetes.list deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main EOF apt update apt install -y kubelet kubeadm kubectl systemctl enable kubelet
5初始化master节点
拉取下载镜像
kubeadm config images pull --image-repository=registry.aliyuncs.com/google_containers --kubernetes-version="v1.28.2"
运行如下命令完成初始化
kubeadm init --kubernetes-version=v1.28.2 --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12 --image-repository=registry.aliyuncs.com/google_containers --upload-certs
集群初始化成功显示:
设定kubectl
#复制认证为Kubernetes系统管理员的配置文件至当前用户root的家目录 root@master:~# mkdir .kube root@master:~# cp -i /etc/kubernetes/admin.conf $HOME/.kube/config root@master:~# ls .kube/ config
下图所示表示成功
部署网络插件
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml kubectl apply -f kube-flannel.yml
加入节点
使用kubeadm join加入节点,节点要安装配置的项目跟kubeadm init以前的操作完全一致
kubeadm join 192.168.215.129:6443 --token 97pgmv.vxjakgynflgdh4zw --discovery-token-ca-cert-hash sha256:52163ce2f39c0ccc44558e7339c52bfd006c78944088efb2f94dcc57d424b816
下图所示,表示节点加入成功
部署应用
nginx-deployment.yml文件
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment namespace: default spec: replicas: 1 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx ports: - containerPort: 80
kubectl apply nginx-deployment.yml
nginx-service.yml文件
apiVersion: v1 kind: Service metadata: name: nginx-service namespace: default spec: selector: app: nginx #loadBalancerIP: 192.168.215.200 ports: - protocol: TCP port: 80 targetPort: 80 type: NodePort #NodePort LoadBalancer
kubectl apply -f nginx-service.yml
测试
curl 192.168.215.129:31370 #IP是母机地址,此地址返回部署成功页面