如何搭建ingress-nginx
2019-11-06 本文已影响0人
无处安放的躁动
前言
什么是ingress-nginx
什么Kubernetes Ingress Controller
什么是Kubernetes Ingress Controller
什么是daemonset
如何实现ingress-nginx
- 部署需要对外暴露服务
- 部署ingress-nginx
- 设置ingress-nginx 通过servicename关联需要暴露的
- 如何让外部访问到ingress-nginx
- 通过nodeport暴露ingress-nginx
- 通过DaemonSet暴露ingress-nginx
如何使用ingress-nginx
部署测试使用的前端以及后端
- 前端nginx的deployment配置文件
vim nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: nginx-pod
name: nginx-pod
spec:
replicas: 2
selector:
matchLabels:
app: nginx-pod
template:
metadata:
labels:
app: nginx-pod
spec:
imagePullSecrets:
- name: registry-pull-secret
containers:
- image: nginx
imagePullPolicy: Always
name: nginx-pod
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx-pod-service
labels:
app: nginx-pod
spec:
type: ClusterIP
clusterIP: None
ports:
- port: 80
targetPort: 80
selector:
app: nginx-pod
- kubectl 部署nginx
# nginx 的namespace为default
kubectl apply -f nginx.yaml
- 后端tomcat的deployment文件实例
vim tomcat.yaml
apiVersion: v1
kind: Namespace
metadata:
name: gitlab
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: gitlab
name: ldd-attendance
namespace: szlaozi
spec:
replicas: 1
selector:
matchLabels:
app: gitlab
template:
metadata:
labels:
app: gitlab
spec:
imagePullSecrets:
- name: registry-pull-secret
containers:
- image: tomcat
imagePullPolicy: Always
name: ldd-attendance
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: ldd-attendance-service
labels:
app: gitlab
namespace: szlaozi
spec:
type: NodePort
ports:
- port: 80
nodePort: 30101
targetPort: 8080
selector:
app: gitlab
- kubectl 部署tomcat
# nginx 的namespace为nginx
kubectl apply -f nginx.yaml
部署ingress-nginx
官方文件站点
- 下载文件mandatory.yaml
文件列表
apiVersion: v1
kind: Namespace
metadata:
name: ingress-nginx
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
---
kind: ConfigMap
apiVersion: v1
metadata:
name: nginx-configuration
namespace: ingress-nginx
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
---
kind: ConfigMap
apiVersion: v1
metadata:
name: tcp-services
namespace: ingress-nginx
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
---
kind: ConfigMap
apiVersion: v1
metadata:
name: udp-services
namespace: ingress-nginx
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: nginx-ingress-serviceaccount
namespace: ingress-nginx
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:
name: nginx-ingress-clusterrole
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
rules:
- apiGroups:
- ""
resources:
- configmaps
- endpoints
- nodes
- pods
- secrets
verbs:
- list
- watch
- apiGroups:
- ""
resources:
- nodes
verbs:
- get
- apiGroups:
- ""
resources:
- services
verbs:
- get
- list
- watch
- apiGroups:
- ""
resources:
- events
verbs:
- create
- patch
- apiGroups:
- "extensions"
- "networking.k8s.io"
resources:
- ingresses
verbs:
- get
- list
- watch
- apiGroups:
- "extensions"
- "networking.k8s.io"
resources:
- ingresses/status
verbs:
- update
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: Role
metadata:
name: nginx-ingress-role
namespace: ingress-nginx
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
rules:
- apiGroups:
- ""
resources:
- configmaps
- pods
- secrets
- namespaces
verbs:
- get
- apiGroups:
- ""
resources:
- configmaps
resourceNames:
# Defaults to "<election-id>-<ingress-class>"
# Here: "<ingress-controller-leader>-<nginx>"
# This has to be adapted if you change either parameter
# when launching the nginx-ingress-controller.
- "ingress-controller-leader-nginx"
verbs:
- get
- update
- apiGroups:
- ""
resources:
- configmaps
verbs:
- create
- apiGroups:
- ""
resources:
- endpoints
verbs:
- get
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: RoleBinding
metadata:
name: nginx-ingress-role-nisa-binding
namespace: ingress-nginx
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: nginx-ingress-role
subjects:
- kind: ServiceAccount
name: nginx-ingress-serviceaccount
namespace: ingress-nginx
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: nginx-ingress-clusterrole-nisa-binding
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: nginx-ingress-clusterrole
subjects:
- kind: ServiceAccount
name: nginx-ingress-serviceaccount
namespace: ingress-nginx
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-ingress-controller
namespace: ingress-nginx
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
template:
metadata:
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
annotations:
prometheus.io/port: "10254"
prometheus.io/scrape: "true"
spec:
# wait up to five minutes for the drain of connections
terminationGracePeriodSeconds: 300
serviceAccountName: nginx-ingress-serviceaccount
nodeSelector:
kubernetes.io/os: linux
containers:
- name: nginx-ingress-controller
image: quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.26.1
args:
- /nginx-ingress-controller
- --configmap=$(POD_NAMESPACE)/nginx-configuration
- --tcp-services-configmap=$(POD_NAMESPACE)/tcp-services
- --udp-services-configmap=$(POD_NAMESPACE)/udp-services
- --publish-service=$(POD_NAMESPACE)/ingress-nginx
- --annotations-prefix=nginx.ingress.kubernetes.io
securityContext:
allowPrivilegeEscalation: true
capabilities:
drop:
- ALL
add:
- NET_BIND_SERVICE
# www-data -> 33
runAsUser: 33
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
ports:
- name: http
containerPort: 80
- name: https
containerPort: 443
livenessProbe:
failureThreshold: 3
httpGet:
path: /healthz
port: 10254
scheme: HTTP
initialDelaySeconds: 10
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 10
readinessProbe:
failureThreshold: 3
httpGet:
path: /healthz
port: 10254
scheme: HTTP
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 10
lifecycle:
preStop:
exec:
command:
- /wait-shutdown
---
- 执行部署
kubectl apply -f mandatory.yaml
设置ingress-nginx规则
分析
- 一个ingress 规则只可以定义在一个namespace
- 我们的前端和后端在不同的namespace default以及szlaozi
- 要实现同一个地址可以访问前端和后端,可以通过定义两个ingress规则来实现,如下
- ingress-nginx 规则ingress-nginx.yaml
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: ingress-rules
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: james.szlaozi.com
http:
paths:
- path: /
backend:
serviceName: nginx-pod-service
servicePort: 80
- ingress-nginx 规则ingress-tomcat.yaml
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: ingress-tomcat-rules
namespace: szlaozi
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: james.szlaozi.com
http:
paths:
- path: /backend
backend:
serviceName: ldd-attendance-service
servicePort: 8080
- 执行ingress-nginx 规则
# nginx
kubectl apply -f ingress-nginx.yaml
# tomcat
kubectl apply -f ingress-tomcat.yaml
【暴露服务1】通过nodeport 暴露服务让k8s外部访问
apiVersion: v1
kind: Service
metadata:
name: ingress-nginx
namespace: ingress-nginx
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
spec:
type: NodePort
ports:
- name: http
port: 80
targetPort: 80
protocol: TCP
- name: https
port: 443
targetPort: 443
protocol: TCP
selector:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
【暴露服务2】通过DaemonSet方式让外部访问
- 略
外部访问测试
- 如何得知端口以及地址
# 下面命令得知nodeport端口为:31401,而ip地址为:node节点ip,但是必须解析域名才可以访问
# 所以需要客户端访问前解析james.szlaozi.com的地址为node节点地址
[root@k8smaster dome]# kubectl get svc -n ingress-nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-nginx NodePort 10.96.217.139 <none> 80:31401/TCP,443:31605/TCP 4h23m
- 修改windows hosts文件,如上面规则所诉host 为:james.szlaozi.com
# ip为hosts节点
# 文件地址: C:\Windows\System32\drivers\etc\hosts
192.168.100.10 james.szlaozi.com
-
浏览器访问前端
页面访问前端 -
浏览器访问后端
页面访问后端 -
通过IP无法访问
直接通过IP访问为禁止