创建资源对象

创建名为nginx-deploy的控制器资源对象

[root@master ~]# kubectl run nginx-deploy --image=nginx:1.12 --replicas=2
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/nginx-deploy created

[root@master ~]# kubectl get pods
NAME                           READY   STATUS              RESTARTS   AGE
client                         1/1     Running             0          7d16h
nginx-deploy-54b5756c7-72c7d   0/1     ContainerCreating   0          17s
nginx-deploy-54b5756c7-phntx   0/1     ContainerCreating   0          17s

创建名为nginx-svc的Service对象

[root@master ~]# kubectl get pods
NAME                           READY   STATUS    RESTARTS   AGE
client                         1/1     Running   0          7d16h
nginx-deploy-54b5756c7-72c7d   1/1     Running   0          2m26s
nginx-deploy-54b5756c7-phntx   1/1     Running   0          2m26s
[root@master ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   11d

[root@master ~]# kubectl expose deployment/nginx-deploy --name=nginx-svc --port=80
service/nginx-svc exposed

[root@master ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP   11d
nginx-svc    ClusterIP   10.101.181.169   <none>        80/TCP    3s
[root@master ~]# kubectl get svc -o wide
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE   SELECTOR
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP   11d   <none>
nginx-svc    ClusterIP   10.101.181.169   <none>        80/TCP    9s    run=nginx-deploy

也可以根据资源清单创建资源对象,即命令式对象配置文件,例如,假设存在定义了Deployment对象的nginx-deploy.yaml文件,和定义了Service对象的nginx-svc.yaml文件,使用kubectlcreate命令即可进行基于命令式对象配置文件的创建操作:

$kubectl create -f nginx-deploy.yaml -f nginx-svc.yaml

还可以将创建交由kubectl自行确定,用户只需要声明期望的状态,这种方式称为声明式对象配置。例如,假设存在定义了Deployment对象的nginx-deploy.yaml文件,以及定义了Service对象的nginx-svc.yaml文件,那么使用kubectlapply命令即可实现声明式配置:

$kubectl apply -f nginx-deploy.yaml -f nginx-svc.yaml
查看资源对象
# 查看service资源对象
[root@master ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP   11d
nginx-svc    ClusterIP   10.101.181.169   <none>        80/TCP    3s

# 并列出其他信息
[root@master ~]# kubectl get svc -o wide
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE   SELECTOR
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP   11d   <none>
nginx-svc    ClusterIP   10.101.181.169   <none>        80/TCP    9s    run=nginx-deploy

# 查看存在的namespace资源对象
[root@master ~]# kubectl get namespaces
NAME          STATUS   AGE
default       Active   11d
kube-public   Active   11d
kube-system   Active   11d

# 同时查看
[root@master ~]# kubectl get pods,services -o wide
NAME                               READY   STATUS    RESTARTS   AGE     IP           NODE    NOMINATED NODE   READINESS GATES
pod/client                         1/1     Running   0          7d16h   10.244.2.3   node2   <none>           <none>
pod/nginx-deploy-54b5756c7-72c7d   1/1     Running   0          11m     10.244.2.4   node2   <none>           <none>
pod/nginx-deploy-54b5756c7-phntx   1/1     Running   0          11m     10.244.1.4   node1   <none>           <none>

NAME                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE     SELECTOR
service/kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP   11d     <none>
service/nginx-svc    ClusterIP   10.101.181.169   <none>        80/TCP    6m39s   run=nginx-deploy

# 查看namespace为kube-system中标签为k8s-app的所有Pod对象
[root@master ~]# kubectl get pods -l k8s-app -n kube-system
NAME                       READY   STATUS    RESTARTS   AGE
coredns-86c58d9df4-cm4pm   1/1     Running   1          11d
coredns-86c58d9df4-g2mf4   1/1     Running   1          11d
kube-proxy-bm4g7           1/1     Running   1          10d
kube-proxy-cm88s           1/1     Running   1          10d
kube-proxy-m8wsf           1/1     Running   2          11d
打印资源对象的详细信息
[root@master ~]# kubectl describe pods -l component=kube-apiserver -n kube-system
Name:               kube-apiserver-master
Namespace:          kube-system
Priority:           2000000000
PriorityClassName:  system-cluster-critical
Node:               master/192.168.18.64
Start Time:         Mon, 14 Jan 2019 17:45:57 +0800
Labels:             component=kube-apiserver
                    tier=control-plane
                    ...
                    ...
[root@master ~]# kubectl describe service nginx-svc
Name:              nginx-svc
Namespace:         default
Labels:            run=nginx-deploy
Annotations:       <none>
Selector:          run=nginx-deploy
Type:              ClusterIP
IP:                10.102.230.186
Port:              <unset>  80/TCP
TargetPort:        80/TCP
Endpoints:         10.244.1.6:80,10.244.2.6:80
Session Affinity:  None
Events:            <none>
[root@master ~]# kubectl describe service -h
[root@master ~]# kubectl describe node -l kubernetes.io/hostname=node2
Name:               node2
Roles:              <none>
Labels:             beta.kubernetes.io/arch=amd64
                    beta.kubernetes.io/os=linux
                    kubernetes.io/hostname=node2
Annotations:        flannel.alpha.coreos.com/backend-data: {"VtepMAC":"ae:49:f9:62:b7:20"}
                    flannel.alpha.coreos.com/backend-type: vxlan
                    flannel.alpha.coreos.com/kube-subnet-manager: true
                    flannel.alpha.coreos.com/public-ip: 192.168.18.66
                    kubeadm.alpha.kubernetes.io/cri-socket: /var/run/dockershim.sock
                    node.alpha.kubernetes.io/ttl: 0
                    volumes.kubernetes.io/controller-managed-attach-detach: true
CreationTimestamp:  Fri, 11 Jan 2019 17:12:49 +0800
Taints:             <none>
Unschedulable:      false
Conditions:
  Type             Status  LastHeartbeatTime                 LastTransitionTime                Reason                       Message
  ----             ------  -----------------                 ------------------                ------                       -------
  MemoryPressure   False   Tue, 22 Jan 2019 11:26:26 +0800   Fri, 11 Jan 2019 17:12:49 +0800   KubeletHasSufficientMemory   kubelet has sufficient memory available
  DiskPressure     False   Tue, 22 Jan 2019 11:26:26 +0800   Fri, 11 Jan 2019 17:12:49 +0800   KubeletHasNoDiskPressure     kubelet has no disk pressure
  PIDPressure      False   Tue, 22 Jan 2019 11:26:26 +0800   Fri, 11 Jan 2019 17:12:49 +0800   KubeletHasSufficientPID      kubelet has sufficient PID available
  Ready            True    Tue, 22 Jan 2019 11:26:26 +0800   Fri, 11 Jan 2019 17:25:56 +0800   KubeletReady                 kubelet is posting ready status
Addresses:
  InternalIP:  192.168.18.66
  Hostname:    node2
Capacity:
 cpu:                1
 ephemeral-storage:  17394Mi
 hugepages-1Gi:      0
 hugepages-2Mi:      0
 memory:             1867264Ki
 pods:               110
Allocatable:
 cpu:                1
 ephemeral-storage:  16415037823
 hugepages-1Gi:      0
 hugepages-2Mi:      0
 memory:             1764864Ki
 pods:               110
System Info:
 Machine ID:                 03e8bf0f8b4c4319b2b27dff38620ba4
 System UUID:                3F704D56-210E-A7C5-4544-32704D0FB1A2
 Boot ID:                    9dd190ac-8ff7-4a34-afc3-ff66f57f2c04
 Kernel Version:             3.10.0-514.el7.x86_64
 OS Image:                   CentOS Linux 7 (Core)
 Operating System:           linux
 Architecture:               amd64
 Container Runtime Version:  docker://18.9.0
 Kubelet Version:            v1.13.1
 Kube-Proxy Version:         v1.13.1
PodCIDR:                     10.244.2.0/24
Non-terminated Pods:         (4 in total)
  Namespace                  Name                            CPU Requests  CPU Limits  Memory Requests  Memory Limits  AGE
  ---------                  ----                            ------------  ----------  ---------------  -------------  ---
  default                    client                          0 (0%)        0 (0%)      0 (0%)           0 (0%)         7d17h
  default                    nginx-deploy-54b5756c7-btwm4    0 (0%)        0 (0%)      0 (0%)           0 (0%)         16m
  kube-system                kube-flannel-ds-amd64-h9f9b     100m (10%)    100m (10%)  50Mi (2%)        50Mi (2%)      10d
  kube-system                kube-proxy-cm88s                0 (0%)        0 (0%)      0 (0%)           0 (0%)         10d
Allocated resources:
  (Total limits may be over 100 percent, i.e., overcommitted.)
  Resource           Requests    Limits
  --------           --------    ------
  cpu                100m (10%)  100m (10%)
  memory             50Mi (2%)   50Mi (2%)
  ephemeral-storage  0 (0%)      0 (0%)
Events:              <none>
打印容器中的日志信息
[root@master ~]# kubectl logs kube-apiserver-master -n kube-system
Flag --insecure-port has been deprecated, This flag will be removed in a future version.
I0114 09:45:58.640525       1 server.go:557] external host was not specified, using 192.168.18.64
I0114 09:45:58.640746       1 server.go:146] Version: v1.13.1
I0114 09:45:59.399233       1 plugins.go:158] Loaded 8 mutating admission controller(s) successfully in the following order: NamespaceLifecycle,LimitRanger,ServiceAccount,NodeRestriction,Priority,DefaultTolerationSeconds,DefaultStorageClass,MutatingAdmissionWebhook.
...
...

