Ubuntu搭建K3S+rancher
欢迎转载,请支持原创,保留原文链接:blog.ilibrary.me
Mac上搭建
k3s不能直接跑在Mac上,在Mac上可以通过K3D搭建,把k3s跑在容器里。 具体步骤参考Kubernetes + Rancher Cluster Manager 2.6 on your macOS laptop with k3d/k3s in 5 min
步骤:
brew install k3d kubectl helm
- Use k3d to spin up a single-node Kubernetes cluster (using the k3s distro)
k3d cluster create k3d-rancher \ --api-port 6550 \ --servers 1 \ --image rancher/k3s:v1.20.10-k3s1 \ --port 443:443@loadbalancer \ --wait --verbose
- Use helm to bootstrap cert-manager then Rancher to the cluster
### Install cert-manager with helm helm repo add jetstack https://charts.jetstack.io helm repo update kubectl create namespace cert-manager helm install cert-manager jetstack/cert-manager \ --namespace cert-manager \ --version v1.5.3 \ --set installCRDs=true --wait --debug kubectl -n cert-manager rollout status deploy/cert-manager date ### Install the helm repos for rancher helm repo add rancher-latest https://releases.rancher.com/server-charts/latest helm repo update kubectl create namespace cattle-system helm install rancher rancher-latest/rancher \ --namespace cattle-system \ --version=2.6.1 \ --set hostname=rancher.localhost \ --set bootstrapPassword=congratsthanandayme \ --wait --debug kubectl -n cattle-system rollout status deploy/rancher kubectl -n cattle-system get all,ing date
- Use the Rancher GUI to observe the cluster. browse to https://rancher.localhost. If you see this screen where you can’t manually accept the risk, you may have to use the thisisunsafe chrome trick. You do this trick by clicking on the red warning triangle and literally typing “thisisunsafe” (believe me!)
- bootstrap password:
congratsthanandayme
- bootstrap password还可以通过docker命令获取:
docker logs container-id 2>&1 | grep "Bootstrap Password:"
- 或者通过helm获取:
kubectl get secret --namespace cattle-system bootstrap-secret -o go-template='\n'
- bootstrap password:
- 可以通过
.localhost
来访问127.0.0.1
brew install k3d kubectl helm
k3d version
k3d cluster create devcluster
在linux上搭建
- 搭建云服务, ubuntu系统.
- 安装docker:
apt install docker.io
- 安装k3s master node:
curl -sfL https://get.k3s.io | sh -s - --docker # 自动安装, 并且以docker为容器运行时,默认为containerd. sudo k3s kubectl get nodes # confirm it is running cat /var/lib/rancher/k3s/server/node-token # 获取master node token
K3s 包含并默认为containerd, 一个行业标准的容器运行时。要使用 Docker 而不是 containerd,可以用下面两种方法:
- 使用
--docker
选项安装K3s:curl -sfL https://get.k3s.io | sh -s - --docker
- 在 K3s 节点上安装 Docker。可以使用 Rancher 的一个Docker 安装脚本来安装 Docker:
curl https://releases.rancher.com/install-docker/19.03.sh | sh
- 参考使用 Docker 作为容器运行时.
卸载k3s:
/usr/local/bin/k3s-uninstall.sh
- 使用
- 查看containers: ` k3s ctr task ls`
- 安装K3S worker nodes:
需要用到上一步在master node获取到的master node token.
curl -sfL http://get.k3s.io | K3S_URL=https://<master_IP>:6443 K3S_TOKEN=<join_token> sh -s - --docker # 用docker作为运行时。默认是 containerd作为运行时.
- install helm:
curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 chmod 700 get_helm.sh ./get_helm.sh
Notes: helm包下载很慢,容易断,如果脚本安装不了,就本地下载helm tar.gz, 然后scp拷贝到服务器上,然后解压安装,官方有文档:
tar -zxvf helm-v3.0.0-linux-amd64.tar.gz mv linux-amd64/helm /usr/local/bin/helm
- 更新helm repo
helm repo add stable https://charts.helm.sh/stable helm repo add rancher-stable https://releases.rancher.com/server-charts/stable helm repo update
- (optional)安装 cert-manager,
kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.11.0/cert-manager.yaml
kubectl create namespace cattle-system && kubectl create namespace cert-manager
- 通过helm安装rancher:
# 注意rancher.localhost, localhost后缀是可以自动解析到本地127.0.0.1的
helm install rancher rancher-stable/rancher \
--namespace cattle-system \
--set hostname=rancher.localhost \
--set bootstrapPassword=verySecretivePasswordIndeed \
--set ingress.tls.source=secret
或者
helm install rancher rancher-latest/rancher --namespace cattle-system --set hostname=k3s-rancher.localhost
kubectl -n cattle-system rollout status deploy/rancher
kubectl -n cattle-system get deploy rancher
- 获取setup link:
echo https://rancher.mydomain.com/dashboard/?setup=$(kubectl get secret --namespace cattle-system bootstrap-secret -o go-template='')
- helm版本rancher获取rancher登录密码:
k3s kubectl get secret --namespace cattle-system bootstrap-secret -o go-template='\n'
- rancher.ilibrary.me账号密码:
admin/BtfEQI1E7yajt9lx
在Rancher里创建cluster
参考multiple k8s cluster management with rancher k3s lightweight k8s cluster for edge
- 按照说明里做。
- 做了以后可能要等1个多小时cluster才会从reconciling变成active.
- 变成active以后可以按页面的说明部署一个hello world玩玩。
Notes
Error: INSTALLATION FAILED: repo rancher-stable not found
- helm repo add rancher-stable https://releases.rancher.com/server-charts/stable
Error: INSTALLATION FAILED: Kubernetes cluster unreachable: Get "http://localhost:8080/version": dial tcp [::1]:8080: connect: connection refused
- export KUBECONFIG=/etc/rancher/k3s/k3s.yaml
Error: INSTALLATION FAILED: chart requires kubeVersion: < 1.25.0-0 which is incompatible with Kubernetes v1.25.6+k3s1
- 安装指定版本:
curl -sfL https://get.k3s.io | INSTALL_K3S_VERSION=v1.23.16+k3s1 sh -
- 如果curl命令超时,可以手动下载k3s binary, 放
/usr/local/bin
目录下, 然后再运行上面的curl命令,脚本会跳过k3s下载步骤,走后面的配置流程。
- 安装指定版本:
- rainbow部署以后通过ingress发布,https://ranxxx.ixxx.me/rainbow不工作。
- chart里面的host写的不对,改成目标host就好了。
Error: INSTALLATION FAILED: unable to build kubernetes objects from release manifest: resource mapping not found for name: "rancher" namespace: "" from "": no matches for kind "Issuer" in version "cert-manager.io/v1" ensure CRDs are installed first
手动打包部署
- 创建chart:
helm create mychart
- 打包:
helm package mychart
或者指定输出的版本helm package mychart --version=1.0.1
- 上传
mychart-1.0.1.tgz
到服务器:scp mychart-1.0.1.tgz root@xxxx.com:~/
- ssh到服务器, 安装chart:
helm install mychart ./mychart-1.0.1.tgz # or helm install -n my-app --namespace myspace mychart-1.0.1.tgz # or tar -zxvf mychart-1.0.1.tgz helm install -n my-app --namespace myspace mychart/
- 如果有在线tgz包,也可以在命令行直接安装在线包:
helm install jenkins https://example.com/charts/jenkins-1.2.3.tgz
自动打包部署
- 进入Rancher CD页面
- 点击
git repos
->Create
, - 填名称
- 填git repo地址:
git@github.com:xxxxx/myk8s.git
,xxxxx/myk8s
代表个人github repo.- Rancher有个官方example:
https://github.com/rancher/pipeline-example-go.git
- Rancher有个官方example:
- 手动生成ssh key pair, 把private/pub key填入rancher
- 把pub key设置到repo的deploy keys里面去。
Rancher管理
删除Rancher
- 列出所有安装好的App:
helm list -A
- 删除指定namespace下面的指定App:
helm uninstall rancher -n cattle-system
删除k3d
- k3d是一个binary, 它会管理所有k3s相关的容器.
which k3d
, 查看k3d的路径.k3d cluster delete -a
, 删除所有k3d创建的资源.
scripts
k3d
- k3d cluster list
- k3d cluster delete -a
- k3d cluster create devcluster
- k3d cluster create devhacluster –servers 3 –agents 1
- k3d cluster delete devhacluster
k3s
- kubectl get nodes
Helm
- helm repo add jetstack https://charts.jetstack.io
- helm repo update
- 安装的时候监控日志直到安装完成:
helm install cert-manager jetstack/cert-manager \ --namespace cert-manager \ --version v1.5.3 \ --set installCRDs=true --wait --debug
kubectl
- kubectl create namespace cert-manager
- kubectl -n cert-manager rollout status deploy/cert-manager
- kubectl cluster-info
- kubectl get po -o wide -A
Ref
- https://computingforgeeks.com/install-kubernetes-on-ubuntu-using-k3s/
- https://selectfrom.dev/multiple-k8s-cluster-management-with-rancher-k3s-lightweight-k8s-cluster-for-edge-and-eea1f71175d0
- K8s - Kubernetes重要概念介绍(Cluster、Master、Node、Pod、Controller、Service、Namespace)
- Kubernetes指南
- 单机部署k3s, 提供了一个sh, k3s-helm-install.sh
- Autok3s, Rancher 中国团队推出了一款针对 K3s 的效率提升工具:AutoK3s。只需要输入一行命令,即可快速创建 K3s 集群并添加指定数量的 master 节点和 worker 节点.
- How to install rancher on k3s
- 【大强哥-k8s从入门到放弃12】Deployment资源详解, 对K8S基本概念讲得很清楚。
- Kubernetes + Rancher Cluster Manager 2.6 on your macOS laptop with k3d/k3s in 5 min
- Provision K3S cluster on mac using K3D
- 打包Helm Chart
- Rancher CI/CD 官方流水线