k8s流程叙述

Kubernetes (K8s) 是一个开源的,用于管理容器化应用的平台,它提供了部署,扩展和管理容器化应用的机制。

以下是一个简单的Kubernetes部署流程的描述:

  • 1、安装和设置Kubernetes集群。

  • 2、创建一个Docker镜像,并把应用打包进去。

  • 3、创建一个Kubernetes的Deployment配置文件,用于定义应用容器的副本数量,使用的镜像,和其他配置信息。

  • 4、使用kubectl命令行工具来应用这个配置文件,这将创建一个Deployment对象。

  • 5、创建一个Service配置文件,用于暴露应用,以便外部访问。

  • 6、应用这个Service配置文件,创建一个Service对象。

  • 7、检查应用的状态,确保Pod正在运行,并且Service已经准备好接收流量。

以下是一个简单的示例,展示如何使用kubectl和YAML文件来部署一个简单的“Hello World”应用:

Deployment配置文件

# hello-world-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-world
spec:
  replicas: 3
  selector:
    matchLabels:
      app: hello-world
  template:
    metadata:
      labels:
        app: hello-world
    spec:
      containers:
      - name: hello-world
        image: "gcr.io/hello-minikube-zero-install/hello-world:v1"
        ports:
        - containerPort: 8080

解读hello-world-deployment.yaml

这个hello-world-deployment.yaml文件是一个Kubernetes的Deployment资源定义文件。它定义了一个名为hello-world的Deployment,用于在Kubernetes集群中部署和管理应用程序的实例(Pods)。下面是对这个YAML文件的详细解读:

apiVersion

apiVersion: apps/v1

这一行指定了Kubernetes API的版本,这里是apps/v1,它是管理Deployment等应用级别资源的稳定API版本。

kind

kind: Deployment

这指定了资源对象的类型,这里是Deployment。Deployment是Kubernetes中的一种控制器,用于声明式地更新应用程序和服务。它可以创建和管理一组Pod,确保Pod的数量和状态符合预期。

metadata

metadata:  
  name: hello-world

在metadata部分,定义了Deployment的名称,这里是hello-world。这个名字在Kubernetes集群中必须是唯一的。

spec

spec:  
  replicas: 3  
  selector:  
    matchLabels:  
      app: hello-world  
  template:  
    ...

spec部分包含了Deployment的规格说明:

  • replicas: 3:指定了需要运行的Pod副本数量为3。
  • selector:定义了如何选择一组Pods进行管理。这里通过标签选择器matchLabels选择所有标签为app: hello-world的Pods。
  • template:定义了如何创建Pod的模板。Kubernetes将使用此模板来创建新的Pod实例。

template

template:  
  metadata:  
    labels:  
      app: hello-world  
  spec:  
    containers:  
    - name: hello-world  
      image: "gcr.io/hello-minikube-zero-install/hello-world:v1"  
      ports:  
      - containerPort: 8080

在template部分:

  • metadata定义了Pod的标签,这里是app: hello-world。这些标签用于与Deployment的selector匹配,以便Deployment能够管理这些Pod。
  • spec定义了Pod中容器的具体信息:
    • containers列表中包含了一个容器,其名称是hello-world。
    • image指定了容器的镜像,这里是gcr.io/hello-minikube-zero- install/hello-world:v1。这意味着Kubernetes将从Google Container Registry(GCR)中拉取这个镜像来创建容器。
    • ports定义了容器内部监听的端口,这里是8080。这并不会自动将端口暴露给外部网络,但可以在Service资源中使用这个端口来配置外部访问。
      总之,这个hello-world-deployment.yaml文件定义了一个名为hello-world的Deployment,它将确保有3个Pod实例运行,每个Pod都运行着gcr.io/hello-minikube-zero-install/hello-world:v1镜像的容器,并且这些容器都监听在8080端口上。这些Pod通过标签app: hello-world被Deployment管理。

Service配置文件

# hello-world-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: hello-world-service
spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: 8080
    nodePort: 30007
  selector:
    app: hello-world

解读 Service配置文件

hello-world-service.yaml 文件定义了一个 Kubernetes Service 资源,用于提供对一组 Pod 的访问。在这个例子中,Service 名为 hello-world-service,并且它被配置为 NodePort 类型,允许从集群外部通过节点的 IP 地址和静态端口(在这个例子中是 30007)来访问服务。以下是该文件的详细解读:

apiVersion

apiVersion: v1

这指定了 Kubernetes API 的版本,v1 是 Service 资源的核心 API 版本。

kind

kind: Service

这表示资源对象的类型是 Service。Service 定义了 Pod 的逻辑集合和访问它们的策略。

metadata

metadata:  
  name: hello-world-service

在 metadata 部分,定义了 Service 的名称,这里是 hello-world-service。

spec

spec:  
  type: NodePort  
  ports:  
  - port: 80  
    targetPort: 8080  
    nodePort: 30007  
  selector:  
    app: hello-world

spec 部分包含了 Service 的规格说明:

- type: NodePort:这指定了 Service 的类型为 NodePort。在这种类型下,Kubernetes 集群的每个节点都会将 Service 的端口(在这个例子中是 30007)暴露给外部流量。通过访问任何节点的 IP 地址和端口 30007,都可以访问到这个 Service。
- ports:定义了 Service 监听的端口和如何将流量转发到后端 Pods。这里定义了一个端口映射:
- port: 80:这是 Service 集群内部的端口,集群内的其他服务或 Pods 可以通过这个端口访问 hello-world-service。
- targetPort: 8080:这是后端 Pods 上实际监听的端口,Service 会将流量转发到这个端口。
- nodePort: 30007:这是分配给 Service 的 NodePort 端口,外部流量可以通过这个端口访问 Service。
- selector:定义了 Service 如何选择一组 Pods。这里通过标签选择器 app: hello-world 来选择所有标签为 app: hello-world 的 Pods。这意味着 hello-world-service 将把流量转发到所有具有这个标签的 Pods。

部署应用:

kubectl apply -f hello-world-deployment.yaml
kubectl apply -f hello-world-service.yaml

检查状态:

kubectl get pods
kubectl get svc

访问应用:

minikube service hello-world-service
或者使用任何一个节点的IP和端口30007访问服务。

以上是一个基本的Kubernetes部署流程,具体的步骤可能会根据集群的配置和应用的需求有所不同。

  • 10
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值