k8s-kubectl 命令管理(常规命令操作集锦)

文章目录

  • 前言
  • 一、kubectl管理
    • 使用kubectl命令管理项目的生命周期
    • 1.1 项目周期
      • 1:创建nginx
      • 2:发布nginx
      • 3:更新nginx 为1.14版本
      • 4:回滚nginx
      • 5:删除nginx,查看deployment(控制器--定位资源的类型)

前言

k8s管理资源的方法:

  • 1:kubectl 命令管理
    项目周期:创建—》发布—》更新—》回滚—》删除

  • 2:文件形式管理 yaml json

  • 3:harbor对接K8S pod管理

一、kubectl管理

Kubectl是管理k8s集群的命令行工具,通过生成的json格式传递给apiserver进行创建、查看、管理的操作。

  • 更全的kubectl命令请查看kubectl --help
[root@master dashboard]# 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  #(设置标记,要配合taint使用,taint:污点)
uncordon 标记 node 为 schedulable
drain Drain node in preparation for maintenance  #(一个清除服务)
taint 更新一个或者多个 node 上的 taints   #(一旦被标记为污点状态,则pod不会在这个节点创建)

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  #(验证)

Advanced Commands:  #(高级指令)
apply 通过文件名或标准输入流(stdin)对资源进行配置  #(资源创建、更新、业务升级)
patch 使用 strategic merge patch 更新一个资源的 field(s)  #(更新一个指令性资源)
replace 通过 filename 或者 stdin替换一个资源  #(替换)
wait Experimental: Wait for a specific condition on one or many resources.  #(等待)
convert 在不同的 API versions 转换配置文件  #(做二次开发的时候写参数)

Settings Commands:
label 更新在这个资源上的 labels   #(更新一个资源到备注,打注释)
annotate 更新一个资源的注解
completion Output shell completion code for the specified shell (bash or zsh)   #(输出代码到指定的bash环境下)

Other Commands:
alpha Commands for features in alpha
api-resources Print the supported API resources on the server   #(告诉你哪一条指令可以缩写,kubectl api-resources)
api-versions Print the supported API versions on the server, in the form of "group/version"
config 修改 kubeconfig 文件
plugin Provides utilities for interacting with plugins.   #(插件)
version 输出 client 和 server 的版本信息

Usage:
kubectl [flags] [options]

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

使用kubectl命令管理项目的生命周期

  • 项目的生命周期:创建、发布、更新、回滚、删除
  • 创建 kubectl run命令
格式:
kubectl run NAME --image=image [--env="key=value"] [--port=port] [--replicas=replicas]
[--dry-run=bool] [--overrides=inline-json] [--command] -- [COMMAND] [args...] [options]

创建一个nginx的pod

[root@master ~]# 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

动态查看过程 -w(Ctrl+C 中断监控)

[root@master ~]# kubectl get pods -w
NAME                     READY   STATUS              RESTARTS   AGE
nginx-7697996758-7ffmp   0/1     ContainerCreating   0          4s
nginx-7697996758-f254k   0/1     ContainerCreating   0          4s
nginx-7697996758-jspdw   0/1     ContainerCreating   0          4s
nginx-7697996758-jspdw   1/1   Running   0     16s
nginx-7697996758-7ffmp   1/1   Running   0     17s
nginx-7697996758-f254k   1/1   Running   0     32s

查看更详细的信息,查看资源创建在那个节点上

[root@master ~]# kubectl get pods -o wide
NAME                     READY   STATUS    RESTARTS   AGE   IP            NODE        NOMINATED NODE
nginx-7697996758-7ffmp   1/1     Running   0          97s   172.17.41.2   14.0.0.55   <none>
nginx-7697996758-f254k   1/1     Running   0          97s   172.17.41.3   14.0.0.55   <none>
nginx-7697996758-jspdw   1/1     Running   0          97s   172.17.55.2   14.0.0.33   <none>

