一 用k8s部署微服务应用
以我们之前用docker部署过的eureka应用为例,首先添加配置文件eureka-app-deployment.yaml用于创建Deployment
apiVersion: apps/v1
kind: Deployment
metadata:name: eureka-app-deployment # deployment名字labels:app: eureka-app
spec:replicas: 1 #副本个数selector:matchLabels:app: eureka-app template:metadata:labels:app: eureka-appspec:containers:- name: eureka-app #pod名字# 指定Docker Hub中的镜像地址image: zhuge666/microservice-eureka-server:0.0.1ports:- containerPort: 8761 # 容器内部端口
通过应用配置文件来创建一个Deployment部署
kubectl apply -f eureka-app-deployment.yaml
查看下deploy和pod信息
kubectl get deploy,pod
我们可以通过kubectl logs命令来查看应用的启动日志
kubectl logs -f pod/eureka-app-deployment-7cd4b6f4d4-lkzs4
如果想要从外部访问应用,需要创建Service,添加配置文件eureka-app-service.yaml用于创建Service
apiVersion: v1
kind: Service
metadata:name: eureka-app-service
spec:type: NodePortselector:app: eureka-app #选择有该标签的pod容器ports:- name: httpprotocol: TCPport: 8761 #service的端口targetPort: 8761 #pod的端口,一般与pod内部容器的服务端口一致
通过应用配置文件来创建Service
kubectl apply -f eureka-app-service.yaml
查看服务Service信息
[macro@linux-local k8s]$ kubectl get services
此时就可以通过外网来访问了:http://192.168.65.160:30558/
二 用k8s部署电商项目微服务
以电商微服务里的product服务为例,我们来创建对应的deployment和service,做之前需要把商品服务做成镜像推到docker镜像仓库里去,参考之前的docker章节。
docker login
docker tag mall/tulingmall-product:0.0.1 zhuge666/tulingmall-product:0.0.1
docker push zhuge666/tulingmall-product:0.0.1
新增文件tulingmall-product-deployment.yaml,内容如下:
apiVersion: apps/v1
kind: Deployment
metadata:name: tulingmall-product-deploymentlabels:app: tulingmall-product
spec:replicas: 1selector:matchLabels:app: tulingmall-producttemplate:metadata:labels:app: tulingmall-productspec:containers:- name: tulingmall-productimage: zhuge666/tulingmall-product:0.0.1#imagePullPolicy: Always # 1)Always 总是拉取镜像, 2)IfNotPresent(默认该值) 本地有则使用本地镜像,3)Never 只使用本地镜像,从不拉取,即使本地没有镜像ports:- containerPort: 8866env: # 环境- name: TZvalue: Asia/Shanghai- name: spring.cloud.nacos.config.server-addr # 指定配置中心nacosvalue: 192.168.65.42:8848- name: LOG_FILEvalue: /var/logsvolumeMounts:- mountPath: /var/logs #容器日志目录(挂载到下边的宿主机目录)name: log-volumevolumes:- name: log-volumehostPath:path: /mydata/k8s-app/tulingmall-product/logs #宿主机目录type: DirectoryOrCreatednsPolicy: Default #继承Pod所在宿主机的DNS设置,使pod能访问外网
配置环境变量
执行如下命令创建商品服务的deployment:
kubectl apply -f tulingmall-product-deployment.yaml
# 查看
kubectl get all
新增文件tulingmall-product-service.yaml,内容如下:
(其实我们实际部署时不需要写这个文件,因为微服务里有fegin或Ribbion这种内部调用框架去互相调用服务;但是部署gateway时一定要配置对应的service.yaml文件,因为gateway项目是需要外网访问的)
apiVersion: v1
kind: Service
metadata:name: tulingmall-product-service
spec:type: NodePortselector:app: tulingmall-productports:- name: httpprotocol: TCPport: 8866targetPort: 8866
执行如下命令创建商品服务的service:
kubectl apply -f tulingmall-product-service.yaml
查看商品服务的对外暴露端口:
访问下查询商品的接口,如果有json数据返回,代表服务正常:
http://192.168.65.210:30911/pms/productInfo/1
用相同的方法部署下电商里的order,member,gateway,authcenter等服务,这里不一一详述了,附上每个服务k8s部署的yaml文件供大家参考。
authcenter服务
tulingmall-authcenter-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: tulingmall-authcenter-deploymentlabels:app: tulingmall-authcenter
spec:replicas: 1selector:matchLabels:app: tulingmall-authcentertemplate:metadata:labels:app: tulingmall-authcenterspec:containers:- name: tulingmall-authcenterimage: zhuge666/tulingmall-authcenter:0.0.1imagePullPolicy: Alwaysports:- containerPort: 9999env: - name: TZvalue: Asia/Shanghai- name: spring.cloud.nacos.config.server-addrvalue: 192.168.65.42:8848- name: LOG_FILEvalue: /var/logsvolumeMounts:- mountPath: /var/logsname: log-volumevolumes:- name: log-volumehostPath:path: /mydata/k8s-app/tulingmall-authcenter/logstype: DirectoryOrCreatednsPolicy: Default #继承Pod所在宿主机的DNS设置,使pod能访问外网
tulingmall-authcenter-service.yaml
apiVersion: v1
kind: Service
metadata:name: tulingmall-authcenter-service
spec:type: NodePortselector:app: tulingmall-authcenterports:- name: httpprotocol: TCPport: 9999targetPort: 9999
gateway服务
tulingmall-gateway-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: tulingmall-gateway-deploymentlabels:app: tulingmall-gateway
spec:replicas: 1selector:matchLabels:app: tulingmall-gatewaytemplate:metadata:labels:app: tulingmall-gatewayspec:containers:- name: tulingmall-gatewayimage: zhuge666/tulingmall-gateway:0.0.1imagePullPolicy: Alwaysports:- containerPort: 8888env:- name: TZvalue: Asia/Shanghai- name: spring.cloud.nacos.config.server-addrvalue: 192.168.65.42:8848- name: LOG_FILEvalue: /var/logsvolumeMounts:- mountPath: /var/logsname: log-volumevolumes:- name: log-volumehostPath:path: /mydata/k8s-app/tulingmall-gateway/logstype: DirectoryOrCreatednsPolicy: Default #继承Pod所在宿主机的DNS设置,使pod能访问外网
tulingmall-gateway-service.yaml
(其他模块可以不写自己的service.yaml,gateway需要外网访问,所以一定要写service.yaml)
apiVersion: v1
kind: Service
metadata:name: tulingmall-gateway-service
spec:type: NodePortselector:app: tulingmall-gatewayports:- name: httpprotocol: TCPport: 8888targetPort: 8888
order服务
tulingmall-order-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: tulingmall-order-deploymentlabels:app: tulingmall-order
spec:replicas: 1selector:matchLabels:app: tulingmall-ordertemplate:metadata:labels:app: tulingmall-orderspec:containers:- name: tulingmall-orderimage: zhuge666/tulingmall-order:0.0.1imagePullPolicy: Alwaysports:- containerPort: 8844env:- name: TZvalue: Asia/Shanghai- name: spring.cloud.nacos.config.server-addrvalue: 192.168.65.42:8848- name: LOG_FILEvalue: /var/logs- name: JAVA_TOOL_OPTIONSvalue: -Xms1G -Xmx1G -Xmn512M -Xss512K -XX:MetaspaceSize=256M -XX:MaxMetaspaceSize=256M -javaagent:/agent/skywalking-agent.jar -DSW_AGENT_NAME=tulingmall-order -DSW_AGENT_COLLECTOR_BACKEND_SERVICES=192.168.65.204:11800volumeMounts:- mountPath: /var/logsname: log-volumevolumes:- name: log-volumehostPath:path: /mydata/k8s-app/tulingmall-order/logstype: DirectoryOrCreatednsPolicy: Default #继承Pod所在宿主机的DNS设置,使pod能访问外网
tulingmall-order-service.yaml
apiVersion: v1
kind: Service
metadata:name: tulingmall-order-service
spec:type: NodePortselector:app: tulingmall-orderports:- name: httpprotocol: TCPport: 8844targetPort: 8844
member服务
tulingmall-member-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: tulingmall-member-deploymentlabels:app: tulingmall-member
spec:replicas: 1selector:matchLabels:app: tulingmall-membertemplate:metadata:labels:app: tulingmall-memberspec:containers:- name: tulingmall-memberimage: zhuge666/tulingmall-member:0.0.1imagePullPolicy: Alwaysports:- containerPort: 8877env:- name: TZvalue: Asia/Shanghai- name: spring.cloud.nacos.config.server-addrvalue: 192.168.65.42:8848- name: LOG_FILEvalue: /var/logs- name: JAVA_TOOL_OPTIONSvalue: -Xmx1g -Xms1g -XX:MaxMetaspaceSize=256m -javaagent:/agent/skywalking-agent.jar -DSW_AGENT_NAME=tulingmall-member -DSW_AGENT_COLLECTOR_BACKEND_SERVICES=192.168.65.204:11800volumeMounts:- mountPath: /var/logsname: log-volumevolumes:- name: log-volumehostPath:path: /mydata/k8s-app/tulingmall-member/logstype: DirectoryOrCreatednsPolicy: Default #继承Pod所在宿主机的DNS设置,使pod能访问外网
tulingmall-member-service.yaml
apiVersion: v1
kind: Service
metadata:name: tulingmall-member-service
spec:type: NodePortselector:app: tulingmall-memberports:- name: httpprotocol: TCPport: 8877targetPort: 8877
创建网关的Ingress(相当于Nginx)
最后,我们来创建网关服务的Ingress,创建文件tulingmall-gateway-ingress.yaml,内容如下:
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:name: tulingmall-gateway-ingress
spec:rules:- host: gateway.tuling.com #转发域名http:paths:- path: /backend:serviceName: tulingmall-gateway-serviceservicePort: 8888 #service的端口
执行如下命令生效规则:
kubectl apply -f tulingmall-gateway-ingress.yaml
查看生效的ingress规则:
kubectl get ing
在访问机器配置host,win10客户机在目录:C:\Windows\System32\drivers\etc,在host里增加如下host(ingress部署的机器ip对应访问的域名)
192.168.65.203 gateway.tuling.com
或者
192.168.65.210 gateway.tuling.com
配置完后直接在客户机用域名请求下网关: