美文网首页
kubernetes deployment ,service 学

kubernetes deployment ,service 学

作者: TEYmL | 来源:发表于2021-02-04 13:32 被阅读0次

    集群环境

    系统

    root@k8smaster:~# hostnamectl
       Static hostname: k8smaster
             Icon name: computer-vm
               Chassis: vm
            Machine ID: 71081e3f45c24ac98465767027e72119
               Boot ID: 7b03cf9bf3e04b4ab9cbe77d2637cdee
        Virtualization: vmware
      Operating System: Ubuntu 18.04.4 LTS
                Kernel: Linux 4.15.0-76-generic
          Architecture: x86-64
    

    集群IP

    k8smaster:10.203.1.90
    k8snode1:10.203.1.91

    Chapter 1

    Running a nginx service

    创建一个服务通过Container内部80端口映射出来的nginx deployment

    命令

    kubectl create deployment my-first-nginx --image=nginx --replicas=2 --port=80
    #Note:原PDF中使用的命令为`kubectl run my-first-nginx --image=nginx --replicas=2 --port=80`这个是旧版kubernetes的命令
    新版本的Kubernetes(例如v.1.19或v.1.20)使用run命令只会创建出pod,想要创建deployment的命令是create deployment
    

    查看结果

    root@k8smaster:~# kubectl get all      
    NAME                                  READY   STATUS    RESTARTS   AGE
    pod/my-first-nginx-7c769d949c-dcgsq   1/1     Running   0          3m44s
    pod/my-first-nginx-7c769d949c-f26pd   1/1     Running   0          3m44s
    
    NAME                     TYPE           CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
    service/kubernetes       ClusterIP      10.96.0.1      <none>        443/TCP        3h41m
    
    NAME                             READY   UP-TO-DATE   AVAILABLE   AGE
    deployment.apps/my-first-nginx   2/2     2            2           3m44s
    
    NAME                                        DESIRED   CURRENT   READY   AGE
    replicaset.apps/my-first-nginx-7c769d949c   2         2         2       3m44s
    #创建deployment之后会自动创建一个replicaset资源,作用是维持pod数量(create命令不使用--replicas参数时也会创建出replicaset资源,数量为1),并且根据--replicas参数的值创建出相应数量的pod
    

    暴露nginx服务

    负载均衡模式

    在云服务器上可以创建出负载均衡的服务(On cloud providers that support an external load balancer (such as Google compute engine), using the LoadBalancer type will provision a load balancer for external access.)
    命令

    kubectl expose deployment my-first-nginx --port=80 --type=LoadBalancer
    

    查看结果

    #可以看到生成一个service/my-first-nginx的资源,外部端口使用的是31119
    root@k8smaster:~# kubectl get all
    NAME                                  READY   STATUS    RESTARTS   AGE
    pod/my-first-nginx-7c769d949c-dcgsq   1/1     Running   0          5m22s
    pod/my-first-nginx-7c769d949c-f26pd   1/1     Running   0          5m22s
    
    NAME                     TYPE           CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
    service/kubernetes       ClusterIP      10.96.0.1      <none>        443/TCP        3h43m
    service/my-first-nginx   LoadBalancer   10.97.14.201   <pending>     80:31119/TCP   102s
    
    NAME                             READY   UP-TO-DATE   AVAILABLE   AGE
    deployment.apps/my-first-nginx   2/2     2            2           5m22s
    
    NAME                                        DESIRED   CURRENT   READY   AGE
    replicaset.apps/my-first-nginx-7c769d949c   2         2         2       5m22s
    
    测试

    在局域网的其他机器访问k8smaster以及k8snode1节点的IP+31119端口,都能获取到nginx服务

    #访问k8smaster
    Klays-Stephen-4:~ klaylin$ curl 10.203.1.90:31119
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    <style>
        body {
            width: 35em;
            margin: 0 auto;
            font-family: Tahoma, Verdana, Arial, sans-serif;
        }
    </style>
    </head>
    <body>
    <h1>Welcome to nginx!</h1>
    <p>If you see this page, the nginx web server is successfully installed and
    working. Further configuration is required.</p>
    
    <p>For online documentation and support please refer to
    <a href="http://nginx.org/">nginx.org</a>.<br/>
    Commercial support is available at
    <a href="http://nginx.com/">nginx.com</a>.</p>
    
    <p><em>Thank you for using nginx.</em></p>
    </body>
    </html>
    Klays-Stephen-4:~ klaylin$ 
    
    
    #访问k8snode1
    Klays-Stephen-4:~ klaylin$ curl 10.203.1.91:31119
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    <style>
        body {
            width: 35em;
            margin: 0 auto;
            font-family: Tahoma, Verdana, Arial, sans-serif;
        }
    </style>
    </head>
    <body>
    <h1>Welcome to nginx!</h1>
    <p>If you see this page, the nginx web server is successfully installed and
    working. Further configuration is required.</p>
    
    <p>For online documentation and support please refer to
    <a href="http://nginx.org/">nginx.org</a>.<br/>
    Commercial support is available at
    <a href="http://nginx.com/">nginx.com</a>.</p>
    
    <p><em>Thank you for using nginx.</em></p>
    </body>
    </html>
    Klays-Stephen-4:~ klaylin$
    
    查看service详细信息

    可以看到此服务的集群IP为10.97.14.201,到pod的Endpoints ip分别是10.244.1.2:80和10.244.1.3:80。集群中的机器可以通过集群IP以及endpoint ip+80端口访问到nginx服务。
    外部的机器访问宿主机的ip+31119端口的请求会通过集群ip 10.97.14.201 分发到endpoint ip

    root@k8smaster:~# kubectl describe service/my-first-nginx
    Name:                     my-first-nginx
    Namespace:                default
    Labels:                   app=my-first-nginx
    Annotations:              <none>
    Selector:                 app=my-first-nginx
    Type:                     LoadBalancer
    IP Families:              <none>
    IP:                       10.97.14.201
    IPs:                      10.97.14.201
    Port:                     <unset>  80/TCP
    TargetPort:               80/TCP
    NodePort:                 <unset>  31119/TCP
    Endpoints:                10.244.1.2:80,10.244.1.3:80
    Session Affinity:         None
    External Traffic Policy:  Cluster
    Events:                   <none>
    

    Chapter 2

    ReplicaSet

    概念

    Pod声明,主要描述Pod的replicas和其他行为,例如是否在Pod stop之后重启还是创建新的Pod,labels是ReplicaSet最重要的属性

    Yaml文件

    root@k8smaster:~# cat my-first-replicaset.yaml 
    apiVersion: apps/v1
    kind: ReplicaSet
    metadata:
      name: my-first-replicaset
      labels:
        version: 0.0.1
    spec:
      replicas: 3
      selector:
        matchLabels:#声明replicaset管理的pod带有哪些标签,与template,metadata下的labels需要对应
          project: My-Happy-Web
          role: frontend
      template:
        metadata:
          labels:#作用于Pod
            project: My-Happy-Web
            role: frontend
            env: dev
        spec:
          containers:
          - name: happy-web
            image: nginx:latest
    

    举例

    matchLabels与template,metadata下的labels无法对应

    yaml文件中labels中的project值不一致,无法成功创建ReplicaSet

    #yaml文件
    root@k8smaster:~# cat my-2nd-replicaset.yaml 
    apiVersion: apps/v1
    kind: ReplicaSet
    metadata:
      name: my-2nd-replicaset
      labels:
        version: 0.0.1
    spec:
      replicas: 3
      selector:
        matchLabels:
          project: My-Happy-Web
          role: frontend
      template:
        metadata:
          labels:
            project: My-Happy-Webbbb
            role: frontend
            env: dev
        spec:
          containers:
          - name: happy-web
            image: nginx:latest
            
    #创建ReplicaSet失败
    root@k8smaster:~# kubectl apply -f my-2nd-replicaset.yaml 
    The ReplicaSet "my-2nd-replicaset" is invalid: spec.template.metadata.labels: Invalid value: map[string]string{"env":"dev", "project":"My-Happy-Webbbb", "role":"frontend"}: `selector` does not match template `labels`        
    

    ReplicaSet与Pod的关系

    创建一个带Labels的pod
    #Labels中有project=My-Happy-Web,role=frontend,env=test的属性
    root@k8smaster:~# kubectl run standalone-pod --image=centos --labels="project=My-Happy-Web,role=frontend,env=test" --restart=Never --command sleep 3600
    pod/standalone-pod created
    
    创建一个Labels属性相同的ReplicaSet
    #yaml文件,matchLabels为“project: My-Happy-Web,role: frontend”,replicas为3
    root@k8smaster:~# cat my-first-replicaset.yaml 
    apiVersion: apps/v1
    kind: ReplicaSet
    metadata:
      name: my-first-replicaset
      labels:
        version: 0.0.1
    spec:
      replicas: 3
      selector:
        matchLabels:
          project: My-Happy-Web
          role: frontend
      template:
        metadata:
          labels:
            project: My-Happy-Web
            role: frontend
            env: dev
        spec:
          containers:
          - name: happy-web
            image: nginx:latest
    

    apply yaml文件,查看pod,可以看到apply 之后只创建了两个名称带有“my-first-replicaset”的pod

    root@k8smaster:~# kubectl get all
    NAME                                  READY   STATUS    RESTARTS   AGE
    pod/my-2nd-nginx-59fc4f454c-xprzc     1/1     Running   0          4d
    pod/my-first-nginx-7c769d949c-dcgsq   1/1     Running   0          4d1h
    pod/my-first-nginx-7c769d949c-f26pd   1/1     Running   0          4d1h
    pod/my-first-pod                      2/2     Running   0          3d23h
    pod/my-first-replicaset-6fhhd         1/1     Running   0          12m
    pod/my-first-replicaset-ll6br         1/1     Running   0          12m
    pod/standalone-pod                    1/1     Running   0          43m
    

    这是因为刚刚创建的独立的pod设定的Labels属性与这个ReplicaSet的matchLabels相对于,所以这个pod也会被这个ReplicaSet控制

    查看独立创建的pod详细信息

    可以看到第15行有记录这个pod由名字为my-first-replicaset的ReplicaSet资源控制

    1  root@k8smaster:~# kubectl describe pod/standalone-pod
         2  Name:         standalone-pod
         3  Namespace:    default
         4  Priority:     0
         5  Node:         k8smaster/10.203.1.90
         6  Start Time:   Sun, 20 Dec 2020 22:56:32 -0800
         7  Labels:       env=test
         8                project=My-Happy-Web
         9                role=frontend
        10  Annotations:  <none>
        11  Status:       Running
        12  IP:           10.244.0.4
        13  IPs:
        14    IP:           10.244.0.4
        15  Controlled By:  ReplicaSet/my-first-replicaset
        16  Containers:
        17    standalone-pod:
        18      Container ID:  docker://07c218d704baa911542e56ca1474ba6c49c359020a5c2d9600e22eb803ef5558
        19      Image:         centos
        20      Image ID:      docker-pullable://centos@sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
        21      Port:          <none>
        22      Host Port:     <none>
        23      Command:
        24        sleep
        25        3600
        26      State:          Running
        27        Started:      Sun, 20 Dec 2020 22:57:10 -0800
        28      Ready:          True
        29      Restart Count:  0
        30      Environment:    <none>
        31      Mounts:
        32        /var/run/secrets/kubernetes.io/serviceaccount from default-token-4b5sf (ro)
        33  Conditions:
        34    Type              Status
        35    Initialized       True 
        36    Ready             True 
        37    ContainersReady   True 
        38    PodScheduled      True 
        39  Volumes:
        40    default-token-4b5sf:
        41      Type:        Secret (a volume populated by a Secret)
        42      SecretName:  default-token-4b5sf
        43      Optional:    false
        44  QoS Class:       BestEffort
        45  Node-Selectors:  <none>
        46  Tolerations:     node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
        47                   node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
        48  Events:
        49    Type    Reason     Age   From               Message
        50    ----    ------     ----  ----               -------
        51    Normal  Scheduled  34m   default-scheduler  Successfully assigned default/standalone-pod to k8smaster
        52    Normal  Pulling    34m   kubelet            Pulling image "centos"
        53    Normal  Pulled     33m   kubelet            Successfully pulled image "centos" in 32.241649541s
        54    Normal  Created    33m   kubelet            Created container standalone-pod
        55    Normal  Started    33m   kubelet            Started container standalone-pod
    

    Deployment

    概念

    Pod声明,会取代ReplicaSet,创建一个deployment会自动创建相对应的ReplicaSet,ReplicaSet创建Pods[图片上传失败...(image-27a784-1612416755456)]

    Deployment与ReplicaSet的关系

    除了上述的一对一的关系,还存在一个Deployment对应多个ReplicaSet 不同版本的关系[图片上传失败...(image-59827b-1612416755457)]

    测试

    创建一个deployment

    yaml文件内容如下,使用的是nginx:lasted版本部署的replicas为3的服务

    root@k8smaster:~# cat deploy1.yaml 
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: my-nginx
    spec:
      replicas: 3
      selector:
        matchLabels:
          run: my-nginx
      template:
        metadata:
          labels:
            run: my-nginx
        spec:
          containers:
          - name: my-nginx
            image: nginx
            ports:
            - containerPort: 80
    
    使用kubectl create命令创建deployment
    root@k8smaster:~# kubectl create -f deploy1.yaml --save-config --record     
    deployment.apps/my-nginx created
    

    命令加入“--save-config”和“--record”的参数

    • “--save-config”参数的作用是允许后续修改yaml文件之后使用kubectl apply命令update deployment
    • “--record”参数的作用是会记录命令行历史
    查看deployment

    可以看到第29行,记录了当前的deployment创建出来的ReplicaSet

         1  root@k8smaster:~# kubectl describe deployment.apps/my-nginx 
         2  Name:                   my-nginx
         3  Namespace:              default
         4  CreationTimestamp:      Wed, 23 Dec 2020 23:01:09 -0800
         5  Labels:                 <none>
         6  Annotations:            deployment.kubernetes.io/revision: 1
         7                          kubernetes.io/change-cause: kubectl create --filename=deploy1.yaml --save-config=true --record=true
         8  Selector:               run=my-nginx
         9  Replicas:               3 desired | 3 updated | 3 total | 3 available | 0 unavailable
        10  StrategyType:           RollingUpdate
        11  MinReadySeconds:        0
        12  RollingUpdateStrategy:  25% max unavailable, 25% max surge
        13  Pod Template:
        14    Labels:  run=my-nginx
        15    Containers:
        16     my-nginx:
        17      Image:        nginx
        18      Port:         80/TCP
        19      Host Port:    0/TCP
        20      Environment:  <none>
        21      Mounts:       <none>
        22    Volumes:        <none>
        23  Conditions:
        24    Type           Status  Reason
        25    ----           ------  ------
        26    Available      True    MinimumReplicasAvailable
        27    Progressing    True    NewReplicaSetAvailable
        28  OldReplicaSets:  <none>
        29  NewReplicaSet:   my-nginx-5b56ccd65f (3/3 replicas created)
        30  Events:
        31    Type    Reason             Age   From                   Message
        32    ----    ------             ----  ----                   -------
        33    Normal  ScalingReplicaSet  55s   deployment-controller  Scaled up replica set my-nginx-5b56ccd65f to 3
    
    update deployment
    1. 将nginx的image版本修改成1.12.0
    root@k8smaster:~# kubectl set image deployment my-nginx my-nginx=nginx:1.12.0 --record
    deployment.apps/my-nginx image updated
    
    1. 查看deployment,可以看到第28行记录的是刚刚update前的deployment,而第29行是新的deployment
         1  root@k8smaster:~# kubectl describe deployment.apps/my-nginx                           
         2  Name:                   my-nginx
         3  Namespace:              default
         4  CreationTimestamp:      Wed, 23 Dec 2020 23:01:09 -0800
         5  Labels:                 <none>
         6  Annotations:            deployment.kubernetes.io/revision: 2
         7                          kubernetes.io/change-cause: kubectl set image deployment my-nginx my-nginx=nginx:1.12.0 --record=true
         8  Selector:               run=my-nginx
         9  Replicas:               3 desired | 1 updated | 4 total | 3 available | 1 unavailable
        10  StrategyType:           RollingUpdate
        11  MinReadySeconds:        0
        12  RollingUpdateStrategy:  25% max unavailable, 25% max surge
        13  Pod Template:
        14    Labels:  run=my-nginx
        15    Containers:
        16     my-nginx:
        17      Image:        nginx:1.12.0
        18      Port:         80/TCP
        19      Host Port:    0/TCP
        20      Environment:  <none>
        21      Mounts:       <none>
        22    Volumes:        <none>
        23  Conditions:
        24    Type           Status  Reason
        25    ----           ------  ------
        26    Available      True    MinimumReplicasAvailable
        27    Progressing    True    ReplicaSetUpdated
        28  OldReplicaSets:  my-nginx-5b56ccd65f (3/3 replicas created)
        29  NewReplicaSet:   my-nginx-b9cd4969c (1/1 replicas created)
        30  Events:
        31    Type    Reason             Age   From                   Message
        32    ----    ------             ----  ----                   -------
        33    Normal  ScalingReplicaSet  6m6s  deployment-controller  Scaled up replica set my-nginx-5b56ccd65f to 3
        34    Normal  ScalingReplicaSet  4s    deployment-controller  Scaled up replica set my-nginx-b9cd4969c to 1
    
    1. 过一段时间再查看deployment,可以看到第29行,新的ReplicaSet已经部署完成3个pod,而old ReplicaSet也会被清空
         1  root@k8smaster:~# kubectl describe deployment.apps/my-nginx 
         2  Name:                   my-nginx
         3  Namespace:              default
         4  CreationTimestamp:      Wed, 23 Dec 2020 23:01:09 -0800
         5  Labels:                 <none>
         6  Annotations:            deployment.kubernetes.io/revision: 2
         7                          kubernetes.io/change-cause: kubectl set image deployment my-nginx my-nginx=nginx:1.12.0 --record=true
         8  Selector:               run=my-nginx
         9  Replicas:               3 desired | 3 updated | 3 total | 3 available | 0 unavailable
        10  StrategyType:           RollingUpdate
        11  MinReadySeconds:        0
        12  RollingUpdateStrategy:  25% max unavailable, 25% max surge
        13  Pod Template:
        14    Labels:  run=my-nginx
        15    Containers:
        16     my-nginx:
        17      Image:        nginx:1.12.0
        18      Port:         80/TCP
        19      Host Port:    0/TCP
        20      Environment:  <none>
        21      Mounts:       <none>
        22    Volumes:        <none>
        23  Conditions:
        24    Type           Status  Reason
        25    ----           ------  ------
        26    Available      True    MinimumReplicasAvailable
        27    Progressing    True    NewReplicaSetAvailable
        28  OldReplicaSets:  <none>
        29  NewReplicaSet:   my-nginx-b9cd4969c (3/3 replicas created)
        30  Events:
        31    Type    Reason             Age    From                   Message
        32    ----    ------             ----   ----                   -------
        33    Normal  ScalingReplicaSet  9m51s  deployment-controller  Scaled up replica set my-nginx-5b56ccd65f to 3
        34    Normal  ScalingReplicaSet  3m49s  deployment-controller  Scaled up replica set my-nginx-b9cd4969c to 1
        35    Normal  ScalingReplicaSet  3m16s  deployment-controller  Scaled down replica set my-nginx-5b56ccd65f to 2
        36    Normal  ScalingReplicaSet  3m16s  deployment-controller  Scaled up replica set my-nginx-b9cd4969c to 2
        37    Normal  ScalingReplicaSet  2m37s  deployment-controller  Scaled down replica set my-nginx-5b56ccd65f to 1
        38    Normal  ScalingReplicaSet  2m37s  deployment-controller  Scaled up replica set my-nginx-b9cd4969c to 3
        39    Normal  ScalingReplicaSet  2m30s  deployment-controller  Scaled down replica set my-nginx-5b56ccd65f to 0
    
    查看deployment历史
    root@k8smaster:~# kubectl rollout history deployment my-nginx
    deployment.apps/my-nginx 
    REVISION  CHANGE-CAUSE
    1         kubectl create --filename=deploy1.yaml --save-config=true --record=true
    2         kubectl set image deployment my-nginx my-nginx=nginx:1.12.0 --record=true
    
    
    回滚到版本1
    root@k8smaster:~# kubectl rollout undo deployment my-nginx --to-revision=1
    deployment.apps/my-nginx rolled back
    

    查看deployment详细信息,可以看到第29行,ReplicaSet已经恢复到第一次创建的版本

         1  root@k8smaster:~# kubectl describe deployment.apps/my-nginx 
         2  Name:                   my-nginx
         3  Namespace:              default
         4  CreationTimestamp:      Wed, 23 Dec 2020 23:01:09 -0800
         5  Labels:                 <none>
         6  Annotations:            deployment.kubernetes.io/revision: 3
         7                          kubernetes.io/change-cause: kubectl create --filename=deploy1.yaml --save-config=true --record=true
         8  Selector:               run=my-nginx
         9  Replicas:               3 desired | 3 updated | 3 total | 3 available | 0 unavailable
        10  StrategyType:           RollingUpdate
        11  MinReadySeconds:        0
        12  RollingUpdateStrategy:  25% max unavailable, 25% max surge
        13  Pod Template:
        14    Labels:  run=my-nginx
        15    Containers:
        16     my-nginx:
        17      Image:        nginx
        18      Port:         80/TCP
        19      Host Port:    0/TCP
        20      Environment:  <none>
        21      Mounts:       <none>
        22    Volumes:        <none>
        23  Conditions:
        24    Type           Status  Reason
        25    ----           ------  ------
        26    Available      True    MinimumReplicasAvailable
        27    Progressing    True    NewReplicaSetAvailable
        28  OldReplicaSets:  <none>
        29  NewReplicaSet:   my-nginx-5b56ccd65f (3/3 replicas created)
        30  Events:
        31    Type    Reason             Age                From                   Message
        32    ----    ------             ----               ----                   -------
        33    Normal  ScalingReplicaSet  50m                deployment-controller  Scaled up replica set my-nginx-b9cd4969c to 1
        34    Normal  ScalingReplicaSet  50m                deployment-controller  Scaled down replica set my-nginx-5b56ccd65f to 2
        35    Normal  ScalingReplicaSet  50m                deployment-controller  Scaled up replica set my-nginx-b9cd4969c to 2
        36    Normal  ScalingReplicaSet  49m                deployment-controller  Scaled down replica set my-nginx-5b56ccd65f to 1
        37    Normal  ScalingReplicaSet  49m                deployment-controller  Scaled up replica set my-nginx-b9cd4969c to 3
        38    Normal  ScalingReplicaSet  49m                deployment-controller  Scaled down replica set my-nginx-5b56ccd65f to 0
        39    Normal  ScalingReplicaSet  52s                deployment-controller  Scaled up replica set my-nginx-5b56ccd65f to 1
        40    Normal  ScalingReplicaSet  41s                deployment-controller  Scaled down replica set my-nginx-b9cd4969c to 2
        41    Normal  ScalingReplicaSet  41s                deployment-controller  Scaled up replica set my-nginx-5b56ccd65f to 2
        42    Normal  ScalingReplicaSet  32s (x2 over 56m)  deployment-controller  Scaled up replica set my-nginx-5b56ccd65f to 3
        43    Normal  ScalingReplicaSet  32s                deployment-controller  Scaled down replica set my-nginx-b9cd4969c to 1
        44    Normal  ScalingReplicaSet  19s                deployment-controller  Scaled down replica set my-nginx-b9cd4969c to 0
    

    Service

    Pod service

    创建Pod

    root@k8smaster:~# kubectl run nginx-pod --image=nginx --port=80 --restart="Never" --labels="project=My-Happy-Web,role=frontend,env=test"
    pod/nginx-pod created
    

    暴露service

    root@k8smaster:~# kubectl expose pod nginx-pod --port=8080 --target-port=80 --name="nginx-service"
    service/nginx-service exposed
    

    相关文章

      网友评论

          本文标题:kubernetes deployment ,service 学

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