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
基础安装
# 修改IP文件
vi /etc/sysconfig/network-scripts/ifcfg-enp0s3
ONBOOT=no ---> 修改为yes
# 重启网络
service network restart
# 安装网络工具
yum install -y net-tools wget telnet
# 查看IP
ifconfig
# 修改hostname
sudo hostnamectl set-hostname k8s-1
reboot
# 配置/etc/hosts
192.168.216.7 k8s-1
192.168.216.6 k8s-2
192.168.216.8 k8s-3
# 关闭swap分区
sudo swapoff -a
# yum更新、同步时间
yum update
yum install -y ntpdate
ntpdate ntp.aliyun.com
# 确保虚机不会自动suspend
sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target
# 安装docker
wget -P /etc/yum.repos.d/ https://download.docker.com/linux/centos/docker-ce.repo
yum install -y docker-ce
# 安装kubelet、kubectl
cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-\$basearch
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
exclude=kubelet kubeadm kubectl
EOF
# 将 SELinux 设置为 permissive 模式(相当于将其禁用)
sudo setenforce 0
sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
sudo yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
sudo systemctl enable --now kubelet
vi /etc/sysconfig/docker
OPTIONS = 改为如下:
--selinux-enabled=false
sudo systemctl daemon-reload && sudo systemctl restart docker
sudo sed -i '/ swap / s/^/#/' /etc/fstab
reboot
echo "1">/proc/sys/net/bridge/bridge-nf-call-iptables
echo "1">/proc/sys/net/bridge/bridge-nf-call-ip6tables
yum install bash-completion -y
source /usr/share/bash-completion/bash_completion
source <(kubectl completion bash)
Master节点
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
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
kubectl get nodes
mkdir -p /opt/k8s
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
vi kube-flannel.yml,找到行“--kube-subnet-mgr”,在其下增加如下一行:
“- --iface=enp0s8”(用你的实际网卡名替换enp0s8)
kubectl apply -f ./kube-flannel.yml
# 查看安装状态
kubectl get nodes
kubectl get pods --all-namespaces
# 安装helm
cd /opt
wget https://get.helm.sh/helm-v3.7.1-linux-amd64.tar.gz
tar -zxvf helm-v3.7.1-linux-amd64.tar.gz
cp linux-amd64/helm /usr/local/bin
helm version
# 添加仓库
helm repo add stable https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
helm repo add aliyun https://apphub.aliyuncs.com/
helm repo add bitnami https://charts.bitnami.com/bitnami/
helm repo add azure https://mirror.azure.cn/kubernetes/charts/
# 安装 kubernetes-dashboard
helm install kubernetes-dashboard aliyun/kubernetes-dashboard -n kube-system
#重装
echo y | kubeadm reset
rm -rf /var/lib/kubelet
rm -rf /etc/kubernetes
rm -rf /root/.kube/*
Worker节点
kubeadm join 192.168.216.7:6443 --token 10bgpq.vrdm5ytnx880hxjm \
--discovery-token-ca-cert-hash sha256:56381f3d0f71bc14d7fd7c04600a1280fce846110faa88cb3d717b571318dcf1
scp /etc/kubernetes/admin.conf root@192.168.216.6:/etc/kubernetes/admin.conf
echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> ~/.bash_profile
source ~/.bash_profile
kubectl label node k8s-2 node-role.kubernetes.io/worker=worker
测试
# 安装nginx
kubectl create deployment nginx --image=nginx
# 创建service
kubectl expose deployment nginx --port=80 --type=NodePort
# 查看端口
kubectl get pods,svc
NAME READY STATUS RESTARTS AGE
pod/nginx-5c7588df-hrhfh 1/1 Running 0 2m16s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3h6m
service/nginx NodePort 10.97.240.92 <none> 80:30054/TCP 23s
清除测试环境
echo y | kubeadm reset
systemctl stop kubelet
systemctl stop docker
rm -rf /var/lib/cni/
rm -rf /var/lib/kubelet/*
rm -rf /etc/cni/
rm -rf /etc/kubernetes
rm -rf /root/.kube/*
ifconfig cni0 down
ifconfig flannel.1 down
ifconfig docker0 down
ip link delete cni0
ip link delete flannel.1
systemctl restart kubelet
systemctl restart docker