kubernetes基础介绍及有关命令

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对象,而且它们也被称为集群附件

网络模型

三种网络模型

  • 节点网络
  • service集群网络
  • pod网络

同节点Pod之间的通信

在容器启动前,会为容器创建一个虚拟Ethernet接口对,这个接口对类似于管道的两端,其中一端在主机命名空间中,另外一端在容器命名空间中,并命名为eth0。在主机命名空间的接口会绑定到网桥。网桥的地址段会取IP赋值给容器的eth0接口。

不同节点上的Pod通信

我们已经知道一个节点上的容器都会连接到同一网桥,因此要让运行在不同节点上的容器之间能够通信,这些节点的网桥就需要以某种方式连接起来。 跨整个集群的Pod的IP地址必须是唯一的,所有跨节点的网桥必须使用不重叠的网络地址段,以防止不同节点上的Pod拿到同一IP地址,即确保没有IP地址冲突。

发送到B节点上的容器时,报文会先通过veth接口对到网桥,再由网桥到A节点的物理适配器,再通过网线传输到B节点的物理适配器,再通过B的网桥,经过接口对到达目标容器。

注意:上述情形仅在节点连接到相同网关,之间没有任何路由设备时有效。否则,路由设备会因为IP私有产生丢包现象,除非设置路由规则。但随着节点的增加,路由的配置会变得非常困难。因此我们使用SDN(软件定义网络)技术来简化此类问题,SDN可以忽略底层网络拓扑,使其就像连接到同一网关。

Pod与Service

在不同节点上的Pod通信中,我们知道了Pod是以IP地址进行通信,但Kubernetes 的集群中, Pod 可能会频繁的销毁和创建,也就是说 Pod 的 IP 不是固定的。 为了解决这个问题,Service 提供了访问 Pod 的抽象层,即为一组功能相同的Pod提供单一不变的接入点资源。 无论后端的 Pod 如何变化,Service 都作为稳定的前端对外提供服务。 同时,Service 还提供了高可用和负载均衡功能,Service 负责将请求转发给正确的 Pod。

kubectl常用命令

语法
kubectl [command] [TYPE] [NAME] [flags]

command:子命令
TYPE:资源类型
NAME:资源名称
flags:命令参数

命令帮助
kubectl命令的帮助很详细,kubectl -h会列出所有的子命令,在任何子命令后跟 -h,都会输出详细的帮助以及用例,遇到问题可以随时查看帮助。

资源对象
kubectl大部分子命令后都可以指定要操作的资源对象,可以用kubectl api-resources命令参考

全局参数
kubectl options命令可以列出可以全局使用的命令参数
--cluster='': 指定命令操作对象的集群
--context='':  指定命令操作对象的上下文
-n, --namespace='': 指定命令操作对象的Namespace

create

从文件或标准输出中创建pod
# 创建一个deployment类型的pos,名字是nginx1,使用的镜像是nginx
[root@master ~]# kubectl create deployment wb1 --image=nginx
deployment.apps/wb1 created
[root@master ~]# kubectl create deployment nginx1 --image=nginx
deployment.apps/nginx1 created
[root@master ~]# kubectl get pods
NAME                      READY   STATUS    RESTARTS   AGE
nginx1-5c9f6bbd8c-2ng6h   1/1     Running   0          40s

# 创建deployment类型的pos,名字是nginx2,使用的镜像是nginx,replicas是指定创建的个数
[root@master ~]# kubectl create deployment nginx2 --image=nginx --replicas=2
deployment.apps/nginx2 created
[root@master ~]# kubectl get pods
NAME                      READY   STATUS              RESTARTS   AGE
nginx1-5c9f6bbd8c-2ng6h   1/1     Running             0          3m2s
nginx2-85bf7b8976-68q5d   0/1     ContainerCreating   0          42s
nginx2-85bf7b8976-74l6z   1/1     Running             0          42s

run

