技术流ken

运维拯救世界

课堂记录:k8s滚动更新及数据管理(四)–技术流ken

滚动更新实践

实现效果当前生产环境httpd版本为2.2.31,在不停机以及中断服务的情况下,滚动更新到httpd:2.2.32版本

 

第一步:编写部署httpd的yaml文件

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
 name: httpd31
spec:
   replicas: 3
   template:
    metadata:
     labels:
       run: httpd31
    spec:
      containers:
      - name: httpd31
        image: httpd:2.2.31
        imagePullPolicy: IfNotPresent

 

第二步:查看deploy

[root@ken1 ~]# kubectl get deploy -o wide
NAME      READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
httpd31   3/3     3            3           29s   httpd31      httpd:2.2.31   run=httpd31

 

第三步:查看rs

[root@ken1 ~]# kubectl get rs -o wide
NAME               DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES         SELECTOR
httpd31-78c4cf6f   3         3         3       61s   httpd31      httpd:2.2.31   pod-template-hash=78c4cf6f,run=httpd31

 

第四步:查看po

[root@ken1 ~]# kubectl get po -o wide
NAME                     READY   STATUS    RESTARTS   AGE   IP             NODE   NOMINATED NODE   READINESS GATES
httpd31-78c4cf6f-2f2wp   1/1     Running   0          84s   10.244.2.110   ken3   <none>           <none>
httpd31-78c4cf6f-cmhz7   1/1     Running   0          84s   10.244.2.111   ken3   <none>           <none>
httpd31-78c4cf6f-qvpct   1/1     Running   0          84s   10.244.1.32    ken2   <none>           <none>

 

 

滚动更新

第五步;复制一份上述yml文件并把版本改为2.2.32

[root@ken1 ~]# cp httpd31.yml httpd32.yml 
[root@ken1 ~]# cat httpd32.yml 
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
 name: httpd31
spec:
   replicas: 3
   template:
    metadata:
     labels:
       run: httpd31
    spec:
      containers:
      - name: httpd31
        image: httpd:2.2.32
        imagePullPolicy: IfNotPresent

 

 

第六步:执行该yml文件

[root@ken1 ~]# kubectl apply -f httpd32.yml 

 

第七步:查看deploy

[root@ken1 ~]# kubectl get deploy -o wide
NAME      READY   UP-TO-DATE   AVAILABLE   AGE    CONTAINERS   IMAGES         SELECTOR
httpd31   3/3     3            3           4m1s   httpd31      httpd:2.2.32   run=httpd31

 

 

第八步:查看rs

[root@ken1 ~]# kubectl get rs -o wide
NAME                 DESIRED   CURRENT   READY   AGE     CONTAINERS   IMAGES         SELECTOR
httpd31-78c4cf6f     0         0         0       4m22s   httpd31      httpd:2.2.31   pod-template-hash=78c4cf6f,run=httpd31
httpd31-7f8489457b   3         3         3       42s     httpd31      httpd:2.2.32   pod-template-hash=7f8489457b,run=httpd31

 

第九步:查看po

[root@ken1 ~]# kubectl get po -o wide
NAME                       READY   STATUS    RESTARTS   AGE   IP             NODE   NOMINATED NODE   READINESS GATES
httpd31-7f8489457b-8rccl   1/1     Running   0          77s   10.244.2.112   ken3   <none>           <none>
httpd31-7f8489457b-j28z9   1/1     Running   0          77s   10.244.1.33    ken2   <none>           <none>
httpd31-7f8489457b-p688p   1/1     Running   0          74s   10.244.1.34    ken2   <none>           <none>

 

第十步:查看deploy详细信息

