21天转型容器实战营(七容器进阶之Kubernetes 应用生命周期原理分析)

1、通过Deployment方式,使用redis镜像创建1个Pod。通过kubectl获得redis启动日志。
2、通过命令行,创建1个deployment,副本数为3,镜像为nginx:latest。然后滚动升级 到nginx:1.9.1。
一 查看第一个yaml文件
[root@cce-21day-cluster-62954-81jwz ~]# cat day7-deployment1.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: cce21days-app3-huaweicloud
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: cce21days-app3-huaweicloud
  template:
    metadata:
      labels:
        app: cce21days-app3-huaweicloud
    spec:
      containers:
        - image: 'redis:latest'
          name: container-0
      imagePullSecrets:
        - name: default-secret
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
              - matchExpressions:
                - key: kubernetes.io/hostname
                  operator: In
                  values:
                    - 192.168.162.50
[root@cce-21day-cluster-62954-81jwz ~]# 
二创建deployment pod
[root@cce-21day-cluster-62954-81jwz ~]# kubectl create -f day7-deployment1.yaml
deployment "cce21days-app3-huaweicloud" created
[root@cce-21day-cluster-62954-81jwz ~]# kubectl get pods -owide
NAME                                          READY     STATUS    RESTARTS   AGE       IP            NODE
cce21days-app3-huaweicloud-748c8656b4-5dcz7   1/1       Running   0          43s       172.16.0.35   192.168.162.50
三查看容器pod的日志
[root@cce-21day-cluster-62954-81jwz ~]# kubectl logs -f cce21days-app3-huaweicloud-748c8656b4-5dcz7 -c container-0
1:C 10 Dec 2018 13:06:41.994 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 10 Dec 2018 13:06:41.994 # Redis version=5.0.1, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 10 Dec 2018 13:06:41.994 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 10 Dec 2018 13:06:41.995 * Running mode=standalone, port=6379.
1:M 10 Dec 2018 13:06:41.995 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:M 10 Dec 2018 13:06:41.995 # Server initialized
1:M 10 Dec 2018 13:06:41.995 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
1:M 10 Dec 2018 13:06:41.995 * Ready to accept connections
四作业第二个yaml文件
apiVersion: apps/v1
kind: Deployment
metadata:
  name: cce21days-app4-huaweicloud
  namespace: default
spec:
  replicas: 3
  selector:
    matchLabels:
      app: cce21days-app4-huaweicloud
  template:
    metadata:
      labels:
        app: cce21days-app4-huaweicloud
    spec:
      containers:
        - image: 'nginx:latest'
          name: container-0
      imagePullSecrets:
        - name: default-secret
        # 此处亲和性设置是为了将pod调度到有EIP的节点,便于下载外网镜像
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
              - matchExpressions:
                - key: kubernetes.io/hostname
                  operator: In
                  values:
                    - 192.168.1.109

[root@cce-21day-cluster-62954-81jwz ~]# docker pull nginx:1.9.1
1.9.1: Pulling from library/nginx
5641bf7f839b: Pull complete
a3ed95caeb02: Pull complete
d003dd0d7f8a: Pull complete
c5dd085dcc7c: Pull complete
d95a07673dd5: Pull complete
cec5c5855afe: Pull complete
b315c6f2ccf3: Pull complete
Digest: sha256:2f68b99bc0d6d25d0c56876b924ec20418544ff28e1fb89a4c27679a40da811b
Status: Downloaded newer image for nginx:1.9.1
五创建三个pod文件
[root@cce-21day-cluster-62954-81jwz ~]# kubectl create -f day7-deployment2.yaml