查看更多资源:(deploy 控制器资源、rs 副本集资源)

[root@master ~]# kubectl get pods,svc,deploy,rs -o wide
NAME                         READY   STATUS    RESTARTS   AGE   IP            NODE        NOMINATED NODE
pod/nginx-7697996758-7ffmp   1/1     Running   0          41m   172.17.41.2   14.0.0.55   <none>
pod/nginx-7697996758-f254k   1/1     Running   0          41m   172.17.41.3   14.0.0.55   <none>
pod/nginx-7697996758-jspdw   1/1     Running   0          41m   172.17.55.2   14.0.0.33   <none>

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

NAME                          DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
deployment.extensions/nginx   3         3         3            3           41m   nginx        nginx:latest   run=nginx

NAME                                     DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES         SELECTOR
replicaset.extensions/nginx-7697996758   3         3         3       41m   nginx        nginx:latest   pod-template-hash=7697996758,run=nginx

也可以kubectl get all

[root@master ~]# kubectl get all -o wide
NAME                         READY   STATUS    RESTARTS   AGE   IP            NODE        NOMINATED NODE
pod/nginx-7697996758-7ffmp   1/1     Running   0          42m   172.17.41.2   14.0.0.55   <none>
pod/nginx-7697996758-f254k   1/1     Running   0          42m   172.17.41.3   14.0.0.55   <none>
pod/nginx-7697996758-jspdw   1/1     Running   0          42m   172.17.55.2   14.0.0.33   <none>

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

NAME                    DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
deployment.apps/nginx   3         3         3            3           42m   nginx        nginx:latest   run=nginx

NAME                               DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES         SELECTOR
replicaset.apps/nginx-7697996758   3         3         3       42m   nginx        nginx:latest   pod-template-hash=7697996758,run=nginx
  • 删除
[root@master ~]# kubectl delete deploy/nginx
deployment.extensions "nginx" deleted
[root@master ~]# kubectl get all
NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.0.0.1     <none>        443/TCP   11d

##service会保留下来,因为是单独的功能
[root@master ~]# kubectl delete service/kubernetes
service "kubernetes" deleted
[root@master ~]# kubectl get all
No resources found.

1.1 项目周期

1:创建nginx

[root@master ~]# 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@master ~]# kubectl get pods,deployment,rs,svc
NAME                         READY   STATUS    RESTARTS   AGE
pod/nginx-7697996758-6g2w5   1/1     Running   0          59s
pod/nginx-7697996758-jl2qk   1/1     Running   0          59s
pod/nginx-7697996758-s2mts   1/1     Running   0          59s

NAME                          DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
deployment.extensions/nginx   3         3         3            3           59s

NAME                                     DESIRED   CURRENT   READY   AGE
replicaset.extensions/nginx-7697996758   3         3         3       59s

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

2:发布nginx

service提供负载均衡的功能(相当于建立端口映射)
语法:

kubectl expose (-f FILENAME | TYPE NAME) [--port=port] [--protocol=TCP|UDP|SCTP]
[--target-port=number-or-name] [--name=name] [--external-ip=external-ip-of-service] [--type=type]
[root@master ~]# kubectl expose deployment nginx --port=80 --name=nginx-service --type=NodePort
service/nginx-service exposed

[root@master ~]# kubectl get all
NAME                         READY   STATUS    RESTARTS   AGE
pod/nginx-7697996758-6g2w5   1/1     Running   0          19m
pod/nginx-7697996758-jl2qk   1/1     Running   0          19m
pod/nginx-7697996758-s2mts   1/1     Running   0          19m

NAME                    TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
service/kubernetes      ClusterIP   10.0.0.1     <none>        443/TCP        20m
service/nginx-service   NodePort    10.0.0.100   <none>        80:38448/TCP   3m41s

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

NAME                               DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-7697996758   3         3         3       19m

//查看资源对象简写
[root@master ~]# kubectl api-resources

