一 用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-app
    spec:
      containers:
        - name: eureka-app #pod名字
          # 指定Docker Hub中的镜像地址
          image: zhuge666/microservice-eureka-server:0.0.1
          ports:
            - 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: NodePort
  selector:
    app: eureka-app #选择有该标签的pod容器
  ports:
  - name: http
    protocol: TCP
    port: 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-deployment
  labels:
    app: tulingmall-product
spec:
  replicas: 1
  selector:
    matchLabels:
      app: tulingmall-product
  template:
    metadata:
      labels:
        app: tulingmall-product
    spec:
      containers:
        - name: tulingmall-product
          image: zhuge666/tulingmall-product:0.0.1
          #imagePullPolicy: Always  # 1)Always 总是拉取镜像, 2)IfNotPresent(默认该值) 本地有则使用本地镜像,3)Never 只使用本地镜像,从不拉取,即使本地没有镜像
          ports:
            - containerPort: 8866
          env: # 环境
            - name: TZ
              value: Asia/Shanghai
            - name: spring.cloud.nacos.config.server-addr # 指定配置中心nacos
              value: 192.168.65.42:8848
            - name: LOG_FILE
              value: /var/logs
          volumeMounts:
            - mountPath: /var/logs #容器日志目录(挂载到下边的宿主机目录)
              name: log-volume
      volumes:
        - name: log-volume
          hostPath:
            path: /mydata/k8s-app/tulingmall-product/logs #宿主机目录
            type: DirectoryOrCreate
      dnsPolicy: 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: NodePort
  selector:
    app: tulingmall-product
  ports:
  - name: http
    protocol: TCP
    port: 8866
    targetPort: 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-deployment
  labels:
    app: tulingmall-authcenter
spec:
  replicas: 1
  selector:
    matchLabels:
      app: tulingmall-authcenter
  template:
    metadata:
      labels:
        app: tulingmall-authcenter
    spec:
      containers:
        - name: tulingmall-authcenter
          image: zhuge666/tulingmall-authcenter:0.0.1
          imagePullPolicy: Always
          ports:
            - containerPort: 9999
          env: 
            - name: TZ
              value: Asia/Shanghai
            - name: spring.cloud.nacos.config.server-addr
              value: 192.168.65.42:8848
            - name: LOG_FILE
              value: /var/logs
          volumeMounts:
            - mountPath: /var/logs
              name: log-volume
      volumes:
        - name: log-volume
          hostPath:
            path: /mydata/k8s-app/tulingmall-authcenter/logs
            type: DirectoryOrCreate
      dnsPolicy: Default  #继承Pod所在宿主机的DNS设置,使pod能访问外网

tulingmall-authcenter-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: tulingmall-authcenter-service
spec:
  type: NodePort
  selector:
    app: tulingmall-authcenter
  ports:
  - name: http
    protocol: TCP
    port: 9999
    targetPort: 9999

gateway服务
tulingmall-gateway-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: tulingmall-gateway-deployment
  labels:
    app: tulingmall-gateway
spec:
  replicas: 1
  selector:
    matchLabels:
      app: tulingmall-gateway
  template:
    metadata:
      labels:
        app: tulingmall-gateway
    spec:
      containers:
        - name: tulingmall-gateway
          image: zhuge666/tulingmall-gateway:0.0.1
          imagePullPolicy: Always
          ports:
            - containerPort: 8888
          env:
            - name: TZ
              value: Asia/Shanghai
            - name: spring.cloud.nacos.config.server-addr
              value: 192.168.65.42:8848
            - name: LOG_FILE
              value: /var/logs
          volumeMounts:
            - mountPath: /var/logs
              name: log-volume
      volumes:
        - name: log-volume
          hostPath:
            path: /mydata/k8s-app/tulingmall-gateway/logs
            type: DirectoryOrCreate
      dnsPolicy: Default  #继承Pod所在宿主机的DNS设置,使pod能访问外网

tulingmall-gateway-service.yaml
(其他模块可以不写自己的service.yaml,gateway需要外网访问,所以一定要写service.yaml)

apiVersion: v1
kind: Service
metadata:
  name: tulingmall-gateway-service
spec:
  type: NodePort
  selector:
    app: tulingmall-gateway
  ports:
  - name: http
    protocol: TCP
    port: 8888
    targetPort: 8888

order服务
tulingmall-order-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: tulingmall-order-deployment
  labels:
    app: tulingmall-order
spec:
  replicas: 1
  selector:
    matchLabels:
      app: tulingmall-order
  template:
    metadata:
      labels:
        app: tulingmall-order
    spec:
      containers:
        - name: tulingmall-order
          image: zhuge666/tulingmall-order:0.0.1
          imagePullPolicy: Always
          ports:
            - containerPort: 8844
          env:
            - name: TZ
              value: Asia/Shanghai
            - name: spring.cloud.nacos.config.server-addr
              value: 192.168.65.42:8848
            - name: LOG_FILE
              value: /var/logs
            - name: JAVA_TOOL_OPTIONS
              value: -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:11800
          volumeMounts:
            - mountPath: /var/logs
              name: log-volume
      volumes:
        - name: log-volume
          hostPath:
            path: /mydata/k8s-app/tulingmall-order/logs
            type: DirectoryOrCreate
      dnsPolicy: Default  #继承Pod所在宿主机的DNS设置,使pod能访问外网   

tulingmall-order-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: tulingmall-order-service
spec:
  type: NodePort
  selector:
    app: tulingmall-order
  ports:
  - name: http
    protocol: TCP
    port: 8844
    targetPort: 8844

member服务
tulingmall-member-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: tulingmall-member-deployment
  labels:
    app: tulingmall-member
spec:
  replicas: 1
  selector:
    matchLabels:
      app: tulingmall-member
  template:
    metadata:
      labels:
        app: tulingmall-member
    spec:
      containers:
        - name: tulingmall-member
          image: zhuge666/tulingmall-member:0.0.1
          imagePullPolicy: Always
          ports:
            - containerPort: 8877
          env:
            - name: TZ
              value: Asia/Shanghai
            - name: spring.cloud.nacos.config.server-addr
              value: 192.168.65.42:8848
            - name: LOG_FILE
              value: /var/logs
            - name: JAVA_TOOL_OPTIONS
              value: -Xmx1g -Xms1g -XX:MaxMetaspaceSize=256m -javaagent:/agent/skywalking-agent.jar -DSW_AGENT_NAME=tulingmall-member -DSW_AGENT_COLLECTOR_BACKEND_SERVICES=192.168.65.204:11800
          volumeMounts:
            - mountPath: /var/logs
              name: log-volume
      volumes:
        - name: log-volume
          hostPath:
            path: /mydata/k8s-app/tulingmall-member/logs
            type: DirectoryOrCreate
      dnsPolicy: Default  #继承Pod所在宿主机的DNS设置,使pod能访问外网

tulingmall-member-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: tulingmall-member-service
spec:
  type: NodePort
  selector:
    app: tulingmall-member
  ports:
  - name: http
    protocol: TCP
    port: 8877
    targetPort: 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-service
          servicePort: 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

配置完后直接在客户机用域名请求下网关:
在这里插入图片描述

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