kubernetes的pod分类,网络模型以及kuberctl命令的使用

1.什么是pod

在kubernetes集群中,pod的创建方式可以分为两类

  • 自主式pod:kubernetes直接创建出来的pod,这种pod删除后就没有了,也不会重建
  • 控制器创建的pod,通过控制器创建的pod,这种pod删除之后还会自动创建,相当于自我恢复

pod控制器是管理pod的中间层,使用了pod控制器之后,我们只需要告诉pod控制器想要运行多少个什么样的pod就可以了,它会创建出满足条件的pod,并确保每个pod处于用户期望的状态,如果pod运行过程中出现了故障,控制器也会基于指定策略重启或者重建pod

1.2 pod控制器种类

  • ReplicationController

    比较原始的pod控制器,已经启用,由ReplicaSet替代

  • ReplicaSet

    保证指定数量pod运行,并支持对pod数量进行变更、镜像版本变更

  • Deployment

    通过控制ReplicaSet资源来控制pod,并支持滚动升级、版本回退,企业中最常用的资源类型

  • Horizontal Pod Autoscaler

    可以根据集群负载自动调整pod的数量,实现削峰填谷,当pod处于高峰的时候自动增加pod的副本数,当pod处于低峰时自动减少pod的副本数,hpa不会将pod的副本数减少的比期望的副本数还要低

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

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

  • Job:它创建出来的pod只要完成任务就立即退出,不需要重启或重建,用于执行一次性任务

  • Cronjob:它创建的Pod负责周期性任务控制,不需要持续后台运行
    以上所有控制器都是用来实现一种特定的应用管理的。

2. kubernetes网络模型

Kubernetes 采用的是基于扁平地址空间的、非NAT的网络模型,每个Pod有自己唯一的IP地址。

网络是由系统管理员或CNI(container network interface)插件建立的,而非K8S本身。

K8S并不会要求用户使用指定的网络技术,但是会授权Pod(容器)在不同节点上的相互通信。

2.2 同节点Pod之间的通信

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

2.3 不同节点上的Pod通信

  • 物理桥接,但是在一定规模下容易产生网络风暴,不建议使用
  • Overlay Network 通过隧道的方式转发报文

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

2.4 Pod与Service

各节点之间是相互通信的,节点也就是真机之间的通信,因为service网络是一个iptables规则,且与真机是相连的,而pod和service是我们初始化的时候通过flannel网络进行互联,且属于同一网段。

3. kubectl命令的使用

[root@master ~]# kubectl create deployment --help
Create a deployment with the specified name.

Aliases:
deployment, deploy

Examples:
  # Create a deployment named my-dep that runs the busybox image.
  kubectl create deployment my-dep --image=busybox
  
  # Create a deployment with command
  kubectl create deployment my-dep --image=busybox -- date
  
  # Create a deployment named my-dep that runs the nginx image with 3 replicas.
  kubectl create deployment my-dep --image=nginx --replicas=3
  
  # Create a deployment named my-dep that runs the busybox image and expose port 5701.
  kubectl create deployment my-dep --image=busybox --port=5701

Options:
      --allow-missing-template-keys=true: If true, ignore any errors in templates when a field or map key is missing in
the template. Only applies to golang and jsonpath output formats.
      --dry-run='none': Must be "none", "server", or "client". If client strategy, only print the object that would be
sent, without sending it. If server strategy, submit server-side request without persisting the resource.
      --field-manager='kubectl-create': Name of the manager used to track field ownership.
      --image=[]: Image names to run.
  -o, --output='': Output format. One of:
json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-as-json|jsonpath-file.
      --port=-1: The port that this container exposes.
  -r, --replicas=1: Number of replicas to create. Default is 1.
      --save-config=false: If true, the configuration of current object will be saved in its annotation. Otherwise, the
annotation will be unchanged. This flag is useful when you want to perform kubectl apply on this object in the future.
      --template='': Template string or path to template file to use when -o=go-template, -o=go-template-file. The
template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].
      --validate=true: If true, use a schema to validate the input before sending it

Usage:
  kubectl create deployment NAME --image=image -- [COMMAND] [args...] [options]

创建一个busybox镜像名为my-dep的容器 
[root@master ~]# kubectl create deployment my-dep --image=busybox
deployment.apps/my-dep created
[root@master ~]#   
[root@master ~]# kubectl get pods -o wide
NAME                      READY   STATUS      RESTARTS   AGE    IP           NODE    NOMINATED NODE   READINESS GATES
my-dep-68d7dcffc4-drt92   0/1     Completed   0          9s     10.244.3.2   node1   <none>           <none>
nginx-6799fc88d8-qqf8d    1/1     Running     1          2d7h   10.244.2.3   node2   <none>           <none>

