Windows虚拟化3台Linux虚拟机 —- CentOS操作系统
基于kubeadm 方式

/etc/hosts

127.0.0.1 k8s-x localhost
::1 k8s-x localhost
192.168.216.7 k8s-1
192.168.216.6 k8s-2
192.168.216.8 k8s-3

服务器信息
2C 2G 15G
双网卡
image.png

基础安装

  1. # 修改IP文件
  2. vi /etc/sysconfig/network-scripts/ifcfg-enp0s3
  3. ONBOOT=no ---> 修改为yes
  4. # 重启网络
  5. service network restart
  6. # 安装网络工具
  7. yum install -y net-tools wget telnet
  8. # 查看IP
  9. ifconfig
  10. # 修改hostname
  11. sudo hostnamectl set-hostname k8s-1
  12. reboot
  13. # 配置/etc/hosts
  14. 192.168.216.7 k8s-1
  15. 192.168.216.6 k8s-2
  16. 192.168.216.8 k8s-3
  17. # 关闭swap分区
  18. sudo swapoff -a
  19. # yum更新、同步时间
  20. yum update
  21. yum install -y ntpdate
  22. ntpdate ntp.aliyun.com
  23. # 确保虚机不会自动suspend
  24. sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target
  25. # 安装docker
  26. wget -P /etc/yum.repos.d/ https://download.docker.com/linux/centos/docker-ce.repo
  27. yum install -y docker-ce
  28. # 安装kubelet、kubectl
  29. cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
  30. [kubernetes]
  31. name=Kubernetes
  32. baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-\$basearch
  33. enabled=1
  34. gpgcheck=1
  35. repo_gpgcheck=1
  36. gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
  37. exclude=kubelet kubeadm kubectl
  38. EOF
  39. # 将 SELinux 设置为 permissive 模式(相当于将其禁用)
  40. sudo setenforce 0
  41. sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
  42. sudo yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
  43. sudo systemctl enable --now kubelet
  44. vi /etc/sysconfig/docker
  45. OPTIONS = 改为如下:
  46. --selinux-enabled=false
  47. sudo systemctl daemon-reload && sudo systemctl restart docker
  48. sudo sed -i '/ swap / s/^/#/' /etc/fstab
  49. reboot
  50. echo "1">/proc/sys/net/bridge/bridge-nf-call-iptables
  51. echo "1">/proc/sys/net/bridge/bridge-nf-call-ip6tables
  52. yum install bash-completion -y
  53. source /usr/share/bash-completion/bash_completion
  54. source <(kubectl completion bash)

Master节点

  1. sudo kubeadm init --image-repository registry.aliyuncs.com/google_containers --pod-network-cidr=10.244.0.0/16 --ignore-preflight-errors=cri --control-plane-endpoint "192.168.216.7:6443" --apiserver-advertise-address=192.168.216.7
  2. mkdir -p $HOME/.kube
  3. sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  4. sudo chown $(id -u):$(id -g) $HOME/.kube/config
  5. kubectl get nodes
  6. mkdir -p /opt/k8s
  7. wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
  8. vi kube-flannel.yml,找到行“--kube-subnet-mgr”,在其下增加如下一行:
  9. “- --iface=enp0s8”(用你的实际网卡名替换enp0s8
  10. kubectl apply -f ./kube-flannel.yml
  11. # 查看安装状态
  12. kubectl get nodes
  13. kubectl get pods --all-namespaces
  14. # 安装helm
  15. cd /opt
  16. wget https://get.helm.sh/helm-v3.7.1-linux-amd64.tar.gz
  17. tar -zxvf helm-v3.7.1-linux-amd64.tar.gz
  18. cp linux-amd64/helm /usr/local/bin
  19. helm version
  20. # 添加仓库
  21. helm repo add stable https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
  22. helm repo add aliyun https://apphub.aliyuncs.com/
  23. helm repo add bitnami https://charts.bitnami.com/bitnami/
  24. helm repo add azure https://mirror.azure.cn/kubernetes/charts/
  25. # 安装 kubernetes-dashboard
  26. helm install kubernetes-dashboard aliyun/kubernetes-dashboard -n kube-system
  27. #重装
  28. echo y | kubeadm reset
  29. rm -rf /var/lib/kubelet
  30. rm -rf /etc/kubernetes
  31. rm -rf /root/.kube/*


Worker节点

  1. kubeadm join 192.168.216.7:6443 --token 10bgpq.vrdm5ytnx880hxjm \
  2. --discovery-token-ca-cert-hash sha256:56381f3d0f71bc14d7fd7c04600a1280fce846110faa88cb3d717b571318dcf1
  3. scp /etc/kubernetes/admin.conf root@192.168.216.6:/etc/kubernetes/admin.conf
  4. echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> ~/.bash_profile
  5. source ~/.bash_profile
  6. kubectl label node k8s-2 node-role.kubernetes.io/worker=worker

测试

  1. # 安装nginx
  2. kubectl create deployment nginx --image=nginx
  3. # 创建service
  4. kubectl expose deployment nginx --port=80 --type=NodePort
  5. # 查看端口
  6. kubectl get pods,svc
  7. NAME READY STATUS RESTARTS AGE
  8. pod/nginx-5c7588df-hrhfh 1/1 Running 0 2m16s
  9. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  10. service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3h6m
  11. service/nginx NodePort 10.97.240.92 <none> 80:30054/TCP 23s

清除测试环境

  1. echo y | kubeadm reset
  2. systemctl stop kubelet
  3. systemctl stop docker
  4. rm -rf /var/lib/cni/
  5. rm -rf /var/lib/kubelet/*
  6. rm -rf /etc/cni/
  7. rm -rf /etc/kubernetes
  8. rm -rf /root/.kube/*
  9. ifconfig cni0 down
  10. ifconfig flannel.1 down
  11. ifconfig docker0 down
  12. ip link delete cni0
  13. ip link delete flannel.1
  14. systemctl restart kubelet
  15. systemctl restart docker