//查看关联后端的节点
[root@master ~]# kubectl get endpoints
NAME            ENDPOINTS                                      AGE
kubernetes      14.0.0.11:6443,14.0.0.66:6443                  21m
nginx-service   172.17.41.2:80,172.17.41.3:80,172.17.55.2:80   5m12s

//网络状态详细信息
[root@master ~]# kubectl get pods -o wide
NAME                     READY   STATUS    RESTARTS   AGE   IP            NODE        NOMINATED NODE
nginx-7697996758-6g2w5   1/1     Running   0          24m   172.17.41.3   14.0.0.55   <none>
nginx-7697996758-jl2qk   1/1     Running   0          24m   172.17.41.2   14.0.0.55   <none>
nginx-7697996758-s2mts   1/1     Running   0          24m   172.17.55.2   14.0.0.33   <none>

//服务暴露的端口
[root@master ~]# kubectl get svc
NAME            TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
kubernetes      ClusterIP   10.0.0.1     <none>        443/TCP        27m
nginx-service   NodePort    10.0.0.100   <none>        80:38448/TCP   11m
  • 在node01操作,查看负载均衡端口38804
    • kubernetes里kube-proxy支持三种模式,在v1.8之前我们使用的是iptables 以及 userspace两种模式,在kubernetes 1.8之后引入了ipvs(负载均衡)模式
[root@node01 ~]# yum install ipvsadm -y

##查看负载均衡
[root@node01 ~]# ipvsadm -L -n
......
TCP  14.0.0.33:38448 rr
  -> 172.17.41.2:80               Masq    1      0          0         
  -> 172.17.41.3:80               Masq    1      0          0         
  -> 172.17.55.2:80               Masq    1      0          0      
  • 在node02操作 同样安装ipvsadmin工具查看
