Pod控制器应用进阶三

版权声明:知识就是为了传播! https://blog.csdn.net/weixin_36171533/article/details/82463423

spec:

spec:
    name
    image
    imagePullPolicy:Always,Never,IfNotPresent
    ports:
        name
        containerPort
    livenessProbe
    readinessProbe
    liftcycle
ExecAction: exec
TCPSocketAction: tcpSocker
HTTPGetAction: httpGet

Pod控制器
    ReplicaSet:创建一定数量的pod,支持扩缩容
    主要有三个组件:1,用户期望的pod副本 2,标签选择器 3.如果pod不够怎么办,扩建
    Pod资源模板可以帮助用户新建
    Deployment可以比ReplicaSet更加强大,增加声明式配置和升级后的回滚,是目前最好的Pod控制器
    DaemonSet:系统级的Pod,只能在node上运行一个,每个集群精确运行一个
    Job:只能执行运行一次的,比如这个程序运行完了,这个容器就没有用了
    CronJob:周期性运行Job和CronJob都是不用长期运行的容器
    StatetufulSet:运行mysql,redis
    有状态的运维任务时很困难的,比如mysql和redis集群,一般转换成脚本,有状态的应用托管到K8s上是很难的

    Deployment在更新中展示强大的功能

[root@master test]# cat rs-demo.yaml 
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: myapp
  namespace: default
spec:
  replicas: 2
  selector:
    matchLabels:
      app: myapp
      release: canary
  template:
    metadata:
      name: myapp-pod
      labels:
        app: myapp
        release: canary
        environment: qa
    spec:
        containers:
        - name: myapp-container
          image: ikubernetes/myapp:v1
          ports:
          - name: http
            containerPort: 80
[root@master test]# kubectl get pod
NAME                          READY     STATUS    RESTARTS   AGE
client                        1/1       Running   0          1d
myapp-ffw8l                   1/1       Running   0          9s
myapp-rz67d                   1/1       Running   0          9s
readiness-httpget-container   1/1       Running   0          4h

[root@master test]# kubectl get rs
NAME      DESIRED   CURRENT   READY     AGE
myapp     2         2         2         1m

获取其中的一个Pod信息,查看详情
[root@master test]# kubectl describe pods myapp-ffw8l
Name:               myapp-ffw8l
Namespace:          default
Priority:           0
PriorityClassName:  <none>
Node:               node2/192.168.68.30
Start Time:         Thu, 06 Sep 2018 15:57:31 +0800
Labels:             app=myapp
                    environment=qa
                    release=canary
