作者简介
Vyacheslav,拥有运维和项目管理经验的软件工程师
这篇文章将承接我此前搭建的本地Docker开发环境,具体步骤已经放在在以下网址:
https://github.com/Voronenko/traefik2-compose-template
除了经典的docker化的项目之外,我还有其他的Kubernetes项目。尽管Kubernetes已经成为容器编排的事实标准,但是不得不承认Kubernetes是一个既消耗资源又消耗金钱的平台。由于我并不经常需要外部集群,因此我使用轻量级K3s发行版来进行Kubernetes本地开发。
K3s是为IoT和边缘计算而构建的经过认证的Kubernetes发行版之一,还能够按产品规模部署到VM。
我使用K3s的方式是这样的:在我的工作笔记本上本地安装K3s,尽管有时我需要在本地部署较重的测试工作负载,为此,我准备了两个神器——两个运行ESXi的外部Intel NUCs。
默认情况下,K3s安装Traefik 1.x作为ingress,如果你对此十分满意,那么无需往下继续阅读了。
在我的场景中,我同时会牵涉到好几个项目,特别是经典的docker和docker swarm,因此我经常遇到在独立模式下部署Traefik的情况。
因此,本文其余部分将深入介绍如何将外部traefik2配置为K3s集群的ingress。
安装Kubernetes K3s系列集群
你可以按照常规方式使用命令curl -sfL https://get.k3s.io | sh -
安装K3s,或者你可以使用轻量实用程序k3sup安装(https://github.com/alexellis/k3sup)。具体步骤在之前的文章介绍过。
与我们的设置不同的是,我们使用命令--no-deploy traefik
专门安装了不带traefik组件的K3s。
export CLUSTER_MASTER=192.168.3.100
export CLUSTER_DEPLOY_USER=slavko
k3sup install --ip $CLUSTER_MASTER --user $CLUSTER_DEPLOY_USER --k3s-extra-args '--no-deploy traefik'
执行后,你将获得使用kubectl所需的连接详细信息。安装K3s后,你可以快速检查是否可以看到节点。
# Test your cluster with - export path to k3s cluster kubeconfig:
export KUBECONFIG=/home/slavko/kubeconfig
kubectl get node -o wide
注:这里没有固定的安装模式,你甚至可以使用docker-compose自行启动它。
server:
image: rancher/k3s:v0.8.0
command: server --disable-agent --no-deploy traefik
environment:
- K3S_CLUSTER_SECRET=somethingtotallyrandom
- K3S_KUBECONFIG_OUTPUT=/output/kubeconfig.yaml
- K3S_KUBECONFIG_MODE=666
volumes:
# k3s will generate a kubeconfig.yaml in this directory. This volume is mounted
# on your host, so you can then 'export KUBECONFIG=/somewhere/on/your/host/out/kubeconfig.yaml',
# in order for your kubectl commands to work.
- /somewhere/on/your/host/out:/output
# This directory is where you put all the (yaml) configuration files of
# the Kubernetes resources.
- /somewhere/on/your/host/in:/var/lib/rancher/k3s/server/manifests
ports:
- 6443:6443
node:
image: rancher/k3s:v0.8.0
privileged: true
links:
- server
environment:
- K3S_URL=https://server:6443
- K3S_CLUSTER_SECRET=somethingtotallyrandom
volumes:
# this is where you would place a alternative traefik image (saved as a .tar file with
# 'docker save'), if you want to use it, instead of the traefik:v2.0 image.
- /sowewhere/on/your/host/custom-image:/var/lib/rancher/k3s/agent/images
配置Traefik 2,与Kubernetes一起使用
在文章开头提到的链接中,我已经在我的系统中安装了Traefik 2,并根据该链接内容,服务于一些需求。现在是时候配置Traefik 2 Kubernetes后端了。
Traefik 2使用CRD(自定义资源定义)来完成这一点。定义的最新示例可以在以下链接中找到,但这些示例仅适用于Traefik 2也作为Kubernetes工作负载的一部分执行的情况:
https://docs.traefik.io/reference/dynamic-configuration/kubernetes-crd/
对于外部Traefik 2,我们仅需要以下描述的定义子集。
我们引入一系列自定义资源定义,以允许我们来描述我们的Kubernetes服务将会如何暴露到外部,traefik-crd.yaml
:
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: ingressroutes.traefik.containo.us
spec:
group: traefik.containo.us
version: v1alpha1
names:
kind: IngressRoute
plural: ingressroutes
singular: ingressroute
scope: Namespaced
---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: ingressroutetcps.traefik.containo.us
spec:
group: traefik.containo.us
version: v1alpha1
names:
kind: IngressRouteTCP
plural: ingressroutetcps
singular: ingressroutetcp
scope: Namespaced
---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: middlewares.traefik.containo.us
spec:
group: traefik.containo.us
version: v1alpha1
names:
kind: Middleware
plural: middlewares
singular: middleware
scope: Namespaced
---
apiVersion: apiextensions.k8s.io/v1bet