[root@ken1 ~]# kubectl describe deploy httpd31
Name:                   httpd31
Namespace:              default
CreationTimestamp:      Thu, 22 Aug 2019 09:38:12 +0800
Labels:                 run=httpd31
Annotations:            deployment.kubernetes.io/revision: 2
                        kubectl.kubernetes.io/last-applied-configuration:
                          {"apiVersion":"extensions/v1beta1","kind":"Deployment","metadata":{"annotations":{},"name":"httpd31","namespace":"default"},"spec":{"repli...
Selector:               run=httpd31
Replicas:               3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  1 max unavailable, 1 max surge
Pod Template:
  Labels:  run=httpd31
  Containers:
   httpd31:
    Image:        httpd:2.2.32
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
OldReplicaSets:  <none>
NewReplicaSet:   httpd31-7f8489457b (3/3 replicas created)
Events:
  Type    Reason             Age    From                   Message
  ----    ------             ----   ----                   -------
  Normal  ScalingReplicaSet  5m45s  deployment-controller  Scaled up replica set httpd31-78c4cf6f to 3
  Normal  ScalingReplicaSet  2m5s   deployment-controller  Scaled up replica set httpd31-7f8489457b to 1
  Normal  ScalingReplicaSet  2m5s   deployment-controller  Scaled down replica set httpd31-78c4cf6f to 2
  Normal  ScalingReplicaSet  2m5s   deployment-controller  Scaled up replica set httpd31-7f8489457b to 2
  Normal  ScalingReplicaSet  2m2s   deployment-controller  Scaled down replica set httpd31-78c4cf6f to 1
  Normal  ScalingReplicaSet  2m2s   deployment-controller  Scaled up replica set httpd31-7f8489457b to 3
  Normal  ScalingReplicaSet  2m2s   deployment-controller  Scaled down replica set httpd31-78c4cf6f to 0

 

k8s回滚任意版本

 

实践:需要准备三份yml文件,版本分别为2.2.31 2.2.32 2.2.34, 三分yaml文件名称必须一致,只需要镜像版本不同就行

第一步:复制yaml文件

[root@ken1 ~]# cat httpd34.yml 
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
 name: httpd31
spec:
   replicas: 3
   template:
    metadata:
     labels:
       run: httpd31
    spec:
      containers:
      - name: httpd31
        image: httpd:2.2.34
        imagePullPolicy: IfNotPresent

 

第二步:部署31版本yml文件

[root@ken1 ~]# kubectl apply -f  httpd31.yml --record
deployment.extensions/httpd31 configured
[root@ken1 ~]# kubectl get deploy -o wide
NAME      READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
httpd31   3/3     3            3           12m   httpd31      httpd:2.2.31   run=httpd31

 

第三步:部署32版本的yml文件

[root@ken1 ~]# kubectl apply -f  httpd32.yml --record
deployment.extensions/httpd31 configured
[root@ken1 ~]# kubectl get deploy -o wide
NAME      READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
httpd31   3/3     3            3           13m   httpd31      httpd:2.2.32   run=httpd31

 

第四步:部署34版本的yml文件

[root@ken1 ~]# kubectl apply -f  httpd34.yml --record
deployment.extensions/httpd31 configured
[root@ken1 ~]# kubectl get deploy -o wide
NAME      READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
httpd31   2/3     2            2           14m   httpd31      httpd:2.2.34   run=httpd31

 

实践回滚到31版本

 

第五步:查看命令执行的历史信息

[root@ken1 ~]# kubectl rollout history deploy httpd31
deployment.extensions/httpd31 
REVISION  CHANGE-CAUSE
3         kubectl apply --filename=httpd31.yml --record=true
4         kubectl apply --filename=httpd32.yml --record=true
5         kubectl apply --filename=httpd34.yml --record=true

 

 

第六步:实现回滚

[root@ken1 ~]# kubectl rollout undo deploy httpd31 --to-revision=3
deployment.extensions/httpd31 rolled back
[root@ken1 ~]# kubectl get deploy -o wide
NAME      READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
httpd31   3/3     3            3           18m   httpd31      httpd:2.2.31   run=httpd31

 

 

指定版本记录的条数

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
 name: httpd31
spec:
   revisionHistoryLimit: 10
   replicas: 3
   template:
    metadata:
     labels:
       run: httpd31
    spec:
      containers:
      - name: httpd31
        image: httpd:2.2.31
        imagePullPolicy: IfNotPresent
~                                     

 

k8ss数据管理

 

1.emptyDir

2.hostPath

3.NFS

4.pv/pvc

 

一、实践emptyDir

 

第一步:编写yml文件

apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
  - name: nginx
    image: nginx
    imagePullPolicy: IfNotPresent
    volumeMounts:
    - name: ken
      mountPath: /usr/share/nginx/html
  volumes:
  - name: ken
    emptyDir: {}

 

第二步:执行yml文件

[root@ken1 ~]# kubectl apply -f nginx.yml 

 

第三步:查看pod

[root@ken1 ~]# kubectl get po
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          68s

 

第四步:在节点查看容器详细信息

[root@ken3 ken]# docker inspect 937208cf64ee 
...
"Mounts": [
            {
                "Type": "bind",
                "Source": "/var/lib/kubelet/pods/704e6443-9b31-45b5-b41d-39e8e84bacb2/volumes/kubernetes.io~empty-dir/ken",
                "Destination": "/usr/share/nginx/html",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            },
...

 


第五步:编写测试文件

[root@ken3 ~]# cd /var/lib/kubelet/pods/704e6443-9b31-45b5-b41d-39e8e84bacb2/volumes/kubernetes.io~empty-dir/ken
[root@ken3 ken]# echo "test emptyDir" > index.html

 

第六步:主节点测试

[root@ken1 ~]# curl 10.244.2.119
test emptyDir

 

二、实践hostPath

效果相当于执行: docker run -v /tmp:/usr/share/nginx/html

第一步:编写yml文件

apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
  - name: nginx
    image: nginx
    imagePullPolicy: IfNotPresent
    volumeMounts:
    - name: ken
      mountPath: /usr/share/nginx/html
  volumes:
   - name: ken
     hostPath:
      path: /tmp

 

第二步:执行yml文件

[root@ken1 ~]# kubectl apply -f nginx-hostp.yml 

 

第三步:进行验证

[root@ken1 ~]# ls /tmp
ks-script-Kgah4x                                                          yum.log
systemd-private-1efe767de1b4430fb77c8d90bafce1de-vmtoolsd.service-nfl5a9  yum_save_tx.2019-08-21.17-03.Hc7vcy.yumtx
systemd-private-21c03f170cff40688b56b7b97b6d60d9-vmtoolsd.service-oR1NRh
[root@ken1 ~]# kubectl get po
NAME          READY   STATUS    RESTARTS   AGE
nginx         1/1     Running   0          31m
nginx-hostp   1/1     Running   0          31s
[root@ken1 ~]# kubectl exec nginx-hostp ls /usr/share/nginx/html
ks-script-Kgah4x
systemd-private-290df9f339b647a099c58e3b2d499278-vmtoolsd.service-2MNZBs
systemd-private-73ed2c6655a748c1b8190aa51cf682bb-vmtoolsd.service-nysa3U
yum.log

 

 

三、实践NFS

每个节点必须要安装nfs

主节点部署NFS服务器端

每个节点安装nfs-utils

第一步:部署nfs服务器端

[root@ken1 ~]# yum install nfs-utils rpcbind -y
[root@ken1 ~]# cat /etc/exports
/ken *(rw)
[root@ken1 ~]# chown -R nfsnobody: /ken
[root@ken1 ~]# systemctl restart nfs rpcbind

 


第二步:客户端下载nfs

[root@ken2 ~]# yum install nfs-utils -y

 

第三步:编写yml文件

apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
  - name: nginx
    image: nginx
    imagePullPolicy: IfNotPresent
    volumeMounts:
    - name: ken
      mountPath: /usr/share/nginx/html
  volumes:
  - name: ken
    nfs:
     path: /ken
     server: 192.168.64.11

 

第四步:执行yml文件

[root@ken1 ~]# kubectl apply -f nginx-nfs.yml 

 

第五步:验证

[root@ken1 ~]# ls /ken
index.html  test
[root@ken1 ~]# cat /ken/index.html 
test
[root@ken1 ~]# echo "nfs" >> /ken/index.html 
[root@ken1 ~]# kubectl get po -o wide
NAME          READY   STATUS    RESTARTS   AGE     IP             NODE   NOMINATED NODE   READINESS GATES
nginx         1/1     Running   0          38m     10.244.2.119   ken3   <none>           <none>
nginx-hostp   1/1     Running   0          7m32s   10.244.1.41    ken2   <none>           <none>
nginx-nfs     1/1     Running   0          32s     10.244.2.120   ken3   <none>           <none>
[root@ken1 ~]# curl 10.244.2.120
test
nfs

 

四、实践pv/pvc

 

第一步:部署NFS

上同,略

 

第二步:编写pv的yml文件

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mypv
spec:
  capacity:
   storage: 1Gi
  accessModes:
   - ReadWriteMany
  nfs:
   path: /ken
   server: 192.168.64.11

 

第三步:执行yml文件

[root@ken1 ~]# kubectl apply -f pv.yml 

 

第四步:查看pv

[root@ken1 ~]# kubectl get pv
NAME   CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
mypv   1Gi        RWX            Retain           Available                                   17h

 

 

注:

accessModes有三类

  • ReadWriteOnce – 可以被单个节点进行读写挂载
  • ReadOnlyMany – 可以被多个节点进行只读挂载
  • ReadWriteMany – 可以被多个节点进行读写挂载

 

reclaim policy有三类

  • retain – pvc被删除之后,数据保留
  • recyle- 删除pvc之后会删除数据(被废弃)
  • delete –  删除pvc之后会删除数据

 

创建pvc

 

第一步:编写yml文件

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
 name: mypvc1
spec:
  accessModes:
  - ReadWriteMany
  volumeName: mypv
  resources:
   requests:
    storage: 1Gi


 

第二步:执行pvc

[root@ken1 ~]# kubectl apply -f pvc.yml

 

第三步:查看pvc

[root@ken1 ~]# kubectl get pvc
NAME     STATUS   VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
mypvc1   Bound    mypv     1Gi        RWX                           19s
[root@ken1 ~]# kubectl get pv
NAME   CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM            STORAGECLASS   REASON   AGE
mypv   1Gi        RWX            Retain           Bound    default/mypvc1                           17h
[root@ken1 ~]# 

 

 

 

使用pvc

第一步:编写部署nginx的yml文件

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pvc
spec:
  containers:
  - name: nginx
    image: nginx
    imagePullPolicy: IfNotPresent
    volumeMounts:
    - name: ken
      mountPath: /usr/share/nginx/html
  volumes:
  - name: ken
    persistentVolumeClaim:
     claimName: mypvc1

 

第二步:执行yml文件

[root@ken1 ~]# kubectl apply -f nginx-pvc.yml 

 

第三步:测试

[root@ken1 ~]# kubectl exec -it nginx-pvc /bin/bash
root@nginx-pvc:/# ls
bin  boot  dev	etc  home  lib	lib64  media  mnt  opt	proc  root  run  sbin  srv  sys  tmp  usr  var
root@nginx-pvc:/# cd /usr/share/nginx/i
bash: cd: /usr/share/nginx/i: No such file or directory
root@nginx-pvc:/# cd /usr/share/nginx/html/
root@nginx-pvc:/usr/share/nginx/html# ls
index.html  test
root@nginx-pvc:/usr/share/nginx/html# touch ken
root@nginx-pvc:/usr/share/nginx/html# exit
exit
[root@ken1 ~]# ls /ken
index.html  ken  test

 

 

释放pv

 

第一步:先停掉pod

[root@ken1 ~]# kubectl delete -f nginx-pvc.yml 

 

第二步:删除pvc

[root@ken1 ~]# kubectl delete pvc mypvc1

 

第三步:查看pv状态

[root@ken1 ~]# kubectl get pv
NAME   CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS     CLAIM            STORAGECLASS   REASON   AGE
mypv   1Gi        RWX            Retain           Released   default/mypvc1                           17h

 

第四步:删除pv

[root@ken1 ~]# kubectl delete -f pv.yml 

 

第五步:查看数据

[root@ken1 ~]# ls /ken
index.html  ken  test

 

第六步:再次创建pv

[root@ken1 ~]# kubectl apply -f pv.yml 
persistentvolume/mypv created
[root@ken1 ~]# kubectl get pv
NAME   CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
mypv   1Gi        RWX            Retain           Available                                   4s

 

发表评论

电子邮件地址不会被公开。