Kubernetes学习之路(二)K8S操作需求

参考总结自CloudMan老师公众号《每天5分钟玩转Kubernetes》https://item.jd.com/26225745440.html

创建K8s应用

[root@linux-node1 ~]#kubectl run net-test --image=alpine --port=80 --replicas=2 sleep 36000   #创建名称为net-test的应用,镜像指定为alpine,副本数为2个
deployment.apps "net-test" created

[root@linux-node1 ~]#kubectl get pod -o wide #查看pod的状态信息,此时是API Server从etcd中读取这些数据
NAME                        READY     STATUS              RESTARTS   AGE       IP          NODE
net-test-7b949fc785-2v2qz   1/1       Running             0          56s       10.2.87.2   192.168.56.120
net-test-7b949fc785-6nrhm   0/1       ContainerCreating   0          56s       <none>      192.168.56.130
[root@linux-node1 ~]#kubectl get deployment net-test /kubectl describe deployment net-test #查看net-test应用运行信息/查看net-test应用详细信息
NAME       DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
net-test   2         2         2            2           22h
[root@linux-node1 ~]# kubectl get replicaset  #获取副本集信息
NAME                          DESIRED   CURRENT   READY     AGE
net-test-5767cb94df           2         2         2         23h
[root@linux-node1 ~]# kubectl describe replicaset net-test-5767cb94df  #查看副本集的详细信息
[root@linux-node1 ~]# kubectl get pod  #获取Pod信息,可以看到2个副本都处于Running状态
NAME                                READY     STATUS    RESTARTS   AGE
net-test-5767cb94df-djt98           1/1       Running   0          22h
net-test-5767cb94df-zb8m4           1/1       Running   0          23h
[root@linux-node1 ~]# kubectl describe pod net-test-5767cb94df-djt98 #查看pod的详细信息
[root@linux-node1 ~]# vim nginx-deployment.yaml  #使用yaml的方式进行创建应用

yaml文件内容分析

apiVersion: apps/v1  #apiVersion是当前配置格式的版本
kind: Deployment    #kind是要创建的资源类型,这里是Deploymnet
metadata:        #metadata是该资源的元数据,name是必须的元数据项
  name: nginx-deployment
  labels:
    app: nginx
spec:          #spec部分是该Deployment的规则说明
  replicas: 3      #relicas指定副本数量,默认为1
  selector:
    matchLabels:
      app: nginx
  template:      #template定义Pod的模板,这是配置的重要部分
    metadata:      #metadata定义Pod的元数据,至少要顶一个label,label的key和value可以任意指定
      labels:
        app: nginx
    spec:       #spec描述的是Pod的规则,此部分定义pod中每一个容器的属性,name和image是必需的
      containers:
      - name: nginx
        image: nginx:1.13.12
        ports:
        - containerPort: 80

[root@linux-node1 ~]#kubectl create -f nginx-deployment.yaml #根据yaml文件创建,yaml文件描述了应用信息,和需要达到的预期状态。

[root@linux-node1 ~]# kubectl get deployment #查看deployment

NAME               DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
net-test           2         2         2            2           32m
nginx-deployment   3         3         3            0           10s

[root@linux-node1 ~]# kubectl describe deployment nginx-deployment #查看deployment详情

Name:                   nginx-deployment
Namespace:              default
CreationTimestamp:      Thu, 16 Aug 2018 16:13:37 +0800
Labels:                 app=nginx
Annotations:            deployment.kubernetes.io/revision=1
Selector:               app=nginx
Replicas:               3 desired | 3 updated | 3 total | 0 available | 3 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=nginx
  Containers:
   nginx:
    Image:        nginx:1.13.12
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      False   MinimumReplicasUnavailable
  Progressing    True    ReplicaSetUpdated