六滚动升级镜像至nginx:1.9.1版本
kubectl set image deployment/cce21days-app4-huaweicloud container-0=nginx:1.9.1
查看具体滚动升级过程
[root@cce-21day-cluster-62954-81jwz ~]# kubectl get pods -owide
NAME                                          READY     STATUS              RESTARTS   AGE       IP           NODE
cce21days-app4-huaweicloud-7f9b4f87f9-bbjpt   0/1       ContainerCreating   0          3s        <none>       192.168.98.57
cce21days-app4-huaweicloud-7f9b4f87f9-xc92g   1/1       Running             0          9s        172.16.0.8   192.168.98.57
cce21days-app4-huaweicloud-fd7df8dc-2pfvw     1/1       Running             0          35s       172.16.0.5   192.168.98.57
cce21days-app4-huaweicloud-fd7df8dc-b8bvt     1/1       Running             0          35s       172.16.0.6   192.168.98.57
[root@cce-21day-cluster-62954-81jwz ~]# kubectl get pods -owide
NAME                                          READY     STATUS              RESTARTS   AGE       IP           NODE
cce21days-app4-huaweicloud-7f9b4f87f9-bbjpt   0/1       ContainerCreating   0          5s        <none>       192.168.98.57
cce21days-app4-huaweicloud-7f9b4f87f9-xc92g   1/1       Running             0          11s       172.16.0.8   192.168.98.57
cce21days-app4-huaweicloud-fd7df8dc-2pfvw     1/1       Running             0          37s       172.16.0.5   192.168.98.57
cce21days-app4-huaweicloud-fd7df8dc-b8bvt     1/1       Running             0          37s       172.16.0.6   192.168.98.57
[root@cce-21day-cluster-62954-81jwz ~]# kubectl get pods -owide
NAME                                          READY     STATUS    RESTARTS   AGE       IP            NODE
cce21days-app4-huaweicloud-7f9b4f87f9-2zsxw   1/1       Running   0          16s       172.16.0.10   192.168.98.57
cce21days-app4-huaweicloud-7f9b4f87f9-bbjpt   1/1       Running   0          22s       172.16.0.9    192.168.98.57
cce21days-app4-huaweicloud-7f9b4f87f9-xc92g   1/1       Running   0          28s       172.16.0.8    192.168.98.57
七查看升级后的镜像文件
通过kubectl get pods -oyaml可以看到pod的镜像已经替换成nginx:1.9.1了
[root@cce-21day-cluster-62954-81jwz ~]# kubectl get pods cce21days-app4-huaweicloud-7f9b4f87f9-2zsxw -oyaml | grep image -A10
  - image: nginx:1.9.1
    imagePullPolicy: Always
    name: container-0
    resources: {}
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    volumeMounts:
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: default-token-8qgzd
      readOnly: true
  dnsPolicy: ClusterFirst
  imagePullSecrets:
  - name: default-secret
  nodeName: 192.168.98.57
  restartPolicy: Always
  schedulerName: default-scheduler
  securityContext: {}
  serviceAccount: default
  serviceAccountName: default
  terminationGracePeriodSeconds: 30
  tolerations:
  - effect: NoExecute
--
    image: nginx:1.9.1
    imageID: docker-pullable://nginx@sha256:2f68b99bc0d6d25d0c56876b924ec20418544ff28e1fb89a4c27679a40da811b
    lastState: {}
    name: container-0
    ready: true
    restartCount: 0
    state:
      running:
        startedAt: 2018-12-10T13:34:47Z
  hostIP: 192.168.98.57
  managementIP: 192.168.98.57
  phase: Running
[root@cce-21day-cluster-62954-81jwz ~]#
查询升级历史:

[root@cce-21day-cluster-62954-81jwz ~]# kubectl rollout history deploy/cce21days-app4-huaweicloud
deployments "cce21days-app4-huaweicloud"
REVISION  CHANGE-CAUSE
1         <none>
2         <none>

[root@cce-21day-cluster-62954-81jwz ~]# kubectl rollout history deploy/cce21days-app4-huaweicloud --revision=2
deployments "cce21days-app4-huaweicloud" with revision #2
Pod Template:
  Labels:  app=cce21days-app4-huaweicloud
   pod-template-hash=3956094395
  Containers:
   container-0:
    Image: nginx:1.9.1
    Port:  <none>
    Environment:   <none>
    Mounts:    <none>
  Volumes: <none>

[root@cce-21day-cluster-62954-81jwz ~]#

猜你喜欢

转载自blog.csdn.net/xsjzdrxsjzdr/article/details/84900736
今日推荐