在集群中运行一个指定的镜像的pod(自主式pod)
[root@master ~]# kubectl run nginx --image nginx
pod/nginx created
[root@master ~]# kubectl get pods
NAME                   READY   STATUS              RESTARTS   AGE
nginx                  0/1     ContainerCreating   0          11s

[root@master ~]# kubectl get pods
NAME                   READY   STATUS              RESTARTS   AGE
nginx                  0/1     ContainerCreating   0          11s
wb1-5dbfb96758-hhfhb   1/1     Running             0          16m
[root@master ~]# kubectl run nginx1 --image=nginx --labels="app=web"
pod/nginx1 created
[root@master ~]# kubectl get pods
NAME                   READY   STATUS    RESTARTS   AGE
nginx                  1/1     Running   0          2m9s
nginx1                 1/1     Running   0          18s

[root@master ~]# kubectl run nginx2 --image=nginx --labels="app=web"
pod/nginx2 created
[root@master ~]# kubectl run nginx3 --image=nginx --labels="app=web"
pod/nginx3 created
[root@master ~]# kubectl get pods
NAME                   READY   STATUS    RESTARTS   AGE
nginx                  1/1     Running   0          5m49s
nginx1                 1/1     Running   0          3m58s
nginx2                 1/1     Running   0          73s
nginx3                 1/1     Running   0          43s
[root@master ~]# kubectl delete pod -l app=web
pod "nginx1" deleted
pod "nginx2" deleted
pod "nginx3" deleted

[root@master ~]# kubectl run web123 --image=nginx --dry-run=client
pod/web123 created (dry run)
[root@master ~]# kubectl run -i -t web123 --image=busybox --restart=Never
If you don't see a command prompt, try pressing enter.
/ # ls -l
total 16
drwxr-xr-x    2 root     root         12288 Dec  7 00:20 bin
drwxr-xr-x    5 root     root           380 Dec 19 10:22 dev
drwxr-xr-x    1 root     root            66 Dec 19 10:22 etc
drwxr-xr-x    2 nobody   nobody           6 Dec  7 00:20 home
dr-xr-xr-x  219 root     root             0 Dec 19 10:22 proc
drwx------    1 root     root            26 Dec 19 10:22 root
dr-xr-xr-x   13 root     root             0 Dec 19 10:21 sys
drwxrwxrwt    2 root     root             6 Dec  7 00:20 tmp
drwxr-xr-x    3 root     root            18 Dec  7 00:20 usr
drwxr-xr-x    1 root     root            17 Dec 19 10:22 var

delete

删除资源的文件名,标准输出,资源和名称,或资源和标签选择器
[root@master ~]# kubectl get pods,svc
NAME                          READY   STATUS    RESTARTS   AGE
pod/nginx-85b98978db-dgkbp    1/1     Running   0          97m
pod/nginx1-5c9f6bbd8c-2ng6h   1/1     Running   0          11m
pod/nginx2-85bf7b8976-68q5d   1/1     Running   0          9m8s
pod/nginx2-85bf7b8976-74l6z   1/1     Running   0          9m8s
pod/nginx3-59475d8756-l8mcq   1/1     Running   0          7m17s
pod/wb1-5dbfb96758-hhfhb      1/1     Running   0          11m

NAME                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
service/kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        144m
service/nginx        NodePort    10.105.224.204   <none>        80:31753/TCP   97m
[root@master ~]# kubectl delete deployment,svc nginx
deployment.apps "nginx" deleted
service "nginx" deleted

[root@master ~]# kubectl get pods,svc
NAME                          READY   STATUS    RESTARTS   AGE
pod/nginx1-5c9f6bbd8c-2ng6h   1/1     Running   0          13m
pod/nginx2-85bf7b8976-68q5d   1/1     Running   0          10m
pod/nginx2-85bf7b8976-74l6z   1/1     Running   0          10m
pod/nginx3-59475d8756-l8mcq   1/1     Running   0          8m50s
pod/wb1-5dbfb96758-hhfhb      1/1     Running   0          13m
NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   146m

