配置默认的backend
为了让Ingress Controller正常启动,需要给它配置一个默认的backend,用于当客户端访问的URL地址不存在时,能够返回一个正确的404应答。
这个backend服务用任何应用实现都可以,只要满足默认对/path的访问返回404的应答,同时能够提供/healthz路径以提供kubelet完成对它的健康检查。
这里default-backend的定义文件复制于kubernetes官方文档。
https://github.com/kubernetes/ingress/blob/master/examples/deployment/nginx/default-backend.yaml
所需镜像地址为:gcr.io/google_containers/defaultbackend:1.0
这里我已经push到了本地仓库里。
内容如下:
apiVersion: extensions/v1beta1kind:Deploymentmetadata: name:default-http-backendlabels: k8s-app:default-http-backendnamespace: kube-systemspec: replicas:1template: metadata: labels: k8s-app:default-http-backendspec: terminationGracePeriodSeconds:60containers: - name:default-http-backend#Anyimage is permissableaslongas: #1.Itserves a404page at / #2.Itserves200on a /healthz endpoint image:192.168.121.140:5000/defaultbackend livenessProbe: httpGet: path: /healthz port:8080scheme:HTTPinitialDelaySeconds:30timeoutSeconds:5ports: - containerPort:8080resources: limits: cpu:10m memory:20Mi requests: cpu:10m memory:20Mi---apiVersion: v1kind:Servicemetadata: name:default-http-backendnamespace: kube-system labels: k8s-app:default-http-backendspec: ports: - port:80targetPort:8080selector: k8s-app:default-http-backend
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
创建backend
# kubectl apply -f default-backend.yaml
1
创建Ingress Controller
在kubernetes中,Ingress Controller将以Pod的形式运行,监控apiserver的/ingress接口后端的backend services,如果service发生变化,则Ingress Controller自动更新其转发规则。
在此使用谷歌提供的nginx-ingress-controller镜像。镜像地址为:gcr.io/google_containers/nginx-ingress-controller:0.9.0-beta.11(yaml中修改为了本地镜像)
这里是针对TCP的负载均衡例子。
对kubernetes学习记录(6)——Guestbook Demo frontend service做负载均衡。
首先是nginx-tcp-ingress-configmap.yaml
yaml定义文件复制自https://github.com/kubernetes/ingress/blob/master/examples/tcp/nginx/nginx-tcp-ingress-configmap.yaml
内容如下:(将9000端口转发到defaulf/frontend:80服务端口上)
apiVersion: v1kind: ConfigMapmetadata: name: nginx-tcp-ingress-configmapnamespace: kube-systemdata:9000:"default/frontend:80"
1
2
3
4
5
6
7
创建
# kubectl create -f nginx-tcp-ingress-configmap.yaml
1
nginx-tcp-ingress-controller.yaml定义文件复制自https://github.com/kubernetes/ingress/blob/master/examples/tcp/nginx/nginx-tcp-ingress-controller.yaml
内容如下:
apiVersion: v1kind: ReplicationControllermetadata: name: nginx-ingress-controllerlabels: k8s-app: nginx-ingress-lbnamespace: kube-systemspec: replicas:1selector: k8s-app: nginx-tcp-ingress-lbtemplate: metadata: labels: k8s-app: nginx-tcp-ingress-lbname: nginx-tcp-ingress-lbspec: terminationGracePeriodSeconds:60containers:-image:192.168.121.140:5000/nginx-ingress-controllername: nginx-tcp-ingress-lbreadinessProbe: httpGet: path: /healthz port:10254scheme: HTTP livenessProbe: httpGet: path: /healthz port:10254scheme: HTTP initialDelaySeconds:10timeoutSeconds:1env:-name: POD_NAME valueFrom: fieldRef: fieldPath: metadata.name-name: POD_NAMESPACE valueFrom: fieldRef: fieldPath: metadata.namespace ports:-containerPort:80hostPort:80-containerPort:443hostPort:443-containerPort:9000hostPort:9000args:-/nginx-ingress-controller---default-backend-service=$(POD_NAMESPACE)/default-http-backend---tcp-services-configmap=$(POD_NAMESPACE)/nginx-tcp-ingress-configmap
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
创建:
# kubectl create -f nginx-tcp-ingress-controller.yaml
1
验证
从上图看出nginx-ingress-controller-fm45m运行在192.168.121.145上。之前的nginx-tcp-ingress-configmap定义的端口号是9000。
访问192.168.121.145:9000直接可以访问到Guestbook。