kubernetes(k8s) ingress及ingress docker-controller
什么是Ingress? --- HTTP 7层路由机制。将集群外部的HTTP和HTTPS路由暴露给集群中的服务, 流量路由由Ingress资源上定义的规则控制
internet
|
[ Ingress ]
--|-----|--
[ Services ]
利用Ingress实现对外部访问(以nginx为例)
1. 创建后端pod及service, 这里的service仅起到识别后端pod组的功能,不对外提供服务
使用deploy创建service和pod,下面是deploy yaml文件内容: apiVersion: v1 kind: Service metadata: name: svc-app namespace: develop # 这里是service需要工作的namespace spec: selector: app: nginx-test release: v0.2 ports: - name: http targetPort: 80 port: 80 --- apiVersion: apps/v1 kind: Deployment metadata: name: deploy-demo namespace: develop # 这里是自定义的后端pod需要工作的namespace spec: replicas: 3 selector: matchLabels: app: nginx-test release: v0.2 template: metadata: labels: app: nginx-test release: v0.2 spec: containers: - name: deploy-ngx image: nginx ports: - name: http containerPort: 80 # pod中的容器服务工作端口 执行命令创建后端服务: # kubectl apply -f deploy-demo.yaml
2. 创建Ingress-nginx Controller服务
# kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/mandatory.yaml 这里我使用的是通用的安装方法,如果需要定制,可以参考: https://github.com/kubernetes/ingress-nginx/blob/master/docs/deploy/index.md
3. 自定义Ingress策略(通过Ingress发布app)
Ingress 策略yaml文件: apiVersion: extensions/v1beta1 kind: Ingress metadata: name: ingress-app namespace: develop # 需要代理的后端Pod工作的namespace annotations: kubernetes.io/ingress.class: "nginx" # 这里必须要使用annotations说明Ingress的类型使用的是nginx spec: rules: - host: myapp.k8s.com # 这里是用虚拟主机来访问(自己本地测试需要修改主机/etc/hosts的域名解析,把这个host添加进去),也可以使用path解析 http: paths: - path: backend: serviceName: svc-app # 后端Pod的service name,通过service来生成nginx的upstrem servicePort: 80 执行以下命令创建: # kubectl apply -f ingress-app.yaml
本机host文件添加域名解析
4. 创建service-nodeport服务,这一步很重要,目的是为了ingress controller能接入外部流量
# wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/baremetal/service-nodeport.yaml #下载源配置文件 修改文件内容如下
apiVersion: v1 kind: Service metadata: name: ingress-nginx namespace: ingress-nginx spec: selector: # 这里需要注意selector标签必须和Ingress controller中的label匹配 app.kubernetes.io/name: ingress-nginx app.kubernetes.io/part-of: ingress-nginx type: NodePort ports: - name: http port: 80 targetPort: 80 protocol: TCP nodePort: 30080 # 从pod映射到Node上的服务端口 - name: https port: 443 targetPort: 443 protocol: TCP nodePort: 30443
5. 然后就可以本机浏览器访问 myapp.k8s.com
由于我是用的AWS EC2,没有持有myapp.k8s.com这个域名解析,使用的本地解析,所以只能在机器上使用curl命令测试,测试结果如下:
HTTPS特别说明:
如果需要支持https访问,需要创建证书。以自建证书部署tomcat为例:
1. 沿用之前的Ingress Controller和service-nodeport不变
2. 创建后端tomcat pod及service:
root@k8s-master:/home/ubuntu/manifests/basic/ingress# cat deploy-tomcat.yaml apiVersion: v1 kind: Service metadata: name: tomcat-svc namespace: develop spec: selector: app: tomcat release: v1 ports: - name: https targetPort: 8080 port: 8080 --- apiVersion: apps/v1 kind: Deployment metadata: name: tomcat-deploy namespace: develop spec: replicas: 2 selector: matchLabels: app: tomcat release: v1 template: metadata: labels: app: tomcat release: v1 spec: containers: - name: tomcat image: tomcat ports: - name: https containerPort: 8080 # tomcat工作端口 # kubectl apply -f deploy-tomcat.yaml
3. 自建SSL证书并通过ingress完成tomcat对外暴露:
# openssl genrsa -out tls.key 2048 # openssl req -new -x509 -key tls.key -out tls.crt -subj /C=CN/ST=Chengdu/L=Chengdu/O=DevOps/CN=tomcat.k8s.com # kubectl create secret tls tomcat-ingress-secret --cert=tls.crt --key=tls.key
修改Ingress yaml策略:
root@k8s-master:/home/ubuntu/manifests/basic/ingress# cat ingress-tomcat.yaml apiVersion: extensions/v1beta1 kind: Ingress metadata: name: ingress-tomcat namespace: develop annotations: kubernetes.io/ingress.class: "nginx" # 这里必须要使用annotations说明Ingress的类型使用的是nginx spec: tls: # 必须添加证书获取方式这一块 - hosts: - tomcat.k8s.com secretName: tomcat-ingress-secret rules: - host: tomcat.k8s.com http: paths: - path: backend: serviceName: tomcat-svc # tomcat后端pod service name servicePort: 443 执行以下命令创建: # kubectl apply -f ingress-app.yaml 然后就可以通过浏览器访问 https://tomcat.k8s.com:30443 # 由于是自建证书,浏览器不信任,但是依旧可以通过https方式打开 使用curl命令访问需要 curl --insecure https://tomcat.k8s.com:30443 # 自建证书浏览器不识别可能依旧是不安全的,所以不再校验。 --insecure表示忽略校验步骤 -k和--insecure 等价