kubernetes基础用法

什么是pod?

  • pod相当于逻辑主机,每个pod都有自己的ip地址
  • pod内的容器共享相同的ip和端口空间
  • 默认情况下,每个容器的文件系统与其他容器完全隔离
  • 可以理解为:容器组,同时pod相当于逻辑主机,进入pod后仿佛进入一个linux主机,命令都可用(linux系统下),该“主机”内又有很多容器,进入后又仿佛是又进了一个linux主机。

pod分类

pod分为两种类型:

  • 自主式pod
  • 控制器管理的pod
  1. 自我管理的pod,创建以后仍然需要提交给apiserver,由apiserver接收以后借助于调度器将其调度至指定的node节点,由node启动此pod,如果此pod出现故障,需要重启容器则由kubelet来完成;如果node节点故障了,那么此pod将会消失。其无法实现全局调度。所以不推荐使用此种pod。

  2. Pod控制器是管理pod的中间层,使用Pod控制器之后,只需要告诉Pod控制器,想要多少个什么样的Pod就可以了,它会创建出满足条件的Pod并确保每一个Pod资源处于用户期望的目标状态。如果Pod资源在运行中出现故障,它会基于指定策略重新编排Pod

常见的pod控制器

replicationController:当启动一个pod时,这个pod如果不够用可以再启一个副本,而后由控制器来管理同一类pod的各种副本与对象。一旦副本少了就会自动增加。采取多退少补的规则,精确符合我们所定义的期望;支持滚动更新。(比较原始的pod控制器,已经被废弃,由ReplicaSet替代)

  • replicaset:由一个名叫Deployment的声明式更新的控制器来管理
  • Deployment:Deployment只能管理无状态的应用
  • StateFulSet:有状态副本集,可以管理有状态的应用
  • DaemonSet:如果需要在每个node上运行一个副本的时候可以用DaemonSet
  • Job:它创建出来的pod只要完成任务就立即退出,不需要重启或重建,用于执行一次性任务
  • Cronjob:它创建的Pod负责周期性任务控制,不需要持续后台运行
    以上所有控制器都是用来实现一种特定的应用管理的。

核心组件

在这里插入图片描述

HPA

Deployment还支持二级控制器HPA(HorizontalPodAutoscaler,水平pod自动伸缩控制器)一般情况下我们可以确保一个node上有2个pod运行,万一用户访问流量增加,2个pod不足以承受这么多的流量怎么办?此时我们就应该要增加pod资源,那么到底应该加几个?HPA控制器可自动监控pod、自动进行扩展。

service

假如有2个pod,pod有其生命周期,万一pod所在的节点宕机了,那么此pod将应该要在其他的节点上重建,而重建完的pod与原来pod已经不是同一个pod了,只是两者都是运行的同一服务而已。且每个容器都有其IP地址,重建的pod中的容器其IP地址与之前的pod中容器的IP地址是不一样的,如此一来就会存在一个问题,客户端如何访问这些pod中的容器呢?

措施:服务发现:就比如集贸市场的注册摊位和声明地址,注册摊位就是买东西的摊位,有一天这个摊位的商贩换地方了,就会在原来的摊位上留下一个摊位声明告诉顾客自己换地方了,但是它出售的商品还是一样的。只是换个地方买而已,这就是服务发现。

pod是有生命周期的,一个pod随时都有可能离去,随时都有可能会有其他pod加入进来,假如它们提供的同一种服务,客户端是无法通过固定的手段来访问这些pod的,因为pod本身是不固定的,它们随时可能被替换掉,无论使用主机名还是IP地址,都随时会被替换掉。为了尽可能的降低客户端与pod间协调的复杂度,k8s为每一组提供同类服务的pod和其客户端之间添加了一个中间层,这个中间层是固定的,这个中间层就叫service。
service只要不被删除,其地址与名称皆是固定的,当客户端需要在其配置文件中写上访问某个服务时,它不再需要自动发现,只需要在配置文件中写明service的名称即可,而这个service是个调度器,其不但能够提供一个稳定的访问入口,还可以做反向代理,当service接收到客户端的请求后,会将其代理到后端的pod之上,一旦pod宕机了会立即新建一个pod,这个新建的pod会立即被service关联上,作为service后端的可用pod之一。

