1.安装K8S集群
1.1 配置需求
硬件:
主机 | 操作系统 | CUP | MEM | 角色要求 |
---|---|---|---|---|
k8s-master | ubuntu | 2 | 2G | master |
k8s-node1 | ubuntu | 2 | 2G | worker |
k8s-node2 | ubuntu | 2 | 2G | worker |
1.2 设置主机名
hostnamectl set-hostname xxx
三台主机名:
master1 192.168.216.100
worker1 192.168.216.101
worker2 192.168.216.102
1.3 主机IP地址
ip a s
1.4 安装ssh工具
apt-get update
apt-get install openssh-server
1.5 安装docker
apt-get install docker.io
查看docker版本
docker version
1.6 下载k8s
apt-get update && apt-get install -y apt-transport-https curl
apt-get install -y kubelet kubeadm kubectl --allow-unauthenticated
直接运行这个指令下载会报错,需要换源
echo deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main | sudo tee /etc/apt/sources.list.d/kubernetes.list
换源刷新apt
apt update
如果报错The following signatures couldn’t be verified because the public key is not available
则执行下面命令,为其添加 key。
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add
然后再安装k8s,这里最新的版本是1.21.1,但是它需要的镜像国内源没有更新,所以安装旧的版本
apt-get install -y kubectl=1.18.5-00 --allow-downgrades --allow-unauthenticated
apt-get install kubernetes-cni=0.8.6-00
apt-get install -y kubelet=1.18.5-00 kubeadm=1.18.5-00 kubectl=1.18.5-00
这时候执行以下指令看看有没有安装
kubectl version
会提示
Client Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.5", GitCommit:"e6503f8d8f769ace2f338794c914a96fc335df0f", GitTreeState:"clean", BuildDate:"2020-06-26T03:47:41Z", GoVersion:"go1.13.9", Compiler:"gc", Platform:"linux/amd64"}
The connection to the server localhost:8080 was refused - did you specify the right host or port?
1.7 配置环境
打开hosts⽂件 (根据自己的ip配置)
vim /etc/hosts
192.168.236.177 master
192.168.236.178 node1
192.168.236.179 node2
关闭swap
sudo swapoff -a
k8s初始化
kubeadm init --image-repository=registry.aliyuncs.com/google_containers --pod-network-cidr=10.244.0.0/16 --kubernetes-version=v1.18.5
成功会显示,记住最后一条,这个是用来加入结点的
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.5.138:6443 --token 33tg59.293qc9carbgtaw94 \
--discovery-token-ca-cert-hash sha256:6a0bc72e1c9c57866b2a0dec94226c70d4bd40474d944b60398b5135dd2bb976
根据提示执行
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
安装fannel
查看k8s结点状态
kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-master NotReady master 58m v1.18.5
可以发现结点没有运行起来,这里需要安装fannel
先在http://ip.tool.chinaz.com/找到域名对应的ip地址,查找raw.githubusercontent.com的ip
然后进入到hosts添加
vi /etc/hosts
把下面这行加入到里面
61.175.228.187 raw.githubusercontent.com
然后再执行安装fannel的指令
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
再次查看结点情况就会运行起来了(可能需要等几分钟)
NAME STATUS ROLES AGE VERSION
k8s-master Ready master 63m v1.18.5
一些错误
出现以下错误时
E: Could not get lock /var/lib/dpkg/lock-frontend - open (11: Resource temporarily unavailable)
E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process using it?
使用以下指令删除lock
sudo rm /var/lib/dpkg/lock-frontend
sudo rm /var/lib/dpkg/lock
kubeadm init --pod-network-cidr=10.244.0.0/16 --ignore-preflight-errors=NumCPU
如果要修改k8s的版本,可以执行下面这个指令删除
apt-get remove -y kubelet kubeadm kubectl