在Linux上安装k8s

环境

本例运行的系统

LSB Version:    :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch
Distributor ID: CentOS
Description:    CentOS Linux release 8.0.1905 (Core)
Release:        8.0.1905
Codename:       Core

硬件

[root@MiWiFi-R3-srv ~]# cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c
4  Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz

硬盘容量 50G。

硬盘容量必须足够大,CPU至少是2核。

需提前安装:

  • docker

过程

安装kubectl

cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

yum install -y kubectl

检测安装是否成功

kubectl cluster-info

执行结果:

[root@VM_0_9_centos k8s]# kubectl cluster-info

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
The connection to the server localhost:8080 was refused - did you specify the right host or port?

安装minikube

curl -Lo minikube http://kubernetes.oss-cn-hangzhou.aliyuncs.com/minikube/releases/v1.2.0/minikube-linux-amd64
chmod +x minikube
mv minikube /usr/local/bin/

检查安装结果

minikube version

出现下面结果,那么安装成功

[root@VM_0_9_centos k8s]# minikube version
minikube version: v1.2.0

启动集群

minikube start --vm-driver=none -p hello

若正常创建了集群,结果应该是

[root@MiWiFi-R3-srv home]# minikube start --vm-driver=none -p hello
😄  minikube v1.2.0 on linux (amd64)
✅  using image repository registry.cn-hangzhou.aliyuncs.com/google_containers
🔄  Restarting existing none VM for "hello" ...
⌛  Waiting for SSH access ...
🐳  Configuring environment for Kubernetes v1.15.0 on Docker 19.03.5
🔄  Relaunching Kubernetes v1.15.0 using kubeadm ...
🤹  Configuring local host environment ...

⚠️  The 'none' driver provides limited isolation and may reduce system security and reliability.
⚠️  For more information, see:
👉  https://github.com/kubernetes/minikube/blob/master/docs/vmdriver-none.md

⚠️  kubectl and minikube configuration will be stored in /root
⚠️  To use kubectl or minikube commands as your own user, you may
⚠️  need to relocate them. For example, to overwrite your own settings:

    ▪ sudo mv /root/.kube /root/.minikube $HOME
    ▪ sudo chown -R $USER $HOME/.kube $HOME/.minikube

💡  This can also be done automatically by setting the env var CHANGE_MINIKUBE_NONE_USER=true
⌛  Verifying: apiserver proxy etcd scheduler^@ controller dns
🏄  Done! kubectl is now configured to use "hello"

错误及解决

exec: “docker”: executable file not found

minikube start --vm-driver=none -p hello

执行结果如下:

[root@VM_0_9_centos k8s]# minikube start --vm-driver=none
😄  minikube v1.2.0 on linux (amd64)
✅  using image repository registry.cn-hangzhou.aliyuncs.com/google_containers
🔥  Creating none VM (CPUs=2, Memory=2048MB, Disk=20000MB) ...
E0115 14:43:28.642908   24323 start.go:559] StartHost: create: precreate: exec: "docker": executable file not found in $PATH

💣  Unable to start VM: create: precreate: exec: "docker": executable file not found in $PATH

😿  Sorry that minikube crashed. If this was unexpected, we would love to hear from you:
👉  https://github.com/kubernetes/minikube/issues/new

解决

出错的原因,没有安装docker,安装docker即可。

Wait failed: waiting for k8s-app=kube-dns

minikube start --vm-driver=none -p hello

执行结果如下:

⌛  Verifying: apiserver proxy^@ etcd^@ scheduler controller dns^@^@^@^@^@^@^@^@^@^@
💣  Wait failed: waiting for k8s-app=kube-dns: timed out waiting for the condition

解决:

原因可能是硬盘容量太小,作者将硬盘容量设置为50G后,此错误消失。

相关知识点

k8s调试

日志查看

kubectl logs -f redis-master-5664d4d9b9-48mhp

centos8安装docker

安装

# 卸载旧版docker
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
# 安装依赖
yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
# 设置稳定版的docker-ce yum仓库
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
# 下载符合版本要求的containerd.io rpm并安装containerd.io
wget https://download.docker.com/linux/centos/7/x86_64/stable/Packages/containerd.io-1.2.6-3.3.el7.x86_64.rpm
yum install ./containerd.io-1.2.6-3.3.el7.x86_64.rpm
# 安装docker
yum install docker-ce docker-ce-cli
# 设置开机自启docker
systemctl enable docker

错误及解决

yum install docker-ce docker-ce-cli containerd.io

错误

package docker-ce-3:19.03.5-3.el7.x86_64 requires containerd.io >= 1.2.2-3, but none of the providers can be installed
  - cannot install the best candidate for the job
  - package containerd.io-1.2.10-3.2.el7.x86_64 is excluded
  - package containerd.io-1.2.2-3.3.el7.x86_64 is excluded
  - package containerd.io-1.2.2-3.el7.x86_64 is excluded
  - package containerd.io-1.2.4-3.1.el7.x86_64 is excluded
  - package containerd.io-1.2.5-3.1.el7.x86_64 is excluded
  - package containerd.io-1.2.6-3.3.el7.x86_64 is excluded
(尝试添加 '--skip-broken' 来跳过无法安装的软件包 或 '--nobest' 来不只使用最佳选择的软件包)

解决

# 单独安装符合版本要求的containerd.io
wget https://download.docker.com/linux/centos/7/x86_64/stable/Packages/containerd.io-1.2.6-3.3.el7.x86_64.rpm
yum install ./containerd.io-1.2.6-3.3.el7.x86_64.rpm