微服务 在 k8s 中部署网关 Gateway 服务
在 k8s 中部署网关 Gateway 服务
网关就是门户,由它向后路由
1)构建镜像
[root@master microservic-test]# cd gateway-service/
[root@master gateway-service]# ls
Dockerfile pom.xml src target
[root@master gateway-service]# cd target/
[root@master target]# ls
classes gateway-service.jar gateway-service.jar.original generated-sources maven-archiver maven-status
[root@master gateway-service]# cat Dockerfile
FROM java:8-jdk-alpine
RUN apk add -U tzdata && \
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
COPY ./target/gateway-service.jar ./
EXPOSE 9999
CMD java -jar /gateway-service.jar
[root@master gateway-service]# docker build -t reg.harbor.com/microservice/gateway:v1 .
Sending build context to Docker daemon 43.45MB
Step 1/5 : FROM java:8-jdk-alpine
---> 3fd9dd82815c
Step 2/5 : RUN apk add -U tzdata && ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
---> Using cache
---> a9034491fdd5
Step 3/5 : COPY ./target/gateway-service.jar ./
---> db38bc07283d
Step 4/5 : EXPOSE 9999
---> Running in a292d38e4aaa
Removing intermediate container a292d38e4aaa
---> 75e7856ae711
Step 5/5 : CMD java -jar /gateway-service.jar
---> Running in 349f78c72d84
Removing intermediate container 349f78c72d84
---> 292feb2950b5
Successfully built 292feb2950b5
Successfully tagged reg.harbor.com/microservice/gateway:v1
[root@master gateway-service]# docker push reg.harbor.com/microservice/gateway:v1
The push refers to repository [reg.harbor.com/microservice/gateway]
7f8ea654d86b: Pushed
8994a8716f11: Mounted from microservice/eureka
a1e7033f082e: Mounted from microservice/eureka
78075328e0da: Mounted from microservice/eureka
9f8566ee5135: Mounted from microservice/eureka
v1: digest: sha256:32bd210362f8638c80e967d6ca7744ddfd16953c58d7c4d2d17198cd79e7093b size: 1370
2)服务的配置文件
为所有微服务提供统一的入口,只要来的请求都会经过gateway
[root@master ~]# cd microservic-test/gateway-service/src/main/resources/
[root@master resources]# cat application-fat.yml
spring:
cloud:
gateway:
discovery:
locator:
#开启以服务id去注册中心获取转发地址
enabled: true
##灏..serviceId
lower-case-service-id: true
routes:
- id: product-service
uri: lb://product-service
filters:
- StripPrefix=1
predicates:
- Path=/product/**
- id: order-service
uri: lb://order-service
filters:
- StripPrefix=1
predicates:
- Path=/order/**
- id: stock-service
uri: lb://stock-service
filters:
- StripPrefix=1
predicates:
- Path=/stock/**
eureka:
instance:
prefer-ip-address: true
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://eureka-0.eureka.ms:8888/eureka,http://eureka-1.eureka.ms:8888/eureka,http://eureka-2.eureka.ms:8888/eureka
3)部署服务
[root@master k8s]# cat gateway.yaml
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: gateway
namespace: ms
spec:
rules:
- host: gateway.ctnrs.com
http:
paths:
- path: /
backend:
serviceName: gateway
servicePort: 9999
---
apiVersion: v1
kind: Service
metadata:
name: gateway
namespace: ms
spec:
ports:
- port: 9999
name: gateway
selector:
project: ms
app: gateway
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: gateway
namespace: ms
spec:
replicas: 1
selector:
matchLabels:
project: ms
app: gateway
template:
metadata:
labels:
project: ms
app: gateway
spec:
imagePullSecrets:
- name: registry-pull-secret
containers:
- name: gateway
image: reg.harbor.com/microservice/gateway:v1
imagePullPolicy: Always
ports:
- protocol: TCP
containerPort: 9999
resources:
requests:
cpu: 0.5
memory: 256Mi
limits:
cpu: 1
memory: 1Gi
readinessProbe:
tcpSocket:
port: 9999
initialDelaySeconds: 60
periodSeconds: 10
livenessProbe:
tcpSocket:
port: 9999
initialDelaySeconds: 60
periodSeconds: 10
[root@master k8s]# kubectl get pod -n ms
NAME READY STATUS RESTARTS AGE
eureka-0 1/1 Running 0 23h
eureka-1 1/1 Running 0 22h
eureka-2 1/1 Running 0 22h
gateway-77bb7dbbc8-xqkk5 1/1 Running 0 2m18s
[root@master k8s]# kubectl get svc -n ms
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
eureka ClusterIP None <none> 8888/TCP 23h
gateway ClusterIP 10.233.53.75 <none> 9999/TCP 10m
[root@master k8s]# kubectl get ingress -n ms
NAME CLASS HOSTS ADDRESS PORTS AGE
eureka <none> eureka.ctnrs.com 80 22h
gateway <none> gateway.ctnrs.com 80 10s
[root@master k8s]# kubectl describe ingress gateway -n ms
Name: gateway
Namespace: ms
Address:
Default backend: default-http-backend:80 (<error: endpoints "default-http-backend" not found>)
Rules:
Host Path Backends
---- ---- --------
gateway.ctnrs.com
/ gateway:9999 (10.233.90.39:9999)
Annotations: Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal CREATE 7m12s nginx-ingress-controller Ingress ms/gateway
Normal CREATE 7m12s nginx-ingress-controller Ingress ms/gateway
可以看到网关服务的pod也注册到eureak,所以启动服务的时候默认都会注册到eureak
目录 返回
首页