Annotations:        <none>
Status:             Running
IP:                 10.244.2.17
Controlled By:      ReplicaSet/myapp
Containers:
  myapp-container:
    Container ID:   docker://787f74a16dfdcbe9c72a0dc1d962d6b4fbb050724108cf73190f2a67afd5440c
    Image:          ikubernetes/myapp:v1
    Image ID:       docker-pullable://ikubernetes/myapp@sha256:9c3dc30b5219788b2b8a4b065f548b922a34479577befb54b03330999d30d513
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Thu, 06 Sep 2018 15:57:32 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-8zzcr (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-8zzcr:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-8zzcr
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  2m    default-scheduler  Successfully assigned default/myapp-ffw8l to node2
  Normal  Pulled     2m    kubelet, node2     Container image "ikubernetes/myapp:v1" already present on machine
  Normal  Created    2m    kubelet, node2     Created container
  Normal  Started    2m    kubelet, node2     Started containe


[root@master test]# curl 10.244.2.17
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[root@master test]# curl 10.244.2.17/hostname.html
myapp-ffw8l


如果删除一个Pod,他会自动创建一个
[root@master test]# kubectl get pod
NAME                          READY     STATUS    RESTARTS   AGE
client                        1/1       Running   0          1d
myapp-ffw8l                   1/1       Running   0          7m
myapp-rz67d                   1/1       Running   0          7m
readiness-httpget-container   1/1       Running   0          4h
[root@master test]# kubectl delete pods myapp-ffw8l
pod "myapp-ffw8l" deleted
[root@master test]# kubectl get pod
NAME                          READY     STATUS    RESTARTS   AGE
client                        1/1       Running   0          1d
myapp-r99dr                   1/1       Running   0          4s
myapp-rz67d                   1/1       Running   0          8m
readiness-httpget-container   1/1       Running   0          4h
删除Pod之前就已经创建了

kubectl get pods --show-labels
[root@master test]# kubectl get pods --show-labels
NAME                          READY     STATUS    RESTARTS   AGE       LABELS
client                        1/1       Running   0          1d        run=client
myapp-r99dr                   1/1       Running   0          1m        app=myapp,environment=qa,release=canary
myapp-rz67d                   1/1       Running   0          10m       app=myapp,environment=qa,release=canary
readiness-httpget-container   1/1       Running   0          4h        <none>

[root@master test]# kubectl label pods client release=canary
pod/client labeled
[root@master test]# kubectl get pods --show-labels
NAME                          READY     STATUS    RESTARTS   AGE       LABELS
client                        1/1       Running   0          1d        release=canary,run=client
myapp-r99dr                   1/1       Running   0          3m        app=myapp,environment=qa,release=canary
myapp-rz67d                   1/1       Running   0          11m       app=myapp,environment=qa,release=canary
readiness-httpget-container   1/1       Running   0          4h        <none>

自动扩容:将myapp扩容到10个
kubectl edit rs myapp
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: extensions/v1beta1
kind: ReplicaSet
metadata:
  creationTimestamp: 2018-09-06T07:57:31Z
  generation: 2
  labels:
    app: myapp
    environment: qa
    release: canary
  name: myapp
  namespace: default
  resourceVersion: "88845"
  selfLink: /apis/extensions/v1beta1/namespaces/default/replicasets/myapp
  uid: 81fa7645-b1aa-11e8-a432-000c29f33006
spec:
  replicas: 10
  selector:
    matchLabels:
      app: myapp
      release: canary
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: myapp
        environment: qa
        release: canary
      name: myapp-pod
    spec:
      containers:
      - image: ikubernetes/myapp:v1
 imagePullPolicy: IfNotPresent
        name: myapp-container
        ports:
        - containerPort: 80
          name: http
          protocol: TCP
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
status:
  availableReplicas: 10
  fullyLabeledReplicas: 10
  observedGeneration: 2
  readyReplicas: 10
  replicas: 10

 我们查看一下:
 [root@master test]# kubectl get pods
NAME                          READY     STATUS    RESTARTS   AGE
client                        1/1       Running   0          1d
myapp-7fhdt                   1/1       Running   0          10s
myapp-8dnzm                   1/1       Running   0          10s
myapp-92cnh                   1/1       Running   0          10s
myapp-9dvll                   1/1       Running   0          10s
myapp-h55jv                   1/1       Running   0          10s
myapp-kw656                   1/1       Running   0          10s
myapp-r99dr                   1/1       Running   0          7m
myapp-rz67d                   1/1       Running   0          15m
myapp-v62f6                   1/1       Running   0          10s
myapp-zkk7k                   1/1       Running   0          10s
readiness-httpget-container   1/1       Running   0          5h


我们将版本升级成v2
kubectl edit rs myapp
 image: ikubernetes/myapp:v2


我们查看一下:
[root@master test]# kubectl get rs -o wide
NAME      DESIRED   CURRENT   READY     AGE       CONTAINERS        IMAGES                 SELECTOR
myapp     10        10        10        18m       myapp-container   ikubernetes/myapp:v2   app=myapp,release=canary
虽然这里是V2,但是我们访问的时候还是v1
[root@master test]# curl 10.244.2.20
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[root@master test]# curl 10.244.2.20/hostname.html
myapp-7fhdt
[root@master test]# kubectl delete pods myapp-7fhdt
pod "myapp-7fhdt" deleted
[root@master test]# kubectl get rs -o wide
NAME      DESIRED   CURRENT   READY     AGE       CONTAINERS        IMAGES                 SELECTOR
myapp     10        10        10        20m       myapp-container   ikubernetes/myapp:v2   app=myapp,release=canary
[root@master test]# kubectl get pods -o wide
NAME                          READY     STATUS    RESTARTS   AGE       IP            NODE      NOMINATED NODE
client                        1/1       Running   0          1d        10.244.2.3    node2     <none>
myapp-8dnzm                   1/1       Running   0          5m        10.244.1.12   node1     <none>
myapp-92cnh                   1/1       Running   0          5m        10.244.1.13   node1     <none>
myapp-9dvll                   1/1       Running   0          5m        10.244.2.21   node2     <none>
myapp-h55jv                   1/1       Running   0          5m        10.244.2.19   node2     <none>
myapp-kw656                   1/1       Running   0          5m        10.244.1.15   node1     <none>
myapp-r99dr                   1/1       Running   0          12m       10.244.2.18   node2     <none>
myapp-rz67d                   1/1       Running   0          21m       10.244.1.11   node1     <none>
myapp-v62f6                   1/1       Running   0          5m        10.244.2.22   node2     <none>
myapp-x4hwp                   1/1       Running   0          29s       10.244.2.23   node2     <none>
myapp-zkk7k                   1/1       Running   0          5m        10.244.1.14   node1     <none>
readiness-httpget-container   1/1       Running   0          5h        10.244.2.15   node2     <none>
[root@master test]# curl 10.244.2.23/hostname.html
myapp-x4hwp
[root@master test]# curl 10.244.2.23
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>

也就是我们只有在删除容器时候在创建的时候才是v2版本,如果不删除还是v1版本,这样适合做蓝绿发布,可以做滚动式更新,也可控制更新节奏

猜你喜欢

转载自blog.csdn.net/weixin_36171533/article/details/82463423