/

k8s部署Spring Cloud应用_wanght笔记

k8s 部署 redis sentinel k8s 部署springcloud_选择器

 

k8s 部署 redis sentinel k8s 部署springcloud_docker_02

一  构建镜像 

k8s 部署 redis sentinel k8s 部署springcloud_选择器_03

cd ~/

docker build -t centos7-jdk8:v1 ./jdk/

docker build -t sp-eureka:v1 ./eureka/

docker build -t sp-item:v1 ./item/

docker build -t sp-user:v1 ./user/

docker build -t sp-order:v1 ./order/

 

k8s 部署 redis sentinel k8s 部署springcloud_选择器_04

 二  导出镜像,再导入其他服务器

从191导出五个镜像,压缩后导入到192/193

docker save \
        centos7-jdk8:v1 \
        sp-eureka:v1 \
        sp-item:v1 \
        sp-user:v1 \
        sp-order:v1 \
        | gzip > img.gz
# 将文件复制到 192 和 193
scp img.gz 192.168.64.192:/root/

scp img.gz 192.168.64.193:/root/

k8s 部署 redis sentinel k8s 部署springcloud_html_05

# 在 192 和 193 执行导入
docker load -i img.gz

k8s 部署 redis sentinel k8s 部署springcloud_spring_06

k8s 部署 redis sentinel k8s 部署springcloud_docker_07

 

k8s 部署 redis sentinel k8s 部署springcloud_spring_08

 三  部署

1 eureka

  • 先清理不使用的容器

k8s 部署 redis sentinel k8s 部署springcloud_html_09

  • 用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

k8s 部署 redis sentinel k8s 部署springcloud_docker_10

 

k8s 部署 redis sentinel k8s 部署springcloud_html5_11

 

k8s 部署 redis sentinel k8s 部署springcloud_docker_12

 

k8s 部署 redis sentinel k8s 部署springcloud_选择器_13

 

k8s 部署 redis sentinel k8s 部署springcloud_docker_14

  •  部署 service, 对外暴露eureka访问,使eureka1能连接eureka2,相互访问

这里我们只暴露一个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
cat <<EOF > eureka2-svc.yml
apiVersion: v1
kind: Service
metadata:
  name: eureka2
spec:
  type: NodePort           # 在每个节点上开放访问端口
  ports:
  - port: 2002              # 集群内部访问该服务的端口
    targetPort: 2002       # 容器的端口
    nodePort: 30124        # 外部访问端口
  selector:
    app: eureka2
EOF
  • 执行部署
k create -f eureka1-svc.yml
k create -f eureka2-svc.yml
  • 部署后访问测试:

http://192.168.64.191:30123/ http://192.168.64.191:30124/

k8s 部署 redis sentinel k8s 部署springcloud_html5_15

k8s 部署 redis sentinel k8s 部署springcloud_spring_16

 再次查看eureka1和eureka2的日志中响应状态都是200

k8s 部署 redis sentinel k8s 部署springcloud_html_17

k8s 部署 redis sentinel k8s 部署springcloud_spring_18

 2  item-service

pod副本数量适量打开

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

3  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

4  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

具体操作: 都启动1到2个

k8s 部署 redis sentinel k8s 部署springcloud_html_19

 ...

k8s 部署 redis sentinel k8s 部署springcloud_spring_20

 

k8s 部署 redis sentinel k8s 部署springcloud_选择器_21