赞
踩
工作负载型资源(workload):
Pod、ReplicaSet、Deployment、StatefulSet、DaemonSet、Job、CronJob(ReplicationController在v1.11版本被废弃)
服务发现及负载均衡型资源(ServiceDiscovery LoadBalance):
Service、Ingress、…
配置与存储型资源:
Volume(存储卷)、CSI(容器存储接口,可以扩展各种各样的第三方存储卷)
特殊类型的存储卷:
ConfigMap(当配置中心来使用的资源类型)、Secret(保存敏感数据)、DownwardAPI(把外部环境中的信息输出给容器)
以上这些资源都是配置在名称空间级别
集群级资源:
Namespace、Node、Role、ClusterRole、RoleBinding(角色绑定)、ClusterRoleBinding(集群角色绑定)
元数据型资源:
HPA(Pod水平扩展)、PodTemplate(Pod模板,用于让控制器创建Pod时使用的模板)、LimitRange(用来定义硬件资源限制的)
# 组/版本号
[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
batch/v1
certificates.k8s.io/v1
coordination.k8s.io/v1
crd.projectcalico.org/v1
discovery.k8s.io/v1
events.k8s.io/v1
flowcontrol.apiserver.k8s.io/v1
flowcontrol.apiserver.k8s.io/v1beta3
networking.k8s.io/v1
node.k8s.io/v1
policy/v1
rbac.authorization.k8s.io/v1
scheduling.k8s.io/v1
storage.k8s.io/v1
v1
[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 <ObjectMeta>
Standard object's metadata. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
spec <PodSpec>
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 <PodStatus>
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
vim nginx-deployment.yaml
apiVersion: apps/v1 #指定api版本标签
kind: Deployment #定义资源的类型/角色,deployment为副本控制器,此处资源类型可以是Deployment、Job、Ingress、Service等
metadata: #定义资源的元数据信息,比如资源的名称、namespace、标签等信息
name: nginx-deployment #定义资源的名称,在同一个namespace空间中必须是唯一的
namespace: default #默认就是default,可以不用写
labels: #定义Deployment资源标签
app: nginx
spec: #定义deployment资源需要的参数属性,诸如是否在容器失败时重新启动容器的属性
replicas: 3 #定义副本数量
selector: #定义标签选择器
matchLabels: #定义匹配标签
app: nginx #需与 .spec.template.metadata.labels 定义的标签保持一致
template: #定义业务模板,如果有多个副本,所有副本的属性会按照模板的相关配置进行匹配
metadata:
labels: #定义Pod副本将使用的标签,需与 .spec.selector.matchLabels 定义的标签保持一致
app: nginx
spec:
containers: #定义容器属性
- name: nginx #定义一个容器名,一个 - name: 定义一个容器
image: nginx:1.15.4 #定义容器使用的镜像以及版本
ports:
- containerPort: 80 #定义容器的对外的端口
#创建service服务对外提供访问并测试
vim nginx-service.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-service
labels:
app: nginx
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
nodePort: 30080
selector:
app: nginx
apiVersion: v1
kind: Pod
metadata:
name: pod-demo
namespace: default
labels:
app: myapp
spec:
containers:
- name: myapp-1
image: wangyanglinux/myapp:v1.0
- name: busybox-1
image: wangyanglinux/tools:busybox
command:
- "/bin/sh"
- "-c"
- "sleep 3600"
kubectl create -f 01.pod-demo.yaml
[root@master 4]# kubectl get pod -n default
NAME READY STATUS RESTARTS AGE
pod-demo 0/2 ContainerCreating 0 59s
# 获取当前的资源,pod
$ kubectl get pod
-A,--all-namespaces 查看当前所有名称空间的资源
-n 指定名称空间,默认值 default,kube-system 空间存放是当前组件资源
--show-labels 查看当前的标签
-l 筛选资源,key、key=value
-o wide 详细信息包括 IP、分配的节点
-w 监视,打印当前的资源对象的变化部分
# 进入 Pod 内部的容器执行命令
$ kubectl exec -it podName -c cName -- command
-c 可以省略,默认进入唯一的容器内部
# 查看资源的描述
$ kubectl explain pod.spec
# 查看 pod 内部容器的 日志
$ kubectl logs podName -c cName
# 查看资源对象的详细描述
$ kubectl describe pod podName
# 删除资源对象
$ kubectl delete kindName objName
--all 删除当前所有的资源对象
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
pod-demo 2/2 Running 1 (18m ago) 158m
[root@master ~]# kubectl get pod -A
NAMESPACE NAME READY STATUS RESTARTS AGE
default pod-demo 2/2 Running 1 (19m ago) 158m
kube-system calico-kube-controllers-558d465845-5zr5h 1/1 Running 1 (169m ago) 3h34m
kube-system calico-node-bpg47 1/1 Running 14 (169m ago) 15h
kube-system calico-node-dcqhb 1/1 Running 4 (165m ago) 15h
kube-system calico-node-nl2dw 1/1 Running 3 (164m ago) 15h
kube-system calico-typha-5b56944f9b-gcvzz 1/1 Running 4 (165m ago) 15h
kube-system coredns-857d9ff4c9-6cb2b 1/1 Running 1 (169m ago) 3h34m
kube-system coredns-857d9ff4c9-tvrff 1/1 Running 1 (169m ago) 3h34m
kube-system etcd-master 1/1 Running 7 (169m ago) 15h
kube-system kube-apiserver-master 1/1 Running 7 (169m ago) 15h
kube-system kube-controller-manager-master 1/1 Running 8 (169m ago) 15h
kube-system kube-proxy-ddqbc 1/1 Running 3 (164m ago) 15h
kube-system kube-proxy-lr5qj 1/1 Running 7 (169m ago) 15h
kube-system kube-proxy-p6hlv 1/1 Running 4 (165m ago) 15h
kube-system kube-scheduler-master 1/1 Running 8 (169m ago) 15h
[root@master ~]# kubectl get pod -n default
NAME READY STATUS RESTARTS AGE
pod-demo 2/2 Running 1 (19m ago) 159m
[root@master ~]# kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
pod-demo 2/2 Running 1 (20m ago) 159m app=myapp
[root@master ~]# kubectl get pod --show-labels -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
pod-demo 2/2 Running 1 (20m ago) 160m 10.244.104.9 node2 <none> <none> app=myapp
[root@master ~]# kubectl exec -it pod-demo -c myapp-1 -- /bin/bash
pod-demo:/# ps a
PID USER TIME COMMAND
1 root 0:00 nginx: master process /usr/local/nginx/sbin/nginx
8 nginx 0:00 nginx: worker process
62 root 0:00 /bin/bash
69 root 0:00 ps a
[root@master ~]# kubectl explain pods.spec.containers.name
KIND: Pod
VERSION: v1
FIELD: name <string>
DESCRIPTION:
Name of the container specified as a DNS_LABEL. Each container in a pod must
have a unique name (DNS_LABEL). Cannot be updated.
[root@master ~]# kubectl logs pod-demo -c myapp-1
10.0.17.100 - - [21/Aug/2024:13:12:49 +0800] "GET / HTTP/1.1" 200 48 "-" "curl/7.76.1"
10.0.17.100 - - [21/Aug/2024:13:22:23 +0800] "GET / HTTP/1.1" 200 48 "-" "curl/7.76.1"
10.0.17.100 - - [21/Aug/2024:13:23:15 +0800] "GET / HTTP/1.1" 200 59 "-" "curl/7.76.1"
[root@master ~]# kubectl describe pod pod-demo
Name: pod-demo
Namespace: default
Priority: 0
Service Account: default
Node: node2/10.0.17.102
Start Time: Wed, 21 Aug 2024 11:05:45 +0800
Labels: app=myapp
Annotations: cni.projectcalico.org/containerID: a2ba351a800e16ace038139cfb1aa8e560ac9d15634ac70fb9fc7aab9a952682
cni.projectcalico.org/podIP: 10.244.104.9/32
cni.projectcalico.org/podIPs: 10.244.104.9/32
Status: Running
IP: 10.244.104.9
IPs:
IP: 10.244.104.9
Containers:
myapp-1:
Container ID: docker://71dcc16e0e281d48d5fe392d96a90e2914318751515cd47aacdee972ec274940
Image: wangyanglinux/myapp:v1.0
Image ID: docker-pullable://wangyanglinux/myapp@sha256:77d7ec4cd4c00f79304ee9e53ca3d72e0aba22fbaf7a86797528649e3fc66e41
Port: <none>
Host Port: <none>
State: Running
Started: Wed, 21 Aug 2024 11:06:01 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-gx2qz (ro)
busybox-1:
Container ID: docker://7a90d76ed08f0e05334cec13de1dd74e544e233f3b6b1575a5f8d14f5e6104e4
Image: wangyanglinux/tools:busybox
Image ID: docker-pullable://wangyanglinux/tools@sha256:a024bc31a3a6d57ad06e0a66efa453c8cbdf818ef8d720ff6d4a36027dd1f0ae
Port: <none>
Host Port: <none>
Command:
/bin/sh
-c
sleep 3600
State: Running
Started: Wed, 21 Aug 2024 13:25:38 +0800
Last State: Terminated
Reason: Completed
Exit Code: 0
Started: Wed, 21 Aug 2024 11:06:11 +0800
Finished: Wed, 21 Aug 2024 13:25:38 +0800
Ready: True
Restart Count: 1
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-gx2qz (ro)
Conditions:
Type Status
PodReadyToStartContainers True
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
kube-api-access-gx2qz:
Type: Projected (a volume that contains injected data from multiple sources)
TokenExpirationSeconds: 3607
ConfigMapName: kube-root-ca.crt
ConfigMapOptional: <nil>
DownwardAPI: true
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
---- ------ ---- ---- -------
Normal Created 30m (x2 over 170m) kubelet Created container busybox-1
Normal Started 30m (x2 over 170m) kubelet Started container busybox-1
Normal Pulled 30m kubelet Container image "wangyanglinux/tools:busybox" already present on machine
[root@master ~]# kubectl delete pod pod-demo
pod "pod-demo" deleted
[root@master ~]# kubectl delete pod pod-demo
pod "pod-demo" deleted
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。