OldReplicaSets:  <none>
NewReplicaSet:   nginx-deployment-6c45fc49cb (3/3 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  1m    deployment-controller  Scaled up replica set nginx-deployment-6c45fc49cb to 3
[root@linux-node1 ~]# kubectl get pod    #查看pod在状态,正在创建中,此时应该正在拉取镜像
NAME                                READY     STATUS              RESTARTS   AGE
net-test-5767cb94df-djt98           1/1       Running             0          22m
net-test-5767cb94df-hcwv7           1/1       Unknown             0          34m
net-test-5767cb94df-zb8m4           1/1       Running             0          34m
nginx-deployment-6c45fc49cb-dmc22   0/1       ContainerCreating   0          2m
nginx-deployment-6c45fc49cb-fd8xm   0/1       ContainerCreating   0          2m
nginx-deployment-6c45fc49cb-sc8sh   0/1       ContainerCreating   0          2m
[root@linux-node1 ~]# kubectl describe pod nginx-deployment-6c45fc49cb-dmc22  #查看具体某个pod的状态信息
[root@linux-node1 ~]# kubectl get pod -o wide  #创建成功,状态为Running
NAME                                READY     STATUS    RESTARTS   AGE       IP          NODE
net-test-5767cb94df-djt98           1/1       Running   0          24m       10.2.73.3   192.168.56.13
net-test-5767cb94df-hcwv7           1/1       Unknown   0          36m       10.2.10.2   192.168.56.12
net-test-5767cb94df-zb8m4           1/1       Running   0          36m       10.2.73.2   192.168.56.13
nginx-deployment-6c45fc49cb-dmc22   1/1       Running   0          4m        10.2.73.6   192.168.56.13
nginx-deployment-6c45fc49cb-fd8xm   1/1       Running   0          4m        10.2.73.4   192.168.56.13
nginx-deployment-6c45fc49cb-sc8sh   1/1       Running   0          4m        10.2.73.5   192.168.56.13

[root@linux-node1 ~]#kubectl delete deployment nginx-deployment/kubectl delete -f nginx-deployment.yaml   #删除资源

[root@linux-node1 ~]# curl --head http://10.2.73.6 #测试Pod访问

HTTP/1.1 200 OK
Server: nginx/1.13.12
Date: Thu, 16 Aug 2018 08:18:14 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 09 Apr 2018 16:01:09 GMT
Connection: keep-alive
ETag: "5acb8e45-264"
Accept-Ranges: bytes
[root@linux-node1 ~]# kubectl set image deployment/nginx-deployment nginx=nginx:1.15.2 --record    #nginx的版本升级,由1.13.2升级为1.15.2,记录需要加参数--record
deployment.apps "nginx-deployment" image updated
[root@linux-node1 ~]# kubectl get deployment -o wide  #查看更新后的deployment,可以看到当前4个副本,说明还在滚动升级中
NAME               DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE       CONTAINERS   IMAGES         SELECTOR
net-test           2         2         2            2           39m       net-test     alpine         run=net-test
nginx-deployment   3         4         1            3           6m        nginx        nginx:1.15.2   app=nginx
[root@linux-node1 ~]# kubectl rollout history deployment/nginx-deployment #查看更新历史记录
deployments "nginx-deployment"
REVISION  CHANGE-CAUSE
1         <none>
2         kubectl set image deployment/nginx-deployment nginx=nginx:1.15.2 --record=true
[root@linux-node1 ~]# kubectl rollout history deployment/nginx-deployment --revision=1  #查看具体某一个版本的升级历史
deployments "nginx-deployment" with revision #1
Pod Template:
  Labels:    app=nginx
    pod-template-hash=2701970576
  Containers:
   nginx:
    Image:    nginx:1.13.12
    Port:    80/TCP
    Host Port:    0/TCP
    Environment:    <none>
    Mounts:    <none>
  Volumes:    <none>
[root@linux-node1 ~]# kubectl get pod -o wide   #查看更新后的Deployment
NAME                                READY     STATUS    RESTARTS   AGE       IP          NODE
net-test-5767cb94df-djt98           1/1       Running   0          30m       10.2.73.3   192.168.56.13
net-test-5767cb94df-hcwv7           1/1       Unknown   0          42m       10.2.10.2   192.168.56.12
net-test-5767cb94df-zb8m4           1/1       Running   0          42m       10.2.73.2   192.168.56.13
nginx-deployment-64749d4b59-djttr   1/1       Running   0          37s       10.2.73.8   192.168.56.13
nginx-deployment-64749d4b59-jp7fw   1/1       Running   0          3m        10.2.73.7   192.168.56.13
nginx-deployment-64749d4b59-q4fsn   1/1       Running   0          33s       10.2.73.9   192.168.56.13
[root@linux-node1 ~]# curl --head http://10.2.73.7  #访问某个pod
Server: nginx/1.15.2  #版本已经升级为1.15.2
Date: Thu, 16 Aug 2018 08:24:09 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 24 Jul 2018 13:02:29 GMT
Connection: keep-alive
ETag: "5b572365-264"
Accept-Ranges: bytes
[root@linux-node1 ~]# kubectl rollout undo deployment/nginx-deployment   #回滚上一个版本
deployment.apps "nginx-deployment"

查看并访问上一个版本

[root@linux-node1 ~]# kubectl get pod -o wide
NAME                                READY     STATUS    RESTARTS   AGE       IP           NODE
net-test-5767cb94df-djt98           1/1       Running   0          32m       10.2.73.3    192.168.56.13
net-test-5767cb94df-hcwv7           1/1       Unknown   0          43m       10.2.10.2    192.168.56.12
net-test-5767cb94df-zb8m4           1/1       Running   0          43m       10.2.73.2    192.168.56.13
nginx-deployment-6c45fc49cb-b9h84   1/1       Running   0          24s       10.2.73.11   192.168.56.13
nginx-deployment-6c45fc49cb-g4mrg   1/1       Running   0          26s       10.2.73.10   192.168.56.13
nginx-deployment-6c45fc49cb-k29kq   1/1       Running   0          21s       10.2.73.12   192.168.56.13
[root@linux-node1 ~]# curl --head http://10.2.73.10
HTTP/1.1 200 OK
Server: nginx/1.13.12
Date: Thu, 16 Aug 2018 08:25:35 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 09 Apr 2018 16:01:09 GMT
Connection: keep-alive
ETag: "5acb8e45-264"
Accept-Ranges: bytes
注意:回滚完成,每一次更新或者回滚ip都会变化,所以需要通过vip进行访问,这就引入了service

[root@linux-node1 ~]# vim nginx-service.yaml  #使用yaml方式创建service

kind: Service
apiVersion: v1
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80

[root@linux-node1 ~]# kubectl create -f nginx-service.yaml   #创建service

service "nginx-service" created

[root@linux-node1 ~]# kubectl get service #查看service

NAME            TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
kubernetes      ClusterIP   10.1.0.1       <none>        443/TCP   4h
nginx-service   ClusterIP   10.1.213.126   <none>        80/TCP    15s  #这个就是vip
[root@linux-node2 ~]# curl --head http://10.1.213.126  #在node2节点上进行访问vip测试,在node1上无法访问是因为没有安装kube-proxy导致无法访问。
HTTP/1.1 200 OK
Server: nginx/1.13.12
Date: Thu, 16 Aug 2018 08:30:08 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 09 Apr 2018 16:01:09 GMT
Connection: keep-alive
ETag: "5acb8e45-264"
Accept-Ranges: bytes

[root@linux-node2 ~]# ipvsadm -Ln  #查看LVS状态

IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.1.0.1:443 rr persistent 10800
  -> 192.168.56.11:6443           Masq    1      0          0         
TCP  10.1.213.126:80 rr
  -> 10.2.73.10:80                Masq    1      0          1         
  -> 10.2.73.11:80                Masq    1      0          1         
  -> 10.2.73.12:80                Masq    1      0          0 
查看LVS状态可以看到,当访问VIP:10.1.213.126时,会进行负载均衡到各个pod 

[root@linux-node1 ~]# kubectl scale deployment nginx-deployment --replicas 5

deployment.extensions "nginx-deployment" scaled

[root@linux-node1 ~]# kubectl get pod  #查看pod状态,可以看到已经增加到5个副本

NAME                                READY     STATUS    RESTARTS   AGE
net-test-5767cb94df-djt98           1/1       Running   0          38m
net-test-5767cb94df-hcwv7           1/1       Unknown   0          50m
net-test-5767cb94df-zb8m4           1/1       Running   0          50m
nginx-deployment-6c45fc49cb-b9h84   1/1       Running   0          6m
nginx-deployment-6c45fc49cb-g4mrg   1/1       Running   0          7m
nginx-deployment-6c45fc49cb-k29kq   1/1       Running   0          6m
nginx-deployment-6c45fc49cb-n9qkx   1/1       Running   0          24s
nginx-deployment-6c45fc49cb-xpx9s   1/1       Running   0          24s
[root@linux-node2 ~]# ipvsadm -Ln #路由状态
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.1.0.1:443 rr persistent 10800
  -> 192.168.56.11:6443           Masq    1      0          0         
TCP  10.1.213.126:80 rr
  -> 10.2.73.10:80                Masq    1      0          0         
  -> 10.2.73.11:80                Masq    1      0          0         
  -> 10.2.73.12:80                Masq    1      0          1         
  -> 10.2.73.13:80                Masq    1      0          0         
  -> 10.2.73.14:80                Masq    1      0          0  

需求列表:

第三方应用开发需求列表
需求描述对应shell指令
创建k8s应用,指定应用名称、镜像和副本数
kubectl run net-test --image=alpine --replicas=2 sleep 36000
kubectl create -f nginx-deployment.yaml
查看pod的状态信息
kubectl get pod -o wide
获取应用信息
kubectl get deployment net-test
查看应用详细信息
kubectl describe deployment net-test
获取副本集信息
kubectl get replicaset
查看指定副本集详细信息
kubectl describe replicaset net-test-5767cb94df
获取pod信息
kubectl get pod
查看某个pod详细信息
kubectl describe pod net-test-5767cb94df-djt98
删除某个pod
kubectl delete pods nginx-deploy-5b595999-44zwq

 

查看deployment
kubectl get deployment
查看某个deployment详情
kubectl describe deployment nginx-deployment
测试pod访问
curl --head http://10.2.73.6
升级
kubectl set image deployment/nginx-deployment nginx=nginx:1.15.2 --record
查看deployment状态信息
kubectl get deployment -o wide
查看更新历史记录
kubectl rollout history deployment/nginx-deployment
查看某版本升级历史
kubectl rollout history deployment/nginx-deployment --revision=1
回滚到上个版本
kubectl rollout undo deployment/nginx-deployment
创建service

 

kubectl create -f nginx-service.yaml

kubectl expose deployment nginx-deploy --name=nginx --port=80 --target-port=80 --protocol=TCP  #创建一个nginx的service
编辑service
vim nginx-service.yaml
查看路由信息
ipvsadm -Ln
节点扩容到5
kubectl scale deployment nginx-deployment --replicas 5
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值