美文网首页
记录项目实战部署一个服务

记录项目实战部署一个服务

作者: Robin92 | 来源:发表于2022-04-07 22:35 被阅读0次

背景

我们外包给乙方实现一个服务,乙方完成后将文件传给我。交付的内容有两个 docker 镜像和一个模型目录文件,以及一些启动脚本。我先在本地虚拟机的集群中练下手。

所交付的启动脚本及文件信息如下。

两个yaml文件 启动脚本

通过阅读可以发现:

  • 它是用 docker-composer 启动了两个镜像
  • 两个镜像使用了同一网络空间
  • x-rpc.yaml 中挂在了卷的映射,映射到容器中的是 /opt/xxx/model 文件

转写成k8s文件

x.yaml
# xxxx.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: xxxx-deployment
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: xxxx
    spec:
      containers:
        - name: xxxx-dialog
          image: 172.16.156.128:5000/amind/xxxx/dialog:v2.0
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 59999
        - name: xxxx-rpc
          image: 172.16.156.128:5000/amind/xxxx/sentence_rpc:v2.0
          imagePullPolicy: IfNotPresent
          volumeMounts:
          - name: iamvolume
            mountPath: /opt/xxxx_sentence_rpc/model
          ports:
            - containerPort: 8000
      volumes:
        - name: iamvolume
          hostPath:
            path: /root/xxxx/model

中间做了一些必要的操作:

  • 将他给的两个 docker 打 tag 后传到自己的镜像库中。
  • 网搜了一下怎么在 k8s 中映射本地目录(注意部署节点本地目录的存在问题)

启动之后通过命令暴露服务的端口。

kubectl expose deployment tasi-deployment --port=59999 --type=NodePort
image.png

这样之后成功可以通过 172.16.156.128:13828/ 地址访问到服务。

image.png

当前返回 500 的错误是服务返回的,可以证明访问是没问题的。后面的问题应该是两个服务之间还没实现通信。当前项目是通过 python 实现的,python 是动态原因,所以直接在 dialog 容器中修改配置文件就可以完成两个容器的访问。

创建 svc 方式后访问超时

kubectl expose deploy/tasi-deployment --port=59999 --type=NodePort
# 返回:service "tasi-deployment" exposed

创建结果:

image.png
# describe 信息
[root@k8s-master ~]# kubectl describe svc/tasi-deployment
Name:           tasi-deployment
Namespace:      default
Labels:         app=tasi
Selector:       app=tasi
Type:           NodePort
IP:         10.254.110.142
Port:           <unset> 59999/TCP
NodePort:       <unset> 3750/TCP
Endpoints:      172.18.95.4:59999
Session Affinity:   None
No events.

但是通过访问 host:3750 连接超时不可用。重试多次依然无果(但之前确实 OK 过),通过抓包如下(抓包时映射到宿主机的端口变为 40291 了)

抓包

网络上搜索问题原因可能是因为 flannel 绑定网卡错误的原因,但目前还不知道怎么解决。后面会玩再解决,先记录下。

将 svc 写入到配置文件中

修改配置文件,添加如下配置:

# ... 原配置信息
---
apiVersion: v1
kind: Service
metadata:
  name: xxx-service
spec:
  type: NodePort
  ports:
    - port: 59999
      nodePort: 49991
      targetPort: 59999
  selector:
    app: xxx

启动。

指定node部署

重启后发现部署总是发生 k8s-master 节点,一部署起来就造成 master 上各命令执行很慢。所以把它指定 node 进行部署。

首先,查看节点信息

# kubectl get nodes --show-labels
NAME         STATUS    AGE       LABELS
k8s-master   Ready     26d       beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=k8s-master
k8s-node1    Ready     25d       beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=k8s-node1
k8s-node2    Ready     23d       beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=k8s-node2

必知技巧

通过 kubectl explain deployment.spec.template.spec 来查看 yaml 支持的配置格式。如看到:

FIELDS:
   nodeName   <string>
     NodeName is a request to schedule this pod onto a specific node. If it is
     non-empty, the scheduler simply schedules this pod onto that node, assuming
     that it fits resource requirements.
     
   nodeSelector   <object>
     NodeSelector is a selector which must be true for the pod to fit on a node.
     Selector which must match a node's labels for the pod to be scheduled on
     that node. More info:
     http://kubernetes.io/docs/user-guide/node-selection/README
  ...

配置文件 deployment.spec.template.spec.nodeName: k8s-node2 来实现指定 node 部署。重试多次,测试结果。

image.png

相关文章

网友评论

      本文标题:记录项目实战部署一个服务

      本文链接:https://www.haomeiwen.com/subject/stxesrtx.html