k8s部署Spring Cloud應用

下載並導入第18章代碼

https://github.com/benwang6/spring-cloud-repo

只導入前5個項目

修改2,3,4,5項目的yml配置,添加相面配置,使用ip註冊

eureka:
  instance:
    prefer-ip-address: true

按項目順序執行:

  • 右鍵 – run as – maven build…
  • Goals: install
  • 勾選 Skip Tests.

創建四個空文件夾,將2,3,4,5四個項目的jar文件複製到四個文件夾中:

  • eureka
  • item
  • user
  • order

構建鏡像

將 centos:7 鏡像導入服務器

複製centos7-docker-image.gz到服務器並導入

jdk鏡像

新建文件夾 jdk, 複製jdk-8u212-linux-x64.tar.gz到jdk目錄

在 jdk目錄創建 Dockerfile 文件

cat <<EOF > Dockerfile
FROM centos:7
ADD jdk-8u212-linux-x64.tar.gz /opt/
ENV JAVA_HOME=/opt/jdk1.8.0_212 \
    PATH=$PATH:/opt/jdk1.8.0_212/bin:/usr/tomcat/bin
ENTRYPOINT bash
EOF

構建鏡像

docker build -t centos7-jdk8:v1 .

eureka鏡像

進入eureka目錄,創建構建文件

cat <<EOF > Dockerfile
FROM centos7-jdk8:v1
COPY sp05-eureka-0.0.1-SNAPSHOT.jar /opt/
ENTRYPOINT ["java", "-jar", "/opt/sp05-eureka-0.0.1-SNAPSHOT.jar"]
CMD ["--spring.profiles.active=eureka1", "--server.port=2001"]
EOF

構建鏡像

docker build -t sp-eureka:v1 .

item-service 鏡像

進入item目錄,創建構建文件

cat <<EOF > Dockerfile
FROM centos7-jdk8:v1
COPY sp02-itemservice-0.0.1-SNAPSHOT.jar /opt/
ENTRYPOINT ["java", "-jar", "/opt/sp02-itemservice-0.0.1-SNAPSHOT.jar"]
EOF

構建鏡像

docker build -t sp-item:v1 .

user-service 鏡像

進入user目錄,創建構建文件

cat <<EOF > Dockerfile
FROM centos7-jdk8:v1
COPY sp03-userservice-0.0.1-SNAPSHOT.jar /opt/
ENTRYPOINT ["java", "-jar", "/opt/sp03-userservice-0.0.1-SNAPSHOT.jar"]
EOF

構建鏡像

docker build -t sp-user:v1 .

order-service 鏡像

進入docker目錄,創建構建文件

cat <<EOF > Dockerfile
FROM centos7-jdk8:v1
COPY sp04-orderservice-0.0.1-SNAPSHOT.jar /opt/
ENTRYPOINT ["java", "-jar", "/opt/sp04-orderservice-0.0.1-SNAPSHOT.jar"]
EOF

構建鏡像

docker build -t sp-order:v1 .

導出鏡像,再導入其他服務器

docker save \
        centos7-jdk8:v1 \
        sp-eureka:v1 \
        sp-item:v1 \
        sp-user:v1 \
        sp-order:v1 \
        | gzip > img.gz
docker load -i img.gz

部署

eureka

用rs部署容器, 先創建rs部署描述文件

cat <<EOF > eureka1-rs.yml
apiVersion: apps/v1              # RS 是 apps/v1中提供的資源類型
kind: ReplicaSet                 # 資源類型
metadata:
  name: eureka1                    # RS 命名爲 eureka1
spec:
  replicas: 1                    # pod 副本數量
  selector:
    matchLabels:                 # 使用 label 選擇器
      app: eureka1                 # 選取標籤是 "app=eureka1" 的pod
  template:
    metadata:
      labels:
        app: eureka1               # 爲創建的pod添加標籤 "app=eureka1"
    spec:
      containers:
      - name: eureka1             # 容器名
        image: sp-eureka:v1       # 鏡像
        ports:
        - containerPort: 2001    # 容器暴露的端口
          protocol: TCP