//创建3个nginx镜像容器
[root@master ~]# kubectl create deployment my-dep2 --image=nginx --replicas=3
deployment.apps/my-dep2 created
[root@master ~]# kubectl get pods -o wide
NAME                       READY   STATUS              RESTARTS   AGE     IP           NODE    NOMINATED NODE   READINESS GATES
my-dep-68d7dcffc4-drt92    0/1     CrashLoopBackOff    6          9m50s   10.244.3.2   node1   <none>           <none>
my-dep2-64d5dff8d8-6z4pl   0/1     ContainerCreating   0          3s      <none>       node1   <none>           <none>
my-dep2-64d5dff8d8-f7v9z   0/1     ContainerCreating   0          3s      <none>       node2   <none>           <none>
my-dep2-64d5dff8d8-kqs55   0/1     ContainerCreating   0          3s      <none>       node2   <none>           <none>
nginx-6799fc88d8-qqf8d     1/1     Running             1          2d7h    10.244.2.3   node2   <none>           <none>

//暴露80端口号
[root@master ~]# kubectl create deployment web01 --image nginx --port=80
deployment.apps/web01 created
[root@master ~]# kubectl get pods -o wide
NAME                       READY   STATUS              RESTARTS   AGE     IP           NODE    NOMINATED NODE   READINESS GATES
my-dep-68d7dcffc4-drt92    0/1     CrashLoopBackOff    7          12m     10.244.3.2   node1   <none>           <none>
my-dep2-64d5dff8d8-6z4pl   1/1     Running             0          2m57s   10.244.3.3   node1   <none>           <none>
my-dep2-64d5dff8d8-f7v9z   1/1     Running             0          2m57s   10.244.2.4   node2   <none>           <none>
my-dep2-64d5dff8d8-kqs55   1/1     Running             0          2m57s   10.244.2.5   node2   <none>           <none>
my-dep3-596db44fdd-44c7b   0/1     CrashLoopBackOff    1          60s     10.244.3.4   node1   <none>           <none>
nginx-6799fc88d8-qqf8d     1/1     Running             1          2d7h    10.244.2.3   node2   <none>           <none>
web01-59859fb9db-qz6vr     0/1     ContainerCreating   0          2s      <none>       node1   <none>           <none>

[root@master ~]# curl 10.244.3.5:80
<!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>
// 启动一个 nginx pod
[root@master ~]# kubectl run nginx --image nginx
[root@master ~]# kubectl get pods -o wide
NAME                       READY   STATUS             RESTARTS   AGE     IP           NODE    NOMINATED NODE   READINESS GATES
nginx                      1/1     Running            0          4m38s   10.244.2.6   node2   <none>           <none>

[root@master ~]# kubectl delete pods nginx  //删除nginx的pod
pod "nginx" deleted

[root@master ~]# kubectl run nginx --images nginx --port 80  // 暴露容器的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 
Name:         nginx
Namespace:    default
Priority:     0
Node:         node2/192.168.216.215
Start Time:   Mon, 20 Dec 2021 00:21:47 +0800
Labels:       run=nginx
Annotations:  <none>
Status:       Running
IP:           10.244.2.6
IPs:
  IP:  10.244.2.6
Containers:
  nginx:
    Container ID:   docker://2fdf571cd384581813f5fef184d148f87121e5a4f6855ef1ca28b762fb9ab20a
    Image:          nginx
。。。略

//测试运行并不会真的运行
[root@master ~]# kubectl run nginx --image nginx --dry-run server 
W1220 00:31:43.989318  130193 helpers.go:553] --dry-run is deprecated and can be replaced with --dry-run=client.
pod/nginx created (dry run)
[root@master ~]# kubectl delete  my-dep2-64d5dff8d8-f7v9z
error: the server doesn't have a resource type "my-dep2-64d5dff8d8-f7v9z"
[root@master ~]# kubectl delete  my-dep2
error: the server doesn't have a resource type "my-dep2"
[root@master ~]# kubectl delete deployment my-dep2
deployment.apps "my-dep2" deleted
使用deployment类型,因为我们当时创建的时候使用的是deployment类型