get

查看创建的pod
[root@master ~]# kubectl get pods
NAME                      READY   STATUS    RESTARTS   AGE
nginx-85b98978db-dgkbp    1/1     Running   0          90m
nginx1-5c9f6bbd8c-2ng6h   1/1     Running   0          5m2s
nginx2-85bf7b8976-68q5d   1/1     Running   0          2m42s
nginx2-85bf7b8976-74l6z   1/1     Running   0          2m42s
nginx3-59475d8756-l8mcq   1/1     Running   0          51s
wb1-5dbfb96758-hhfhb      1/1     Running   0          5m14s

[root@master ~]# kubectl get service
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        138m
nginx        NodePort    10.105.224.204   <none>        80:31753/TCP   91m

[root@master ~]# kubectl get service,pod
NAME                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
service/kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        138m
service/nginx        NodePort    10.105.224.204   <none>        80:31753/TCP   91m

NAME                          READY   STATUS    RESTARTS   AGE
pod/nginx-85b98978db-dgkbp    1/1     Running   0          91m
pod/nginx1-5c9f6bbd8c-2ng6h   1/1     Running   0          5m52s
pod/nginx2-85bf7b8976-68q5d   1/1     Running   0          3m32s
pod/nginx2-85bf7b8976-74l6z   1/1     Running   0          3m32s
pod/nginx3-59475d8756-l8mcq   1/1     Running   0          101s
pod/wb1-5dbfb96758-hhfhb      1/1     Running   0          6m4s

[root@master ~]# kubectl get ns
NAME              STATUS   AGE
default           Active   139m
kube-node-lease   Active   139m
kube-public       Active   139m
kube-system       Active   139m

[root@master ~]# kubectl get deployment
NAME     READY   UP-TO-DATE   AVAILABLE   AGE
nginx    1/1     1            1           93m
nginx1   1/1     1            1           7m49s
nginx2   2/2     2            2           5m29s
nginx3   1/1     1            1           3m38s
wb1      1/1     1            1           8m1s
[root@master ~]# kubectl get deployment nginx
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
nginx   1/1     1            1           94m

Kubectl常用命令的使用

explain

获取资源
用法:
kubectl explain RESOURCE

//获取资源及其字段的文档
[root@master ~]# kubectl explain pods
KIND:     Pod
VERSION:  v1

DESCRIPTION:
     Pod is a collection of containers that can run on a host. This resource is
     created by clients and scheduled onto hosts.

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 pod. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

   status       <Object>
     Most recently observed status of the pod. This data may not be up to date.
     Populated by the system. Read-only. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

edit

编辑资源

//运行一个pod类型的nginx,名字叫nginx,内型是deployment
[root@master ~]# kubectl create deployment nginx --image nginx 
deployment.apps/nginx created
[root@master ~]# kubectl get pod
NAME                     READY   STATUS              RESTARTS   AGE
nginx-6799fc88d8-cpbg4   0/1     ContainerCreating   0          10s


//使用edit命令编辑
[root@master ~]# kubectl edit deployment nginx
......
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
  creationTimestamp: "2021-12-20T10:33:49Z"
  generation: 1
  labels:
    app: nginx
    name: jjyy  #添加name: jjyy
.....

//查看
[root@master ~]# kubectl describe deploy nginx
Name:                   nginx
Namespace:              default
CreationTimestamp:      Mon, 20 Dec 2021 18:33:49 +0800
Labels:                 app=nginx
                        name=jjyy
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               app=nginx
Replicas:               1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
......

scale

动态扩展

//使用scale扩展
[root@master ~]# kubectl scale deployment/nginx --replicas 3
deployment.apps/nginx scaled
//扩展后查看多了几个相同类型的pod
[root@master ~]# kubectl get pod
NAME                     READY   STATUS    RESTARTS   AGE
nginx-6799fc88d8-8bnn8   1/1     Running   0          37s
nginx-6799fc88d8-cpbg4   1/1     Running   0          15m
nginx-6799fc88d8-t6vpw   1/1     Running   0          37s

