模拟k8s项目的生命周期

模拟k8sngin项目生命周期

项目的生命周期,创建–》发布–》更新–》回滚–》删除

了解pod类型

  • port

port是k8s集群内部访问service的端口,即通过clusterIP: port可以访问到某个service

  • nodePort

nodePort是外部访问k8s集群中service的端口,通过nodeIP: nodePort可以从外部访问到某个service。

  • targetPort

targetPort是pod的端口,从port和nodePort来的流量经过kube-proxy流入到后端pod的targetPort上,最后进入容器。

  • containerPort

containerPort是pod内部容器的端口,targetPort映射到containerPort。

k8s群集提供外部服务图

image-20201009221340454

负载均衡功能

image-20201009222911707

Kubectl是管理k8s集群的命令行工具

[root@localhost bin]# kubectl --help
kubectl controls the Kubernetes cluster manager. 

Find more information at: https://kubernetes.io/docs/reference/kubectl/overview/

Basic Commands (Beginner):
  create         Create a resource from a file or from stdin.
  expose         使用 replication controller, service, deployment 或者 pod
并暴露它作为一个 新的 Kubernetes Service
  run            在集群中运行一个指定的镜像
  set            为 objects 设置一个指定的特征

Basic Commands (Intermediate):
  explain        查看资源的文档
  get            显示一个或更多 resources
  edit           在服务器上编辑一个资源
  delete         Delete resources by filenames, stdin, resources and names, or by resources and
label selector

Deploy Commands:
  rollout        Manage the rollout of a resource
  scale          为 Deployment, ReplicaSet, Replication Controller 或者 Job
设置一个新的副本数量
  autoscale      自动调整一个 Deployment, ReplicaSet, 或者 ReplicationController
的副本数量

Cluster Management Commands:
  certificate    修改 certificate 资源.
  cluster-info   显示集群信息
  top            Display Resource (CPU/Memory/Storage) usage.
  cordon         标记 node 为 unschedulable
  uncordon       标记 node 为 schedulable
  drain          Drain node in preparation for maintenance
  taint          更新一个或者多个 node 上的 taints

Troubleshooting and Debugging Commands:
  describe       显示一个指定 resource 或者 group 的 resources 详情
  logs           输出容器在 pod 中的日志
  attach         Attach 到一个运行中的 container
  exec           在一个 container 中执行一个命令
  port-forward   Forward one or more local ports to a pod
  proxy          运行一个 proxy 到 Kubernetes API server
  cp             复制 files 和 directories 到 containers 和从容器中复制 files 和
directories.
  auth           Inspect authorization

k8s项目创建资源

[root@localhost ~]# kubectl run nginx --image=nginx:latest --port=80 --replicas=3

image 镜像 replicsa 副本集
查看资源地址

[root@localhost ~]# kubectl get pods -o wide
NAME                                READY   STATUS    RESTARTS   AGE     IP            NODE             NOMINATED NODE
nginx-depolyment-5d648c8f47-9k2q9   1/1     Running   0          2m55s   172.17.27.3   192.168.136.30   <none>
nginx-depolyment-5d648c8f47-9p2w4   1/1     Running   0          2m55s   172.17.38.2   192.168.136.40   <none>
nginx-depolyment-5d648c8f47-frqsk   1/1     Running   0          2m55s   172.17.38.3   192.168.136.40   <none>

我们可以去接口看一下

image-20201009220227240

查看所有服务

[root@localhost ~]# kubectl get all
NAME                                    READY   STATUS    RESTARTS   AGE
pod/nginx-depolyment-5d648c8f47-9k2q9   1/1     Running   0          13m
pod/nginx-depolyment-5d648c8f47-9p2w4   1/1     Running   0          13m
pod/nginx-depolyment-5d648c8f47-frqsk   1/1     Running   0          13m

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.0.0.1     <none>        443/TCP   22h

NAME                               DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx-depolyment   3         3         3            3           13m

NAME                                          DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-depolyment-5d648c8f47   3         3         3       13m

查看是否提供服务(还没有发布服务)