[root@master ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP        2d8h
nginx        NodePort    10.101.87.17   <none>        80:30337/TCP   2d7h
[root@master ~]# kubectl get pod
NAME                       READY   STATUS             RESTARTS   AGE
my-dep-68d7dcffc4-pnfl5    0/1     CrashLoopBackOff   7          14m
my-dep3-596db44fdd-44c7b   0/1     CrashLoopBackOff   8          21m
nginx                      1/1     Running            0          15m
nginx-6799fc88d8-qqf8d     1/1     Running            1          2d7h
web01-59859fb9db-qz6vr     1/1     Running            0          20m
[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   2d8h
[root@master ~]# kubectl get pod
NAME                       READY   STATUS             RESTARTS   AGE
my-dep-68d7dcffc4-pnfl5    0/1     CrashLoopBackOff   7          15m
my-dep3-596db44fdd-44c7b   0/1     CrashLoopBackOff   8          22m
nginx                      1/1     Running            0          16m
nginx-6799fc88d8-qqf8d     1/1     Running            1          2d7h
web01-59859fb9db-qz6vr     1/1     Running            0          21m
// 删除service类型的pod

//删除所有pod
[root@master ~]# kubectl delete pods --all
pod "my-dep-68d7dcffc4-pnfl5" deleted
pod "my-dep3-596db44fdd-44c7b" deleted
pod "nginx" deleted
pod "nginx-6799fc88d8-qqf8d" deleted
pod "web01-59859fb9db-qz6vr" deleted
[root@master ~]# kubectl get pod
NAME                       READY   STATUS             RESTARTS   AGE
my-dep-68d7dcffc4-85trf    0/1     CrashLoopBackOff   4          3m33s
my-dep3-596db44fdd-jx9sb   0/1     CrashLoopBackOff   4          3m33s
nginx-6799fc88d8-mqrz5     1/1     Running            0          3m33s
web01-59859fb9db-r9d52     1/1     Running            0          3m33s
//将pod中的80暴露到宿主机上的8080
[root@master ~]# kubectl expose deployment web01 --port 8080 --target-port 80
service/web01 exposed
[root@master ~]# curl 10.244.3.9:80
<!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 get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP    2d8h
web01        ClusterIP   10.105.213.189   <none>        8080/TCP   119s
[root@master ~]# curl 10.105.213.189: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>
 //列出所有的pod
[root@master ~]# kubectl get pods
NAME                       READY   STATUS             RESTARTS   AGE
my-dep-68d7dcffc4-85trf    0/1     CrashLoopBackOff   6          8m
my-dep3-596db44fdd-jx9sb   0/1     CrashLoopBackOff   6          8m
nginx-6799fc88d8-mqrz5     1/1     Running            0          8m
web01-59859fb9db-r9d52     1/1     Running            0          8m

//详细信息
[root@master ~]# kubectl get pods -o wide
NAME                       READY   STATUS             RESTARTS   AGE     IP           NODE    NOMINATED NODE   READINESS GATES
my-dep-68d7dcffc4-85trf    0/1     CrashLoopBackOff   6          8m34s   10.244.2.8   node2   <none>           <none>
my-dep3-596db44fdd-jx9sb   0/1     CrashLoopBackOff   6          8m34s   10.244.2.9   node2   <none>           <none>
nginx-6799fc88d8-mqrz5     1/1     Running            0          8m34s   10.244.3.8   node1   <none>           <none>
web01-59859fb9db-r9d52     1/1     Running            0          8m34s   10.244.3.9   node1   <none>           <none>

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

//列出所有服务
[root@master ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP    2d8h
web01        ClusterIP   10.105.213.189   <none>        8080/TCP   6m2s

//查看某个参数如何定义
[root@master ~]# kubectl explain pod
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

//查看更多
[root@master ~]# kubectl explain pod.spec
KIND:     Pod
VERSION:  v1

RESOURCE: spec <Object>

DESCRIPTION:
     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

     PodSpec is a description of a pod.

FIELDS:
   activeDeadlineSeconds        <integer>
     Optional duration in seconds the pod may be active on the node relative to
     StartTime before the system will actively try to mark it failed and kill


//查看资源信息
[root@master ~]# kubectl describe pod nginx
Name:         nginx-6799fc88d8-qqf8d
Namespace:    default
Priority:     0
Node:         node2/192.168.216.215
Start Time:   Fri, 17 Dec 2021 17:03:35 +0800
Labels:       app=nginx
              pod-template-hash=6799fc88d8
Annotations:  <none>
Status:       Running
IP:           10.244.2.3
IPs:
  IP:           10.244.2.3
Controlled By:  ReplicaSet/nginx-6799fc88d8
Containers:
  nginx:
    Container ID:   docker://58a1f79fc45b533650696800c4bbab91634dd58adf2782fdee94987c3ed53690
    Image:          nginx
    Image ID:       docker-pullable://nginx@sha256:d13dca1855de09e2fe392c58a66dd73d4ff4b71da4d1720bcf3f47b48c53ca1d
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Tue, 21 Dec 2021 12:00:42 +0800
    Last State:     Terminated
      Reason:       Error
      Exit Code:    255
      Started:      Fri, 17 Dec 2021 17:04:16 +0800
      Finished:     Tue, 21 Dec 2021 11:59:52 +0800
    Ready:          True
    Restart Count:  1
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-2xdt8 (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-2xdt8:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-2xdt8
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                 node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type     Reason                  Age                   From     Message


//可编辑资源信息
[root@master ~]# kubectl edit deployment nginx
Edit cancelled, no changes made.

//动态扩展。扩容或缩容 Deployment、ReplicaSet、Replication Controller或 Job 中Pod数量
[root@master ~]# kubectl get deployment
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
nginx   1/1     1            1           3d19h
[root@master ~]# kubectl scale --replicas 3 deployment/nginx
deployment.apps/nginx scaled
[root@master ~]#  kubectl get deployment
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
nginx   2/3     3            2           3d19h
[root@master ~]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-6799fc88d8-658ql   1/1     Running   0          20s
nginx-6799fc88d8-qqf8d   1/1     Running   1          3d19h
nginx-6799fc88d8-tp2ts   1/1     Running   0          20s

//自动扩展,给定一个范围,自动根据业务的访问量增加或减少
//最少为1,最多为5
[root@master ~]# kubectl autoscale --min 1 --max 5 deployment/nginx
horizontalpodautoscaler.autoscaling/nginx autoscaled
[root@master ~]# kubectl get hpa
NAME    REFERENCE          TARGETS         MINPODS   MAXPODS   REPLICAS   AGE
nginx   Deployment/nginx   <unknown>/80%   1         5         0          8s

//显示集群信息
[root@master ~]# kubectl cluster-info
Kubernetes control plane is running at https://192.168.216.200:6443
KubeDNS is running at https://192.168.216.200:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

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

//设置标签
[root@master ~]# kubectl label deployment/nginx user=xxx
deployment.apps/nginx labeled

//列出支持的资源类型
[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
。。。略

//以组的形式列出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
。。。略

//输出pod或指定资源中容器的日志。如果pod中只有一个容器,则容器名是可选的
[root@master ~]# kubectl logs deployment/nginx
Found 3 pods, using pod/nginx-6799fc88d8-qqf8d
/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/21 04:00:42 [notice] 1#1: using the "epoll" event method
2021/12/21 04:00:42 [notice] 1#1: nginx/1.21.4
2021/12/21 04:00:42 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
2021/12/21 04:00:42 [notice] 1#1: OS: Linux 4.18.0-305.3.1.el8.x86_64
2021/12/21 04:00:42 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2021/12/21 04:00:42 [notice] 1#1: start worker processes
2021/12/21 04:00:42 [notice] 1#1: start worker process 31
2021/12/21 04:00:42 [notice] 1#1: start worker process 32
2021/12/21 04:00:42 [notice] 1#1: start worker process 33
2021/12/21 04:00:42 [notice] 1#1: start worker process 34


//进到容器内执行一个命令 可以加-it保持会话
[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.
Tue Dec 21 04:18:49 UTC 2021

//转发一个或多个端口到pod里面去,不指定就是随机
[root@master ~]# kubectl port-forward nginx-6799fc88d8-658ql :80
Forwarding from 127.0.0.1:38497 -> 80
Forwarding from [::1]:38497 -> 80


//拷贝文件或目录到容器中,或者从容器内向外拷贝
[root@master ~]# kubectl cp anaconda-ks.cfg nginx-6799fc88d8-658ql:/tmp
[root@master ~]# kubectl exec nginx-6799fc88d8-658ql -- ls -l /tmp
total 4
-rw------- 1 root root 1084 Dec 21 04:21 anaconda-ks.cfg

4. 小结

在Kubernetes上部署应用流程:

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

基本资源概念

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

命名空间

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

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

4种命名空间

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

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

  • 命令行加-n
  • yaml资源元数据里指定namespace字段
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值