欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

k8s命令

程序员文章站 2022-03-12 18:24:52
...

kubernetes

k8s的pod分类

pod分为两类:自主式pod与控制器管理的pod

自主式pod由k8s管理器进行管理,而static pod由kubelet进行创建与管理

自主式pod

​ 自主式pod总是在前台运行,同时接受k8s管理与调度,当集群当中的pod因为某种原因停止,k8s会根据其副本的数量,重新的生成对应的pod

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

控制器管理的pod

常见的pod控制器:

​ ReplicationController:当启动一个pod时。这个pod如果不够用可以再启个副本,而后由控制器来管理同一类pod的各种副本与对象。一旦副本少了就会自动增加。采取多退少补的规则,精确符合我们所定义的期望。支持滚动更新

​ ReplicaSet:由一个名叫Deployment的声明式更新的控制器来管理

​ Deployment:Deployment只能管理无状态的应用

​ StateFulSet:有状态副本集,可以管理有状态的应用

​ DaemonSet:如果需要在每个node上运行一个副本的时候可以用DaemonSet

核心组键

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的标签选择器。只要创建的pod的label是统一的,无论IP地址和主机如何改变,其都能被service所识别。如此一来,只要pod属于标签选择器,只要其在service的管理范围之内,则其就会被关联到service中,当这个动态的pod关联到service中之后,再进行动态的探测此pod的IP地址、端口,再将其作为自己后端可调度的可用服务蒂王机为象。因此,客户端的请求发送到service,然后由service代理到后端真实的pod中的容器进行响应。

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

AddOns附件

dns pod:装完k8s后第一件事就需要在k8s集群上部署一个dns pod,以确保各service的名称能够被解析可以动态改变,包括动态创建、动态删除、动态修改,比如把service的名称改了,dnspod会自动触发,将dns解析记录中的名称也给改掉;假如我们手动把service的ip地址给改了,改完以后会自动触发,将dns服务中的解析记录给改掉。如此一来,客户端去访问pod资源的时候可以直接访问service的名称,然后由集群中专用的dns服务来负责解析。

​ 这种pod是k8s自身的服务就需要用到的pod,所以我们把它称为基础性的系统架构级的pod对象,而且它们也被称为集群附件

kubectl命令

create

从文件或标准输出中创建pod

# 创建一个deployment类型的pos,名字是web1,使用的镜像是nginx
[[email protected] ~]# kubectl create deployment web1 --image=nginx
deployment.apps/web1 created
# 查看
[[email protected] ~]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
web1-5756d98cd9-fh588    1/1     Running   0          14m


# 创建deployment类型的pos,名字是nginx,使用的镜像是nginx,replicas是指定创建的个数
[[email protected] ~]# kubectl create deployment nginx --image=nginx --replicas=3
deployment.apps/nginx created
# 查看
[[email protected] ~]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-85b98978db-9c9xs   1/1     Running   0          41s
nginx-85b98978db-fqq57   1/1     Running   0          41s
nginx-85b98978db-lfftt   1/1     Running   0          41s
web1-5756d98cd9-fh588    1/1     Running   0          9m21s


# 创建一个名为my-dep的pod,运行nginx镜像并暴露端口1111,仅仅是暴露而已
[[email protected] ~]# kubectl create deployment my-dep --image=nginx --port=1111
deployment.apps/my-dep created

get

显示一个或更多资源

# 查看创建的pod
[[email protected] ~]# kubectl get pods
NAME                    READY   STATUS    RESTARTS   AGE
web1-5756d98cd9-fh588   1/1     Running   0          2m1s