EOF
cat <<EOF > eureka2-rs.yml
apiVersion: apps/v1              # RS 是 apps/v1中提供的資源類型
kind: ReplicaSet                 # 資源類型
metadata:
  name: eureka2                    # RS 命名爲 eureka2
spec:
  replicas: 1                    # pod 副本數量
  selector:
    matchLabels:                 # 使用 label 選擇器
      app: eureka2                 # 選取標籤是 "app=eureka2" 的pod
  template:
    metadata:
      labels:
        app: eureka2               # 爲創建的pod添加標籤 "app=eureka2"
    spec:
      containers:
      - name: eureka2             # 容器名
        image: sp-eureka:v1       # 鏡像
        args: ["--spring.profiles.active=eureka2", "--server.port=2002"]
        ports:
        - containerPort: 2002    # 容器暴露的端口
          protocol: TCP
EOF

部署rs, rs會自動創建容器,啓動eureka服務器

k create -f eureka1-rs.yml
k create -f eureka2-rs.yml

部署 service, 對外暴露eureka訪問

這裏我們只暴露一個eureka服務器進行測試

cat <<EOF > eureka1-svc.yml
apiVersion: v1
kind: Service
metadata:
  name: eureka1
spec:
  type: NodePort           # 在每個節點上開放訪問端口
  ports:
  - port: 2001               # 集羣內部訪問該服務的端口
    targetPort: 2001       # 容器的端口
    nodePort: 30123        # 外部訪問端口
  selector:
    app: eureka1
EOF

執行部署

k create -f eureka1-svc.yml

部署後訪問測試:
http://192.168.64.191:30123/

item-service

cat <<EOF > item-rs.yml
apiVersion: apps/v1              # RS 是 apps/v1中提供的資源類型
kind: ReplicaSet                 # 資源類型
metadata:
  name: item                    # RS 命名爲 item
spec:
  replicas: 1                    # pod 副本數量
  selector:
    matchLabels:                 # 使用 label 選擇器
      app: item                 # 選取標籤是 "app=item" 的pod
  template:
    metadata:
      labels:
        app: item               # 爲創建的pod添加標籤 "app=item"
    spec:
      containers:
      - name: item             # 容器名
        image: sp-item:v1       # 鏡像
EOF

user-service

cat <<EOF > user-rs.yml
apiVersion: apps/v1              # RS 是 apps/v1中提供的資源類型
kind: ReplicaSet                 # 資源類型
metadata:
  name: user                    # RS 命名爲 user
spec:
  replicas: 1                    # pod 副本數量
  selector:
    matchLabels:                 # 使用 label 選擇器
      app: user                 # 選取標籤是 "app=user" 的pod
  template:
    metadata:
      labels:
        app: user               # 爲創建的pod添加標籤 "app=user"
    spec:
      containers:
      - name: user             # 容器名
        image: sp-user:v1       # 鏡像
EOF

order-service

cat <<EOF > order-rs.yml
apiVersion: apps/v1              # RS 是 apps/v1中提供的資源類型
kind: ReplicaSet                 # 資源類型
metadata:
  name: order                    # RS 命名爲 order
spec:
  replicas: 1                    # pod 副本數量
  selector:
    matchLabels:                 # 使用 label 選擇器
      app: order                 # 選取標籤是 "app=order" 的pod
  template:
    metadata:
      labels:
        app: order               # 爲創建的pod添加標籤 "app=order"
    spec:
      containers:
      - name: order             # 容器名
        image: sp-order:v1       # 鏡像
EOF

對外暴露order服務,進行測試

cat <<EOF > order-svc.yml
apiVersion: v1
kind: Service
metadata:
  name: order
spec:
  type: NodePort           # 在每個節點上開放訪問端口
  ports:
  - port: 8201              # 集羣內部訪問該服務的端口
    targetPort: 8201       # 容器的端口
    nodePort: 30201        # 外部訪問端口
  selector:
    app: order
EOF
發佈了11 篇原創文章 · 獲贊 661 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章