pod的简介:

K8S中pod是可以创建kubernetes计算的最小可部署单元,pod可以理解为容器的外壳,可以在内部运行很多容器,一个pod代表着
集群中运行的一个进程,每个pod都有唯一的ip
一个pod类似一个豌豆荚,包含一个或多个容器(通常是docker),这多个容器间共享IPC、Network和UTC,和存储卷,存储卷不再属于容器,而属于pod。

pod的分类:

1,自主式pod
2,控制器管理的pod
(1)replicationcontroller
(2)replicaset
(3)deployment   管理无状态的
(4)statefulset  管理有状态的
(5)job,ctonjob
Deployment为Pod和ReplicaSet提供了一个声明式定义(declarative)方法,用来替代以前的ReplicationController来方便的管理应用。典型的应用场景包括:

定义Deployment来创建Pod和ReplicaSet
滚动升级和回滚应用
扩容和缩容
暂停和继续Deployment

例如:创建一个pod应用。

(1)我们依据控制器来创建pod应用:这里演示的k8s1.18.3版本,
创建控制器的语法格式;
kubectl create deployment NAME --image=image [--dry-run=server|client|none] [options]
[root@k8s-master ~]# kubectl create deployment nginx-deployment --image=nginx:1.14-alpine
deployment.apps/nginx-deployment created
nginx-deployment:为创建的控制器名称。使用的镜像为nginx:1.14-alpine
[root@k8s-master ~]# kubectl get deployment     查看创建的控制器。
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   1/1     1            1           101s
[root@k8s-master ~]# kubectl get pods -o wide   查看pod的详细信息。
NAME                               READY   STATUS    RESTARTS   AGE     IP           NODE        NOMINATED NODE   READINESS GATES
nginx-deployment-bd4bfb877-9x5qd   1/1     Running   0          2m48s   10.244.2.8   k8s-node2   <none>           <none>
详细信息记录这个pod被调度到node2节点运行。我们可以在node1集群内部访问node2

> 注意:在集群内部可以访问pod,在集群外部无法访问。

[root@k8s-node1 ~]# curl 10.244.2.8
<!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>

当我们删除pod时,会有什么情况?
我们上述是根据控制创建的pod,pod由控制器管理和创建。
[root@k8s-master ~]# kubectl delete pods nginx-deployment-bd4bfb877-9x5qd   删除pod时。
pod "nginx-deployment-bd4bfb877-9x5qd" deleted
[root@k8s-master ~]# kubectl get pods -o wide         控制器默认给我们重启一个pod,此时ip地址发生了变化。
NAME                               READY   STATUS    RESTARTS   AGE   IP           NODE        NOMINATED NODE   READINESS GATES
nginx-deployment-bd4bfb877-mp5rk   1/1     Running   0          24s   10.244.2.9   k8s-node2   <none>           <none>


service资源
service是一个抽象的概念,定义了一个服务的多个pod逻辑合集和访问pod的策略,一般把service称为微服务。

(1)举个例子:a服务运行3个pod,b服务怎么访问a服务的pod,pod的ip都不是持久化的,重启之后就会有变化。
这时候b服务可以访问跟a服务绑定的service,service信息是固定的提前告诉b就行了,service通过Label Selector跟a服务的pod绑定,无论a的pod如何变化对b来说都是透明的。
(2)service服务是为pod提供固定访问端点的。

(3)客户端的请求到service,由service代理至后端的pod。service并不是一个具体的应用程序,而是相当于一条ipvs或iptables规则。

创建service

[root@k8s-master ~]# kubectl expose --help   查看service创建帮助。
 kubectl expose (-f FILENAME | TYPE NAME) [--port=port] [--protocol=TCP|UDP|SCTP] [--target-port=number-or-name]
[--name=name] [--external-ip=external-ip-of-service] [--type=type] [options]
[root@k8s-master ~]# kubectl expose deployment nginx-deployment --name=nginx --port=80 --target-port=80 --protocol=TCP
service/nginx exposed
[root@k8s-master ~]# kubectl get svc -o wide  查看创建的service
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE     SELECTOR
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP   6d22h   <none>
nginx        ClusterIP   10.99.105.235   <none>        80/TCP    58s     app=nginx-deployment

[root@k8s-master ~]# kubectl describe svc nginx  查看service服务中nginx service服务的详细信息
Name:              nginx
Namespace:         default
Labels:            app=nginx-deployment
Annotations:       <none>
Selector:          app=nginx-deployment
Type:              ClusterIP
IP:                10.99.105.235
Port:              <unset>  80/TCP
TargetPort:        80/TCP
Endpoints:         10.244.2.9:80
Session Affinity:  None
Events:            <none>
[root@k8s-master ~]# 