[root@localhost ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.0.0.1     <none>        443/TCP   22h

发布nginx服务提供负载均衡功能

[root@localhost ~]# kubectl expose deployment nginx --port=80 --target-port=80 --name=nginx-service --type=NodePort

Deployment为Pod和ReplicaSet提供了一个声明式定义方

查看是否提供服务(有服务可以访问了)

[root@localhost ~]# kubectl get svc
NAME            TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
kubernetes      ClusterIP   10.0.0.1     <none>        443/TCP        22h
nginx-service   NodePort    10.0.0.176   <none>        80:30967/TCP   23s

image-20201009231009274

网络状态详细信息

[root@localhost ~]# kubectl get pods -o wide
NAME                     READY   STATUS    RESTARTS   AGE   IP            NODE             NOMINATED NODE
nginx-7697996758-9hqnk   1/1     Running   0          65m   172.17.38.2   192.168.136.40   <none>
nginx-7697996758-g6pfx   1/1     Running   0          65m   172.17.38.3   192.168.136.40   <none>
nginx-7697996758-z5pht   1/1     Running   0          65m   172.17.27.3   192.168.136.30   <none>

查看资源对象简写

[root@localhost bin]# kubectl api-resources

查看关联后端的节点

[root@localhost ~]# kubectl get ep
NAME            ENDPOINTS                                      AGE
kubernetes      192.168.136.60:6443,192.168.136.88:6443        23h
nginx-service   172.17.27.3:80,172.17.38.2:80,172.17.38.3:80   61m

为什么可以访问nginx?

在这里插入图片描述

创建过程

  1. kubelet取代了master执行任务创建资源
  2. port是k8s集群内部访问service的端口,targetPort是pod的端口都是80
  3. 通过kube-proxy,实现流量从service到pod的转发
  4. nodePort是外部访问k8s集群中service的端口

查看proxy负载均衡端口

kubernetes里kube-proxy支持三种模式,在v1.8之前我们使用的是iptables 以及 userspace两种模式,在kubernetes 1.8之后引入了ipvs模式

在node01操作 同样安装ipvsadmin工具查看

[root@localhost ~]# yum install ipvsadm -y
[root@localhost ~]# ipvsadm -L -n    
TCP  192.168.136.40:30001 rr
  -> 172.17.27.2:8443             Masq    1      0          0         
TCP  192.168.136.40:30967 rr
  -> 172.17.27.3:80               Masq    1      0          0         
  -> 172.17.38.2:80               Masq    1      0          0         
  -> 172.17.38.3:80               Masq    1      0          0    

在node02操作 同样安装ipvsadmin工具查看

[root@localhost ~]# yum install ipvsadm -y
[root@localhost ~]# ipvsadm -L -n
TCP  192.168.136.30:30967 rr
  -> 172.17.27.3:80               Masq    1      0          0         
  -> 172.17.38.2:80               Masq    1      0          0         
  -> 172.17.38.3:80               Masq    1      0          0       

访问地址

[root@localhost bin]# kubectl logs nginx-7697996758-9hqnk

更新nginx为1.14版本

查看版本号

image-20201010000518672

查看版本号

image-20201010000738535

更新命令

我们可以看帮助信息

[root@localhost bin]# kubectl set --help
]Configure application resources 

These commands help you make changes to existing application resources.

Available Commands:
  env            Update environment variables on a pod template
  image          更新一个 pod template 的镜像
  resources      在对象的 pod templates 上更新资源的 requests/limits
  selector       设置 resource 的 selector
  serviceaccount Update ServiceAccount of a resource
  subject        Update User, Group or ServiceAccount in a
RoleBinding/ClusterRoleBinding

Usage:
  kubectl set SUBCOMMAND [options]

Use "kubectl <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all
commands).

获取修改模板

[root@localhost bin]# kubectl set image --help
Examples:
  # Set a deployment's nginx container image to 'nginx:1.9.1', and its busybox
container image to 'busybox'.
  kubectl set image deployment/nginx busybox=busybox nginx=nginx:1.9.1

找到更新命令开始更新为1.14

[root@localhost bin]# kubectl set image deployment/nginx nginx=nginx:1.14
deployment.extensions/nginx image updated

处于动态监听状态

[root@localhost ~]# kubectl get pods -w
NAME                     READY   STATUS              RESTARTS   AGE
nginx-6ff7c89c7c-2hxbj   0/1     ContainerCreating   0          5s   创建
nginx-7697996758-9hqnk   1/1     Running             0          95m
nginx-7697996758-g6pfx   1/1     Running             0          95m
nginx-7697996758-z5pht   1/1     Running             0          95m
nginx-6ff7c89c7c-2hxbj   1/1   Running   0     38s
nginx-7697996758-g6pfx   1/1   Terminating   0     96m   删除
nginx-6ff7c89c7c-n9kwl   0/1   Pending   0     0s
nginx-6ff7c89c7c-n9kwl   0/1   Pending   0     1s
nginx-6ff7c89c7c-n9kwl   0/1   ContainerCreating   0     1s  创建
nginx-7697996758-g6pfx   0/1   Terminating   0     96m   删除
nginx-7697996758-g6pfx   0/1   Terminating   0     96m
nginx-7697996758-g6pfx   0/1   Terminating   0     96m
nginx-6ff7c89c7c-n9kwl   1/1   Running   0     23s
nginx-7697996758-9hqnk   1/1   Terminating   0     96m
nginx-6ff7c89c7c-7wmwv   0/1   Pending   0     0s
nginx-6ff7c89c7c-7wmwv   0/1   Pending   0     0s
nginx-6ff7c89c7c-7wmwv   0/1   ContainerCreating   0     0s
nginx-7697996758-9hqnk   0/1   Terminating   0     96m
nginx-7697996758-9hqnk   0/1   Terminating   0     96m
nginx-7697996758-9hqnk   0/1   Terminating   0     96m
nginx-7697996758-9hqnk   0/1   Terminating   0     96m
nginx-6ff7c89c7c-7wmwv   1/1   Running   0     18s
nginx-7697996758-z5pht   1/1   Terminating   0     96m
nginx-7697996758-z5pht   0/1   Terminating   0     96m
nginx-7697996758-z5pht   0/1   Terminating   0     97m
nginx-7697996758-z5pht   0/1   Terminating   0     97m

