kubernetes应用分布式部署方案(1)

JinLonghua    2020-08-13 23:24

在粗略阅读了kubernetes详细(又臭又长,看的头都要炸了)的文档之后,对kubernetes有了些大概的了解(全靠别人录的介绍视频),写一个简单点的部署方案。
在部署了一个分布式应用后,略微介绍一下相关内容。由于了解真的是很大概,后续应该还会继续更新例如服务发现等内容(看了一遍文档真的看不懂)。

kubernetes使用docker等容器来部署相关应用,这些应用的沟通一般通过HTTP等网络协议进行沟通。而部署的应用要对外提供服务,就要对外暴露端口。而部署分布式应用,就要部署多个同一镜像,而当这些应用部署在同一个物理机上时,最先想到的就是端口是否会冲突了?例如部署同一个监听80端口的web应用,而这个应用被运行了多个示例,直接在物理机上运行肯定会遇到端口绑定错误。而kubernetes通过给每一个运行的容器分配了单独的内网ip地址(一般是10.xxx.xxx.xxx),使这些容器绑定同一端口时不会发生错误。要是外部网络能够访问就要通过kubernetes内置的nginx等工具对流量进行转发,这个转发过程可能会发生多次,load-balancer以及端口映射等。ok,这些知识就差不多可以开始部署应用了(俺扯不下去了)。
啊,还有就是分布式部署多个应用,要部署多少个直接在配置文件里写就行了,有命令可以更新的。并且kubernetes提供HTTP的API来进行管理。具体咋做我也不会。

部署步骤
  1. 首先将要部署的容器用docker之类的打包成镜像,使保证运行docker run  --net=host 【镜像名称】 时能正常访问
  2. 写一个yaml的配置描述,其中replicas变量就是部署的应用个数目前下面配置中设置部署了两个,镜像名称写在image里,要暴露的端口写在ports里:范例:
  3. apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
    kind: Deployment
    metadata:
      name: textrank-deployment
    spec:
      selector:
        matchLabels:
          app: textrank
      replicas: 2 # tells deployment to run 2 pods matching the template
      template:
        metadata:
          labels:
            app: textrank
        spec:
          containers:
          - name: textrank
            image: textrank:0.1
            ports:
            - containerPort: 6889
    
    
  4. 根据配置描述创建pod : kubectl apply -f ./textrank.yaml --record
  5. 可以根据一下命令获取pod的详细信息,部署正确的话就会出现ready:
  6. kubectl get deployments textrank-deployment
    kubectl describe deployments textrank-deployment
    
  7. kubectl get replicasets
    kubectl describe replicasets
  8. 在节点上对外暴露端口:
  9. kubectl expose deployment textrank-deployment --type=NodePort --name=example-service
    
  10. 显示对外暴露端口信息
    kubectl describe services example-service
    
  11. 判断一下能否访问,能的话就可以使用拉
  12. curl http://<public-node-ip>:<node-port>
    
  13. To delete the Service, enter this command:

    kubectl delete services example-service
    

    To delete the Deployment, the ReplicaSet, and the Pods that are running the Hello World application, enter this command:

    kubectl delete deployment hello-world

最后更多详细信息请看:https://kubernetes.io/docs/tasks/access-application-cluster/service-access-application-cluster/
https://kubernetes.io/docs/tutorials/stateless-application/expose-external-ip-address/
我就是复制粘贴的,这东西排版真的难用
Last Modified: 2020-08-17 10:59
Views: 1.4K

[[total]] comments

Post your comment
  1. [[item.time]]
    [[item.user.username]] [[item.floor]]Floor
  2. Click to load more...
  3. Post your comment