# 查看所有的service
[[email protected] ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        25h
my-dep       NodePort    10.97.89.122     <none>        80:32213/TCP   5m17s
nginx        ClusterIP   10.106.246.141   <none>        8000/TCP       15m

# 查看多个信息,用","隔开
[[email protected] ~]# kubectl get pod,svc
NAME                          READY   STATUS    RESTARTS   AGE
pod/my-dep-6fcbf46469-drs28   1/1     Running   0          36m
pod/nginx-85b98978db-9c9xs    1/1     Running   0          42m
pod/nginx-85b98978db-fqq57    1/1     Running   0          42m
pod/nginx-85b98978db-lfftt    1/1     Running   0          42m
pod/web1-5756d98cd9-fh588     1/1     Running   0          51m

NAME                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
service/kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        26h
service/my-dep       NodePort    10.97.89.122     <none>        80:32213/TCP   14m
service/nginx        ClusterIP   10.106.246.141   <none>        8000/TCP       24m


# 查看名称空间
[[email protected] ~]# kubectl get ns
NAME              STATUS   AGE
default           Active   25h
kube-node-lease   Active   25h
kube-public       Active   25h
kube-system       Active   25h


# 查看指定类型的pod
[[email protected] ~]# kubectl get deployment
NAME     READY   UP-TO-DATE   AVAILABLE   AGE
my-dep   1/1     1            1           29m
nginx    3/3     3            3           35m
web1     1/1     1            1           44m

[[email protected] ~]# kubectl get deployment nginx
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
nginx   3/3     3            3           35m


# 查看更详细的信息
[[email protected] ~]# kubectl get pod -o wide
NAME                      READY   STATUS    RESTARTS   AGE   IP           NODE                NOMINATED NODE   READINESS GATES
my-dep-6fcbf46469-drs28   1/1     Running   0          30m   10.244.2.7   node2.example.com   <none>           <none>
nginx-85b98978db-9c9xs    1/1     Running   0          36m   10.244.1.4   node1.example.com   <none>           <none>
nginx-85b98978db-fqq57    1/1     Running   0          36m   10.244.1.3   node1.example.com   <none>           <none>
nginx-85b98978db-lfftt    1/1     Running   0          36m   10.244.2.6   node2.example.com   <none>           <none>
web1-5756d98cd9-fh588     1/1     Running   0          45m   10.244.2.5   node2.example.com   <none>           <none>


# 列出一个由pod中指定的类型和名称标识的pod。json/yaml输出格式
[[email protected] ~]# kubectl get deployment my-dep -o json
{
    "apiVersion": "apps/v1",
    "kind": "Deployment",
    "metadata": {
        "annotations": {
            "deployment.kubernetes.io/revision": "1"
        },
        "creationTimestamp": "2021-12-19T08:52:18Z",
        "generation": 1,
        "labels": {
            "app": "my-dep"
        },
 ..................
 
 
[[email protected] ~]# kubectl get deployment my-dep -o yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
  creationTimestamp: "2021-12-19T08:52:18Z"
  generation: 1
  labels:
    app: my-dep
  name: my-dep
  namespace: default
  resourceVersion: "11093"
  uid: 8b996347-546b-4b21-80db-ebbdd65e8eb0
....................
expose

暴露一个服务的端口

# 查看正在运行的pod
[[email protected] ~]# kubectl get pods
NAME                      READY   STATUS    RESTARTS   AGE
my-dep-6fcbf46469-drs28   1/1     Running   0          11m
nginx-85b98978db-9c9xs    1/1     Running   0          17m
nginx-85b98978db-fqq57    1/1     Running   0          17m
nginx-85b98978db-lfftt    1/1     Running   0          17m
web1-5756d98cd9-fh588     1/1     Running   0          26m
# 暴露nginx容器里面的80端口到集群的8000端口
[[email protected] ~]# kubectl expose deployment nginx --port=8000 --target-port=80
service/nginx exposed
# 查看
[[email protected] ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP    25h
nginx        ClusterIP   10.106.246.141   <none>        8000/TCP   29s
# 访问
[[email protected] ~]# curl 10.106.246.141:8000
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
........


# 暴露my-dep的pod,类型为NodePort
[[email protected] ~]# kubectl get pods
NAME                      READY   STATUS    RESTARTS   AGE
my-dep-6fcbf46469-drs28   1/1     Running   0          20m
nginx-85b98978db-9c9xs    1/1     Running   0          27m
nginx-85b98978db-fqq57    1/1     Running   0          27m
nginx-85b98978db-lfftt    1/1     Running   0          27m
web1-5756d98cd9-fh588     1/1     Running   0          36m
# 查看
[[email protected] ~]# kubectl expose deployment my-dep --port 80 --type=NodePort
service/my-dep exposed
[[email protected] ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        25h
my-dep       NodePort    10.97.89.122     <none>        80:32213/TCP   9s
nginx        ClusterIP   10.106.246.141   <none>        8000/TCP       10m
# 访问(在浏览器访问端口32213)
[[email protected] ~]# curl 10.97.89.122
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
.........
delete

删除资源的文件名,标准输出,资源和名称,或资源和标签选择器

# 查看后删除service和pod名字叫nginx的
[[email protected] ~]# kubectl get svc,pods
NAME                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
service/kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        26h
service/my-dep       NodePort    10.97.89.122     <none>        80:32213/TCP   17m
service/nginx        ClusterIP   10.106.246.141   <none>        8000/TCP       28m

NAME                          READY   STATUS    RESTARTS   AGE
pod/my-dep-6fcbf46469-drs28   1/1     Running   0          39m
pod/nginx-85b98978db-9c9xs    1/1     Running   0          46m
pod/nginx-85b98978db-fqq57    1/1     Running   0          46m
pod/nginx-85b98978db-lfftt    1/1     Running   0          46m
pod/web1-5756d98cd9-fh588     1/1     Running   0          54m

[[email protected] ~]# kubectl delete deployment,svc nginx
deployment.apps "nginx" deleted
service "nginx" deleted

# 删除后查看
[[email protected] ~]# kubectl get svc,pods
NAME                 TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
service/kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP        26h
service/my-dep       NodePort    10.97.89.122   <none>        80:32213/TCP   18m

NAME                          READY   STATUS    RESTARTS   AGE
pod/my-dep-6fcbf46469-drs28   1/1     Running   0          40m
pod/web1-5756d98cd9-fh588     1/1     Running   0          55m

# 删除某一个类型的pod
[[email protected] ~]# kubectl get pod
NAME                      READY   STATUS    RESTARTS   AGE
my-dep-6fcbf46469-drs28   1/1     Running   0          42m
web1-5756d98cd9-fh588     1/1     Running   0          57m

[[email protected] ~]# kubectl delete deployment web1
deployment.apps "web1" deleted

[[email protected] ~]# kubectl get pod
NAME                      READY   STATUS    RESTARTS   AGE
my-dep-6fcbf46469-drs28   1/1     Running   0          42m

# 删除一个service
[[email protected] ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP        26h
my-dep       NodePort    10.97.89.122   <none>        80:32213/TCP   22m

[[email protected] ~]# kubectl delete svc my-dep
service "my-dep" deleted

[[email protected] ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   26h

# 删除所有指定类型的pod
[[email protected] ~]# kubectl get pods
NAME                      READY   STATUS    RESTARTS   AGE
my-dep-6fcbf46469-drs28   1/1     Running   0          46m

[[email protected] ~]# kubectl delete deployment --all
deployment.apps "my-dep" deleted

[[email protected] ~]# kubectl get pods
No resources found in default namespace.
run

在集群中运行一个指定的镜像的pod(自主式pod)

# 使用run运行的pod默认为pod类型
[[email protected] ~]# kubectl run nginx --image nginx
pod/nginx created

[[email protected] ~]# kubectl get pods
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          38s

# 运行一个pod叫nginx1,使用镜像nginx,指定标签为app=nginx
[[email protected] ~]# kubectl run nginx1 --image=nginx --labels="app=nginx"
pod/nginx1 created

[[email protected] ~]# kubectl get pods
NAME     READY   STATUS    RESTARTS   AGE
nginx    1/1     Running   0          8m3s
nginx1   1/1     Running   0          66s

# 查看具体的描述
[[email protected] ~]# kubectl describe pod nginx1
Name:         nginx1
Namespace:    default
Priority:     0
Node:         node2.example.com/192.168.220.21
Start Time:   Sun, 19 Dec 2021 04:48:06 -0500
Labels:       app=nginx  # 标签是nginx
Annotations:  <none>
Status:       Running
IP:           10.244.2.8
.............

# 多创建几个,它们的标签都是nginx
[[email protected] ~]# kubectl run nginx2 --image=nginx --labels="app=nginx"
pod/nginx2 created
[[email protected] ~]# kubectl run nginx3 --image=nginx --labels="app=nginx"
pod/nginx3 created

# 查看
[[email protected] ~]# kubectl get pods
NAME     READY   STATUS    RESTARTS   AGE
nginx    1/1     Running   0          12m
nginx1   1/1     Running   0          5m41s
nginx2   1/1     Running   0          55s
nginx3   1/1     Running   0          50s

# 删除时指定标签就可以删除对应标签的pod
[[email protected] ~]# kubectl delete pod -l app=nginx
pod "nginx1" deleted
pod "nginx2" deleted
pod "nginx3" deleted

[[email protected] ~]# kubectl get pods
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          13m

# 试运行,不会真正的创建运行,可以指定在client/server端
[[email protected] ~]# kubectl run web --image=nginx --dry-run=client
pod/web created (dry run)

[[email protected] ~]# kubectl get pods
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          19m

# 启动一个pod,并将其放在前台,如果它退出,不要重新启动它
[[email protected] ~]# kubectl run -i -t busybox --image=busybox --restart=Never
If you don't see a command prompt, try pressing enter.
/ # ls
bin   dev   etc   home  proc  root  sys   tmp   usr   var
/ # exit

[[email protected] ~]# kubectl get pods
NAME      READY   STATUS      RESTARTS   AGE
busybox   0/1     Completed   0          16s
nginx     1/1     Running     0          23m
edit

编辑一个资源

# 运行一个pod类型的nginx,名字叫web1,定义的标签是app=nginx
[[email protected] ~]# kubectl run web1 --image=nginx --labels="app=nginx"
pod/web1 created
# 查看
[[email protected] ~]# kubectl get pods
NAME   READY   STATUS    RESTARTS   AGE
web1   1/1     Running   0          19s
# 使用edit命令编辑
[[email protected] ~]# kubectl edit pods/web1
# 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-20T06:43:02Z"
  labels:
    app: test   # 修改为test
  name: web1
........ 
# 查看是否更改成功
[[email protected] ~]# kubectl describe pods/web1
Name:         web1
Namespace:    default
Priority:     0
Node:         node1.example.com/192.168.220.20
Start Time:   Mon, 20 Dec 2021 01:43:02 -0500
Labels:       app=test    # 编辑成功
........
scale

动态扩展

#创建一个deployment类型的nginx的pod 
[[email protected] ~]# kubectl create deployment nginx --image=nginx
deployment.apps/nginx created
# 查看
[[email protected] ~]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-85b98978db-gqq2s   1/1     Running   0          19s
web1                     1/1     Running   0          17m
# 使用scale扩展
[[email protected] ~]# kubectl scale --replicas 4 deployment/nginx
deployment.apps/nginx scaled
# 扩展后查看多了几个相同类型的pod
[[email protected] ~]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-85b98978db-b5r9p   1/1     Running   0          33s
nginx-85b98978db-gqq2s   1/1     Running   0          2m16s
nginx-85b98978db-t8bcq   1/1     Running   0          33s
nginx-85b98978db-vxkmt   1/1     Running   0          33s
web1                     1/1     Running   0          19m

# 如果只需要2个deployment类型的nginx的pod
[[email protected] ~]# kubectl scale --replicas 2 deployment/nginx
deployment.apps/nginx scaled
# 查看
[[email protected] ~]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-85b98978db-gqq2s   1/1     Running   0          4m20s
nginx-85b98978db-t8bcq   1/1     Running   0          2m37s
web1                     1/1     Running   0          21m
autoscale

自动扩展,给定一个范围,自动根据业务的访问量增加或减少

[[email protected] ~]# kubectl autoscale --min=2 --max=7 --cpu-percent=60 deployment/nginx
horizontalpodautoscaler.autoscaling/nginx autoscaled

[[email protected] ~]# kubectl get hpa
NAME    REFERENCE          TARGETS         MINPODS   MAXPODS   REPLICAS   AGE
nginx   Deployment/nginx   <unknown>/60%   2         7         0          9s
cluster-info

显示集群信息

[[email protected] ~]# kubectl cluster-info
Kubernetes control plane(kubernetes 控制面板) is running at https://192.168.220.17:6443
CoreDNS is running at https://192.168.220.17:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
(为了进一步调试和诊断集群问题,使用'kubectl cluster-info dump'。)
drain

驱逐节点上的应用,准备下线维护

describe

显示指定pod的详细信息

[[email protected] ~]# kubectl describe pod web1
Name:         web1
Namespace:    default
Priority:     0
Node:         node1.example.com/192.168.220.20
Start Time:   Mon, 20 Dec 2021 01:43:02 -0500
Labels:       app=test
Annotations:  <none>
Status:       Running
IP:           10.244.1.8
IPs:
  IP:  10.244.1.8
.............
logs

查看日志

[[email protected] ~]# kubectl logs deployment/nginx
Found 4 pods, using pod/nginx-85b98978db-gqq2s
/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 07:00:17 [notice] 1#1: using the "epoll" event method
2021/12/20 07:00:17 [notice] 1#1: nginx/1.21.4
2021/12/20 07:00:17 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
2021/12/20 07:00:17 [notice] 1#1: OS: Linux 4.18.0-257.el8.x86_64
2021/12/20 07:00:17 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2021/12/20 07:00:17 [notice] 1#1: start worker processes
2021/12/20 07:00:17 [notice] 1#1: start worker process 31
2021/12/20 07:00:17 [notice] 1#1: start worker process 32
2021/12/20 07:00:17 [notice] 1#1: start worker process 33
2021/12/20 07:00:17 [notice] 1#1: start worker process 34
attach

附加在一个容器里

[[email protected] ~]# kubectl attach web1
If you don't see a command prompt, try pressing enter.


exec

进到容器内执行一个命令

[[email protected] ~]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-85b98978db-4h4hl   1/1     Running   0          45m
nginx-85b98978db-gqq2s   1/1     Running   0          58m
nginx-85b98978db-t8bcq   1/1     Running   0          56m
nginx-85b98978db-w4r4v   1/1     Running   0          45m
web1                     1/1     Running   0          75m

[[email protected] ~]# kubectl exec web1 -- date
Mon Dec 20 07:58:28 UTC 2021

[[email protected] ~]# kubectl exec web1 -- ls -l /
total 12
drwxr-xr-x   2 root root 4096 Dec  1 00:00 bin
drwxr-xr-x   2 root root    6 Oct  3 09:15 boot
drwxr-xr-x   5 root root  360 Dec 20 06:43 dev
drwxr-xr-x   1 root root   41 Dec  2 10:59 docker-entrypoint.d
-rwxrwxr-x   1 root root 1202 Dec  2 10:58 docker-entrypoint.sh
drwxr-xr-x   1 root root   19 Dec 20 06:43 etc
drwxr-xr-x   2 root root    6 Oct  3 09:15 home
drwxr-xr-x   1 root root   45 Dec  1 00:00 lib
drwxr-xr-x   2 root root   34 Dec  1 00:00 lib64
drwxr-xr-x   2 root root    6 Dec  1 00:00 media
drwxr-xr-x   2 root root    6 Dec  1 00:00 mnt
drwxr-xr-x   2 root root    6 Dec  1 00:00 opt
dr-xr-xr-x 199 root root    0 Dec 20 06:43 proc
drwx------   2 root root   37 Dec  1 00:00 root
drwxr-xr-x   1 root root   38 Dec 20 06:43 run
drwxr-xr-x   2 root root 4096 Dec  1 00:00 sbin
drwxr-xr-x   2 root root    6 Dec  1 00:00 srv
dr-xr-xr-x  13 root root    0 Dec 20 06:43 sys
drwxrwxrwt   1 root root    6 Dec  2 10:59 tmp
drwxr-xr-x   1 root root   66 Dec  1 00:00 usr
drwxr-xr-x   1 root root   19 Dec  1 00:00 var

[[email protected] ~]# kubectl exec -it web1 -- /bin/bash
[email protected]:/# 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
[email protected]:/# exit
exit
port-forward

转发端口到pod里面

[[email protected] ~]# kubectl create deployment nginx --image=nginx
deployment.apps/nginx created

[[email protected] ~]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-85b98978db-9vbc7   1/1     Running   0          18s
web1                     1/1     Running   0          3h4m

[[email protected] ~]# kubectl port-forward deployment/nginx 80
Forwarding from 127.0.0.1:80 -> 80
Forwarding from [::1]:80 -> 80
Handling connection for 80

[[email protected] ~]# curl http://127.0.0.1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
...............

[email protected] ~]# kubectl port-forward --address 0.0.0.0 deployment/nginx 80
Forwarding from 0.0.0.0:80 -> 80

#允许所有IP访问80端口
[[email protected] ~]# curl http://192.168.220.17
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
.................
cp

拷贝文件或目录到容器中,或者从容器内向外拷贝

[[email protected] ~]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-85b98978db-9vbc7   1/1     Running   0          13m
web1                     1/1     Running   0          3h17m

[[email protected] ~]# kubectl cp /root/init nginx-85b98978db-9vbc7:/

[[email protected] ~]# kubectl exec nginx-85b98978db-9vbc7 -- ls -l /
total 16
drwxr-xr-x   2 root root 4096 Dec  1 00:00 bin
drwxr-xr-x   2 root root    6 Oct  3 09:15 boot
drwxr-xr-x   5 root root  360 Dec 20 09:47 dev
drwxr-xr-x   1 root root   41 Dec  2 10:59 docker-entrypoint.d
-rwxrwxr-x   1 root root 1202 Dec  2 10:58 docker-entrypoint.sh
drwxr-xr-x   1 root root   19 Dec 20 09:47 etc
drwxr-xr-x   2 root root    6 Oct  3 09:15 home
-rw-r--r--   1 root root  178 Dec 20 10:00 init
..................
label

给资源设置、更新标签

[[email protected] ~]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-85b98978db-9vbc7   1/1     Running   0          19m
web1                     1/1     Running   0          3h23m

# 添加标签
[[email protected] ~]# kubectl label --overwrite=true deployment nginx app=test
deployment.apps/nginx unlabeled

#查看
[[email protected] ~]# kubectl describe deployment/nginx
Name:                   nginx
Namespace:              default
CreationTimestamp:      Mon, 20 Dec 2021 04:46:45 -0500
Labels:                 app=test
...........

# 追加标签
[[email protected] ~]# kubectl label pods nginx-85b98978db-9vbc7 xyz=123
pod/nginx-85b98978db-9vbc7 labeled

# 查看
[[email protected] ~]# kubectl describe pod nginx-85b98978db-9vbc7
Name:         nginx-85b98978db-9vbc7
Namespace:    default
Priority:     0
Node:         node2.example.com/192.168.220.21
Start Time:   Mon, 20 Dec 2021 04:46:45 -0500
Labels:       app=nginx
              pod-template-hash=85b98978db
              xyz=123
 ..................             
              
# 更改标签
[[email protected] ~]# kubectl label pod nginx-85b98978db-9vbc7 --overwrite xyz=111
pod/nginx-85b98978db-9vbc7 labeled
# 查看
[[email protected] ~]# kubectl describe pod nginx-85b98978db-9vbc7
Name:         nginx-85b98978db-9vbc7
Namespace:    default
Priority:     0
Node:         node2.example.com/192.168.220.21
Start Time:   Mon, 20 Dec 2021 04:46:45 -0500
Labels:       app=nginx
              pod-template-hash=85b98978db
              xyz=111
 ........................            
api-resources

查看所有资源

[[email protected] ~]# 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
................
apr-versions

显示受支持的API版本

[[email protected] ~]# 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
................
deploument

控制器部署镜像

[[email protected] ~]# kubectl create deployment nginx111 --image lizhenliang/java-demo

[email protected] ~]# kubectl get pods
NAME                       READY   STATUS    RESTARTS   AGE
nginx-85b98978db-9vbc7     1/1     Running   0          28m
nginx111-f44dcbcb5-mzmn8   1/1     Running   0          34s
web1                       1/1     Running   0          3h32m

# 暴露端口使用service
[[email protected] ~]# kubectl expose deployment nginx111 --port=80 --target-port=8080 --type=NodePort
service/nginx111 exposed

[[email protected] ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        2d3h
nginx111     NodePort    10.104.225.197   <none>        80:30081/TCP   2m22s

[[email protected] ~]# curl 192.168.220.17:30081
<!DOCTYPE html>
<html>
<head lang="en">
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>把美女带回家应用案例</title>
 .................       

上一篇: K8S命令

下一篇: 443. String Compression