系统设置

  1. # 所有节点执行
  2. # 禁用swap
  3. swapoff -a
  4. # 编辑fstab文件
  5. vim /etc/fstab
  6. # 注释这行:UUID=5dc6ff07-0b1c-418e-8105-4e5ebed85caa none swap sw 0 0
  7. # 关闭防火墙
  8. systemctl stop firewalld
  9. # 禁用Selinux
  10. apt install selinux-utils
  11. setenforce 0

安装docker

  1. # 所有节点执行
  2. # 切换到root用户
  3. sudo su root
  4. # 安装相关工具
  5. apt-get update
  6. apt-get install -y apt-transport-https curl
  7. # 添加秘钥
  8. curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
  9. # 完成后出现:OK
  10. # 安装docker
  11. apt-get install docker.io -y
  12. # 安装docker完成后,查看版本
  13. docker version
  14. # 修改配置文件
  15. vim /etc/docker/daemon.json
  16. {"registry-mirrors": ["https://tyhbjv3j.mirror.aliyuncs.com"], "live-restore": true}

初始化安装

安装kubectl、kubelet、kubeadm。

  1. # 所有节点执行
  2. # 添加秘钥:
  3. curl -s https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg| sudo apt-key add -
  4. # 完成后出现:OK
  5. # 添加kubernetes软件源
  6. cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
  7. deb http://mirrors.ustc.edu.cn/kubernetes/apt kubernetes-xenial main
  8. EOF
  9. # 安装kubectl、kubelet、kubeadm
  10. apt-get update
  11. apt-get install -y kubelet kubeadm kubectl
  12. # 设置开机启动
  13. systemctl enable kubelet

配置Master

  1. # 在Master上执行
  2. # 添加环境变量
  3. vim /etc/profile
  4. export KUBECONFIG=/etc/kubernetes/admin.conf
  5. # 重启kubelet
  6. systemctl restart kubelet
  7. # 配置镜像文件
  8. # 先查看需要安装的镜像版本
  9. kubeadm config images list --kubernetes-version=v1.16.1
  10. # 安装对应版本的镜像(根据上述命令的输出结果修改以下镜像版本)
  11. docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.16.1
  12. docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.16.1
  13. docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.16.1
  14. docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.1
  15. docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.3.15-0
  16. docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.6.2
  17. docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.16.1
  18. # 重新tag一下images
  19. docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.16.1 k8s.gcr.io/kube-controller-manager:v1.16.1
  20. docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.16.1 k8s.gcr.io/kube-scheduler:v1.16.1
  21. docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.16.1 k8s.gcr.io/kube-proxy:v1.16.1
  22. docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.1 k8s.gcr.io/pause:3.1
  23. docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.3.15-0 k8s.gcr.io/etcd:3.3.15-0
  24. docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.6.2 k8s.gcr.io/coredns:1.6.2
  25. docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.16.1 k8s.gcr.io/kube-apiserver:v1.16.1
  26. # 查看镜像
  27. docker images
  28. # 初始化kubernetes集群
  29. kubeadm init --kubernetes-version=v1.16.1 --pod-network-cidr 10.244.0.0/16
  30. mkdir -p $HOME/.kube
  31. sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  32. sudo chown $(id -u):$(id -g) $HOME/.kube/config
  33. # 配置flannel网络
  34. kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
  35. # 上述命令会由于网络原因我无法执行,使用以下操作替代
  36. # 下载kube-flannel.yml文件直接放置到系统中
  37. kubectl apply -f kube-flannel.yml
  38. # 查看pod
  39. kubectl get pods

配置镜像

  1. # 在nodes上执行
  2. # 安装对应版本的镜像
  3. docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.16.1
  4. docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.16.1
  5. docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.16.1
  6. docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.1
  7. docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.3.15-0
  8. docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.6.2
  9. docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.16.1
  10. # 重新tag一下images
  11. docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.16.1 k8s.gcr.io/kube-controller-manager:v1.16.1
  12. docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.16.1 k8s.gcr.io/kube-scheduler:v1.16.1
  13. docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.16.1 k8s.gcr.io/kube-proxy:v1.16.1
  14. docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.1 k8s.gcr.io/pause:3.1
  15. docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.3.15-0 k8s.gcr.io/etcd:3.3.15-0
  16. docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.6.2 k8s.gcr.io/coredns:1.6.2
  17. docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.16.1 k8s.gcr.io/kube-apiserver:v1.16.1

生成秘钥

  1. # 在Master上执行
  2. kubeadm token create
  3. # 得到token,如:9ekiqm.c7157v1o79fm1atd
  4. openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'
  5. # 得到hash sha256,如5c1e2b3b85a05145f205da25929264b9f1132b31a43c5cb4d71d06df25522b18

加入集群

token跟sha256就是我们在master上面获取到的。

  1. # 在nodes上操作
  2. kubeadm join --token 55c2c6.2a4bde1bc73a6562 192.168.0.16:6443 --discovery-token-ca-cert-hash sha256:0fdf8cfc6fecc18fded38649a4d9a81d043bf0e4bf57341239250dcc62d2c832

验证

  1. # 查看集群状态
  2. kubectl get nodes

参考

语雀:Ubuntu-18.04安装部署K8s
https://www.yuque.com/dingdongzhao/wr0lat/yxmytt