为上面命令加上-f选项,可以持续监控指定容器中的日志输出

在容器中执行命令
 kubectl exec POD [-c CONTAINER] -- COMMAND [args...] [options]

例如

[root@master ~]# kubectl exec kube-apiserver-master -n kube-system -- ls
bin
boot
dev
etc
...
...
删除资源对象
[root@master ~]# kubectl delete deployments -l run=myapp
deployment.extensions "myapp" deleted

删除受控于控制器的对象来说,删除之后控制器会重建出类似的对象,所以删除此类对象可以考虑删除相关控制器

这里可以参考 kubectl delete -h

kubernetes常用基础命令的更多相关文章

  1. Linux常用基础命令整理:关机命令、查看目录下文件命令等

    Linux常用基础命令整理:关机命令.查看目录下文件命令等 整理了一些Linux常用基础命令,欢迎指正. 首先记住四个热键,学会这四个键,收益一辈子. Tab按键---命令补齐功能Ctrl+c按键-- ...

  2. CentOS常用基础命令大全

    这篇文章主要介绍了CentOS常用基础命令大全,学习centos的朋友需要掌握的知识,需要的朋友可以参考下 1.关机 (系统的关机.重启以及登出 ) 的命令shutdown -h now 关闭系统(1 ...

  3. MySQL数据库入门常用基础命令

    MySQL数据库入门———常用基础命令      数据——公司的生命线,因此在大多数的互联网公司,都在使用开源的数据库产品,MySQL也因此关注度与使用率非常的高,所以做为运维的屌丝们,掌握它的一些基 ...

  4. Docker 使用笔记-常用基础命令

    常用基础命令 环境:Linux Ubuntu 1.查看docker信息 docker version | docker info 2.启动docker服务 sudo systemctl start d ...

  5. Linux常用基础命令(二)

    Linux常用基础命令 一.-ls--列表显示目录内容 二.-alias--设置别名 三.-du--统计目录及文件空间占用情况 四.-mkdir--创建新目录 五.-touch--创建空文件 六.-l ...

  6. 前后端同学,必会的Linux常用基础命令

    无论是前端还是后端同学,一些常用的linux命令还是必须要掌握的.发布版本.查看日志等等都会用到.以下是我简单的总结了一些简单又常用的命令,欢迎大家补充.希望能帮助到大家 本文首发于公众号 程序员共成 ...

  7. Docker 容器安装及常用基础命令

    为什么用docker 作为一种新兴的虚拟化方式,Docker 跟传统的虚拟化方式相比具有众多的优势. Docker 在如下几个方面具有较大的优势: 更快速的交付和部署 Docker在整个开发周期都可以 ...

  8. MySQL 常用基础命令

    一.启动与关闭 1.1 Linux下启动mysql 的命令: a. rpm包安装:service mysqld start b. 源码包安装:/usr/local/mysql/bin/mysqld_s ...

  9. windows CMD常用基础命令

    一.ping 主要是测试本机TCP/IP协议配置正确性与当前网络现状. ping命令的基本使用格式是: ping IP地址/主机名/域名 [-t] [-a] [-n count] [-l size] ...

随机推荐

  1. SQL点点滴滴_判断字段或者字符中是否包含有特殊字符

    SQL Server中,如果我们想判断一个字符串或者数据字段中是否包含有特殊字符.可以使用正则来实现.除了大小字母和数字之外全是特殊字符[^a-zA-Z0-9]

  2. Scratch-Blockly配置过程

    原文地址:https://blog.csdn.net/litianquan/article/details/82735876 Scratch-Blockly配置过程 由于Blockly案例库开发项目需 ...

  3. C#的抽象类和接口的区别,在什么时候使用才合适?

    理解抽象类  abstract class和interface在c#语言中都是用来进行抽象类(本文 中的抽象类并非从abstract class翻译而来,它表示的是一个抽象体,而abstract cl ...

  4. [翻译] WZFlashButton

    WZFlashButton This is a custom button with flash-like effect. Inspired by Ripple Effect. 这是一个自定义的按钮, ...

  5. vs 编译error1083

    1)右键查看该项目的属性 2)点击配置属性——〉  C/C++  ——〉  常规  ——〉 附加包含目录——〉将报错文件所在目录添加进去 3) 将项目的本地路径替换为工程相对路径 一般来说,打不开文件 ...

  6. Centos7下面安装eclipse

    下载Eclipse压缩包 下载路径 http://mirrors.neusoft.edu.cn/eclipse/technology/epp/downloads/release/luna/SR2/ec ...

  7. August 28th 2017 Week 35th Monday

    The truth may hurt for a little while but a lie hurts forever. 真相会让我们痛一阵,但谎言会让我们痛一生. Once you make a ...

  8. [转]unix/linux中的dup()系统调用

    [转]unix/linux中的dup()系统调用    在linux纷繁复杂的内核代码中,sys_dup()的代码也许称得上是最简单的之一了,但是就是这么一个简单的系统调用,却成就了unix/linu ...

  9. .NET正则表达式Regex

    一.IsMatch(Input,patter[,options]) 否则匹配 如果表达式在字符串中匹配,返回布尔值. if (Regex.IsMatch("a.b.c.d", @& ...

  10. 网络Socket编程(简易qq实现之C/S通信1)

    1. 目标:实现两个用户之间的通信,利用的是简单的Socket知识以及简略界面 2. 界面:分为客户端与服务器端(如下图) 3. 基本功能:客户端先向服务器端发送一个消息,这样就可以让客户端与服务器端 ...