1、镜像
docker pull traefik:v1.7.33-alpine
2、tag
docker images | grep traefik
traefik v1.7.33-alpine d2edc46527be 3 months ago 89MB
docker tag d2edc46527be registry.cn-shenzhen.aliyuncs.com/hqyinfra/traefik:v1.7.33-alpine
3、推送
docker push registry.cn-shenzhen.aliyuncs.com/hqyinfra/traefik:v1.7.33-alpine
4、rbac.yaml
kubectl apply -f rbac.yaml
kind: ServiceAccount
apiVersion: v1
metadata:
name: traefik
namespace: kube-system
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: traefik
rules:
- apiGroups:
- ""
resources:
- services
- endpoints
- secrets
verbs:
- get
- list
- watch
- apiGroups:
- extensions
resources:
- ingresses
verbs:
- get
- list
- watch
- apiGroups:
- extensions
resources:
- ingresses/status
verbs:
- update
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: traefik
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: traefik
subjects:
- kind: ServiceAccount
name: traefik
namespace: kube-system
5、ds.yaml
kubectl apply -f ds.yaml
kind: DaemonSet
apiVersion: apps/v1
metadata:
name: traefik
namespace: kube-system
labels:
app: traefik
spec:
selector:
matchLabels:
app: traefik
template:
metadata:
labels:
app: traefik
name: traefik
spec:
serviceAccountName: traefik
terminationGracePeriodSeconds: 60
containers:
- image: registry.cn-shenzhen.aliyuncs.com/hqyinfra/traefik:v1.7.33-alpine
name: traefik
ports:
- name: http
containerPort: 80
hostPort: 8848
- name: admin
containerPort: 8080
securityContext:
capabilities:
drop:
- ALL
add:
- NET_BIND_SERVICE
args:
- --api
- --kubernetes
- --logLevel=INFO
- --insecureskipverify=true
- --metrics.prometheus
6、svc.yaml
kubectl apply -f svc.yaml
kind: Service
apiVersion: v1
metadata:
name: traefik
namespace: kube-system
spec:
selector:
app: traefik
ports:
- protocol: TCP
port: 80
name: http
- protocol: TCP
port: 8080
name: admin
7、ingress.yaml
kubectl apply -f ingress.yaml
kind: Ingress
apiVersion: networking.k8s.io/v1
metadata:
name: traefik
namespace: kube-system
spec:
rules:
- host: traefik.candy.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: traefik
port:
number: 8080
8、配置hosts
vi /etc/hosts
127.0.0.1 traefik.candy.com
9、部署转发的nginx
docker pull nginx:1.21.5-alpine
docker images | grep nginx
nginx 1.21.5-alpine cc44224bfe20 2 weeks ago 23.5MB
docker tag cc44224bfe20 registry.cn-shenzhen.aliyuncs.com/hqyinfra/nginx:v1.21.5-alpine
docker push registry.cn-shenzhen.aliyuncs.com/hqyinfra/nginx:v1.21.5-alpine
docker run -d -p 80:80 -p 443:443 --name nginx registry.cn-shenzhen.aliyuncs.com/hqyinfra/nginx:v1.21.5-alpine
10、修改nginx配置
kubectl get nodes -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
docker-desktop Ready control-plane,master 91d v1.21.5 192.168.65.4 <none> Docker Desktop 5.10.47-linuxkit docker://20.10.8
docker exec -it nginx sh
vi /etc/nginx/conf.d/candy.com.conf
candy.com.conf
upstream default_backend_traefik {
# node ip
server 192.168.65.4:8848;
}
server {
server_name *.candy.com;
location / {
proxy_pass http://default_backend_traefik;
proxy_set_header Host $http_host;
proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
}
}
nginx -s reload
exit
11、浏览器访问:traefik.candy.com