企业运维实战--k8s学习笔记 通过Ingress-nginx实现k8s七层负载均衡Ingress加密认证以及地址重写
Posted Shinmy.
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了企业运维实战--k8s学习笔记 通过Ingress-nginx实现k8s七层负载均衡Ingress加密认证以及地址重写相关的知识,希望对你有一定的参考价值。
1.Ingress服务简介
一种全局的、为了代理不同后端 Service 而设置的负载均衡服务,就是 Kubernetes 里的Ingress 服务。
Ingress由两部分组成:Ingress controller和Ingress服务。
Ingress Controller 会根据你定义的 Ingress 对象,提供对应的代理能力。业界常用的各种反向代理项目,比如 nginx、HAProxy、Envoy、Traefik 等,都已经为Kubernetes 专门维护了对应的 Ingress Controller。
ingress服务的使用必须在策略网络中才能实现,如calico网络
ingress相当于一个7层的负载均衡器,是k8s对反向代理的一个抽象。大概的工作原理也确实类似于Nginx,可以理解成在 Ingress 里建立一个个映射规则 , ingress Controller 通过监听 Ingress这个api对象里的配置规则并转化成 Nginx 的配置(kubernetes声明式API和控制循环) , 然后对外部提供服务。ingress包括:ingress controller和ingress resources
ingress controller:核心是一个deployment,实现方式有很多,比如nginx, Contour, Haproxy, trafik, Istio,需要编写的yaml有:Deployment, Service, ConfigMap, ServiceAccount(Auth),其中service的类型可以是NodePort或者LoadBalancer。
ingress resources:这个就是一个类型为Ingress的k8s api对象了,这部分则是面向开发人员。
2.ingress部署
mkdir ingress
cd ingress/
下载所需要的deploy.yaml
vi deploy.yaml
image: ingress-nginx/controller:v1.0.3
image: ingress-nginx/kube-webhook-certgen:v1.5.1
在自己的仓库中添加公开项目ingress-nginx专门存放相关的镜像
执行deploy.yaml清单,查看ns状态
kubectl apply -f deploy.yaml
kubectl get ns
查看ingress-nginx的所有信息,可以看到ingress-nginx-controller已经running
kubectl -n ingress-nginx get all
查看svc暴露端口
kubectl -n ingress-nginx get svc
测试:访问server1ip地址加上端口
3.域名访问+ingress-nginx(配置ingress-nginx七层均衡)
3.1创建pod:nginx myapp
vi deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: myapp:v1
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deployment
labels:
app: myapp
spec:
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myapp:v2
拉起deployment,通过标签来锁定域名
3.2配置添加svc服务
vi svc.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-svc
spec:
ports:
- protocol: TCP
port: 80
targetPort: 80
selector:
app: nginx
---
apiVersion: v1
kind: Service
metadata:
name: myapp-svc
spec:
ports:
- protocol: TCP
port: 80
targetPort: 80
selector:
app: myapp
拉起服务并查看endpoint
应用yaml文件,创建服务
kubectl apply -f svc.yaml
查看服务信息
kubectl get svc
修改svc配置文件为负载均衡
kubectl -n ingress-nginx edit svc ingress-nginx-controller
查看修改内容
kubectl -n ingress-nginx get svc
external-ip 为 172.25.76.10
3.3服务与ingress连接,并给定域名 ,用于匹配service
vi ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-nginx
spec:
rules:
- host: www1.westos.org
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: nginx-svc
port:
number: 80
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-myapp
spec:
rules:
- host: www2.westos.org
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: myapp-svc
port:
number: 80
拉起资源清单
kubectl apply -f ingress.yaml
查看ingress反向代理信息
kubectl get ingress
测试机添加解析到服务节点172.25.76.10
vi /etc/hosts
172.25.76.10 www1.westos.org www2.westos.org
访问测试:
[root@foundation76 ingress]# curl www2.westos.org
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
[root@foundation76 ingress]# curl www1.westos.org
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
4.Ingress 认证
因为认证需要系统自带的认证功能!
所以需要安装生成认证整数的插件
yum install -y httpd-tools
4.1.生成证书
生成证书,输入认证密码
创建证书到k8s secret
htpasswd -c auth lcf #生成证书,输入认证密码
kubectl create secret generic basic-auth --from-file=auth
# 创建证书到k8s secret
kubectl get secrets
编辑文件ingress.yaml导入证书
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-nginx
annotations:
nginx.ingress.kubernetes.io/auth-type: basic
nginx.ingress.kubernetes.io/auth-secret: basic-auth
nginx.ingress.kubernetes.io/auth-realm: 'Authentication Required - lcf'
spec:
tls:
- hosts:
- www1.westos.org
secretName: tls-secret
rules:
- host: www1.westos.org
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: nginx-svc
port:
number: 80
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-myapp
annotations:
nginx.ingress.kubernetes.io/auth-type: basic
nginx.ingress.kubernetes.io/auth-secret: basic-auth
nginx.ingress.kubernetes.io/auth-realm: 'Authentication Required - lcf'
spec:
tls:
- hosts:
- www2.westos.org
secretName: tls-secret
rules:
- host: www2.westos.org
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: myapp-svc
port:
number: 80
执行ingress.yaml文件
kubectl apply -f ingress.yaml
访问测试
curl -k https://www1.westos.org -u lcf:westos
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
5. 重定向
5.1.重定向到 /hostname.html
修改文件ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-nginx
annotations:
nginx.ingress.kubernetes.io/auth-type: basic
nginx.ingress.kubernetes.io/auth-secret: basic-auth
nginx.ingress.kubernetes.io/auth-realm: 'Authentication Required - lcf'
nginx.ingress.kubernetes.io/app-root: /hostname.html
spec:
tls:
- hosts:
- www1.westos.org
secretName: tls-secret
rules:
- host: www1.westos.org
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: nginx-svc
port:
number: 80
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-myapp
annotations:
nginx.ingress.kubernetes.io/auth-type: basic
nginx.ingress.kubernetes.io/auth-secret: basic-auth
nginx.ingress.kubernetes.io/auth-realm: 'Authentication Required - lcf'
nginx.ingress.kubernetes.io/app-root: /hostname.html
spec:
tls:
- hosts:
- www2.westos.org
secretName: tls-secret
rules:
- host: www2.westos.org
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: myapp-svc
port:
number: 80
再次执行文件ingress.yaml,查看信息:
kubectl apply -f ingress.yaml
kubectl describe ingress
访问www1.westos.org发现自动重定向到了www1.westos.org/hostname.html
以上是关于企业运维实战--k8s学习笔记 通过Ingress-nginx实现k8s七层负载均衡Ingress加密认证以及地址重写的主要内容,如果未能解决你的问题,请参考以下文章
企业运维实战-k8s学习笔记17.k8s集群+Prometheus监控部署基于prometheus实现k8s集群的hpa动态伸缩虚拟机部署prometheus监控