//如果只需要2个deployment类型的nginx的pod
[root@master ~]# kubectl scale deployment/nginx --replicas 2
deployment.apps/nginx scaled
//查看发现只有两个(删除是随机的)
[root@master ~]# kubectl get pod
NAME                     READY   STATUS    RESTARTS   AGE
nginx-6799fc88d8-8bnn8   1/1     Running   0          3m22s
nginx-6799fc88d8-cpbg4   1/1     Running   0          18m

autoscale

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

//最少3个,当不满足条件时会自动创建
[root@master ~]# kubectl autoscale deployment/nginx --min 3 --max=5
horizontalpodautoscaler.autoscaling/nginx autoscaled
[root@master ~]# kubectl get pod
NAME                     READY   STATUS              RESTARTS   AGE
nginx-6799fc88d8-8bnn8   1/1     Running             0          8m1s
nginx-6799fc88d8-cpbg4   1/1     Running             0          23m
nginx-6799fc88d8-ctjmt   0/1     ContainerCreating   0          4s

//当大于5个时,先创建,之后也会随机删除,直到满足条件五个
[root@master ~]# kubectl scale deployment/nginx --replicas 7
deployment.apps/nginx scaled
[root@master ~]# kubectl get pod
NAME                     READY   STATUS        RESTARTS   AGE
nginx-6799fc88d8-84mfw   1/1     Running       0          52s
nginx-6799fc88d8-8bnn8   1/1     Running       0          20m
nginx-6799fc88d8-cpbg4   1/1     Running       0          35m
nginx-6799fc88d8-ctjmt   1/1     Running       0          12m
nginx-6799fc88d8-jzksn   0/1     Terminating   0          52s
nginx-6799fc88d8-rf74m   1/1     Running       0          52s
nginx-6799fc88d8-swjv9   0/1     Terminating   0          52s

cluster-info

显示集群信息

[root@master ~]# kubectl cluster-info
Kubernetes control plane is running at https://192.168.145.250:6443
KubeDNS is running at https://192.168.145.250:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

cordon

可调度

[root@master ~]# kubectl cordon node2
node/node2 already cordoned
[root@master ~]# kubectl get node
NAME     STATUS                     ROLES                  AGE    VERSION
master   Ready                      control-plane,master   2d4h   v1.20.0
node1    NotReady                   <none>                 2d4h   v1.20.0
node2    Ready,SchedulingDisabled   <none>                 2d4h   v1.20.0
[root@master ~]# kubectl get pod -o wide
NAME                     READY   STATUS    RESTARTS   AGE    IP            NODE    NOMINATED NODE   READINESS GATES
nginx-6799fc88d8-cpbg4   1/1     Running   0          132m   10.244.1.24   node1   <none>           <none>
nginx-6799fc88d8-plvhf   1/1     Running   0          95m    10.244.1.31   node1   <none>           <none>
nginx-6799fc88d8-rf74m   1/1     Running   1          97m    10.244.2.4    node2   <none>  

[root@master ~]# kubectl scale deployment/nginx --replicas 2
[root@master ~]# kubectl get pod -o wide
NAME                     READY   STATUS        RESTARTS   AGE     IP            NODE     NOMINATED NODE   READINESS GATES
nginx-6799fc88d8-9wk4h   0/1     Pending       0          3m46s   <none>        <none>   <none>           <none>
nginx-6799fc88d8-cpbg4   1/1     Terminating   0          138m    10.244.1.24   node1    <none>           <none>
nginx-6799fc88d8-plvhf   1/1     Terminating   0          101m    10.244.1.31   node1    <none>           <none>
nginx-6799fc88d8-rf74m   1/1     Running       1          103m    10.244.2.4    node2    <none>           <none>
nginx-6799fc88d8-snm2v   0/1     Pending       0          16s     <none>        <none>   <none> 