[root@node02 ~]# yum install ipvsadm -y
[root@node02 ~]# ipvsadm -L -n
.......
TCP  14.0.0.55:38448 rr
  -> 172.17.41.2:80               Masq    1      0          0         
  -> 172.17.41.3:80               Masq    1      0          0         
  -> 172.17.55.2:80               Masq    1      0          0        
  • 在master01操作 查看访问日志(注意:如果访问其他node无法访问检查proxy组件
    • 网页访问:14.0.0.33:38448
[root@master ~]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-7697996758-6g2w5   1/1     Running   0          61m
nginx-7697996758-jl2qk   1/1     Running   0          61m
nginx-7697996758-s2mts   1/1     Running   0          61m
[root@master ~]# kubectl logs nginx-7697996758-6g2w5
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
[root@master ~]# kubectl logs nginx-7697996758-jl2qk
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
[root@master ~]# kubectl logs nginx-7697996758-s2mts
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
172.17.55.1 - - [11/Oct/2020:11:50:28 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36" "-"
2020/10/11 11:50:28 [error] 28#28: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 172.17.55.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "14.0.0.33:38448", referrer: "http://14.0.0.33:38448/"
172.17.55.1 - - [11/Oct/2020:11:50:28 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "http://14.0.0.33:38448/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36" "-"

3:更新nginx 为1.14版本

  • 谷歌浏览器重新加载刷新页面查看nginx版本信息
    k8s-kubectl 命令管理(常规命令操作集锦)_第1张图片
    k8s-kubectl 命令管理(常规命令操作集锦)_第2张图片
[root@master ~]# 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  --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all commands).
  • 获取修改模板
[root@master ~]# 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


[root@master ~]# kubectl set image deployment/nginx nginx=nginx:1.14
deployment.extensions/nginx image updated
  • 处于动态监听状态(滚动更新:先创建再销毁
[root@master ~]# kubectl get pods -w
NAME                     READY   STATUS              RESTARTS   AGE
nginx-6ff7c89c7c-vw8ld   0/1     ContainerCreating   0          1s
nginx-779989c576-7b8jt   1/1     Running             0          70s
nginx-779989c576-klkwn   1/1     Running             0          59s
nginx-779989c576-z9gmj   1/1     Running             0          82s
nginx-6ff7c89c7c-vw8ld   1/1   Running   0     2s
nginx-779989c576-klkwn   1/1   Terminating   0     60s
nginx-6ff7c89c7c-r5xvd   0/1   Pending   0     0s
nginx-6ff7c89c7c-r5xvd   0/1   Pending   0     0s
nginx-6ff7c89c7c-r5xvd   0/1   ContainerCreating   0     0s
nginx-779989c576-klkwn   0/1   Terminating   0     61s
nginx-6ff7c89c7c-r5xvd   1/1   Running   0     2s
nginx-779989c576-7b8jt   1/1   Terminating   0     73s
nginx-6ff7c89c7c-qvcrv   0/1   Pending   0     0s
nginx-779989c576-klkwn   0/1   Terminating   0     62s
nginx-6ff7c89c7c-qvcrv   0/1   Pending   0     0s
nginx-779989c576-klkwn   0/1   Terminating   0     62s
nginx-6ff7c89c7c-qvcrv   0/1   ContainerCreating   0     0s
nginx-779989c576-7b8jt   0/1   Terminating   0     74s
nginx-779989c576-7b8jt   0/1   Terminating   0     75s
nginx-779989c576-7b8jt   0/1   Terminating   0     75s
nginx-6ff7c89c7c-qvcrv   1/1   Running   0     2s
nginx-779989c576-z9gmj   1/1   Terminating   0     87s
nginx-779989c576-z9gmj   0/1   Terminating   0     88s
nginx-779989c576-z9gmj   0/1   Terminating   0     95s
nginx-779989c576-z9gmj   0/1   Terminating   0     95s

#Ctrl+c中断监听,更新速度快
^C[root@master ~]# kubectget pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-6ff7c89c7c-qvcrv   1/1     Running   0          45s
nginx-6ff7c89c7c-r5xvd   1/1     Running   0          47s
nginx-6ff7c89c7c-vw8ld   1/1     Running   0          49s

刷新页面:
k8s-kubectl 命令管理(常规命令操作集锦)_第3张图片

4:回滚nginx

[root@master ~]# 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  --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all commands).
  • 查看历史版本
[root@master ~]# kubectl rollout history deployment/nginx
deployment.extensions/nginx 
REVISION  CHANGE-CAUSE
1         <none>
2         <none>
  • 执行回滚
[root@master ~]# kubectl rollout undo deployment/nginx
deployment.extensions/nginx

[root@master ~]# kubectl get pods -w
NAME                     READY   STATUS              RESTARTS   AGE
nginx-6ff7c89c7c-r5xvd   1/1     Running             0          10m
nginx-6ff7c89c7c-vw8ld   1/1     Running             0          10m
nginx-779989c576-dns5t   1/1     Running             0          5s
nginx-779989c576-pwgtq   0/1     ContainerCreating   0          2s
nginx-779989c576-pwgtq   1/1   Running   0     3s
nginx-6ff7c89c7c-r5xvd   1/1   Terminating   0     10m
nginx-779989c576-dm4d7   0/1   Pending   0     0s
nginx-779989c576-dm4d7   0/1   Pending   0     0s
nginx-779989c576-dm4d7   0/1   ContainerCreating   0     0s
nginx-6ff7c89c7c-r5xvd   0/1   Terminating   0     10m
nginx-779989c576-dm4d7   1/1   Running   0     3s
nginx-6ff7c89c7c-vw8ld   1/1   Terminating   0     10m
nginx-6ff7c89c7c-vw8ld   0/1   Terminating   0     10m
nginx-6ff7c89c7c-vw8ld   0/1   Terminating   0     10m
nginx-6ff7c89c7c-vw8ld   0/1   Terminating   0     10m
nginx-6ff7c89c7c-vw8ld   0/1   Terminating   0     10m
nginx-6ff7c89c7c-r5xvd   0/1   Terminating   0     10m
nginx-6ff7c89c7c-r5xvd   0/1   Terminating   0     10m
  • 检查回滚状态
[root@master ~]# kubectl rollout status deployment/nginx
deployment "nginx" successfully rolled out

k8s-kubectl 命令管理(常规命令操作集锦)_第4张图片

5:删除nginx,查看deployment(控制器–定位资源的类型)

[root@master ~]# kubectl get deploy
NAME    DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
nginx   3         3         3            3           110m
[root@master ~]# kubectl delete deployment/nginx
deployment.extensions "nginx" deleted
[root@master ~]# kubectl get deploy
No resources found.
[root@master ~]# kubectl get pods
No resources found.
  • 删除服务SVC
[root@master ~]# kubectl get svc
NAME            TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
kubernetes      ClusterIP   10.0.0.1     <none>        443/TCP        113m
nginx-service   NodePort    10.0.0.100   <none>        80:38448/TCP   97m
[root@master ~]# kubectl delete svc/nginx-service
service "nginx-service" deleted
[root@master ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.0.0.1     <none>        443/TCP   114m
  • 查看具体资源的详细信息
[root@master ~]# 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@master ~]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-7697996758-kvt4r   1/1     Running   0          31s
nginx-7697996758-pk2cw   1/1     Running   0          31s
nginx-7697996758-v44c9   1/1     Running   0          31s

[root@master ~]# kubectl describe pod nginx-7697996758-kvt4r
Name:               nginx-7697996758-kvt4r
Namespace:          default
Priority:           0
PriorityClassName:  <none>
Node:               14.0.0.55/14.0.0.55
Start Time:         Sun, 11 Oct 2020 20:33:00 +0800
Labels:             pod-template-hash=7697996758
                    run=nginx
Annotations:        <none>
Status:             Running
IP:                 172.17.41.2
Controlled By:      ReplicaSet/nginx-7697996758
Containers:
  nginx:
    Container ID:   docker://19f64eacc4ba88315c4daa6ba975b73574985d9da2422d99a8d5cbd9b80bc3d4
    Image:          nginx:latest
    Image ID:       docker-pullable://nginx@sha256:fc66cdef5ca33809823182c9c5d72ea86fd2cef7713cf3363e1a0b12a5d77500
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Sun, 11 Oct 2020 20:33:02 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-x5xpc (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-x5xpc:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-x5xpc
    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  100s  default-scheduler   Successfully assigned default/nginx-7697996758-kvt4r to 14.0.0.55
  Normal  Pulling    99s   kubelet, 14.0.0.55  pulling image "nginx:latest"
  Normal  Pulled     98s   kubelet, 14.0.0.55  Successfully pulled image "nginx:latest"
  Normal  Created    98s   kubelet, 14.0.0.55  Created container
  Normal  Started    98s   kubelet, 14.0.0.55  Started container
  • 查看deployment资源
[root@master ~]# kubectl describe deployment/nginx
Name:                   nginx
Namespace:              default
CreationTimestamp:      Sun, 11 Oct 2020 20:33:00 +0800
Labels:                 run=nginx
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               run=nginx
Replicas:               3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  run=nginx
  Containers:
   nginx:
    Image:        nginx:latest
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   nginx-7697996758 (3/3 replicas created)
Events:
  Type    Reason             Age    From                   Message
  ----    ------             ----   ----                   -------
  Normal  ScalingReplicaSet  4m38s  deployment-controller  Scaled up replica set nginx-7697996758 to 3
  • 进入pod
[root@master ~]# kubectl exec -it nginx-7697996758-kvt4r bash
root@nginx-7697996758-kvt4r:/# 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
root@nginx-7697996758-kvt4r:/# 

你可能感兴趣的:(k8s,docker,kubernetes)