客户端程序访问服务都是通过IP+端口号或主机名+端口的方式来实现的。而service关联后端的pod不是靠它的IP和主机名。而是靠pod的标签选择器。只要其在service所识别。如此一来,只要pod属于标签选择器,只要其在service的管理范围之内,则其就会被关联到service中,当这个动态的pod关联到service中,当这个动态的pod关联到service中之后,再进行动态的探测此pod的IP地址、端口号,再将其作为自己后端可调度的可用服务器主机对象。因此,客户端的请求发送到service,然后service代理到后端真实的pod中的容器进行响应。

service不是一个程序,也不是一个组件,它只是一个iptables的dnat规则,service作为k8s的对象,有其自身的名称,而service的名称相当于服务的名称,而这个名称可以被解析。

网络模型

  • 节点网络
  • service网络
  • pod网络

k8s的三种网络模型分属于三个网段,由此延伸出来三个问题
同一个pod内的多个容器间如何通信?
lo网卡进行通信,因为同一个pod内的容器共用一个网路名称空间,而且在同一个网段。
各pod间如何通信?

  • 物理桥接,但是在一定规模下容易产生网络风暴,不建议使用
  • Overlay Network 通过隧道的方式转发报文
  • pod与service间如何通信?首先各节点之间是相互通信的,节点也就是真机之间的通信,因为service网络是一个iptables规则,且与真机是相连的,而pod和service是我们初始化的时候通过flannel网络进行互联,且属于同一网段。

kubectl命令的使用

create

# 语法
kubectl create deployment NAME --image=image -- [COMMAND] [args...]  
kubectl create deployment NAME --image=image -- [COMMAND] [args...]  //格式

# 使用busybox镜像创建一个test1的pod
[root@master ~]# kubectl create deployment test1 --image busybox
deployment.apps/test1 created

# 可以看到处于退出状态,因为busybox使用的是sh,没有任务就会退出
[root@master ~]# kubectl get pod
NAME                     READY   STATUS         RESTARTS   AGE
nginx-85b98978db-c2g4t   1/1     Running        0          11h
test1-78659499b7-46r6l   0/1     ErrImagePull   0          17s
[root@master ~]# kubectl create deployment test2 --image busybox -- sleep 600
deployment.apps/test2 created
test2-cdc77f4b6-g77t6    1/1     Running            0             4m33s

# 创建使用nginx镜像创建三个pod,名字为web
[root@master ~]# kubectl create deployment web --image nginx --replicas 3
deployment.apps/web created
[root@master ~]# kubectl get pod
NAME                     READY   STATUS              RESTARTS      AGE
web-76b56fd968-bgpbr     0/1     ContainerCreating   0             5s
web-76b56fd968-qf5kj     0/1     ContainerCreating   0             5s
web-76b56fd968-s8c9b     0/1     ContainerCreating   0             5s

# 查看pod运行的节点位置
[root@master ~]# kubectl get pods -o wide
NAME                     READY   STATUS              RESTARTS        AGE     IP           NODE                NOMINATED NODE   READINESS GATES
web-76b56fd968-bgpbr     1/1     Running             0               4m16s   10.244.1.6   node1.example.com   <none>           <none>
web-76b56fd968-qf5kj     1/1     Running             0               4m16s   10.244.1.5   node1.example.com   <none>           <none>
web-76b56fd968-s8c9b     0/1     ContainerCreating   0               4m16s   <none>       node2.example.com   <none>           <none>

# 暴露80端口号
[root@master ~]# kubectl create deployment web01 --image nginx --port=80
deployment.apps/web01 created

run

# 启动一个 nginx pod
[root@master ~]# kubectl run nginx --image nginx
pod/nginx created
# 删除nginx的pod
[root@master ~]#  kubectl get pods -o wide
NAME                     READY   STATUS             RESTARTS   AGE   IP           NODE                NOMINATED NODE   READINESS GATES
nginx                    1/1     Running            0          73s   10.244.1.2   node1.example.com   <none>           <none>
[root@master ~]# kubectl delete pods nginx
pod "nginx" deleted

