zoukankan      html  css  js  c++  java
  • k8s service定义与创建

    1. k8s-service定义与创建

    • 创建service:

      kubectl apply -f service.yaml
      
    • 查看service:

      kubectl get service
      
    • 示例代码

      apiVersion: v1
      kind: Service
      metadata:
        labels:
          app: web
        name: web
      spec:
        type: ClusterIP # 服务类型
        ports:
        - port: 80 # Service端口
          protocol: TCP # 协议
          targetPort: 80 # 容器端口
        selector:
          app: web # 指定关联Pod的标签
      
    • 多端口service配置

      对于某些服务,你需要公开多个端口。 Kubernetes 允许你在 Service 对象上配置多个端口定义。 为服务使用多个端口时,必须提供所有端口名称,以使它们无歧义。 例如:

      apiVersion: v1
      kind: Service
      metadata:
        name: web
      spec:
        selector:
          app: web   # 指定关联Pod的标签
        type: ClusterIP # 服务类型
        ports:
          - name: http     # 容器名称
            protocol: TCP   # 协议
            port: 80      # Service端口
            targetPort: 80    # 容器端口
          - name: https     # 容器名称定义
            protocol: TCP   # 配置协议
            port: 443        # service端口
            targetPort: 443   # 容器端口
      

    2. 案例

    • 我们先启动一个pod容器

      [root@k8s-master deployment]# vim web.yaml 
      [root@k8s-master deployment]# cat web.yaml 
      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: web
        namespace: default
        annotations:       # 记录回滚参数
          kubernetes.io/change-cause: "web.v1-nginx-1.19"   #记录到revision中的内容,记录版本号
      spec:
        replicas: 3 # Pod副本预期数量
        revisionHistoryLimit: 10 # RS历史版本保存数量
        selector:
          matchLabels:
            app: web
        strategy:
          rollingUpdate:
            maxSurge: 25%             # 滚动更新过程最大pod副本数
            maxUnavailable: 25%       # 滚动更新过程中最大不可用pod副本数,
          type: RollingUpdate
        template:
          metadata:
            labels:
              app: web # Pod副本的标签
          spec:
            containers:
            - name: web
              image: nginx:1.16
              readinessProbe:          # 存活检查,如果失败,将杀死容器,来重启
                httpGet:
                  port: 80
                  path: /index.html
                initialDelaySeconds: 10 #启动容器后多少秒健康检查
                periodSeconds: 10 #以后间隔多少秒检查一次
      
              livenessProbe:   # 就绪检查,失败就会剔除 service 
                httpGet:
                  port: 80
                  path: /index.html
      
      
    • 启动pod服务

      [root@k8s-master deployment]# kubectl apply -f web-ClusterIP.yaml 
      deployment.apps/web unchanged
      
    • 使用service服务,暴露web应用

      [root@k8s-master service]# vim web-ClusterIP.yaml 
      [root@k8s-master service]# cat web-ClusterIP.yaml 
      apiVersion: v1
      kind: Service
      metadata:
        labels:
          app: web
        name: web
      spec:
        type: ClusterIP # 服务类型
        ports:
        - port: 80 # Service端口
          protocol: TCP # 协议
          targetPort: 80 # 容器端口
        selector:
          app: web # 指定关联Pod的标签
      
      
    • 启动service服务

      [root@k8s-master service]# kubectl apply -f web-ClusterIP.yaml
      service/web created
      
    • 查看service服务

      [root@k8s-master service]# kubectl get serviceNAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGEkubernetes   ClusterIP   10.96.0.1        <none>        443/TCP   7d5hprobe-demo   ClusterIP   10.104.161.168   <none>        80/TCP    3d6hweb          ClusterIP   10.100.222.42    <none>        80/TCP    33s
      
  • 相关阅读:
    什么是用户画像?具体公司体现在哪?什么作用呢?
    用户画像后台表设计
    【转载】Oracle Spatial中SDO_Geometry详细说明
    【转载】ArcGIS中的WKID
    MyBatis 判断条件为等于的时候,常量需要加 .toString()
    Oracle最大进程连接数问题
    JAVA WEB基础巩固之Json、Ajax
    Eclipse常用功能
    UML类图学习
    由uploadfive看servlet
  • 原文地址:https://www.cnblogs.com/scajy/p/15493204.html
Copyright © 2011-2022 走看看