traefik 负载均衡

Posted by 小炒肉 on December 26, 2016

Traefik 是一款开源的反向代理与负载均衡工具。它最大的优点是能够与常见的微服务系统直接整合,可以实现自动化动态配置。目前支持Docker, Swarm, Mesos/Marathon, Mesos, kubernetes, Consul, Etcd, Zookeeper, BoltDB, Rest API等等后端模型

Traefik 简介

traefik 官网 https://docs.traefik.io/
github https://github.com/containous/traefik

traefik 与 kubernetes

1.1 kubernetes 环境

1
2
3
4
5
6
[[email protected] ~]#kubectl get nodes
NAME         STATUS         AGE
k8s-node-1   Ready,master   5d
k8s-node-2   Ready          5d
k8s-node-3   Ready          5d

1.2 下载 traefik-dm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# 定义 node-2 node-3 标签。

kubectl label nodes k8s-node-2 role=traefik-lb
kubectl label nodes k8s-node-3 role=traefik-lb

# 查看此标签的 nodes

[[email protected] ~]#kubectl get nodes -l role=traefik-lb
NAME         STATUS    AGE
k8s-node-2   Ready     5d
k8s-node-3   Ready     5d




# 下载yaml 文件

curl -O https://raw.githubusercontent.com/containous/traefik/master/examples/k8s/traefik.yaml
		
		
# yaml 可修改 replicas = node 个数
#      增加 restartPolicy: Always
#	   修改 web ui 端口 containerPort: 8080 修改为 containerPort: 8888
#	                    args: 下面增加 --web.address=:8888
#	   增加 nodeSelector:
#              role: traefik-lb
			   
# 修改后的文件如下:

apiVersion: v1
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: traefik-ingress-controller
  namespace: kube-system
  labels:
    k8s-app: traefik-ingress-lb
spec:
  replicas: 2
  selector:
    matchLabels:
      k8s-app: traefik-ingress-lb
  template:
    metadata:
      labels:
        k8s-app: traefik-ingress-lb
        name: traefik-ingress-lb
    spec:
      terminationGracePeriodSeconds: 60
      hostNetwork: true
	  restartPolicy: Always
      nodeSelector:
        role: traefik-lb
      containers:
      - image: traefik
        name: traefik-ingress-lb
        resources:
          limits:
            cpu: 200m
            memory: 30Mi
          requests:
            cpu: 100m
            memory: 20Mi
        ports:
        - name: http
          containerPort: 80
          hostPort: 80
        - name: admin
          containerPort: 8888
        args:
        - --web
        - --web.address=:8888
        - --kubernetes

		
# 创建 traefik deployment

[[email protected] ~]#kubectl create -f traefik.yaml 
deployment "traefik-ingress-controller" created


# 查看 deployment

[[email protected] ~]#kubectl get deploy --all-namespaces
NAMESPACE     NAME                         DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
kube-system   traefik-ingress-controller   1         1         1            1           2m

1.3 部署一个 nginx 应用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 创建一个 nginx yaml 文件

apiVersion: extensions/v1beta1 
kind: Deployment 
metadata: 
  name: nginx-dm
spec: 
  replicas: 2
  template: 
    metadata: 
      labels: 
        name: nginx 
    spec: 
      containers: 
        - name: nginx 
          image: nginx 
          imagePullPolicy: IfNotPresent
          ports: 
            - containerPort: 80

---
apiVersion: v1 
kind: Service
metadata: 
  name: nginx-dm 
spec: 
  ports: 
    - port: 80
      targetPort: 80
      protocol: TCP 
  selector: 
    name: nginx
	

# 创建 deploy 与 svc

[[email protected] ~]#kubectl create -f nginx.yaml 



# 查看 nginx-dm

[[email protected] ~]#kubectl get svc
NAME         CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
kubernetes   10.96.0.1       <none>        443/TCP   5d
nginx-dm     10.105.76.250   <none>        80/TCP    34m
[[email protected] ~]#kubectl get deployment
NAME       DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
nginx-dm   2         2         2            2           35m
	

1.4 创建 traefik ingress

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 创建 traefik-ingress yaml 文件


apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: traefik-ingress
spec:
  rules:
  - host: traefik.test.jicki.me
    http:
      paths:
      - path: /
        backend:
          serviceName: nginx-dm
          servicePort: 80


# 创建 ingress	  
[[email protected] ~]#kubectl create -f traefik-ingress.yaml


# 查看 ing

[[email protected] ~]#kubectl get ing
NAME              HOSTS                   ADDRESS   PORTS     AGE
traefik-ingress   traefik.test.jicki.me             80        32m	
	

1.5 创建 traefik web ui

1
2
3
4
5
6
# 下载 web ui yaml 文件

curl -O https://raw.githubusercontent.com/containous/traefik/master/examples/k8s/ui.yaml

[[email protected] ~]#kubectl create -f ui.yaml

1.6 访问 nginx 应用

1
2
3
4
5
6
7
8
9
10
[[email protected] ~]#curl -I traefik.test.jicki.me
HTTP/1.1 200 OK
Accept-Ranges: bytes
Content-Length: 612
Content-Type: text/html
Date: Mon, 26 Dec 2016 09:18:33 GMT
Etag: "585011f4-264"
Last-Modified: Tue, 13 Dec 2016 15:21:24 GMT
Server: nginx/1.11.7

1.7 访问 traefik web ui

1
2
http://10.6.0.187:8888
http://10.6.0.188:8888

1.8 心跳检测