动态扩容和缩容。

[root@k8s-master ~]# kubectl scale --replicas=5 deployment myapp 通过scale为控制器设置为5个pod,进行扩容
deployment.apps/myapp scaled
[root@k8s-master ~]# kubectl get deployment
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
myapp   3/5     5            3           83s
[root@k8s-master ~]# kubectl get pods -o wide
NAME                     READY   STATUS    RESTARTS   AGE     IP            NODE        NOMINATED NODE   READINESS GATES
myapp-5d587c4d45-4kmlh   1/1     Running   0          94s     10.244.1.7    k8s-node1   <none>           <none>
myapp-5d587c4d45-bd7rj   1/1     Running   0          2m53s   10.244.2.11   k8s-node2   <none>           <none>
myapp-5d587c4d45-qq2dl   1/1     Running   0          94s     10.244.2.13   k8s-node2   <none>           <none>
myapp-5d587c4d45-qw4w9   1/1     Running   0          94s     10.244.2.12   k8s-node2   <none>           <none>
myapp-5d587c4d45-txgvw   1/1     Running   0          94s     10.244.1.6    k8s-node1   <none>           <none>
[root@k8s-master ~]# kubectl get deployment -o wide
NAME    READY   UP-TO-DATE   AVAILABLE   AGE     CONTAINERS   IMAGES                 SELECTOR
myapp   5/5     5            5           3m16s   myapp        ikubernetes/myapp:v1   app=myapp
[root@k8s-master ~]# kubectl scale --replicas=3 deployment myapp  进行缩容。
deployment.apps/myapp scaled
[root@k8s-master ~]# kubectl get deployment -o wide
NAME    READY   UP-TO-DATE   AVAILABLE   AGE     CONTAINERS   IMAGES                 SELECTOR
myapp   3/3     3            3           3h17m   myapp        ikubernetes/myapp:v1   app=myapp
[root@k8s-master ~]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
myapp-5d587c4d45-4kmlh   1/1     Running   0          3h16m
myapp-5d587c4d45-bd7rj   1/1     Running   0          3h17m
myapp-5d587c4d45-txgvw   1/1     Running   0          3h16m
[root@k8s-master ~]# 

如何对pod中容器中的镜像进行版本升级。

[root@k8s-master ~]# kubectl scale --replicas=3 deployment myapp 我们把之前的使用控制器管理的pod。创建缩减为3个pod
deployment.apps/myapp scaled
[root@k8s-master ~]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
myapp-5d587c4d45-4kmlh   1/1     Running   0          3h25m
myapp-5d587c4d45-bd7rj   1/1     Running   0          3h26m
myapp-5d587c4d45-txgvw   1/1     Running   0          3h25m
[root@k8s-master ~]# kubectl describe pods myapp-5d587c4d45-4kmlh  我们可以查看其中一个pod的详细信息。
Name:         myapp-5d587c4d45-4kmlh
Namespace:    default
Priority:     0
Node:         k8s-node1/10.5.100.43
Start Time:   Fri, 03 Jul 2020 11:04:13 +0800
Labels:       app=myapp
              pod-template-hash=5d587c4d45
Annotations:  <none>
Status:       Running
IP:           10.244.1.7
IPs:
  IP:           10.244.1.7
Controlled By:  ReplicaSet/myapp-5d587c4d45
Containers:
  myapp:
    Container ID:   docker://fe74bd715b1bc500e3b128cda131e4cc68ffd4bae071f2eb4cb7444766fb2519
    Image:          ikubernetes/myapp:v1           可以看出这个运行的容器镜像版本为v1
    Image ID:       docker-pullable://ikubernetes/myapp@sha256:9c3dc30b5219788b2b8a4b065f548b922a34479577befb54b03330999d30d513
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Fri, 03 Jul 2020 11:04:50 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-sdtzv (ro)