# 暴露容器的80端口号
[root@master ~]# kubectl run nginx --images nginx --port 80  

# 在容器中设置标签“app=nginx”和“env=prod”
[root@master ~]# kubectl run nginx --image nginx --labels "aap=nginx,env=prod"
pod/nginx created

# 描述nginx信息
[root@master ~]# kubectl describe pod nginx  

#不会真正运行
[root@master ~]# kubectl run nginx --image nginx --dry-run server
W1219 23:07:34.368572   85965 helpers.go:553] --dry-run is deprecated and can be replaced with --dry-run=client.
pod/nginx created (dry run)

delete

[root@master ~]# kubectl get pod
NAME                     READY   STATUS             RESTARTS        AGE
nginx                    1/1     Running            0               3m3s
nginx-85b98978db-c2g4t   1/1     Running            0               12h
test1-78659499b7-46r6l   0/1     CrashLoopBackOff   14 (91s ago)    49m
test2-cdc77f4b6-g77t6    1/1     Running            4 (5m51s ago)   46m
web-76b56fd968-bgpbr     1/1     Running            0               36m
web-76b56fd968-qf5kj     1/1     Running            0               36m
web-76b56fd968-s8c9b     0/1     ErrImagePull       0               36m
web01-66cdb659d8-djnjd   1/1     Running            0             

# 删除test1,使用deployment类型,因为我们当时创建的时候使用的是deployment类型
[root@master ~]# kubectl delete deployment test1
deployment.apps "test1" deleted
[root@master ~]# kubectl get pod
NAME                     READY   STATUS         RESTARTS        AGE
nginx                    1/1     Running        0               3m29s
nginx-85b98978db-c2g4t   1/1     Running        0               12h
test2-cdc77f4b6-g77t6    1/1     Running        4 (6m17s ago)   46m
web-76b56fd968-bgpbr     1/1     Running        0               37m
web-76b56fd968-qf5kj     1/1     Running        0               37m
web-76b56fd968-s8c9b     0/1     ErrImagePull   0               37m
web01-66cdb659d8-djnjd   1/1     Running        0               32m

[root@master ~]# kubectl delete deployment test2
deployment.apps "test2" deleted
[root@master ~]# kubectl get pod
NAME                     READY   STATUS         RESTARTS        AGE
nginx                    1/1     Running        0               3m57s
nginx-85b98978db-c2g4t   1/1     Running        0               12h
test2-cdc77f4b6-g77t6    1/1     Terminating    4 (6m45s ago)   47m
web-76b56fd968-bgpbr     1/1     Running        0               37m
web-76b56fd968-qf5kj     1/1     Running        0               37m
web-76b56fd968-s8c9b     0/1     ErrImagePull   0               37m
web01-66cdb659d8-djnjd   1/1     Running        0               32m


