参考了https://github.com/kubernetes/ingress上的内容进行学习。
有关Ingress的介绍可以阅读http://jimmysong.io/kubernetes-handbook/concepts/ingress.html这篇博客,它是对Kubernetes官方文档中关于Ingress部分的翻译。
先记录安装配置过程,之后再慢慢深入研究。
为了让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/v1beta1
kind: Deployment
metadata:
name: default-http-backend
labels:
k8s-app: default-http-backend
namespace: kube-system
spec:
replicas: 1
template:
metadata:
labels:
k8s-app: default-http-backend
spec:
terminationGracePeriodSeconds: 60
containers:
- name: default-http-backend
# Any image is permissable as long as:
# 1. It serves a 404 page at /
# 2. It serves 200 on a /healthz endpoint
image: 192.168.121.140:5000/defaultbackend
livenessProbe:
httpGet:
path: /healthz
port: 8080
scheme: HTTP
initialDelaySeconds: 30
timeoutSeconds: 5
ports:
- containerPort: 8080
resources:
limits:
cpu: 10m
memory: 20Mi
requests:
cpu: 10m
memory: 20Mi
---
apiVersion: v1
kind: Service
metadata:
name: default-http-backend
namespace: kube-system
labels:
k8s-app: default-http-backend
spec:
ports:
- port: 80
targetPort: 8080
selector:
k8s-app: default-http-backend
创建backend
# kubectl apply -f default-backend.yaml
在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端口转发到default/frontend:80服务端口上)
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-tcp-ingress-configmap
namespace: kube-system
data:
9000: "default/frontend:80"
创建
# kubectl create -f nginx-tcp-ingress-configmap.yaml
nginx-tcp-ingress-controller.yaml定义文件复制自https://github.com/kubernetes/ingress/blob/master/examples/tcp/nginx/nginx-tcp-ingress-controller.yaml
内容如下:
apiVersion: v1
kind: ReplicationController
metadata:
name: nginx-ingress-controller
labels:
k8s-app: nginx-ingress-lb
namespace: kube-system
spec:
replicas: 1
selector:
k8s-app: nginx-tcp-ingress-lb
template:
metadata:
labels:
k8s-app: nginx-tcp-ingress-lb
name: nginx-tcp-ingress-lb
spec:
terminationGracePeriodSeconds: 60
containers:
- image: 192.168.121.140:5000/nginx-ingress-controller
name: nginx-tcp-ingress-lb
readinessProbe:
httpGet:
path: /healthz
port: 10254
scheme: HTTP
livenessProbe:
httpGet:
path: /healthz
port: 10254
scheme: HTTP
initialDelaySeconds: 10
timeoutSeconds: 1
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
ports:
- containerPort: 80
hostPort: 80
- containerPort: 443
hostPort: 443
- containerPort: 9000
hostPort: 9000
args:
- /nginx-ingress-controller
- --default-backend-service=$(POD_NAMESPACE)/default-http-backend
- --tcp-services-configmap=$(POD_NAMESPACE)/nginx-tcp-ingress-configmap
创建:
# kubectl create -f nginx-tcp-ingress-controller.yaml
从上图看出nginx-ingress-controller-fm45m运行在192.168.121.145上。之前的nginx-tcp-ingress-configmap定义的端口号是9000。
访问192.168.121.145:9000直接可以访问到Guestbook。