这里规律是先创建在删除

刷新查看是否更新成功

image-20201010002840971

回滚nginx

查看命令帮助

[root@localhost bin]# kubectl rollout --help

Manage the rollout of a resource.

 

Valid resource types include: 

 

 \* deployments  

 \* daemonsets  

 \* statefulsets

 

Examples:

 \# Rollback to the previous deployment

 kubectl rollout undo deployment/abc

 

 \# Check the rollout status of a daemonset

 kubectl rollout status daemonset/foo

 

Available Commands:

 history   显示 rollout 历史

 pause    标记提供的 resource 为中止状态

 resume    继续一个停止的 resource

 status    显示 rollout 的状态

 undo     撤销上一次的 rollout

 

Usage:

 kubectl rollout SUBCOMMAND [options]

 

Use "kubectl <command> --help" for more information about a given command.

Use "kubectl options" for a list of global command-line options (applies to all

commands).

查看历史版本

[root@localhost ~]#  kubectl rollout history deployment/nginx 
deployment.extensions/nginx 
REVISION  CHANGE-CAUSE
1         <none>
2         <none>

执行回滚

[root@localhost bin]# kubectl rollout undo deployment/nginx

deployment.extensions/nginx

检查回滚状态

[root@localhost bin]# kubectl rollout status deployment/nginx

deployment "nginx" successfully rolled out

处于动态监听状态

[root@localhost ~]# kubectl get pods -w
还是先创建在删除

image-20201010003143358

删除nginx项目

查看deployment

[root@localhost ~]#  kubectl get deploy
NAME    DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
nginx   3         3         3            3           117m

删除nginx

[root@localhost ~]# kubectl delete deployment/nginx
deployment.extensions "nginx" deleted

查看服务SVC

[root@localhost ~]# kubectl get svc
NAME            TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
kubernetes      ClusterIP   10.0.0.1     <none>        443/TCP        24h
nginx-service   NodePort    10.0.0.176   <none>        80:30967/TCP   117m

删除服务SVC

[root@localhost ~]# kubectl delete svc/nginx-service
service "nginx-service" deleted

查看已经删除

[root@localhost ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.0.0.1     <none>        443/TCP   24h

查看具体资源的详细信息

[root@localhost bin]# kubectl run nginx --image=nginx:latest --port=80 --replicas=3

kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.

deployment.apps/nginx created

查看服务

[root@localhost bin]# kubectl get pods
NAME                     READY   STATUS              RESTARTS   AGE
nginx-7697996758-2ph6x   0/1     ContainerCreating   0          13s
nginx-7697996758-jfngb   0/1     ContainerCreating   0          13s
nginx-7697996758-w4z29   0/1     ContainerCreating   0          13s

查看nginx具体资源

[root@localhost ~]# kubectl describe pod nginx-7697996758-2ph6x 
Name:               nginx-7697996758-2ph6x
Namespace:          default
Priority:           0
PriorityClassName:  <none>
Node:               192.168.136.40/192.168.136.40
Start Time:         Sat, 10 Oct 2020 00:35:12 +0800
Labels:             pod-template-hash=7697996758
                    run=nginx
Annotations:        <none>
Status:             Running
IP:                 172.17.38.3
Controlled By:      ReplicaSet/nginx-7697996758
Containers:
  nginx:
    Container ID:   docker://a7d0945872e9b9d71be44a8376df6d300384ad92fcf84b4852a90f1e831b4580
    Image:          nginx:latest
    Image ID:       docker-pullable://nginx@sha256:fc66cdef5ca33809823182c9c5d72ea86fd2cef7713cf3363e1a0b12a5d77500
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Sat, 10 Oct 2020 00:35:34 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-w6xph (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-w6xph:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-w6xph
    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  34s   default-scheduler        Successfully assigned default/nginx-7697996758-2ph6x to 192.168.136.40
  Normal  Pulling    30s   kubelet, 192.168.136.40  pulling image "nginx:latest"
  Normal  Pulled     13s   kubelet, 192.168.136.40  Successfully pulled image "nginx:latest"
  Normal  Created    13s   kubelet, 192.168.136.40  Created container
  Normal  Started    12s   kubelet, 192.168.136.40  Started container

查看deployment资源

[root@localhost bin]# kubectl describe deployment/nginx

进入pod

[root@localhost ~]# kubectl exec -it nginx-7697996758-2ph6x bash
root@nginx-7697996758-2ph6x:/# ls
bin   dev		   docker-entrypoint.sh  home  lib64  mnt  proc  run   srv  tmp  var
boot  docker-entrypoint.d  etc			 lib   media  opt  root  sbin  sys  usr
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值