[root@master ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        12h
nginx        NodePort    10.107.164.48   <none>        80:31836/TCP   12h

# 删除service类型的pod
[root@master ~]# kubectl delete svc nginx
service "nginx" deleted
[root@master ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   12h

# 删除所有pod
[root@master ~]# kubectl delete pods --all  

# 强制删除pod节点
[root@master ~]# kubectl delete pod foo --force  

expose

# 将pod中的80暴露到宿主机上的8080
[root@master ~]# kubectl expose deployment web --port 8080 --target-port 80
service/web exposed
[root@master ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP    12h
web          ClusterIP   10.110.189.161   <none>        8080/TCP   8s

# 访问测试
[root@master ~]# curl 10.110.189.161:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

get

# 列出所有的pod
[root@master ~]# kubectl get pods
NAME                     READY   STATUS              RESTARTGE
nginx-85b98978db-w4gns   1/1     Running             0      m18s
web-76b56fd968-h9fpg     0/1     ContainerCreating   0      m18s
web-76b56fd968-h9zf5     1/1     Running             0      m18s
web-76b56fd968-mfklt     1/1     Running             0      m18s
web01-66cdb659d8-f9c98   1/1     Running             0      m18s

# 显示所有pod的详细信息
[root@master ~]# kubectl get pods -o wide
NAME                     READY   STATUS              RESTARTGE     IP            NODE                NOMINATED NODE   RESS GATES
nginx-85b98978db-w4gns   1/1     Running             0      m35s   10.244.1.12   node1.example.com   <none>           <n
web-76b56fd968-h9fpg     0/1     ContainerCreating   0      m35s   <none>        node2.example.com   <none>           <n
web-76b56fd968-h9zf5     1/1     Running             0      m35s   10.244.1.13   node1.example.com   <none>           <n
web-76b56fd968-mfklt     1/1     Running             0      m35s   10.244.1.11   node1.example.com   <none>           <n
web01-66cdb659d8-f9c98   1/1     Running             0      m35s   10.244.1.14   node1.example.com   <none>           <

# 查看你指定类型的pod,类型加pod名
[root@master ~]# kubectl get deployment web
NAME   READY   UP-TO-DATE   AVAILABLE   AGE
web    2/3     3            2           48m

# 列出所有服务
[root@master ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP    12h
web          ClusterIP   10.110.189.161   <none>        8080/TCP   2m17s

edit 编辑

[root@master ~]# kubectl run nginx --image nginx --labels "app=abc"
pod/nginx created
[root@master ~]# kubectl get pods
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          9s

# 编辑标签
[root@master ~]# kubectl edit pods/nginx

# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: "2021-12-20T12:21:40Z"
  labels:
    app: harry     # 修改这里
  name: nginx
  namespace: default
  resourceVersion: "52558"
  uid: c903f9b2-4d73-489a-bcfd-d57e6baf1107
......       # 编辑完成后 ZZ 或者 wq!退出
pod/nginx edited

# 查看nginx描述
[root@master ~]# kubectl describe pod nginx
Name:         nginx
Namespace:    default
Priority:     0
Node:         node1.example.com/192.168.111.152
Start Time:   Mon, 20 Dec 2021 20:21:40 +0800
Labels:       app=harry
Annotations:  <none>
Status:       Running
IP:           10.244.1.20
......

explain 解释

# 查看定义文件怎么编写的
[root@master ~]# kubectl explain deployment
KIND:     Deployment
VERSION:  apps/v1

DESCRIPTION:
     Deployment enables declarative updates for Pods and ReplicaSets.

FIELDS:
   apiVersion	<string>
     APIVersion defines the versioned schema of this representation of an
     object. Servers should convert recognized schemas to the latest internal
     value, and may reject unrecognized values. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources

   kind	<string>
     Kind is a string value representing the REST resource this object
     represents. Servers may infer this from the endpoint the client submits
     requests to. Cannot be updated. In CamelCase. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds

   metadata	<Object>
     Standard object's metadata. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata

   spec	<Object>
     Specification of the desired behavior of the Deployment.

   status	<Object>
     Most recently observed status of the Deployment.

rollout 回滚

有效的资源类型的:

  • deployments
  • daemonsets
  • statefulsets
[root@master ~]# kubectl create deployment nginx --image nginx
deployment.apps/nginx created

// 回滚成功
[root@master ~]# kubectl rollout status deployments/nginx
deployment "nginx" successfully rolled out
更新版本之后才能看见回滚效果

注意:rollout不能回滚 pod 和 svc 类型

[root@master ~]# kubectl get pods
NAME                     READY   STATUS      RESTARTS   AGE
test                     1/1     Running     0          61m

[root@master ~]# kubectl rollout status pod/test
error: no status viewer has been implemented for Pod

[root@master ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP   46h
nginx        ClusterIP   10.108.208.249   <none>        80/TCP    68m

[root@master ~]# kubectl rollout status svc/nginx
error: no status viewer has been implemented for Service

scale 扩展

[root@master ~]# kubectl get pods 
NAME                     READY   STATUS    RESTARTS   AGE
nginx                    1/1     Running   0          15m
nginx-85b98978db-427j2   1/1     Running   0          9m26s

# 动态扩展:之前只有一个,现在变成五个
[root@master ~]# kubectl scale --replicas 5 deployment/nginx
deployment.apps/nginx scaled
[root@master ~]# kubectl get pods 
NAME                     READY   STATUS              RESTARTS   AGE
nginx                    1/1     Running             0          16m
nginx-85b98978db-2kt54   0/1     ContainerCreating   0          5s
nginx-85b98978db-427j2   1/1     Running             0          9m50s
nginx-85b98978db-tg948   0/1     ContainerCreating   0          5s
nginx-85b98978db-v2jb4   0/1     ContainerCreating   0          5s
nginx-85b98978db-xshml   0/1     ContainerCreating   0          5s

# 当你不需要那么多的 deployment/nginx 时候,你可以指定你想留下几个,他会随机删除不需要的
[root@master ~]# kubectl scale --replicas 3 deployment/nginx
deployment.apps/nginx scaled
[root@master ~]# kubectl get pods 
NAME                     READY   STATUS    RESTARTS   AGE
nginx                    1/1     Running   0          17m
nginx-85b98978db-427j2   1/1     Running   0          10m
nginx-85b98978db-v2jb4   1/1     Running   0          66s
nginx-85b98978db-xshml   1/1     Running   0          66s

autoscale 自动扩展

# 最少1个,最多5个,cpu占比50%
[root@master ~]# kubectl autoscale --min 1 --max 5 --cpu-percent 50 deployment/nginx
horizontalpodautoscaler.autoscaling/nginx autoscaled

# 查看运行情况
[root@master ~]# kubectl get hpa
NAME    REFERENCE          TARGETS         MINPODS   MAXPODS   REPLICAS   AGE
nginx   Deployment/nginx   <unknown>/50%   1         5         0          7s

# 自动扩展成功
[root@master ~]# kubectl get pods
NAME                     READY   STATUS             RESTARTS   AGE
myapp-6d8d776547-2m9lk   1/1     Running            0          57m
myapp-6d8d776547-v9lcd   1/1     Running            0          57m
myapp-6d8d776547-x2drr   1/1     Running            0          57m
nginx                    1/1     Running            0          14m
nginx-6799fc88d8-7nr4l   1/1     Running            0          88m
test-659fb5c67-xs7h2     0/1     CrashLoopBackOff   15         59m
test1-7cbbd465d8-qgx69   1/1     Running            0          56m

# 删除所有 deployment类型的 nginx
[root@master ~]# kubectl delete deployment/nginx
deployment.apps "nginx" deleted

top 查看资源使用率

top           Display resource (CPU/memory) usage
# 运行一个nginx容器,默认为pod类型,名字是test
[root@master ~]# kubectl run test --image nginx
pod/test created
[root@master ~]# kubectl get pods
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          22m
test    1/1     Running   0          6s

describe 描述

# 查看test容器的描述
[root@master ~]# kubectl describe pod/test1
Error from server (NotFound): pods "test1" not found
[root@master ~]# kubectl describe pod/nginx
Name:         nginx
Namespace:    default
Priority:     0
Node:         node1.example.com/192.168.111.152
Start Time:   Mon, 20 Dec 2021 20:21:40 +0800
Labels:       app=harry
Annotations:  <none>
Status:       Running
IP:           10.244.1.20
......

logs 查看日志

[root@master ~]# kubectl logs nginx
/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: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: 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: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2021/12/20 12:21:44 [notice] 1#1: using the "epoll" event method
2021/12/20 12:21:44 [notice] 1#1: nginx/1.21.4
2021/12/20 12:21:44 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
2021/12/20 12:21:44 [notice] 1#1: OS: Linux 4.18.0-315.el8.x86_64
2021/12/20 12:21:44 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2021/12/20 12:21:44 [notice] 1#1: start worker processes
2021/12/20 12:21:44 [notice] 1#1: start worker process 31
2021/12/20 12:21:44 [notice] 1#1: start worker process 32
2021/12/20 12:21:44 [notice] 1#1: start worker process 33
2021/12/20 12:21:44 [notice] 1#1: start worker process 34

exec

  • -it:交互模式
# pod类型查看日期
[root@master ~]# kubectl exec nginx -- date
Mon Dec 20 12:47:34 UTC 2021

# 使用交互模式进入
[root@master ~]# kubectl exec -it nginx -- /bin/bash
root@nginx:/# ls
bin   docker-entrypoint.d   home   media  proc	sbin  tmp
boot  docker-entrypoint.sh  lib    mnt	  root	srv   usr
dev   etc		    lib64  opt	  run	sys   var
root@nginx:/# exit
exit

# 创建一个deployment类型的nginx容器
[root@master ~]# kubectl create deployment nginx --image nginx
deployment.apps/nginx created
[root@master ~]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx                    1/1     Running   0          27m
nginx-85b98978db-vgblp   1/1     Running   0          7s
test                     1/1     Running   0          5m47s

# deployment类型查看日期
[root@master ~]# kubectl exec nginx-85b98978db-vgblp -- date 
Mon Dec 20 12:50:35 UTC 2021

[root@master ~]# kubectl exec -it nginx-85b98978db-vgblp /bin/bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@nginx-85b98978db-vgblp:/# ls
bin   docker-entrypoint.d   home   media  proc	sbin  tmp
boot  docker-entrypoint.sh  lib    mnt	  root	srv   usr
dev   etc		    lib64  opt	  run	sys   var

# svc
[root@master ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP   46h
nginx        ClusterIP   10.108.208.249   <none>        80/TCP    56m

# svc类型查看日期
[root@master ~]# kubectl exec svc/nginx -- dateMon Dec 20 02:31:08 UTC 2021

[root@master ~]# kubectl exec -it svc/nginx /bin/sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.	// 未来会删除这个功能
# ls
bin   docker-entrypoint.d   home   media  proc  sbin  tmp
boot  docker-entrypoint.sh  lib    mnt    root  srv   usr
dev   etc                   lib64  opt    run   sys   var

转发端口

[root@master ~]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx                    1/1     Running   0          35m
nginx-85b98978db-vgblp   1/1     Running   0          7m36s
test                     1/1     Running   0          13m
[root@master ~]# kubectl port-forward deployment/nginx 80
Forwarding from 127.0.0.1:80 -> 80
Forwarding from [::1]:80 -> 80
Handling connection for 80

# 访问测试
[root@master ~]# curl 127.0.0.1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

[root@master ~]# kubectl port-forward --address 0.0.0.0 deployment/nginx :8080
Forwarding from 0.0.0.0:38533 -> 8080

cp 复制

[root@master ~]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx                    1/1     Running   0          41m
nginx-85b98978db-vgblp   1/1     Running   0          13m
test                     1/1     Running   0          18m

# 现在 test容器里面的tmp目录没有文件
[root@master ~]# kubectl exec test -- ls /tmp/
[root@master ~]# ls
anaconda-ks.cfg  init  kube-flannel.yml

# 把本机家目录下的 anaconda-ks.cfg文件复制到 test容器的 tmp目录下
[root@master ~]# kubectl cp /root/anaconda-ks.cfg test:/tmp/
[root@master ~]# kubectl exec test -- ls /tmp/
anaconda-ks.cfg

overwrite 覆盖

# 运行一个pod类型的nginx容器,标签设置为app=test
[root@master ~]# kubectl run nginx --image nginx --labels "app=test"
pod/nginx created

# 查看描述情况
[root@master ~]# kubectl describe pod nginx
Name:         nginx
Namespace:    default
Priority:     0
Node:         node1.example.com/192.168.91.137
Start Time:   Mon, 20 Dec 2021 10:57:33 +0800
Labels:       app=test		// 标签设置成功
Annotations:  <none>
Status:       Running
IP:           10.244.1.19
......以下省略

# 使用 app=bzm 覆盖掉原有的 app=test 标签
[root@master ~]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx                    1/1     Running   0          44m
nginx-85b98978db-vgblp   1/1     Running   0          17m
test                     1/1     Running   0          22m
[root@master ~]# kubectl label pod nginx --overwrite app=bzm
pod/nginx labeled

# 查看描述情况
[root@master ~]# kubectl describe pod nginx
Name:         nginx
Namespace:    default
Priority:     0
Node:         node1.example.com/192.168.111.152
Start Time:   Mon, 20 Dec 2021 20:21:40 +0800
Labels:       app=bzm     // 标签覆盖成功
Annotations:  <none>
Status:       Running
IP:           10.244.1.20
......以下省略

# 添加标签
[root@master ~]# kubectl label pod nginx test1=yy
pod/nginx labeled
# 查看描述情况
[root@master ~]# kubectl describe pod nginx
Name:         nginx
Namespace:    default
Priority:     0
Node:         node1.example.com/192.168.111.152
Start Time:   Mon, 20 Dec 2021 20:21:40 +0800
Labels:       app=bzm
              test1=yy       // 标签添加成功
Annotations:  <none>
Status:       Running
IP:           10.244.1.20

api-resources 查看api资源详细信息

[root@master ~]# kubectl api-resources
NAME                              SHORTNAMES   APIVERSION                             NAMESPACED   KIND
bindings                                       v1                                     true         Binding
componentstatuses                 cs           v1                                     false        ComponentStatus
configmaps                        cm           v1                                     true         ConfigMap
endpoints                         ep           v1                                     true         Endpoints
events                            ev           v1                                     true         Event
limitranges                       limits       v1                                     true         LimitRange
namespaces                        ns           v1                                     false        Namespace
nodes                             no           v1                                     false        Node
persistentvolumeclaims            pvc          v1                                     true         PersistentVolumeClaim
persistentvolumes                 pv           v1                                     false        PersistentVolume
......以下省略N行

api-versions

[root@master ~]# kubectl api-versions
admissionregistration.k8s.io/v1
apiextensions.k8s.io/v1
apiregistration.k8s.io/v1
apps/v1
authentication.k8s.io/v1
authorization.k8s.io/v1
autoscaling/v1
autoscaling/v2
autoscaling/v2beta1
autoscaling/v2beta2
batch/v1
batch/v1beta1
certificates.k8s.io/v1
coordination.k8s.io/v1
discovery.k8s.io/v1
discovery.k8s.io/v1beta1
events.k8s.io/v1
events.k8s.io/v1beta1
flowcontrol.apiserver.k8s.io/v1beta1
flowcontrol.apiserver.k8s.io/v1beta2
networking.k8s.io/v1
node.k8s.io/v1
node.k8s.io/v1beta1
policy/v1
policy/v1beta1
rbac.authorization.k8s.io/v1
scheduling.k8s.io/v1
storage.k8s.io/v1
storage.k8s.io/v1beta1
v1

基础命令组合使用

自动扩展删除

# 测试一下之前做的自动创建是否还生效
[root@master ~]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx                    1/1     Running   0          52m
nginx-85b98978db-vgblp   1/1     Running   0          24m
test                     1/1     Running   0          30m

# 删除nginx容器
[root@master ~]# kubectl delete pod nginx-85b98978db-vgblp
pod "nginx-85b98978db-vgblp" deleted

# 虽然我们删除了nginx容器,由于自动创建(最少一个),它又自动重新创建了一个nginx容器
[root@master ~]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx                    1/1     Running   0          53m
nginx-85b98978db-8sp2n   1/1     Running   0          27s
test                     1/1     Running   0          31m

# 删除 deployment控制器
[root@master ~]# kubectl delete deployment nginx
deployment.apps "nginx" deleted

# 控制器删除了,自动创建就失效了,不会再自动创建
[root@master ~]# kubectl get pods
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          54m
test    1/1     Running   0          32m

使用随机端口访问nginx页面

# 创建一个deployment类型的nginx容器,映射本机端口8080,容器内端口映射80
[root@master ~]# kubectl create deployment nginx --image nginx
deployment.apps/nginx created

# 暴露本机端口8080,容器内端口映射80,类型为节点端口
[root@master ~]# kubectl expose deployment nginx --port 8080 --target-port 80 --type NodePort
service/nginx exposed

[root@master ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP          34h
nginx        NodePort    10.96.226.19     <none>        8080:31906/TCP   15s
web          ClusterIP   10.110.189.161   <none>        8080/TCP         22h
# 页面随机访问端口映射为31906

# 本机访问测试
[root@master ~]# curl 10.96.226.19:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值