k8s使用kubeadm安装-ubuntu

[亡魂溺海] 提交于 2020-08-05 17:02:28

环境ubuntu-server 20.04

参考 https://kubernetes.io/zh/docs/setup/production-environment/tools/kubeadm/install-kubeadm/

https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/create-cluster-kubeadm/

  • 同步集群时间
    # 设定时区, 选择Asia -> 再选择Shanghai -> OK:
    sudo dpkg-reconfigure tzdata
    
    # 安装ntpdate工具
    sudo apt-get install ntpdate
    
    # 将系统时间与网络同步
    sudo ntpdate cn.pool.ntp.org
    
    #将时间写入硬件
    sudo hwclock --systohc
  • 编辑ubuntu apt使用阿里云镜像 sudo vi /etc/apt/sources.list
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
  • 确保 iptables 工具不使用 nftables 后端

update-alternatives --set iptables /usr/sbin/iptables-legacy
update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy
update-alternatives --set arptables /usr/sbin/arptables-legacy
update-alternatives --set ebtables /usr/sbin/ebtables-legacy

  • 安装docker

# step 1: 安装必要的一些系统工具

sudo apt update
sudo apt -y upgrade
sudo apt -y install apt-transport-https ca-certificates curl software-properties-common

# step 2: 安装GPG证书

curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

# Step 3: 写入软件源信息

sudo add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"

# Step 4: 更新并安装Docker-CE

sudo apt-get -y update
sudo apt-get -y install docker-ce
sudo bash -c "cat > /etc/docker/daemon.json" <<EOF
{
  "registry-mirrors": ["https://dockerhub.azk8s.cn","https://hub-mirror.c.163.com"],
  "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 mkdir -p /etc/systemd/system/docker.service.d

# Restart Docker

sudo systemctl daemon-reload
sudo systemctl restart docker
sudo systemctl enable docker
  • 安装 kubeadm、kubelet 和 kubectl
curl -s https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add -
cat <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF
sudo apt-get update && sudo apt-get install -y apt-transport-https curl
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
sudo systemctl enable --now kubelet
sudo systemctl daemon-reload
sudo systemctl restart kubelet
  •  禁用swap
sudo swapoff -a #暂时关闭

# 永久关闭,注释掉swap那一行,推荐永久关闭 ctrl+s保存, ctrl+x退出

sudo nano /etc/fstab
  • 初始化集群(kubernetes-version版本号需修改为yum安装对应的版本,如果有科学工具可不指定image-repository)

sudo kubeadm init \
--apiserver-advertise-address=192.168.1.30 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.18.3 \
--pod-network-cidr=10.244.0.0/16

没有问题的话。有以下成功标志:

Your Kubernetes control-plane has initialized successfully!

To start using your cluster, you need to run the following as a regular user:

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

You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  https://kubernetes.io/docs/concepts/cluster-administration/addons/

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join 192.168.1.10:6443 --token zj9sed.nsv0mr8ym228qpq6 \
    --discovery-token-ca-cert-hash sha256:df276fa7c8551cb914deeb3a73c9705a5f77081c092e2dbd47c29a06a50f6ce8

说明1,执行以下操作,已经写得很清楚了。

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

说明2,你需要安装一个网络(You should now deploy a pod network to the cluster.) 我选择flannel, 简单好用。 参考此页面 https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/create-cluster-kubeadm/

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

说明3. 在其它机器上,加入node节点(node节点除了不执行初始化集群,上面所有都要执行)

sudo kubeadm join 192.168.1.30:6443 --token 4c4e3p.afglzv3elo4uc8ri \
    --discovery-token-ca-cert-hash sha256:8b2b67ba80ce9f89bc019b7161cc9bbef970241532a76e8d0e09dc1fb7a5fa7b

说明4. node节点也可以执行kubectl等命令

mkdir -p $HOME/.kube/
#192.168.1.30为master节点,根据实际情况替换自己的节点
scp name@192.168.1.30:$HOME/.kube/config $HOME/.kube/config

说明5. master也可以安装pod

kubectl taint nodes --all node-role.kubernetes.io/master-

测试是否安装成功,看READY是不是1/1, 状态是否为Running

kubectl get pods -A -o wide

最后效果

  • 有问题以下命令可重置

sudo kubeadm reset && sudo systemctl restart kubelet
sudo rm /etc/kubernetes/ -fR
sudo rm $HOME/.kube -fR
sudo rm /var/lib/etcd -fR
sudo rm /etc/cni/net.d -fR

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!