K8S之Ingress常用Nginx、Traefik示例
Nginx Ingress
- 创建Pod、Service,定义如下
nginx-ingress-demo.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: nginx-deploy
namespace: default
spec:
replicas: 3
selector:
matchLabels:
app: nginx-demo
replease: canary
template:
metadata:
name: my-nginx
labels:
app: nginx-demo
release: canary
spec:
containers:
- name: my-nginx
image: nginx
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx-svc
namespace: default
spec:
selector:
app: nginx-demo
release: canary
ports:
- name: http
port: 80
targetPort: 80
kubectl apply -f nginx-ingress-demo.yaml
- 定义Ingress Nginx
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: ingress-nginx
annotations:
kubenetes.io/ingress.class: "nginx"
spec:
rules:
- host:
host:
paths:
- path:
backend:
serverName: nginx-svc
servicePort: 80
Tracefik Ingress
- 配置Tracefik
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
name: traefik-ingress-controller
namespace: ingress-traefik
labels:
k8s-app: traefik-ingress-lb
spec:
replicas: 1
selector:
matchLabels:
k8s-app: traefik-ingress-lb
name: traefik-ingress-lb
template:
metadata:
labels:
k8s-app: traefik-ingress-lb
name: traefik-ingress-lb
spec:
serviceAccountName: traefik-ingress-controller
terminationGracePeriodSeconds: 60
# tolerations:
# - operator: "Exists"
# nodeSelector:
# kubernetes.io/hostname: master
containers:
- image: traefik:v1.7.17
name: traefik-ingress-lb
volumeMounts:
- name: ssl
mountPath: "/ssl"
- name: config
mountPath: "/config"
ports:
- name: http
containerPort: 80
- name: https
containerPort: 443
- name: admin
containerPort: 8080
args:
- --configfile=/config/traefik.toml
- --api
- --kubernetes
- --logLevel=INFO
volumes:
- name: ssl
secret:
secretName: traefik-cert
- name: config
configMap:
name: traefik-conf
- 定义service
kind: Service
apiVersion: v1
metadata:
name: traefik-ingress-service
namespace: ingress-traefik
spec:
selector:
k8s-app: traefik-ingress-lb
ports:
- protocol: TCP
port: 80
name: web
nodePort: 38000
- protocol: TCP
port: 8080
nodePort: 38080
name: admin
type: NodePort
- 定义Ingress
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: nginx-ingress-demo
annotations:
kubernetes.io/ingress.class: "traefik"
traefik.ingress.kubernetes.io/app-root: /app/
traefik.ingress.kubernetes.io/rewrite-target: /$2
traefik.ingress.kubernetes.io/configuration-snippet: |
rewrite ^(/app)$ $1/ redirect;
rewrite ^/static/(.*)$ /app/static/$1 redirect;
rewrite ^/image/(.*)$ /app/image/$1 redirect;
spec:
rules:
- host:
http:
paths:
- backend:
serviceName: nginx-demo
servicePort: 80
path: /app(/|$)(.*)