安装docker
yum install -y docker
关闭swap
swapoff -a # 临时关闭
sed -ri 's/.*swap.*/#&/' /etc/fstab #永久关闭
修改hostname
hostnamectl set-hostname vm
PS: 如果不修改可能会导致name: Invalid value: "vm_0_16_centos": a DNS-1123 subdomain must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character (e.g. 'example.com', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*')
错误,在kubeadm init时遇到该错误可以按照该方案修复。
修改kubeadm的资源地址
cat > /etc/yum.repos.d/kubernetes.repo << EOF [kubernetes] name=Kubernetes baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64 enabled=1 gpgcheck=0 repo_gpgcheck=0 gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg EOF
安装kubeadm
yum install -y kubelet-1.15.0 kubeadm-1.15.0 kubectl-1.15.0
这里我使用的是1.15.0版本,可以不指定
部署kubernetes
kubeadm init --image-repository registry.aliyuncs.com/google_containers --pod-network-cidr=10.244.0.0/16 --kubernetes-version v1.15.0 --ignore-preflight-errors=NumCPU
,注意需要修改镜像仓库地址,这里的–ignore-preflight-errors=…是需要忽略的错误,我使用的虚拟机是单CPU的,不满足最低配置要求,添加NumCPU表示忽略该错误。
部署过程中可能会遇到如下错误:
- [ERROR FileContent–proc-sys-net-bridge-bridge-nf-call-iptables]: /proc/sys/net/bridge/bridge-nf-call-iptables contents are not set to 1,执行
echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables
和echo 1 > /proc/sys/net/bridge/bridge-nf-call-ip6tables
解决。 - [ERROR FileContent–proc-sys-net-ipv4-ip_forward]: /proc/sys/net/ipv4/ip_forward contents are not set to 1,执行
echo "1" > /proc/sys/net/ipv4/ip_forward
解决。
部署完成后按照提示配置config
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
部署pod
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/a70459be0084506e4ec919aa1c114638878db11b/Documentation/kube-flannel.yml
,如果虚拟机下载yaml文件失败可以手动下载后部署。
此时master就部署好了,如果仅用于测试可以通过kubectl taint nodes --all node-role.kubernetes.io/master-
打开master节点参加工作负载,使用kubectl taint nodes k8s node-role.kubernetes.io/master=true:NoSchedule
关闭。
测试
kubectl create deployment nginx --image=nginx
部署nginx,通过kubectl get pods
查看是否部署成功。
kubectl expose deployment nginx --port=80 --type=NodePort
创建service,访问80端口,验证成功。