k8s 系列之 user 的部署

在這之前我們已經部署完成了 eureka 和 gateway  這個時候我們需要去部署 用戶模塊和統一鑑權的auth 服務了

k8s 系列之 部署eureka集羣

k8s 部署gateway

一 : 遇到問題

最明顯的一個問題就是我們實際做開發的時候很肯能不會將數據庫也虛擬化到自己的 容器服務當中 , 那麼這個時候我們怎麼來連接 數據庫 或者是 緩存 和 搜索引擎之類的第三方服務呢 。

應爲目前在pod當中通信我們是通過service 服務暴露出的 ip地址進行相互通信的 但是這只是限於pod 之間的通信 加入我需要連接阿里雲或者是其他地方的數據庫該怎麼辦呢 。

eureka01           ClusterIP   None           <none>        8040/TCP   15m   app=eureka01,project=ms
eureka02           ClusterIP   None           <none>        8040/TCP   15m   app=eureka02,project=ms
external-service   ClusterIP   10.96.49.224   <none>        80/TCP     38h   <none>
gateway            ClusterIP   10.96.22.245   <none>        8030/TCP   12m   app=gateway,project=ms

所以我們需要引入一個概念了  endpoint   通過這個我們解決了pod容器訪問外部資源的問題 

看我們user的資源清單把 

二 : 部署

第一步 : 建立資源清單

apiVersion: v1
kind: Service
metadata:
  name: cloud-user
  namespace: ms
spec:
  ports:
    - name: cloud-user
      port: 8010
  selector:
    project: ms
    app: cloud-user
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: cloud-user
  namespace: ms
spec:
  replicas: 2
  selector:
    matchLabels:
      project: ms
      app: cloud-user
  template:
    metadata:
      labels:
        project: ms
        app: cloud-user
    spec:
      containers:
        - name: cloud-user
          image: registry.cn-hangzhou.aliyuncs.com/sdongp-dkz/cloud-user:1.0-SNAPSHOT
          ports:
            - name: http
              containerPort: 8010

解釋 :爲什麼沒有設置ingress 呢 ? 因爲當user 服務註冊到 eureka當中的時候 就不需要暴露給外部訪問了 , 外部訪問通過gateway 去轉發到相應的服務。

第二部 : 對應的 user 的 application.yml 配置

# DataSource Config
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://192.168.137.1:3306/conlon_cloud?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
    username: root
    password: 123456
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      #初始化連接池
      initial-size: 5
      #連接池最小
      min-idle: 5
      #最大
      max-active: 30
      #檢測關閉空閒連接 單位 hs
      time-between-eviction-runs-millis: 60000
      #去掉後 . 監控界面sql無法統計,'wall'用於防火牆
      filters: stat,wall


# 註冊到eureka
eureka:
  instance:
    # 可以使用ip註冊
    preferIpAddress: true
  client:
    serviceUrl:
      ## 註冊到 eureka (這裏需要自己配置host 和 nginx)
      defaultZone: http://eureka01.ms.svc.cluster.local:8040/eureka/,http://eureka02.ms.svc.cluster.local:8040/eureka/

同樣是註冊到 eureka 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章