系统设置
# 所有节点执行
# 禁用swap
swapoff -a
# 编辑fstab文件
vim /etc/fstab
# 注释这行:UUID=5dc6ff07-0b1c-418e-8105-4e5ebed85caa none swap sw 0 0
# 关闭防火墙
systemctl stop firewalld
# 禁用Selinux
apt install selinux-utils
setenforce 0
安装docker
# 所有节点执行
# 切换到root用户
sudo su root
# 安装相关工具
apt-get update
apt-get install -y apt-transport-https curl
# 添加秘钥
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
# 完成后出现:OK
# 安装docker
apt-get install docker.io -y
# 安装docker完成后,查看版本
docker version
# 修改配置文件
vim /etc/docker/daemon.json
{"registry-mirrors": ["https://tyhbjv3j.mirror.aliyuncs.com"], "live-restore": true}
初始化安装
安装kubectl、kubelet、kubeadm。
# 所有节点执行
# 添加秘钥:
curl -s https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg| sudo apt-key add -
# 完成后出现:OK
# 添加kubernetes软件源
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb http://mirrors.ustc.edu.cn/kubernetes/apt kubernetes-xenial main
EOF
# 安装kubectl、kubelet、kubeadm
apt-get update
apt-get install -y kubelet kubeadm kubectl
# 设置开机启动
systemctl enable kubelet
配置Master
# 在Master上执行
# 添加环境变量
vim /etc/profile
export KUBECONFIG=/etc/kubernetes/admin.conf
# 重启kubelet
systemctl restart kubelet
# 配置镜像文件
# 先查看需要安装的镜像版本
kubeadm config images list --kubernetes-version=v1.16.1
# 安装对应版本的镜像(根据上述命令的输出结果修改以下镜像版本)
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.16.1
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.16.1
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.16.1
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.1
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.3.15-0
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.6.2
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.16.1
# 重新tag一下images
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.16.1 k8s.gcr.io/kube-controller-manager:v1.16.1
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.16.1 k8s.gcr.io/kube-scheduler:v1.16.1
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.16.1 k8s.gcr.io/kube-proxy:v1.16.1
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.1 k8s.gcr.io/pause:3.1
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.3.15-0 k8s.gcr.io/etcd:3.3.15-0
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.6.2 k8s.gcr.io/coredns:1.6.2
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.16.1 k8s.gcr.io/kube-apiserver:v1.16.1
# 查看镜像
docker images
# 初始化kubernetes集群
kubeadm init --kubernetes-version=v1.16.1 --pod-network-cidr 10.244.0.0/16
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
# 配置flannel网络
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
# 上述命令会由于网络原因我无法执行,使用以下操作替代
# 下载kube-flannel.yml文件直接放置到系统中
kubectl apply -f kube-flannel.yml
# 查看pod
kubectl get pods
配置镜像
# 在nodes上执行
# 安装对应版本的镜像
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.16.1
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.16.1
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.16.1
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.1
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.3.15-0
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.6.2
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.16.1
# 重新tag一下images
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.16.1 k8s.gcr.io/kube-controller-manager:v1.16.1
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.16.1 k8s.gcr.io/kube-scheduler:v1.16.1
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.16.1 k8s.gcr.io/kube-proxy:v1.16.1
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.1 k8s.gcr.io/pause:3.1
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.3.15-0 k8s.gcr.io/etcd:3.3.15-0
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.6.2 k8s.gcr.io/coredns:1.6.2
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.16.1 k8s.gcr.io/kube-apiserver:v1.16.1
生成秘钥
# 在Master上执行
kubeadm token create
# 得到token,如:9ekiqm.c7157v1o79fm1atd
openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'
# 得到hash sha256,如5c1e2b3b85a05145f205da25929264b9f1132b31a43c5cb4d71d06df25522b18
加入集群
token跟sha256就是我们在master上面获取到的。
# 在nodes上操作
kubeadm join --token 55c2c6.2a4bde1bc73a6562 192.168.0.16:6443 --discovery-token-ca-cert-hash sha256:0fdf8cfc6fecc18fded38649a4d9a81d043bf0e4bf57341239250dcc62d2c832
验证
# 查看集群状态
kubectl get nodes
参考
语雀:Ubuntu-18.04安装部署K8s
https://www.yuque.com/dingdongzhao/wr0lat/yxmytt