uncordon

不可调度

[root@master ~]# kubectl uncordon node2
node/node2 uncordoned
[root@master ~]# kubectl get node
NAME     STATUS     ROLES                  AGE    VERSION
master   Ready      control-plane,master   2d4h   v1.20.0
node1    NotReady   <none>                 2d4h   v1.20.0
node2    Ready      <none>                 2d4h   v1.20.0

[root@master ~]# kubectl create deployment nginx2  --image nginx
deployment.apps/nginx2 created
[root@master ~]# kubectl get pod
NAME                      READY   STATUS        RESTARTS   AGE
nginx-6799fc88d8-9wk4h    1/1     Running       0          8m22s
nginx-6799fc88d8-cpbg4    1/1     Terminating   0          143m
nginx-6799fc88d8-plvhf    1/1     Terminating   0          106m
nginx-6799fc88d8-rf74m    1/1     Running       1          108m
nginx-6799fc88d8-snm2v    1/1     Running       0          4m52s
nginx2-5fc4444698-bf76p   1/1     Running       0          25s
[root@master ~]# kubectl get pod -o wide
NAME                      READY   STATUS        RESTARTS   AGE     IP            NODE    NOMINATED NODE   READINESS GATES
nginx-6799fc88d8-9wk4h    1/1     Running       0          8m59s   10.244.2.5    node2   <none>           <none>
nginx-6799fc88d8-cpbg4    1/1     Terminating   0          143m    10.244.1.24   node1   <none>           <none>
nginx-6799fc88d8-plvhf    1/1     Terminating   0          106m    10.244.1.31   node1   <none>           <none>
nginx-6799fc88d8-rf74m    1/1     Running       1          109m    10.244.2.4    node2   <none>           <none>
nginx-6799fc88d8-snm2v    1/1     Running       0          5m29s   10.244.2.6    node2   <none>           <none>
nginx2-5fc4444698-bf76p   1/1     Running       0          62s     10.244.2.7    node2   <none>           <none>

describe

显示指定pod的详细信息

[root@master ~]# kubectl describe pod nginx
Name:         nginx-6799fc88d8-cpbg4
Namespace:    default
Priority:     0
Node:         node1/192.168.129.135
Start Time:   Mon, 20 Dec 2021 18:33:49 +0800
Labels:       app=nginx
              pod-template-hash=6799fc88d8
Annotations:  <none>
Status:       Running
IP:           10.244.1.24
......

logs

查看日志

[root@master ~]# kubectl logs deployment/nginx
......
2021/12/20 10:34:05 [notice] 1#1: start worker processes
2021/12/20 10:34:05 [notice] 1#1: start worker process 32
2021/12/20 10:34:05 [notice] 1#1: start worker process 33
/docker-entrypoint.sh: Configuration complete; ready for start up