[root@k8s-master ~]# kubectl set image deployment myapp myapp=ikubernetes/myapp:v2  通过set修改myapp控制器下容器名为myapp的使用镜像的版本号
deployment.apps/myapp image updated
[root@k8s-master ~]# kubectl get pods
NAME                     READY   STATUS              RESTARTS   AGE
myapp-5d587c4d45-4kmlh   1/1     Running             0          3h29m
myapp-5d587c4d45-bd7rj   1/1     Running             0          3h31m
myapp-5d587c4d45-txgvw   1/1     Running             0          3h29m
myapp-69cf46bb7b-tr4pm   0/1     ContainerCreating   0          4s
[root@k8s-master ~]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
myapp-69cf46bb7b-79zll   1/1     Running   0          2m26s
myapp-69cf46bb7b-sdz22   1/1     Running   0          2m27s
myapp-69cf46bb7b-tr4pm   1/1     Running   0          2m44s
[root@k8s-master ~]# kubectl describe pods myapp-69cf46bb7b-79zll  再次查看镜像的版本号为v2
Name:         myapp-69cf46bb7b-79zll
Namespace:    default
Priority:     0
Node:         k8s-node1/10.5.100.43
Start Time:   Fri, 03 Jul 2020 14:34:22 +0800
Labels:       app=myapp
              pod-template-hash=69cf46bb7b
Annotations:  <none>
Status:       Running
IP:           10.244.1.8
IPs:
  IP:           10.244.1.8
Controlled By:  ReplicaSet/myapp-69cf46bb7b
Containers:
  myapp:
    Container ID:   docker://b392bfca633319f506cc8533798a0b361d160400f382c952640ad73c41010155
    Image:          ikubernetes/myapp:v2
    Image ID:       docker-pullable://ikubernetes/myapp@sha256:85a2b81a62f09a414ea33b74fb8aa686ed9b168294b26b4c819df0be0712d358

[root@k8s-master ~]# kubectl rollout status deployment myapp  通过rollout可以查看更新进程
deployment "myapp" successfully rolled out

假如v1版本对某些业务有兼容性时,我们也可以进行回滚到v1版本,进行灰度。
[root@k8s-master ~]# kubectl rollout undo deployment myapp  使用rollout undo指令进行版本回滚。
deployment.apps/myapp rolled back
[root@k8s-master ~]# kubectl get pods
NAME                     READY   STATUS              RESTARTS   AGE
myapp-5d587c4d45-gbb6r   1/1     Running             0          2s
myapp-5d587c4d45-glzxc   1/1     Running             0          5s
myapp-5d587c4d45-kbvr7   0/1     ContainerCreating   0          1s
myapp-69cf46bb7b-79zll   0/1     Terminating         0          78m
myapp-69cf46bb7b-sdz22   0/1     Terminating         0          78m
myapp-69cf46bb7b-tr4pm   1/1     Running             0          78m
[root@k8s-master ~]# kubectl describe pods myapp-5d587c4d45-gbb6r 
Name:         myapp-5d587c4d45-gbb6r
Namespace:    default
Priority:     0
Node:         k8s-node2/10.5.100.36
Start Time:   Fri, 03 Jul 2020 15:52:29 +0800
Labels:       app=myapp
              pod-template-hash=5d587c4d45
Annotations:  <none>
Status:       Running
IP:           10.244.2.16
IPs:
  IP:           10.244.2.16
Controlled By:  ReplicaSet/myapp-5d587c4d45
Containers:
  myapp:
    Container ID:   docker://3a7b6576de50bf23a837f20439aa43c2525efbedcaa93c8be8ed23114acad494
    Image:          ikubernetes/myapp:v1     可以看到镜像已经变为v1版本了
    Image ID:       docker-pullable://ikubernetes/myapp@sha256:9c3dc30b5219788b2b8a4b065f548b922a34479577befb54b03330999d30d513

我们如何使用浏览器集群外部访问pod中的容器服务。

(1)修改service的默认网络,以myapp为例
k8s中的三种网络:
Node Network: 与外部网络接口
Service Network:又叫集群网络,与pod不在一个网段,只存在于iptables或ipvs规则中,是虚拟的
Pod Network: 节点当中pod的内部网络,可以ping通

在这里插入图片描述
如果端口暴露类型为NodePort,那么外部客户端可以通过集群内任意一台主机ip加暴露的端口进行访问

[root@k8s-master ~]# kubectl edit svc nginx
spec:
  clusterIP: 10.104.124.109
  externalTrafficPolicy: Cluster
  ports:
  - nodePort: 32259
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: myapp
  sessionAffinity: None
  type: NodePort           将最下面网络类型改为NodePort,可以让集群外部进行访问。
  
service/nginx edited  
[root@k8s-master ~]# kubectl get svc -o wide  查看映射的宿主机端口。
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE     SELECTOR
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        9d      <none>
nginx        NodePort    10.104.124.109   <none>        80:32259/TCP   4m16s   app=myapp

在集群外部进行访问验证信息
[root@node4 ~]# curl 10.5.100.102:32259
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[root@node4 ~]# 

浏览器访问试下。
在这里插入图片描述

Logo

开源、云原生的融合云平台

更多推荐