0%

k8s_Microservices_Architecture

Microservices Architecture

Example

首先创建Pod

  • 投票app 的pod

    voting-app-pod.yaml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    apiVersion: v1
    kind: Pod
    metadata:
    name: voting-app-pod #pod name
    labels:
    name: voting-app-pod
    app: demo-voting-app
    spec:
    containers:
    - name: voting-app #container name
    image: kodekloud/examplevotingapp_vote:v1
    ports:
    - containerPort: 80 #the port on which the application listens for this voting app

    我们需要用containerPort属性为voting application明确指定port

  • result app的Pod

    result-app-pod.yaml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    apiVersion: v1
    kind: Pod
    metadata:
    name: result-app-pod
    labels:
    name: result-app-pod
    app: demo-voting-app
    spec:
    containers:
    - name: result-app
    image: kodekloud/examplevotingapp_result:v1
    ports:
    - containerPort: 80
  • redis的Pod

    redis-pod.yaml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    apiVersion: v1
    kind: Pod
    metadata:
    name: redis-pod
    labels:
    name: redis-pod
    app: demo-voting-app
    spec:
    containers:
    - name: redis
    image: redis
    ports:
    - containerPort: 6379
  • postgres的Pod

    postgres-pod.yaml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    apiVersion: v1
    kind: Pod
    metadata:
    name: postgres-pod
    labels:
    name: postgres-pod
    app: demo-voting-app
    spec:
    contianers:
    - name: postgres
    image: postgres
    ports:
    - containerPort: 5432
    env:
    - name: POSTGRES_USER
    value: "postgres"
    - name: POSTGRES_PASSWORD
    value: "postgres"
  • worker的Pod

    worker-app-pod.yaml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    apiVersion: v1
    kind: Pod
    metadata:
    name: worker-app-pod
    labels:
    name: worker-app-pod
    app: demo-voting-app
    spec:
    containers:
    - name: worker-app
    image: kodekloud/examplevotingapp_worker:v1
    ports:
    - containerPort: 80

创建service

  • redis的service (voting-app的Pod连接该service)

    redis-service.yaml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    apiVersion: v1
    kind: Service
    metadata:
    name: redis #voting-app源码里面要连接的数据库为redis,因此这里命名为redis
    labels:
    name: redis-service
    app: demo-voting-app
    spec:
    ports:
    - port: 6379 #service暴露给其他连接的port
    targetPort: 6379 #service要连接的redis的Pod的port
    selector: #link the service to the Pod
    name: redis-pod #redis Pod的label
    app: demo-voting-app
  • postgres的service (result-app的Pod连接该service)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    apiVersion: v1
    kind: Service
    metadata:
    name: db #result-app源码里面连接的数据库名称叫db,因此service要命名为db
    labels:
    name: postgre-service #随意定义
    app: demo-voting-app
    spec:
    ports:
    - port: 5432
    targetPort: 5432
    selector: #link the service to the Pod
    name: postgres-pod #要连接的postgre Pod的labels
    app: demo-voting-app
  • voting-app的service (用户从外面连接该service)

    voting-app-service.yaml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    apiVersion: v1
    kind: Service
    metadata:
    name: result-service
    labels:
    name: voting-service
    app: demo-voting-app
    spec:
    type: NodePort #这里选择NodePort,因为要暴露给外面用户使用
    ports:
    - port: 80
    targetPort: 80
    nodePort: 30004
    selector: #voting-app的Pod的labels
    name: voting-app-pod
    app: demo-voting-app
  • result-app的service

    result-app-service.yaml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    apiVersion: v1
    kind: Service
    metadata:
    name: result-service
    labels:
    name: result-service
    app: demo-voting-app
    spec:
    type: NodePort
    ports:
    - port: 80
    targetPort: 80
    nodePort: 30005
    selector: #result-app的Pod的labels
    name: result-app-pod
    app: demo-voting-app

启动Pod和Service

1
kubectl get pods,svc

使用Deployment来创建k8s微服务

1606112620439

用创建一个个Pod的方法并不能实现Pod的扩容(scale up),但选择Deployment能帮助我们实现,replicaSet,rolling update,rollback等功能

将上面例子中pod的定义文件改写成Deployment的定义文件。

  • voting-app的Deployment 文件

  • voting-app-deployment.yaml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: voting-app-deploy
    labels:
    name: voting-app-deploy
    app: demo-voting-app
    spec:
    replicas: 1
    selector: #pod的labels
    matchLabels:
    name: voting-app-pod
    app: demo-voting-app
    template:
    metadata:
    name: voting-app-pod #pod name
    labels:
    name: voting-app-pod
    app: demo-voting-app
    spec:
    containers:
    - name: voting-app #container name
    image: kodekloud/examplevotingapp_vote:v1
    ports:
    - containerPort: 80
  • result-app-deployment.yaml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: result-app-deploy
    labels:
    name: result-app-deploy
    app: demo-voting-app
    spec:
    replicas: 1
    selector: #pod的labels
    matchLabels:
    name: result-app-pod
    app: demo-voting-app
    template:
    metadata:
    name: result-app-pod
    labels:
    name: result-app-pod
    app: demo-voting-app
    spec:
    containers:
    - name: result-app
    image: kodekloud/examplevotingapp_result:v1
    ports:
    - containerPort: 80
  • redis的Deployment

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: redis-deploy
    labels:
    name: redis-deploy
    app: demo-voting-app
    spec:
    replicas: 1
    selector: #pod的labels
    matchLabels:
    name: redis-pod
    app: demo-voting-appp
    template:
    metadata:
    name: redis-pod
    labels:
    name: redis-pod
    app: demo-voting-app
    spec:
    containers:
    - name: redis
    image: redis
    ports:
    - containerPort: 6379
  • postgres-deploy.yaml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: postgres-deploy
    labels:
    name: postgres-deploy
    app: demo-voting-app
    spec:
    replicas: 1
    selector: #pod的labels
    matchLabels:
    name: postgres-pod
    app: demo-voting-app
    template:
    metadata:
    name: postgres-pod
    labels:
    name: postgres-pod
    app: demo-voting-app
    spec:
    containers:
    - name: postgres
    image: postgres
    ports:
    - containerPort: 5432
    env:
    - name: POSTGRES_USER
    value: "postgres"
    - name: POSTGRES_PASSWORD
    value: "postgres"
  • worker-app-deploy.yaml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: worker-app-deploy
    labels:
    name: worker-app-deploy
    app: demo-voting-app
    spec:
    replicas: 1
    selector: #pod的labels
    matchLabels:
    name: worker-app-pod
    app: demo-voting-app
    template:
    metadata:
    name: worker-app-pod
    labels:
    name: worker-app-pod
    app: demo-voting-app
    spec:
    containers:
    - name: worker-app
    image: kodekloud/examplevotingapp_worker:v1

依次创建deployment和Service

kubectl create -f voting-app-deployment.yaml

kubectl create -f voting-app-service.yaml