[root@master ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP    2d4h
nginx        ClusterIP   10.102.213.63   <none>        8080/TCP   24h
[root@master ~]# curl 10.102.213.63: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>

[root@master ~]#  kubectl logs deployment/nginx
......
2021/12/20 12:44:49 [notice] 1#1: start worker processes
2021/12/20 12:44:49 [notice] 1#1: start worker process 31
2021/12/20 12:44:49 [notice] 1#1: start worker process 32
10.244.0.0 - - [20/Dec/2021:13:04:14 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.61.1" "-"
10.244.0.0 - - [20/Dec/2021:13:05:26 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.61.1" "-"

attach

附加在一个容器里

[root@master ~]# kubectl get pod
NAME                     READY   STATUS    RESTARTS   AGE
nginx-6799fc88d8-cpbg4   1/1     Running   0          43m
nginx-6799fc88d8-plvhf   1/1     Running   0          6m38s
nginx-6799fc88d8-rf74m   1/1     Running   0          8m57s
[root@master ~]# kubectl attach nginx-6799fc88d8-cpbg4      
Defaulting container name to nginx.
Use 'kubectl describe pod/nginx-6799fc88d8-cpbg4 -n default' to see all of the containers in this pod.
If you don't see a command prompt, try pressing enter.
1
2
3
4
5
6
7
8
9
exec
执行容器命令

[root@master ~]# kubectl get pod
NAME                     READY   STATUS    RESTARTS   AGE
nginx-6799fc88d8-cpbg4   1/1     Running   0          45m
nginx-6799fc88d8-plvhf   1/1     Running   0          8m2s
nginx-6799fc88d8-rf74m   1/1     Running   0          10m

[root@master ~]# kubectl exec nginx-6799fc88d8-cpbg4 -- date
Mon Dec 20 11:20:02 UTC 2021

[root@master ~]# kubectl exec -it nginx-6799fc88d8-cpbg4 -- /bin/sh
# 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
# exit
[root@master ~]# 

prot-forward

转发一个或多个端口到pod里面去

[root@master ~]# kubectl get pod
NAME                     READY   STATUS        RESTARTS   AGE
web-96d5df5c8-8fzdv      1/1     Running       0          17s

[root@master ~]# kubectl port-forward deployment/web 80     #可以变成':80'(随机一个端口号,只能本机访问)或者变成指定的端口号'8080:80'(本机访问用80,其它主机访问就可以用8080)
Forwarding from 127.0.0.1:80 -> 80
Forwarding from [::1]:80 -> 80

[root@master ~]# ss -anlt
State          Recv-Q          Send-Q                     Local Address:Port                    Peer Address:Port         
......                        0.0.0.0:*            
LISTEN         0               128                            127.0.0.1:2381                         0.0.0.0:*            
LISTEN         0               128                            127.0.0.1:80                           0.0.0.0:*            

[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/web 80
Forwarding from 0.0.0.0:80 -> 80

//允许所有IP访问80端口
[root@master ~]# curl 192.168.145.250
<!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>

label

更新(增加、修改或删除)资源上的 label(标签)

label 必须以字母或数字开头,可以使用字母、数字、连字符、点和下划线,最长63个字符。
如果–overwrite 为 true,则可以覆盖已有的 label,否则尝试覆盖 label 将会报错。
如果指定了–resource-version,则更新将使用此资源版本,否则将使用现有的资源版本。
//更改标签
[root@master ~]# kubectl describe deployment/nginx
Name:                   nginx
Namespace:              default
CreationTimestamp:      Mon, 20 Dec 2021 22:20:38 +0800
Labels:                 app=nginx
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               app=nginx
Replicas:               5 desired | 5 updated | 5 total | 5 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=nginx
  Containers:
   nginx:
    Image:        nginx
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Progressing    True    NewReplicaSetAvailable
  Available      True    MinimumReplicasAvailable
OldReplicaSets:  <none>
NewReplicaSet:   nginx-6799fc88d8 (5/5 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  37m   deployment-controller  Scaled up replica set nginx-6799fc88d8 to 1
  Normal  ScalingReplicaSet  29m   deployment-controller  Scaled up replica set nginx-6799fc88d8 to 3
  Normal  ScalingReplicaSet  28m   deployment-controller  Scaled up replica set nginx-6799fc88d8 to 5

//追加标签
[root@master ~]# kubectl label deployment/nginx user=jing
deployment.apps/nginx labeled
[root@master ~]# kubectl describe deployment/nginx
Name:                   nginx
Namespace:              default
CreationTimestamp:      Mon, 20 Dec 2021 22:20:38 +0800
Labels:                 app=nginx
                        user=jing

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
1
2
3
4
5
6
api-versions
在服务器上以’组/版本’的形式打印支持的api版本

[root@master ~]#  kubectl api-versions
admissionregistration.k8s.io/v1
admissionregistration.k8s.io/v1beta1
apiextensions.k8s.io/v1
apiextensions.k8s.io/v1beta1
apiregistration.k8s.io/v1
apiregistration.k8s.io/v1beta1
apps/v1
authentication.k8s.io/v1
authentication.k8s.io/v1beta1
authorization.k8s.io/v1
authorization.k8s.io/v1beta1
autoscaling/v1
autoscaling/v2beta1
autoscaling/v2beta2
batch/v1

logs

查看日志

输出pod或指定资源中容器的日志。如果pod中只有一个容器,则容器名是可选的

// 查看nginx的日志
[root@master ~]# kubectl logs deployment/nginx
Found 5 pods, using pod/nginx-6799fc88d8-dwrsh
/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 14:14:43 [notice] 1#1: using the "epoll" event method
2021/12/20 14:14:43 [notice] 1#1: nginx/1.21.4
2021/12/20 14:14:43 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
2021/12/20 14:14:43 [notice] 1#1: OS: Linux 4.18.0-257.el8.x86_64
2021/12/20 14:14:43 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2021/12/20 14:14:43 [notice] 1#1: start worker processes
2021/12/20 14:14:43 [notice] 1#1: start worker process 32
2021/12/20 14:14:43 [notice] 1#1: start worker process 33

attach

附加在一个容器里

连接到一个正在运行的容器

//获取正在运行中的pod nginx的输出,默认连接到pod中的第一个容器
[root@master ~]# kubectl attach nginx
Defaulting container name to nginx.
Use 'kubectl describe pod/nginx -n default' to see all of the containers in this pod.
If you don't see a command prompt, try pressing enter.
1
2
3
4
5
exec
进到容器内执行一个命令

//默认在pod/nginx的第一个容器中运行date并打印输出
[root@master ~]# kubectl exec deployment/nginx date
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
Mon Dec 20 14:38:25 UTC 2021

port-forward

转发一个或多个端口到pod里面去

//将容器中的80端口随即映射到本机的端口
[root@master ~]# kubectl port-forward nginx-6799fc88d8-5tsjt :80
Forwarding from 127.0.0.1:46459 -> 80
Forwarding from [::1]:46459 -> 80

[root@master ~]# curl 127.0.0.1:46459
<!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 ~]#

cp

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

//将本地的anaconda-ks.cfg文件传输到pod/nginx的/tmp目录下
[root@master ~]# kubectl cp anaconda-ks.cfg nginx-6799fc88d8-5tsjt:/tmp
[root@master ~]# kubectl exec pod/nginx-6799fc88d8-5tsjt -- ls -l /tmp
total 4
-rw------- 1 root root 1252 Dec 20 14:48 anaconda-ks.cfg

快速部署一个网站

在Kubernetes部署应用流程:

  • 制作镜
  • dockerfile
  • 使用控制器部署镜像(下面三,任选一个)
  • Delopment
  • StatefulSet
  • DaemonSet
  • 对外暴露应用
  • 创建一个service让它能够访问
  • 日志、监控
  • 日常运维

基本资源概念

  • pod:k8s最小部署单元,一组容器的集合
  • Deployment:最常见的控制器,用于更高级别部署和管理pod
  • Serivce:为一组pod提供负载均衡,对外提供统一访问入口
  • Label:标签,附加到某个资源上,用于关联对象、查询和筛选
  • Namespaces:命名空间,将对象逻辑上隔离,也利于权限控制

命名空间

命名空间(Namespace):Kubernetes将资源对象逻辑上隔离,从而形成多个虚拟集群。 应用场景:

  • 根据不同团队划分命名空间
  • 根据项目划分命名空间

kubectl get namespace

  • default:默认命名空间
  • kube-system: K8s系统方面的命名空间.
  • kube-public: 公开的命名空间,谁都可以访问
  • kube-node-lease: K8s内部命名空间

两种方法指定资源命名空间:

  • 命令行加-n -yaml资源元数据里指定namespace字段
 
posted @ 2021-12-20 03:14  Aimmi  阅读(152)  评论(0)    收藏  举报