k8s - 使用kubeadm部署k8s集群初体验

部署前准备

  1. 一台腾讯云轻量应用服务器,(CPU: 2核 | 内存: 4GB | 硬盘:80GB),master
  2. 一台腾讯云服务器,(CPU: 1核 | 内存: 2GB | 硬盘:50GB),node1
  3. 操作系统皆为Ubuntu 20.04 LTS x86_64 docker
  4. 这里有个大坑,轻量应用服务器和普通云服务器内网不互通,互通要参考内网互联
  5. 参考gist,并结合自身实际

master

切换到root用户

sudo su root

安装 docker

curl -fsSL https://get.docker.com | sudo sh -s -- --mirror Aliyun
sudo usermod -aG docker $USER
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": [
    "https://xx4bwyg2.mirror.aliyuncs.com",
    "http://f1361db2.m.daocloud.io",
    "https://registry.docker-cn.com",
    "http://hub-mirror.c.163.com",
    "https://docker.mirrors.ustc.edu.cn"
  ],
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "storage-driver": "overlay2",
  "storage-opts": [
    "overlay2.override_kernel_check=true"
  ]
}{}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

安装 k8s 套件

# 添加并信任APT证书
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add -

# 添加源地址
add-apt-repository "deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main"

# 更新源并安装最新版 kubenetes
sudo apt update && apt install -y kubelet kubeadm kubectl

# 添加 completion,最好放入 .bashrc 中
source <(kubectl completion bash)
source <(kubeadm completion bash)

关闭 swap(我没有做)

使用kubeadm初始化集群,启动master节点

kubeadm init --image-repository='registry.cn-hangzhou.aliyuncs.com/google_containers' --pod-network-cidr 10.244.0.0/16

配置admin.conf

为了解决各工具提示The connection to the server lopodcalhost:8080 was refused

如果是root用户

# append to .bashrc
export KUBECONFIG=/etc/kubernetes/admin.conf

如果是普通用户

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

安装网络插件

有很多网络解决方法,例如flannel,weave

kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')"
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

node1

切换到root用户

sudo su root

安装 docker

curl -fsSL https://get.docker.com | sudo sh -s -- --mirror Aliyun
sudo usermod -aG docker $USER
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": [
    "https://xx4bwyg2.mirror.aliyuncs.com",
    "http://f1361db2.m.daocloud.io",
    "https://registry.docker-cn.com",
    "http://hub-mirror.c.163.com",
    "https://docker.mirrors.ustc.edu.cn"
  ],
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "storage-driver": "overlay2",
  "storage-opts": [
    "overlay2.override_kernel_check=true"
  ]
}{}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

安装 k8s 套件

# 添加并信任APT证书
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add -

# 添加源地址
add-apt-repository "deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main"

# 更新源并安装最新版 kubenetes
sudo apt update && apt install -y kubelet kubeadm kubectl

# 添加 completion,最好放入 .bashrc 中
source <(kubectl completion bash)
source <(kubeadm completion bash)

加入k8s集群

回到master节点,获取完整的kubeadm join命令

kubeadm token create --print-join-command

输出结果:

kubeadm join 10.0.xx.xx:6443 --token 67k0f6.3honxkg120rmxd3d --discovery-token-ca-cert-hash sha256:56a4f123422e7f3a863914503acd344377a6e0f77dd8f979d66b44b257b1fc5 

注意kubeadm join后面的地址貌似不能改为公网地址。这里有个坑就是master是腾讯云轻量应用服务器,node1是腾讯普通云服务器,我参考了内网互联之后,内网可以ping通。

回到node1节点,使用kubeadm join

kubeadm join 10.0.xx.xx:6443 --token 67k0f6.3honxkg120rmxd3d --discovery-token-ca-cert-hash sha256:56a4f123422e7f3a863914503acd344377a6e0f77dd8f979d66b44b257b1fc5 

配置admin.conf

为了解决各工具提示The connection to the server localhost:8080 was refused

使用scp将master的/etc/kubernetes/admin.conf复制到node1的/etc/kubernetes/admin.conf

scp /etc/kubernetes/admin.conf user@ip:/etc/kubernetes/

如果是root用户

# append to .bashrc
export KUBECONFIG=/etc/kubernetes/admin.conf

如果是普通用户

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

查看 nodes

在master或者node1

kubectl get nodes

这里node1的ROLES可能是None,可以使用以下命令解决

kubectl label node {node_name} node-role.kubernetes.io/worker=worker

效果截图

总结

  1. 感谢这个gist,实践出真理。
  2. 之前使用过minikube玩过单机k8s集群,但是本人觉得云原生嘛,要在云上玩,模拟实际生产环境,才能真正学到东西。
posted @ 2021-12-05 22:12  Rocin  阅读(205)  评论(0编辑  收藏  举报