Ubuntu 20.04作为云端部署安装K8s和kubeedge记录
最近有个项目需要搞云边协同的硬件实现,通过调研决定用kubernetes和kubeedge。但是小白从未接触过k8s和kubeedge,按照网上的教程来总是会出些奇奇怪怪的问题,现在把自己最后一次部署成功的步骤记录下来。
Ubuntu 20.04部署安装K8s和kubeedge
最近有个项目需要搞云边协同的硬件实现,通过调研决定用kubernetes和kubeedge。但是小白从未接触过k8s和kubeedge,按照网上的教程来总是会出些奇奇怪怪的问题,现在把自己最后一次部署成功的步骤记录下来。
1.K8s的安装
网上的教程里对于k8s安装也有不同的方法。这里我主要参考的是
https://gitee.com/wpf0609/ubuntu-install-kubeedge/blob/main/Ubuntu%E5%AE%89%E8%A3%85Kubeedge%E9%9B%86%E7%BE%A4%E6%93%8D%E4%BD%9C%E6%AD%A5%E9%AA%A4.md#%E4%B8%BB%E8%8A%82%E7%82%B9%E5%BC%80%E5%90%AFkubeedge-cloud%E6%9C%8D%E5%8A%A1
但是也有不同的地方。
更换阿里源
英特尔的cpu就直接这么搞就可以了。
# 云端和边缘端
# 备份原来的源
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
sudo vim /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
# 然后进行以下操作
sudo apt-get update
sudo apt-get -y upgrade
sudo apt-get -y install build-essential
sudo apt -y install docker.io
# 如果是 ARM64 架构
# 参考 https://blog.csdn.net/qq_34253926/article/details/121629068
关闭防火墙和swap
sudo sed -i '/ swap / s/^/#/' /etc/fstab
sudo swapoff -a
sudo ufw disable
安装go
到这个网址下载go语言的包,可能需要挂梯子下载
https://dl.google.com/go/go1.17.4.linux-amd64.tar.gz
然后
tar -zxvf go1.17.4.linux-amd64.tar.gz -C /usr/local
上面这一步可能需要sudo
解压完成之后,打开主目录下的.bashrc在文件末尾加上
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
export GOPROXY=https://proxy.golang.com.cn,direct
开一个新的终端用 go version 检查是否安装完成
设置hostname(感觉可有可无)
2.安装部署k8s
安装kubectl、kubelet、kubeadm(都是1.20版本)
# 云端
# 支持https传送
sudo apt install -y apt-transport-https
# 添加访问公钥
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -
# 添加kubernetes的软件源
sudo vim /etc/apt/sources.list.d/kubernetes.list
# 把下面这句话放进去保存
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
# 更新缓存索引
sudo apt update
# 安装指定版本
sudo apt install -y kubelet=1.20.0-00 kubeadm=1.20.0-00 kubectl=1.20.0-00 --allow-downgrades
# 开机自启kubelet
systemctl enable kubelet
初始化集群
这句话中–image-repository一定要指定,不然之后cloudcore步骤会报各种错。–pod-network-cidr最好也要指定,不然之后安装flannel插件之后也不能ready
# 云端
sudo kubeadm init --image-repository=registry.aliyuncs.com/google_containers --kubernetes-version=v1.20.0 --pod-network-cidr=10.244.0.0/16
如果看到如下类似的语句就初始化成功了
kubeadm join 192.168.179.30:6443 --token fkxju7.d39l2sct5bc4w5yo \
--discovery-token-ca-cert-hash sha256:28b467ec8f97537069724028c5d51650983b8bbc2ac29a6e52b210bb2d1896ff
接着执行
# 云端
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
用kubectl get pods -A查看端口是否全部ready,需要安装flannel网络插件
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
kubectl apply -f kube-flannel.yml
apply之后等一两分钟再用kubectl get pods -A查看应该就能都ready了
3.安装kubeedge
#可自行前往官网下载
wget https://github.com/kubeedge/kubeedge/releases/download/v1.9.2/keadm-v1.9.2-linux-amd64.tar.gz
#解压压缩包
tar -zxvf keadm-v1.9.2-linux-amd64.tar.gz
#master部署kubeedge
cp keadm-v1.9.2-linux-amd64/keadm/keadm /usr/local/bin/
#在keadm目录下,执行init操作:
keadm init --advertise-address="你的云端ip" --kubeedge-version=1.9.2
#应该不会出错
生成stream证书和cloudcore配置
到网上找到certgen.sh
然后
sudo mv certgen.sh /etc/kubeedge/
export CLOUDCOREIPS="你的云端ip"
sudo -E /etc/kubeedge/certgen.sh stream
生成证书之后在keadm-v1.9.2-linux-amd64/keadm目录下执行keadm gettoken获取token
修改配置,sudo vim /etc/kubeedge/config/cloudcore.yaml:
modules:(省略号的部分不变)
...
cloudStream:
enable: true
streamPort: 10003
...
dynamicController:
enable: true
...
systemctl daemon-reload
systemctl restart cloudcore
# 运行查看ipTunnelPort
kubectl get cm tunnelport -nkubeedge -oyaml
apiVersion: v1
kind: ConfigMap
metadata:
annotations:
tunnelportrecord.kubeedge.io: '{"ipTunnelPort":{"xx.xx.xx.xx":10351},"port":{"10351":true}}'
creationTimestamp: "2022-03-10T06:01:15Z"
...
# 根据ConfigMap设置iptables
# iptables -t nat -A OUTPUT -p tcp --dport $YOUR-TUNNEL-PORT -j DNAT --to $YOUR-CLOUDCORE-IP:10003
iptables -t nat -A OUTPUT -p tcp --dport 10351 -j DNAT --to xx.xx.xx.xx:10003
cloudcore通过systemd管理
sudo cp /etc/kubeedge/cloudcore.service /etc/systemd/system/cloudcore.service
# 杀掉当前cloudcore进程
pkill cloudcore
# 重启cloudcore
systemctl daemon-reload
systemctl restart cloudcore
# 查看cloudcore是否运行
systemctl status cloudcore
走到这里之后cloudcore应该是active(running)的状态,就证明没有出错。
4.edgemesh安装
#安装helm
curl https://baltocdn.com/helm/signing.asc | sudo apt-key add -
sudo apt-get install apt-transport-https --yes
echo "deb https://baltocdn.com/helm/stable/debian/ all main" | sudo tee /etc/apt/sources.list.d/helm-stable-debian.list
sudo apt-get update
sudo apt-get install helm
#安装edgemesh
helm install edgemesh \
--set agent.image=kubeedge/edgemesh-agent:v1.10.0 \
--set server.image=kubeedge/edgemesh-server:v1.10.0 \
--set server.nodeName=cloud.kubeedge \
--set server.advertiseAddress="{你的云端ip}" \
https://raw.githubusercontent.com/kubeedge/edgemesh/main/build/helm/edgemesh.tgz
#检验部署结果
helm ls
使用kubeadm部署的k8s集群,那么kube-proxy会下发到端侧节点,但是edgecore无法与kube-proxy并存,所以要修改kube-proxy的daemonset节点亲和性,禁止在端侧部署kube-proxy
kubectl edit ds kube-proxy -n kube-system
# 添加以下配置
...
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: node-role.kubernetes.io/edge
operator: DoesNotExist
containers:
...
可视化dashboard安装
参考
https://blog.csdn.net/bicongming/article/details/126422059
根据之前安装的k8s版本选择对应的dashboard版本
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.3.1/aio/deploy/recommended.yaml
#查看安装情况
kubectl get pods --all-namespaces -o wide | grep dashboard
#查看svc
kubectl get svc --all-namespaces
#删除现有的 dashboard 服务,因为该服务的类型是ClusterIP,不便于我们通过浏览器访问,因此需要改成 NodePort 类型的
kubectl delete service kubernetes-dashboard --namespace=kubernetes-dashboard
#创建配置文件dashboard-svc.yaml
vim dashboard-svc.yaml
kind: Service
apiVersion: v1
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
namespace: kubernetes-dashboard
spec:
type: NodePort
ports:
- port: 443
targetPort: 8443
selector:
k8s-app: kubernetes-dashboard
#创建该 Service
kubectl apply -f dashboard-svc.yaml
#再次查看服务,此时dashboard的type已经是nodeport(记住端口号后面要用)
kubectl get svc --all-namespaces
#创建 kubernetes-dashboard 管理员角色,dashboard-svc-account.yaml
vi dashboard-svc-account.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: dashboard-admin
namespace: kube-system
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: dashboard-admin
subjects:
- kind: ServiceAccount
name: dashboard-admin
namespace: kube-system
roleRef:
kind: ClusterRole
name: cluster-admin
apiGroup: rbac.authorization.k8s.io
#执行创建
kubectl apply -f dashboard-svc-account.yaml
#获取name
kubectl get secret -n kube-system |grep admin|awk '{print $1}'
#获取token
kubectl describe secret 上面操作获得的name -n kube-system|grep '^token'|awk '{print $2}'
得到的token就可以用来访问dashboard了
浏览器访问
https://你的云端ip:dashboard端口/
如https://202.114.10.65:30594/,把token填进去就可以了
更多推荐
所有评论(0)