K8S系列文章之 [基于 Alpine 使用 kubeadm 搭建 k8s]

先部署基础环境,然后根据官方文档 K8s - Alpine Linux,进行操作。

将官方文档整理为脚本

整理脚本时,有部分调整

#!/bin/sh

set -x
# 添加源,安装时已经配置
#cat >> /etc/apk/repositories <<"EOF"
#http://mirrors.aliyun.com/alpine/edge/community
#http://mirrors.aliyun.com/alpine/edge/testing
#EOF

# 加载模块
echo "br_netfilter" > /etc/modules-load.d/k8s.conf
modprobe br_netfilter
# 临时加载,改为写入文件,防止重启失效
#echo 1 > /proc/sys/net/ipv4/ip_forward

apk add cni-plugin-flannel
apk add flannel
apk add flannel-contrib-cni
apk add cni-plugins
apk add kubelet
apk add kubeadm
apk add kubectl
apk add uuidgen
apk add nfs-utils
apk add containerd
# 把管理工具 ctr 安装上
apk add containerd-ctr
#apk add bash
#apk add vim

# 关闭 swap
swapoff -a
sed -i "s|.*swap.*|#&|" /etc/fstab

#Fix prometheus errors
mount --make-rshared /
cat > /etc/local.d/sharemetrics.start <<"EOF"
#!/bin/sh
mount --make-rshared /
EOF

chmod +x /etc/local.d/sharemetrics.start
rc-update add local

#Fix id error messages
uuidgen > /etc/machine-id

#Add services
#rc-update add docker
rc-update add containerd
rc-update add kubelet
rc-service containerd start

#kernel stuff
cat >> /etc/sysctl.conf <<"EOF"
net.bridge.bridge-nf-call-iptables=1
net.ipv4.ip_forward= 1
EOF
# set net work
sysctl -p
修改 containerd 配置
  • 查看当前初始化使用的镜像信息

    kubeadm config images list
    
  • 修改镜像加速 /etc/containerd/config.toml

    [plugins."io.containerd.grpc.v1.cri".registry.mirrors]
              [plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]
                endpoint = ["https://kuamavit.mirror.aliyuncs.com", "https://registry-1.docker.io"]
    
  • 修改sandbox /etc/containerd/config.toml

    sandbox_image = "registry.aliyuncs.com/google_containers/pause:3.9"
    
  • 改为配置,重启服务 rc-service containerd restart


  • 可基于以上基础环境克隆设备
  • 设备需要修改静态IP,修改 hostname,修改 hosts 文件

初始化【此前操作,所有节点均需要】
  • 尝试初始化,拉取镜像超级慢,建议先拉取镜像

    kubeadm init --pod-network-cidr=10.244.0.0/16 --node-name=$(hostname) --image-repository registry.aliyuncs.com/google_containers
    
  • 先拉取镜像

    kubeadm config images pull --image-repository registry.aliyuncs.com/google_containers
    
    kubeadm init \
    --node-name=$(hostname) \
    --apiserver-advertise-address=172.16.14.201 \
    --image-repository registry.aliyuncs.com/google_containers \
    --service-cidr=10.96.0.0/12 \
    --pod-network-cidr=10.244.0.0/16 --v=5
    
  • 根据提示,配置环境变量

    mkdir -p $HOME/.kube
    cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
    chown $(id -u):$(id -g) $HOME/.kube/config
    export KUBECONFIG=/etc/kubernetes/admin.conf
    
  • 查看状态,因为没安装网络插件,所以是没启动成功

    # 查看状态,NotReady
    kubectl get nodes
    # 查看pod,coredns 在 pending 状态
    kubectl get pod -A
    
安装网络组件
待处理方案,coredns 加载不上【calico、cilium均有相似问题,待处理】
  • 利用 helm 安装,添加仓库

    # helm repo add projectcalico https://docs.tigera.io/calico/charts
    helm repo add cilium https://helm.cilium.io/
    
  • 安装组件,calico 不好拉,cilium 要更容易拉取

    helm install cilium cilium/cilium --version 1.11.20  --namespace kube-system
    
  • 观察一手

    watch kubectl get pods -A -o wide
    
【可用方案】使用 flannel
  • 拉取 flannel yaml 文件

    wget -c  https://mirror.ghproxy.com/https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
    
  • 拉一把试试

    kubectl apply -f kube-flannel.yml
    # 镜像有点难拉,单独拉
    ctr -n k8s.io i pull docker.io/flannel/flannel:v0.24.2
    # docker.io/flannel/flannel-cni-plugin:v1.4.0-flannel1 这个镜像好像比较好拉
    
节点加入
  • 打印加入 token

    kubeadm token create --print-join-command
    

  • 20
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Coder_Boy_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值