修改注:删除了一些无用的踩坑和说明,优化调整了结构与版面


环境部署

准备四台虚拟机,安装ubuntu18.04版本

配置网络环境

以一台虚拟机为例,其他的等同

首先确定虚拟机的设置正确(一般初始化就是这样不需要改动)




k8s虚拟化证书_docker


配置主机的NAT模式具体参数


k8s虚拟化证书_重启_02


这里写图片描述


k8s虚拟化证书_ubuntu_03


记住网关


k8s虚拟化证书_docker_04


配置笔记本主机具体VMnet8本地地址参数:


k8s虚拟化证书_k8s虚拟化证书_05


说明:下图中的IP地址随意设置,但是要保证不能跟你要设置虚拟机的固定IP一样。我设置的是:192.168.153.1


k8s虚拟化证书_k8s虚拟化证书_06


修改虚拟机中系统为固定ip的配置文件

虚拟机中启动命令行


sudo gedit /etc/netplan/01-network-manager-all.yaml


修改后为


# Let NetworkManager manage all devices on this system
network:
  version: 2
  renderer: NetworkManager
  ethernets:
     ens33: #配置的网卡名称,使用ifconfig -a查看得到
       addresses: [192.168.153.101/24] #your ip for this vm
       gateway4: 192.168.153.2 #the gateway set before
       nameservers:
         addresses: [192.168.153.2]


有个我卡住的很憨很憨的坑,新手注意每个属性冒号:后要有一个空格。

执行命令


sudo netplan apply


查询Ip地址后应该可以看到修改后的ip


可以选做,能够提高运行效率:

ubuntu关闭图形用户界面
sudo systemctl set-default multi-user.target sudo reboot
ubuntu启用图形用户界面
sudo systemctl set-default graphical.target sudo reboot


K8S安装

  1. 安装docker
sudo apt-get install docker.io


为了不在每次使用docker时都用root用户,就改一下它的group,让平民也可以用。


sudo usermod -aG docker $USER


正式启用Docker:


sudo systemctl start docker
sudo systemctl enable docker


2. 安装curl


sudo apt-get update
sudo apt install -y curl
sudo curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add


如果出现This command can only be used by root.的错误,就切换到根目录

su: Authentication failure问题:sudo passwd root

3. 添加安装源


sudo apt-add-repository "deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main"
#安装k8s的重要组件
sudo apt-get install kubeadm kubelet kubectl -y


设置主机名hostname


sudo hostnamectl set-hostname HOSTNAME #(用master,node1,node2替换)


修改host文件


sudo gedit /etc/hosts


127.0.0.1	localhost
127.0.1.1	ubuntu(改这里,改成master)

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters


sudo swapoff -a


初始化master


sudo kubeadm init --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.19.3 --pod-network-cidr=192.168.153.101/24


把ip换成自己在master设置的静态ip地址

报以下错误就要把虚拟机配置修改一下

[ERROR NumCPU]: the number of available CPUs 1 is less than the required 2


k8s虚拟化证书_ubuntu_07


成功后


Your Kubernetes control-plane has initialized successfully!

To start using your cluster, you need to run the following as a regular user:

  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config

You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  https://kubernetes.io/docs/concepts/cluster-administration/addons/

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join 192.168.153.101:6443 --token 51tl72.gt63iey7eq5xe8y8 
    --discovery-token-ca-cert-hash sha256:98a23691705d7a0d02589fdd05c5254ea5fe255e5b5084ff67ef386659bf640b


然后仅在master做下面的操作:


mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml


接下来仅在每个node上做下面的操作,即复制上个图片里的最后一行:


sudo kubeadm join 192.168.153.101:6443 --token rqhtn1.1e4cbbc6dg85krye 
    --discovery-token-ca-cert-hash sha256:e89f384ffda27cbed75a6c1d7975061b5232e472370ec4ce304fc7032b3e0234


成功


k8s虚拟化证书_ubuntu_08


遇到的一些坑:

node节点中遇到以下问题时:

  1. Unfortunately, an error has occurred:
    timed out waiting for the condition


k8s虚拟化证书_docker_09


下面的方法没用


swapoff -a    # will turn off the swap
kubeadm reset
systemctl daemon-reload
systemctl restart kubelet
iptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -X  # will reset iptables


解决方案:

重新安装系统。。。注意步骤是否出错。

最后在master中,查看部署状态:


kubectl get nodes


多等一会。。。。。很无语,找了半天方法解决notready的问题,结果过了一会自己好了。


k8s虚拟化证书_重启_10


2. 出现了以上的错误之后,尝试以下方法//:

重启。


k8s虚拟化证书_ubuntu_11


好了。。。。。。乌鸡鲅鱼

最后注释一下重启系统后应该输的命令:


sudo -i
swapoff -a
exit
strace -eopenat kubectl version


还有就是有个问题。。。虚拟机非常卡,非常非常卡,这是正常的吗(衰