k8s使用kubeadm安装-ubuntu

一字之年前 15天前   阅读数 46 0

环境ubuntu20.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/

  • 编辑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-get update
sudo apt-get -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
sudo apt-get update && sudo apt-get install -y apt-transport-https curl
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 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安装对应的版本)

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 && 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


注意:本文归作者所有,未经作者允许,不得转载

全部评论: 0

    我有话说: