Kubeadm离线安装单master节点k8s

1 目标

1.在所有节点上安装docker和kubeadm

2.部署kubernetes master

3.部署容器网络插件

4.部署kubernetes worker

5.部署dashboard可视化插件

6.部署容器存储插件

2 准备(所有安装机器均需执行)

2.1:/etc/hosts中添加ip和host的映射

2.2:master和node做互信

2.3:关闭防火墙和selinux

1) 关闭selinux

sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

2) 关闭防火墙

systemctl disable firewalld.service
systemctl stop firewalld.service

3) 关闭iptables

systemctl disable iptables.service
systemctl stop iptables.service

2.4:安装docker

将离线包解压,进docker目录,运行sudo yum install * 安装好后再执行

systemctl start docker
systemctl enable docker 

将docker服务启动

2.5:私仓搭建

这里选择了docker的registry2来建立私有仓库。

  1. 下载镜像仓库

    docker pull registry:2
    
  2. 启动镜像仓库

    docker run -d -v /opt/registry:/var/lib/registry -p 4000:5000 --restart=always --name registry registry:2
    
  3. 配置阿里云的Docker加速器,加快pull registry镜像

    mkdir -p /etc/docker
    tee /etc/docker/daemon.json << 'EOF'
    {
    "registry-mirrors": ["https://a5aghnme.mirror.aliyuncs.com"]
    }
    EOF
    
  4. 重启docker:

    systemctl daemon-reload
    systemctl restart docker
    
  5. 测试本地仓库 在本例中ip是10.0.0.5,端口是4000

    docker pull busybox
    docker tag busybox 10.0.0.5:4000/busybox:1.0
    docker push 10.0.0.5:4000/busybox:1.0
    docker pull 10.0.0.5:4000/busybox:1.0
    curl -XGET http://10.0.0.5:4000/v2/_catalog
    
  6. 修改docker配置 添加对私有仓库的信任

    tee /etc/docker/daemon.json << 'EOF'
    {
    "insecure-registries":["10.0.0.5:4000"]
    }
    EOF
    
  7. 重启docker

    systemctl daemon-reload
    systemctl restart docker
    
  8. 仓库镜像导入 有两种方式导入,如果之前有导出的话直接把镜像拷贝到/opt/registry就行,如果是初始化导入的话,需要以下两步: 1)更改tag

    for i in `docker images | grep -v REPOSITORY | awk 'BEGIN{OFS=":";ORS=" "}{print $1,$2}'`;do docker tag $i 10.0.0.5:4000/$i;done
    

    2)上传到私有仓库

    for i in `docker images | grep -v REPOSITORY | awk 'BEGIN{OFS=":";ORS=" "}{print $1,$2}'`;do docker PUSH $i;done
    
  9. 查看镜像是否上传成功

    curl -XGET http://10.0.0.5:4000/v2/_catalog
    
  10. 备份镜像文件

    tar -zcvf k8s-registry.tar.gz /opt/registry
    
  11. 导出registy

    docker save -o registry.tar registry
    

2.6:安装kubeadm

进入 kubeadm目录,运行

yum install *

2.7:网络环境配置

vim /etc/sysctl.conf
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
sysctl -p

确保两个值都是1

cat /proc/sys/net/bridge/bridge-nf-call-iptables
cat /proc/sys/net/bridge/bridge-nf-call-ip6tables 

2.8:kubelet配置修改并启动

vim /etc/sysconfig/kubelet 
KUBELET_EXTRA_ARGS="--fail-swap-on=false” 
systemctl enable kubelet 

3 master部署

3.1:master部署

用flannel的话除了版本号需要改,别的都可以不改。

kubeadm init --kubernetes-version=v1.14.2 --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12 --ignore-preflight-errors=Swap

执行后,页面上会输出类似字符串,需要记录下来。

kubeadm join 192.168.0.7:6443 --token 7d8i09.uz3hrjfftoxzk9y1 --discovery-token-ca-cert-hash sha256:621d62945f30ce15c31ac40b81a1267c8c02a2bfc8730c93e0f6dc152c7de1ec

3.2:添加kubectl访问配置

若给非root用户添加访问权限,需要

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

如果是给root的话

echo export KUBECONFIG=/etc/kubernetes/admin.conf >> /root/.bash_profile 

3.3: 检查

kubectl get cs 

如果得到如下的显示,表示安装正确

4 worker部署

将master部署产生的字符串输入即完成部署

kubeadm join 192.168.0.7:6443 --token 7d8i09.uz3hrjfftoxzk9y1 --discovery-token-ca-cert-hash sha256:621d62945f30ce15c31ac40b81a1267c8c02a2bfc8730c93e0f6dc152c7de1ec

5 网络模块flannel安装

kubectl apply -f kube-flannel.yml

执行后,输入ifconfig,看到flannel网卡,即完成安装

6 集群检查

执行以下命令

kubectl get node

执行后,若各个节点都是Ready状态,即表示集群部署成功。

kubectl get po -n kube-system -o wide

如果etcd,coredns,kube-apiserver,kube-flannel,kube-proxy,kube-controller-manager,kube-scheduler 都是Running状态,表示安装正确。

7 环境重装

7.1 环境清除

当需要重装的话需要清除干净主机上的环境。

以下命令供参考

kubectl drain dataocean-d-030020.te.td --delete-local-data --force --ignore-daemonsets
kubectl drain dataocean-d-030022.te.td --delete-local-data --force --ignore-daemonsets
kubectl drain dataocean-d-030023.te.td --delete-local-data --force --ignore-daemonsets
kubectl drain dataocean-d-030024.te.td --delete-local-data --force --ignore-daemonsets
kubectl delete node dataocean-d-030020.te.td
kubectl delete node dataocean-d-030022.te.td
kubectl delete node dataocean-d-030023.te.td
kubectl delete node dataocean-d-030024.te.td
kubeadm reset --ignore-preflight-errors=Swap
iptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -X
systemctl stop kubelet
systemctl stop docker
rm -rf /var/lib/kubelet/
rm -rf /var/lib/cni/
rm -rf /var/lib/etcd/
rm -rf /etc/kubernetes/
rm -rf /etc/cni/
rm -rf .kube/config 
ifconfig cni0 down
ip link delete cni0
ifconfig flannel.1 down
ip link delete flannel.1

7.2 安装

systemctl start kubelet
systemctl enable kubelet
systemctl start docker
systemctl enable docker
kubeadm init --kubernetes-version=v1.14.2 --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12 --ignore-preflight-errors=Swap

坑的地方注意:pod-network-cidr需要和网络插件的网段地址严格一致,不然会出现各种问题