• Kubernetes(k8s)进阶


    Kubernetes进阶

    一、Namespace(名称空间)

    1.namespace介绍

    Namespace(名称空间)是kubernetes系统中的一种非常重要资源,它的主要作用是用来实现多套环境的资源隔离或者多租户的资源隔离

    默认情况下,kubernetes集群中的所有的Pod都是可以相互访问的。但是在实际中,可能不想让两个Pod之间进行互相的访问,那此时就可以将两个Pod划分到不同的namespace下。kubernetes通过将集群内部的资源分配到不同的Namespace中,可以形成逻辑上的"组",以方便不同的组的资源进行隔离使用和管理。

    可以通过kubernetes的授权机制,将不同的namespace交给不同租户进行管理,这样就实现了多租户的资源隔离。此时还能结合kubernetes的资源配额机制,限定不同租户能占用的资源,例如CPU使用量、内存使用量等等,来实现租户可用资源的管理。


    在这里插入图片描述

    kubernetes在集群启动之后,会默认创建几个namespace

    //查看所有的名称空间
    [root@master ~]# kubectl get namespace
    NAME              STATUS   AGE
    default           Active   2d22h	//所有未指定Namespace的对象都会被分配在default命名空间
    kube-flannel      Active   2d21h	
    kube-node-lease   Active   2d22h	//集群节点之间的心跳维护,v1.13开始引入
    kube-public       Active   2d22h	//此命名空间下的资源可以被所有人访问(包括未认证用户)
    kube-system       Active   2d22h	//所有由Kubernetes系统创建的资源都处于这个命名空间
    [root@master ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2.管理namespace

    查看namespace
    //查看所有的namespace
    --语法:kubectl get namespace
    [root@master ~]# kubectl get namespace
    NAME              STATUS   AGE
    default           Active   2d22h
    kube-flannel      Active   2d21h
    kube-node-lease   Active   2d22h
    kube-public       Active   2d22h
    kube-system       Active   2d22h
    
    //查看指定的namespace
    --语法:kubectl get namespace namespace名称
    [root@master ~]# kubectl get namespace kube-system
    NAME          STATUS   AGE
    kube-system   Active   2d22h
    [root@master ~]# 
    
    //指定输出格式;kubernetes支持的格式有很多,比较常见的是wide、json、yaml
    --语法:kubectl get namespace namespace名称  -o 格式参数
    [root@master ~]# kubectl get namespace kube-system -o yaml
    apiVersion: v1
    kind: Namespace
    metadata:
      creationTimestamp: "2023-11-13T08:40:58Z"
      labels:
        kubernetes.io/metadata.name: kube-system
      name: kube-system
      resourceVersion: "11"
      uid: 055e9695-d07f-4181-b0e5-32b25919f4ef
    spec:
      finalizers:
      - kubernetes
    status:
      phase: Active
    [root@master ~]# 
    
    //查看ns详情
    --语法:kubectl describe namespace namespace名称
    [root@master ~]# kubectl describe namespace kube-system
    Name:         kube-system
    Labels:       kubernetes.io/metadata.name=kube-system
    Annotations:  
    Status:       Active	//Active 命名空间正在使用中  Terminating 正在删除命名空间
    
    No resource quota.		//ResourceQuota 针对namespace做的资源限制
    
    No LimitRange resource.	//LimitRange针对namespace中的每个组件做的资源限制
    [root@master ~]# 
    
    • 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
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    创建namespace
    //创建一个namespace
    --语法:kubectl create namespace namespace名称
    [root@master ~]# kubectl create namespace wanf
    namespace/wanf created
    [root@master ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    yaml文件配置namespace

    写一个yaml文件

    [root@master ~]# vim ns-lc-yaml
    [root@master ~]# cat ns-lc-yaml 
    apiVersion: v1
    kind: Namespace
    metadata:
      name: lc
    [root@master ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    执行对应的创建和删除命令

    //创建
    [root@master ~]# kubectl create -f ns-lc-yaml 
    namespace/lc created
    [root@master ~]# 
    
    //删除
    [root@master ~]# kubectl delete -f ns-lc-yaml 
    namespace "lc" deleted
    [root@master ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9


    二、Pod(最小基本部署单元)

    1.pod介绍

    pod是Kubernetes中最小的可调度单位,它可以包含一个或多个容器。Pod代表应用程序的一个实例,包含了共享的网络和存储资源。Pod可以动态地进行伸缩,并且可以在不同的节点之间迁移。

    Pod是kubernetes集群进行管理的最小单元,程序要运行必须部署在容器中,而容器必须存在于Pod中。Pod可以认为是容器的封装,一个Pod中可以存在一个或者多个容器。


    在这里插入图片描述

    kubernetes在集群启动之后,集群中的各个组件也都是以Pod方式运行的。可以通过下面命令查看:

    //查看名称空间kube-system里面的pod
    [root@master ~]# kubectl get pod -n kube-system
    NAME                             READY   STATUS    RESTARTS       AGE
    coredns-66f779496c-5thsr         1/1     Running   11 (36m ago)   2d22h
    coredns-66f779496c-nqjkx         1/1     Running   11 (36m ago)   2d22h
    etcd-master                      1/1     Running   5 (36m ago)    2d22h
    kube-apiserver-master            1/1     Running   5 (36m ago)    2d22h
    kube-controller-manager-master   1/1     Running   5 (36m ago)    2d22h
    kube-proxy-5m7lh                 1/1     Running   5 (36m ago)    2d22h
    kube-proxy-7dc82                 1/1     Running   5 (36m ago)    2d22h
    kube-proxy-t6fc7                 1/1     Running   5 (36m ago)    2d22h
    kube-scheduler-master            1/1     Running   5 (36m ago)    2d22h
    [root@master ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    2.管理pod

    创建并运行pod

    kubernetes没有提供单独运行Pod的命令,都是通过Pod控制器来实现的

    # 命令格式: kubectl run (pod控制器名称) [参数] 
    # --image  指定Pod的镜像
    # --port   指定端口
    # --namespace  指定namespace
    
    //示例
    [root@master ~]# kubectl run httpd --image=lcwanf/apache:v0.2 --port=80 --namespace wanf
    pod/httpd created	//创建成功
    [root@master ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    查看pod信息
    //查看刚刚创建的pod
    --查看基本信息
    [root@master ~]# kubectl get pods -n wanf 
    NAME    READY   STATUS    RESTARTS   AGE
    httpd   1/1     Running   0          15m
    [root@master ~]# 
    
    --查看详细信息
    [root@master ~]# kubectl describe pod httpd -n wanf
    Name:             httpd
    Namespace:        wanf
    Priority:         0
    Service Account:  default
    Node:             node2/192.168.179.15
    Start Time:       Thu, 16 Nov 2023 15:30:14 +0800
    Labels:           run=httpd
    Annotations:      
    Status:           Running
    IP:               10.244.2.8
    IPs:
      IP:  10.244.2.8
    Containers:
      httpd:
        Container ID:   containerd://301ea62c59afcc16bb8a9ff4b14bbddd4168677c6434acc6bb20d4970d149035
        Image:          lcwanf/apache:v0.2
        Image ID:       docker.io/lcwanf/apache@sha256:03b968f7958f5e47650bc785c3bf3a7646fdade94d1e5db589f6aa4bf470d29a
        Port:           80/TCP
        Host Port:      0/TCP
        State:          Running
          Started:      Thu, 16 Nov 2023 15:30:55 +0800
        Ready:          True
        Restart Count:  0
        Environment:    
        Mounts:
          /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-mk4w7 (ro)
    Conditions:
      Type              Status
      Initialized       True 
      Ready             True 
      ContainersReady   True 
      PodScheduled      True 
    Volumes:
      kube-api-access-mk4w7:
        Type:                    Projected (a volume that contains injected data from multiple sources)
        TokenExpirationSeconds:  3607
        ConfigMapName:           kube-root-ca.crt
        ConfigMapOptional:       
        DownwardAPI:             true
    QoS Class:                   BestEffort
    Node-Selectors:              
    Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                                 node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
    Events:
      Type    Reason     Age   From               Message
      ----    ------     ----  ----               -------
      Normal  Scheduled  17m   default-scheduler  Successfully assigned wanf/httpd to node2
      Normal  Pulling    17m   kubelet            Pulling image "lcwanf/apache:v0.2"
      Normal  Pulled     16m   kubelet            Successfully pulled image "lcwanf/apache:v0.2" in 40.558s (40.558s including waiting)
      Normal  Created    16m   kubelet            Created container httpd
      Normal  Started    16m   kubelet            Started container httpd
    [root@master ~]# 
    
    • 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
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    访问pod
    //获取pod的IP地址
    [root@master ~]# kubectl get pods -n wanf -o wide
    NAME    READY   STATUS    RESTARTS   AGE   IP           NODE    NOMINATED NODE   READINESS GATES
    httpd   1/1     Running   0          18m   10.244.2.8   node2              
    [root@master ~]# 
    
    //使用curl命令访问
    [root@master ~]# curl http://10.244.2.8 
    

    nice bike</h1></body></html> //成功访问 [root@master ~]#
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    删除pod
    //删除指定的pod
    [root@master ~]# kubectl delete pod httpd -n wanf
    pod "httpd" deleted
    [root@master ~]# 
    --查看名称空间wanf里面的pod;已经查询不到任何pod,因为这个pod是手动创建的,
      不是通过deployment创建的,所以不会重新启动一个新的pod。
    [root@master ~]# kubectl get pods -n wanf -o wide
    No resources found in wanf namespace.
    [root@master ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    yaml文件配置pod

    写一个yaml文件

    [root@master ~]# vim pod-test-yaml
    [root@master ~]# cat pod-test-yaml 
    apiVersion: v1
    kind: Pod
    metadata:
      name: httpd
      namespace: wanf
    spec:
      containers:
      - image: lcwanf/apache:v0.2
        name: pod
        ports:
        - name: httpd-test
          containerPort: 80
          protocol: TCP
    [root@master ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    执行对应的创建和删除命令

    //创建
    [root@master ~]# kubectl create -f pod-test-yaml 
    pod/httpd created
    [root@master ~]# 
    --查看创建的pod
    [root@master ~]# kubectl get -f pod-test-yaml 
    NAME    READY   STATUS    RESTARTS   AGE
    httpd   1/1     Running   0          71s
    [root@master ~]# 
    
    //删除
    [root@master ~]# kubectl delete -f pod-test-yaml 
    pod "httpd" deleted
    [root@master ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14


    三、Label(标签)

    1.label介绍

    Label是kubernetes系统中的一个重要概念。它的作用就是在资源上添加标识,用来对它们进行区分和选择。

    Label的特点:

    • 一个Label会以key/value键值对的形式附加到各种对象上,如Node、Pod、Service等等
    • 一个资源对象可以定义任意数量的Label ,同一个Label也可以被添加到任意数量的资源对象上去
    • Label通常在资源对象定义时确定,当然也可以在对象创建后动态添加或者删除

    可以通过Label实现资源的多维度分组,以便灵活、方便地进行资源分配、调度、配置、部署等管理工作。

    一些常用的Label 示例如下:

    • 版本标签:“version”:“release”, “version”:“stable”…
    • 环境标签:“environment”:“dev”,“environment”:“test”,“environment”:“pro”
    • 架构标签:“tier”:“frontend”,“tier”:“backend”

    标签定义完毕之后,还要考虑到标签的选择,这就要使用到Label Selector,即:

    Label用于给某个资源对象定义标识

    Label Selector用于查询和筛选拥有某些标签的资源对象

    当前有两种Label Selector:

    • 基于等式的Label Selector

      name = slave: 选择所有包含Label中key="name"且value="slave"的对象

      env != production: 选择所有包括Label中的key="env"且value不等于"production"的对象

    • 基于集合的Label Selector

      name in (master, slave): 选择所有包含Label中的key="name"且value="master"或"slave"的对象

      name not in (frontend): 选择所有包含Label中的key="name"且value不等于"frontend"的对象

    标签的选择条件可以使用多个,此时将多个Label Selector进行组合,使用逗号","进行分隔即可。例如:

    name=slave,env!=production

    name not in (frontend),env!=production


    2.管理label的命令

    创建标签(为pod打标签)
    //打单个标签
    [root@master ~]# kubectl label pod httpd version=0.2 -n wanf
    pod/httpd labeled
    [root@master ~]# 
    
    //打多个标签,用空格隔开
    [root@master ~]# kubectl label pod httpd version=0.2 app=httpd master=wanf -n wanf
    pod/httpd labeled
    [root@master ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    查看标签
    //语法:kubectl get pod pod名称 -n 名称空间 --show-labels
    [root@master ~]# kubectl get pod httpd -n wanf --show-labels
    NAME    READY   STATUS    RESTARTS   AGE     LABELS
    httpd   1/1     Running   0          7m43s   app=httpd,master=wanf,version=0.2
    [root@master ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    通过标签筛选
    //筛选标签app=httpd的pod
    [root@master ~]# kubectl get pods -n wanf -l app=httpd --show-labels
    NAME    READY   STATUS    RESTARTS   AGE   LABELS
    httpd   1/1     Running   0          34m   app=httpd,master=wanf,version=0.2
    [root@master ~]# 
    //反选,加感叹号!
    [root@master ~]# kubectl get pods -n wanf -l app!=httpd --show-labels
    No resources found in wanf namespace.
    [root@master ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    删除标签
    //删除pod里面master=wanf这个标签
    [root@master ~]# kubectl label pod httpd master- -n wanf
    pod/httpd unlabeled
    [root@master ~]# 
    --查看删除一个标签后的pod
    [root@master ~]# kubectl get pod httpd -n wanf --show-labels
    NAME    READY   STATUS    RESTARTS   AGE   LABELS
    httpd   1/1     Running   0          37m   app=httpd,version=0.2
    [root@master ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    yaml文件配置lable

    写一个yaml文件

    [root@master ~]# vim lable-pod.yaml 
    [root@master ~]# cat lable-pod.yaml 
    apiVersion: v1
    kind: Pod
    metadata:
      name: httpdv1
      namespace: wanf
      labels:
        version: "0.3" 
        env: "test"
    spec:
      containers:
      - image: lcwanf/apache:v0.2
        name: httpd-test
        ports:
        - name: httpd
          containerPort: 80
          protocol: TCP
    [root@master ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    执行对应的创建和删除命令

    //创建
    [root@master ~]# kubectl create -f lable-pod.yaml 
    pod/httpdv1 created
    [root@master ~]# 
    --查看创建的pod,并显示标签
    [root@master ~]# kubectl get pod -n wanf --show-labels
    NAME      READY   STATUS    RESTARTS   AGE   LABELS
    httpdv1   1/1     Running   0          71s   env=test,version=0.3
    [root@master ~]# 
    
    //删除
    [root@master ~]# kubectl delete -f lable-pod.yaml 
    pod "httpdv1" deleted
    [root@master ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14


    四、Deployment(调度器)

    1.deployment介绍

    在kubernetes中,Pod是最小的控制单元,但是kubernetes很少直接控制Pod,一般都是通过Pod控制器来完成的。Pod控制器用于pod的管理,确保pod资源符合预期的状态,当pod的资源出现故障时,会尝试进行重启或重建pod。

    在kubernetes中Pod控制器的种类有很多,本文章只介绍一种:Deployment。

    Deployment:用于定义和管理应用程序的部署。Deployment描述了应用程序的期望状态,并负责在集群中创建和更新Pod。它支持滚动更新、回滚和扩缩容等操作。

    在这里插入图片描述


    2.管理deployment的命令

    创建deployment
    # 命令格式: kubectl create deployment 名称  [参数] 
    # --image  指定pod的镜像
    # --port   指定端口
    # --replicas  指定创建pod数量
    # --namespace  指定namespace
    
    //示例
    --在名称空间wanf下创建一个名为httpd的deployment,镜像为lcwanf/apache:v0.2,pod数量为4个
    [root@master ~]# kubectl create deployment httpd --image=lcwanf/apache:v0.2 --port=80 --replicas=4 -n wanf
    deployment.apps/httpd created
    [root@master ~]# 
    
    --查看deployment创建的pod
    [root@master ~]# kubectl get pods -n wanf
    NAME                    READY   STATUS    RESTARTS   AGE
    httpd-cd9dc9fb8-48v28   1/1     Running   0          2m8s
    httpd-cd9dc9fb8-dx674   1/1     Running   0          2m8s
    httpd-cd9dc9fb8-skkcx   1/1     Running   0          2m8s
    httpd-cd9dc9fb8-t59g6   1/1     Running   0          2m8s
    [root@master ~]# 
    
    //删除由deployment创建的pod,deployment会自动重新启动一个新的pod,保证数量满足定义的标准
    [root@master ~]# kubectl delete pod httpd-cd9dc9fb8-t59g6 -n wanf
    pod "httpd-cd9dc9fb8-t59g6" deleted
    
    --依然是四4个pod
    [root@master ~]# kubectl get pods -n wanf
    NAME                    READY   STATUS    RESTARTS   AGE
    httpd-cd9dc9fb8-48v28   1/1     Running   0          4m27s
    httpd-cd9dc9fb8-b5wg8   1/1     Running   0          4s
    httpd-cd9dc9fb8-dx674   1/1     Running   0          4m27s
    httpd-cd9dc9fb8-skkcx   1/1     Running   0          4m27s
    [root@master ~]# 
    
    • 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
    • 31
    • 32
    • 33
    查看deployment的信息
    //简单查看
    [root@master ~]# kubectl get deployment -n wanf
    NAME    READY   UP-TO-DATE   AVAILABLE   AGE
    httpd   4/4     4            4           2m50s
    [root@master ~]# 
    
    //较详细查看
    # UP-TO-DATE:成功升级的副本数量
    # AVAILABLE:可用副本的数量
    [root@master ~]# kubectl get deployment -n wanf -o wide
    NAME    READY   UP-TO-DATE   AVAILABLE   AGE     CONTAINERS   IMAGES               SELECTOR
    httpd   4/4     4            4           5m43s   apache       lcwanf/apache:v0.2   app=httpd
    [root@master ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    详细查看deployment的信息
    [root@master ~]# kubectl describe deployment -n wanf
    Name:                   httpd
    Namespace:              wanf
    CreationTimestamp:      Thu, 16 Nov 2023 17:00:46 +0800
    Labels:                 app=httpd
    Annotations:            deployment.kubernetes.io/revision: 1
    Selector:               app=httpd
    Replicas:               4 desired | 4 updated | 4 total | 4 available | 0 unavailable
    StrategyType:           RollingUpdate
    MinReadySeconds:        0
    RollingUpdateStrategy:  25% max unavailable, 25% max surge
    Pod Template:
      Labels:  app=httpd
      Containers:
       apache:
        Image:        lcwanf/apache:v0.2
        Port:         80/TCP
        Host Port:    0/TCP
        Environment:  
        Mounts:       
      Volumes:        
    Conditions:
      Type           Status  Reason
      ----           ------  ------
      Available      True    MinimumReplicasAvailable
      Progressing    True    NewReplicaSetAvailable
    OldReplicaSets:  
    NewReplicaSet:   httpd-cd9dc9fb8 (4/4 replicas created)
    Events:
      Type    Reason             Age    From                   Message
      ----    ------             ----   ----                   -------
      Normal  ScalingReplicaSet  7m29s  deployment-controller  Scaled up replica set httpd-cd9dc9fb8 to 4
    [root@master ~]# 
    
    • 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
    • 31
    • 32
    • 33
    删除deployment
    //删除deployment后才会彻底删除其管理的pod
    [root@master ~]# kubectl delete deployment httpd -n wanf
    deployment.apps "httpd" deleted
    [root@master ~]# 
    --查看pod,已经没有任何pod,也不会自动重新启动新的pod
    [root@master ~]# kubectl get pods -n wanf
    No resources found in wanf namespace.
    [root@master ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    通过yaml文件配置deployment

    写一个yaml文件

    [root@master ~]# vim deployment-httpd.yaml
    [root@master ~]# cat deployment-httpd.yaml 
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: httpd
      namespace: wanf
    spec:
      replicas: 5
      selector:
        matchLabels:
          run: httpd
      template:
        metadata:
          labels:
            run: "httpd"
            version: "0.2"
        spec:
          containers:
          - image: lcwanf/apache:v0.2
            name: httpd-test
            ports:
            - containerPort: 80
              protocol: TCP
    [root@master ~]# 
    
    • 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

    执行对应的创建和删除命令

    //创建
    [root@master ~]# kubectl create -f deployment-httpd.yaml 
    deployment.apps/httpd created
    [root@master ~]# 
    --查看创建的deployment
    [root@master ~]# kubectl get -f deployment-httpd.yaml 
    NAME    READY   UP-TO-DATE   AVAILABLE   AGE
    httpd   5/5     5            5           71s
    --查看该deployment所管理的pod
    [root@master ~]# kubectl get pods -n wanf
    NAME                     READY   STATUS    RESTARTS   AGE
    httpd-56957b6fc7-29k5p   1/1     Running   0          33s
    httpd-56957b6fc7-rcrbp   1/1     Running   0          33s
    httpd-56957b6fc7-tlqg4   1/1     Running   0          33s
    httpd-56957b6fc7-tsx2f   1/1     Running   0          33s
    httpd-56957b6fc7-vmhxz   1/1     Running   0          33s
    [root@master ~]# 
    
    //删除
    [root@master ~]# kubectl delete -f deployment-httpd.yaml 
    deployment.apps "httpd" deleted
    [root@master ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22


    五、Service

    1.service介绍

    上述方法已经能够利用Deployment来创建一组Pod来提供具有高可用性的服务。

    虽然每个Pod都会分配一个单独的Pod IP,然而却存在如下两问题:

    • Pod IP 会随着Pod的重建产生变化
    • Pod IP 仅仅是集群内可见的虚拟IP,外部无法访问

    这样对于访问这个服务带来了难度。因此,kubernetes设计了Service来解决这个问题。

    Service可以看作是一组同类Pod对外的访问接口。借助Service,应用可以方便地实现服务发现和负载均衡。

    在这里插入图片描述


    2.管理service的命令

    创建service

    先创建一个Deployment

    [root@master ~]# kubectl create deployment httpd --image=lcwanf/apache:v0.2 --port=80 --replicas=4 -n wanf
    deployment.apps/httpd created
    
    • 1
    • 2
    一、创建集群内部可访问的Service
    //创建type类型为ClusterIP的service并暴露
    [root@master ~]# kubectl expose deployment httpd --name=svc-httpd1 --type=ClusterIP --port=80 --target-port=80 -n wanf
    service/svc-httpd1 exposed
    [root@master ~]# 
    
    //查看service
    # 这里产生了一个CLUSTER-IP,这就是service的IP,在Service的生命周期中,这个地址是不会变动的
    [root@master ~]# kubectl get svc svc-httpd1 -n wanf -o wide
    NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE   SELECTOR
    svc-httpd1   ClusterIP   10.101.14.185           80/TCP    51s   app=httpd
    [root@master ~]# 
    
    //在内部使用service的IP访问
    [root@master ~]# curl 10.101.14.185
    

    nice bike</h1></body></html> [root@master ~]#
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    二、创建集群外部也可访问的Service
    # 上面创建的Service的type类型为ClusterIP,这个ip地址只用集群内部可访问
    # 如果需要创建外部也可以访问的Service,需要修改type为NodePort
    
    //创建一个新的service,修改service的type类型为NodePort 
    [root@master ~]# kubectl expose deployment httpd --name=svc-httpd2 --type=NodePort  --port=80 --target-port=80 -n wanf
    service/svc-httpd2 exposed
    [root@master ~]# 
    
    //查看这个service
    ##会发现出现了NodePort类型的Service,而且有一对Port(80:31754/TCP)
    [root@master ~]# kubectl get svc svc-httpd2 -n wanf -o wide
    NAME         TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE   SELECTOR
    svc-httpd2   NodePort   10.103.63.181           80:31754/TCP   99s   app=httpd
    [root@master ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    在浏览器访问,成功访问

    在这里插入图片描述


    查看service
    //查看某个名称空间里面的所有service
    [root@master ~]# kubectl get svc -n wanf
    NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
    svc-httpd1   ClusterIP   10.101.249.235           80/TCP         2s
    svc-httpd2   NodePort    10.101.168.65            80:30189/TCP   8s
    [root@master ~]# 
    
    //查看某一个service
    [root@master ~]# kubectl get svc svc-httpd1 -n wanf
    NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
    svc-httpd1   ClusterIP   10.101.249.235           80/TCP    28s
    [root@master ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    删除service
    //删除service1
    [root@master ~]# kubectl delete svc svc-httpd1 -n wanf
    service "svc-httpd1" deleted
    
    //删除service2
    [root@master ~]# kubectl delete svc svc-httpd2 -n wanf
    service "svc-httpd2" deleted
    [root@master ~]# 
    
    //查看名称空间wanf里面的service,已经没有任何service
    [root@master ~]# kubectl get svc -n wanf
    No resources found in wanf namespace.
    [root@master ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    yaml文件配置service

    写一个yaml文件

    [root@master ~]# vim svc-httpd-yaml
    [root@master ~]# cat svc-httpd-yaml 
    apiVersion: v1
    kind: Service
    metadata:
      name: svc-httpd
      namespace: wanf
    spec:
      clusterIP: 10.101.249.111	#固定svc的内网ip
      ports:
      - port: 80
        protocol: TCP
        targetPort: 80
      selector:
        run: httpd
      type: ClusterIP
    [root@master ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    执行对应的创建和删除命令

    //创建
    [root@master ~]# kubectl create -f svc-httpd-yaml 
    service/svc-httpd created
    [root@master ~]# 
    --查看创建的service
    [root@master ~]# kubectl get -f svc-httpd-yaml 
    NAME        TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
    svc-httpd   ClusterIP   10.101.249.111           80/TCP    18s
    [root@master ~]# 
    
    //删除service
    [root@master ~]# kubectl delete -f svc-httpd-yaml 
    service "svc-httpd" deleted
    [root@master ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14


  • 相关阅读:
    Python学习笔记--生成器
    软件设计师---程序设计语言考点总结
    Servlet规范之安全
    AI实战营第二期 第七节 《语义分割与MMSegmentation》——笔记8
    【pwn】2022 极客大挑战
    缓存中间件-Redis(二)
    ROS从入门到精通3-4:urdf集成Gazebo联合仿真
    jwt+redis实现登录认证
    进行 Spring 6 迁移的最佳方式
    程序中断方式
  • 原文地址:https://blog.csdn.net/qq_70246330/article/details/134447441