k8s:core:kind
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| k8s:core:kind [2026/05/31 03:43] – phong2018 | k8s:core:kind [2026/05/31 04:07] (current) – [Here's the relationship diagram.] phong2018 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Kubernetes kind: complete reference | + | ====== Kubernetes |
| - | '' | + | ===== Overview ===== |
| - | Each kind has its own controller, behavior, and lifecycle. | + | |
| + | In Kubernetes, every resource manifest (written in YAML or JSON) must declare four top-level fields: | ||
| + | |||
| + | <code yaml> | ||
| + | apiVersion: apps/v1 | ||
| + | kind: Deployment | ||
| + | metadata: | ||
| + | name: my-app | ||
| + | spec: | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | The '' | ||
| ---- | ---- | ||
| - | ===== ConfigMap | + | ===== Core Resource Kinds ===== |
| - | Stores non-sensitive configuration data as key-value pairs. | + | ==== 1. Pod ==== |
| - | Injected into Pods as environment variables or mounted as files. | + | |
| - | **apiVersion:** '' | + | The **smallest deployable unit** in Kubernetes. A Pod wraps one or more containers that share network and storage. |
| <code yaml> | <code yaml> | ||
| apiVersion: v1 | apiVersion: v1 | ||
| - | kind: ConfigMap | + | kind: Pod |
| metadata: | metadata: | ||
| - | name: app-config | + | name: nginx-pod |
| - | | + | |
| - | data: | + | app: nginx |
| - | | + | spec: |
| - | | + | |
| - | | + | - name: nginx |
| - | | + | image: nginx:1.25 |
| - | | + | ports: |
| - | debug: false | + | - containerPort: 80 |
| </ | </ | ||
| - | **Use as env vars:** | + | ^ Field ^ Description |
| - | <code yaml> | + | | '' |
| - | envFrom: | + | | '' |
| - | | + | | '' |
| - | name: app-config | + | |
| - | </ | + | > **Note:** Pods are rarely created directly. Use Deployments or StatefulSets instead for resilience. |
| + | |||
| + | ---- | ||
| + | |||
| + | ==== 2. Deployment ==== | ||
| + | |||
| + | Manages a **ReplicaSet** to ensure a specified number of Pod replicas are running at all times. Supports rolling updates and rollbacks. | ||
| - | **Use as mounted file:** | ||
| <code yaml> | <code yaml> | ||
| - | volumes: | + | apiVersion: apps/v1 |
| - | | + | kind: Deployment |
| - | | + | metadata: |
| - | | + | name: web-app |
| - | volumeMounts: | + | namespace: default |
| - | - name: config-vol | + | spec: |
| - | | + | replicas: 3 |
| + | selector: | ||
| + | | ||
| + | | ||
| + | template: | ||
| + | metadata: | ||
| + | labels: | ||
| + | app: web-app | ||
| + | spec: | ||
| + | | ||
| + | | ||
| + | image: nginx: | ||
| + | ports: | ||
| + | - containerPort: | ||
| </ | </ | ||
| - | > '' | + | ^ Field ^ Description |
| - | + | | '' | |
| - | ^ Field ^ Purpose ^ | + | | '' |
| - | | '' | + | | '' |
| - | | '' | + | | '' |
| - | + | ||
| - | > Use case: app settings, feature flags, config files, environment differences (dev/ | + | |
| ---- | ---- | ||
| - | ===== Secret ===== | + | ==== 3. Service |
| - | Stores sensitive data. Values are base64-encoded at rest. | + | Exposes a set of Pods as a **stable network endpoint**. Handles load balancing across all matching Pods. |
| - | Works like ConfigMap but with access restrictions and audit logging. | + | |
| - | + | ||
| - | **apiVersion:** '' | + | |
| <code yaml> | <code yaml> | ||
| apiVersion: v1 | apiVersion: v1 | ||
| - | kind: Secret | + | kind: Service |
| metadata: | metadata: | ||
| - | name: db-secret | + | name: web-service |
| - | | + | spec: |
| - | type: Opaque | + | |
| - | data: | + | app: web-app |
| - | | + | |
| - | | + | |
| + | port: 80 | ||
| + | targetPort: 80 | ||
| + | | ||
| </ | </ | ||
| - | **Secret types:** | + | ^ Service Type ^ Description |
| + | | '' | ||
| + | | '' | ||
| + | | '' | ||
| + | | '' | ||
| + | |||
| + | ---- | ||
| + | |||
| + | ==== 4. ConfigMap ==== | ||
| - | ^ Type ^ Use case ^ | + | Stores **non-sensitive configuration** as key-value pairs, decoupling config from container images. |
| - | | '' | + | |
| - | | '' | + | |
| - | | '' | + | |
| - | | '' | + | |
| - | | '' | + | |
| - | | '' | + | |
| - | **TLS Secret example:** | ||
| <code yaml> | <code yaml> | ||
| apiVersion: v1 | apiVersion: v1 | ||
| - | kind: Secret | + | kind: ConfigMap |
| metadata: | metadata: | ||
| - | name: tls-secret | + | name: app-config |
| - | type: kubernetes.io/ | + | |
| data: | data: | ||
| - | | + | |
| - | | + | |
| + | log_level: info | ||
| </ | </ | ||
| - | **Use in Pod:** | + | Use in a Pod: |
| <code yaml> | <code yaml> | ||
| - | env: | + | envFrom: |
| - | - name: DB_PASSWORD | + | - configMapRef: |
| - | valueFrom: | + | name: app-config |
| - | | + | |
| - | | + | |
| - | key: password | + | |
| </ | </ | ||
| - | |||
| - | > Use case: passwords, API keys, TLS certs, Docker pull credentials. | ||
| - | |||
| - | <note warning> | ||
| - | Base64 is encoding, NOT encryption. Use tools like Sealed Secrets or Vault for real encryption at rest. | ||
| - | </ | ||
| ---- | ---- | ||
| - | ===== Namespace ===== | + | ==== 5. Secret |
| - | Logical partition inside a cluster. | + | Like ConfigMap, but for **sensitive data** (passwords, tokens, TLS certs). Values are base64-encoded. |
| - | Isolates resources between teams, environments, | + | |
| - | + | ||
| - | **apiVersion:** '' | + | |
| <code yaml> | <code yaml> | ||
| apiVersion: v1 | apiVersion: v1 | ||
| - | kind: Namespace | + | kind: Secret |
| metadata: | metadata: | ||
| - | name: team-backend | + | name: db-credentials |
| - | | + | type: Opaque |
| - | team: backend | + | data: |
| - | env: production | + | |
| + | password: cGFzc3dvcmQ= | ||
| </ | </ | ||
| - | **Built-in namespaces:** | + | > **Security note:** base64 is encoding, not encryption. Use tools like Sealed Secrets or Vault for production. |
| - | ^ Namespace ^ Purpose ^ | + | ---- |
| - | | '' | + | |
| - | | '' | + | ==== 6. StatefulSet ==== |
| - | | '' | + | |
| - | | '' | + | Like Deployment, but designed for **stateful applications** |
| + | * Stable, persistent network identity | ||
| + | * Ordered, graceful deployment and scaling | ||
| + | * Persistent storage per pod | ||
| - | **Deploy to a namespace: | ||
| <code yaml> | <code yaml> | ||
| + | apiVersion: apps/v1 | ||
| + | kind: StatefulSet | ||
| metadata: | metadata: | ||
| - | name: my-app | + | name: mysql |
| - | | + | spec: |
| + | serviceName: | ||
| + | replicas: 3 | ||
| + | selector: | ||
| + | matchLabels: | ||
| + | | ||
| + | | ||
| + | metadata: | ||
| + | labels: | ||
| + | app: mysql | ||
| + | spec: | ||
| + | containers: | ||
| + | | ||
| + | image: mysql:8.0 | ||
| + | env: | ||
| + | - name: MYSQL_ROOT_PASSWORD | ||
| + | value: " | ||
| + | volumeMounts: | ||
| + | - name: data | ||
| + | mountPath: / | ||
| + | volumeClaimTemplates: | ||
| + | - metadata: | ||
| + | name: data | ||
| + | spec: | ||
| + | accessModes: | ||
| + | resources: | ||
| + | requests: | ||
| + | storage: 10Gi | ||
| </ | </ | ||
| - | **Namespace-scoped vs cluster-scoped:** | + | ---- |
| - | ^ Namespace-scoped ^ Cluster-scoped ^ | + | ==== 7. DaemonSet ==== |
| - | | Pod, Deployment, Service | Node, PersistentVolume | | + | |
| - | | ConfigMap, Secret | ClusterRole, | + | |
| - | | Role, RoleBinding | Namespace itself | | + | |
| - | > Use case: isolate dev/staging/prod, separate teams, apply ResourceQuota per namespace. | + | Ensures a **copy of a Pod runs on every node** (or selected nodes). Used for cluster-level services like log collectors, monitoring agents, or network plugins. |
| + | |||
| + | <code yaml> | ||
| + | apiVersion: apps/v1 | ||
| + | kind: DaemonSet | ||
| + | metadata: | ||
| + | name: fluentd | ||
| + | spec: | ||
| + | selector: | ||
| + | matchLabels: | ||
| + | name: fluentd | ||
| + | template: | ||
| + | metadata: | ||
| + | labels: | ||
| + | name: fluentd | ||
| + | spec: | ||
| + | containers: | ||
| + | - name: fluentd | ||
| + | image: fluent/fluentd:v1.16 | ||
| + | </ | ||
| ---- | ---- | ||
| - | ===== Job ===== | + | ==== 8. Job ==== |
| - | Runs a Pod to completion. | + | Runs a **one-off task** |
| - | Retries automatically on failure. | + | |
| - | + | ||
| - | **apiVersion: | + | |
| <code yaml> | <code yaml> | ||
| Line 171: | Line 233: | ||
| name: db-migration | name: db-migration | ||
| spec: | spec: | ||
| - | completions: | ||
| - | parallelism: | ||
| - | backoffLimit: | ||
| template: | template: | ||
| spec: | spec: | ||
| containers: | containers: | ||
| - name: migrate | - name: migrate | ||
| - | image: myapp:v2 | + | image: myapp:latest |
| command: [" | command: [" | ||
| - | restartPolicy: | + | restartPolicy: |
| </ | </ | ||
| - | |||
| - | **Parallel Job example (process 5 items, 2 at a time):** | ||
| - | <code yaml> | ||
| - | spec: | ||
| - | completions: | ||
| - | parallelism: | ||
| - | </ | ||
| - | |||
| - | ^ Field ^ Purpose ^ | ||
| - | | '' | ||
| - | | '' | ||
| - | | '' | ||
| - | | '' | ||
| - | | '' | ||
| - | |||
| - | > Use case: database migrations, data imports, report generation, ML training runs, batch processing. | ||
| ---- | ---- | ||
| - | ===== CronJob ===== | + | ==== 9. CronJob ==== |
| - | + | ||
| - | Runs a Job on a time-based schedule. Same as Linux '' | + | |
| - | **apiVersion:** '' | + | Runs Jobs on a **scheduled (cron) basis**. |
| <code yaml> | <code yaml> | ||
| Line 211: | Line 252: | ||
| kind: CronJob | kind: CronJob | ||
| metadata: | metadata: | ||
| - | name: nightly-backup | + | name: backup-job |
| spec: | spec: | ||
| - | schedule: "0 2 * * *" | + | schedule: "0 2 * * *" |
| - | timeZone: " | + | |
| - | concurrencyPolicy: | + | |
| - | successfulJobsHistoryLimit: | + | |
| - | failedJobsHistoryLimit: | + | |
| jobTemplate: | jobTemplate: | ||
| spec: | spec: | ||
| Line 225: | Line 262: | ||
| - name: backup | - name: backup | ||
| image: backup-tool: | image: backup-tool: | ||
| - | command: [" | + | command: ["/bin/sh", " |
| restartPolicy: | restartPolicy: | ||
| </ | </ | ||
| - | **Cron syntax:** | + | ---- |
| - | < | + | |
| - | ┌───── minute (0-59) | + | |
| - | │ ┌─── hour (0-23) | + | |
| - | │ │ ┌─ day of month (1-31) | + | |
| - | │ │ │ ┌ month (1-12) | + | |
| - | │ │ │ │ ┌ day of week (0-6, Sun=0) | + | |
| - | │ │ │ │ │ | + | |
| - | * * * * * | + | |
| - | Examples: | + | ==== 10. Namespace ==== |
| - | "0 2 * * *" | + | |
| - | "*/15 * * * *" | + | Creates a **virtual cluster** within a Kubernetes cluster, providing isolation between teams or environments. |
| - | "0 9 * * 1" | + | |
| - | "0 0 1 * *" | + | <code yaml> |
| + | apiVersion: v1 | ||
| + | kind: Namespace | ||
| + | metadata: | ||
| + | name: staging | ||
| </ | </ | ||
| - | **concurrencyPolicy values:** | + | ---- |
| - | ^ Value ^ Behavior ^ | + | ==== 11. PersistentVolumeClaim |
| - | | '' | + | |
| - | | '' | + | |
| - | | '' | + | |
| - | > Use case: backups, log rotation, cleanup tasks, scheduled reports, cache warming. | + | Requests **persistent storage** from the cluster for a Pod. |
| + | |||
| + | <code yaml> | ||
| + | apiVersion: v1 | ||
| + | kind: PersistentVolumeClaim | ||
| + | metadata: | ||
| + | name: my-storage | ||
| + | spec: | ||
| + | accessModes: | ||
| + | - ReadWriteOnce | ||
| + | resources: | ||
| + | requests: | ||
| + | storage: 5Gi | ||
| + | </ | ||
| ---- | ---- | ||
| - | ===== Ingress | + | ==== 12. Ingress ==== |
| - | HTTP/HTTPS routing rules. | + | Manages **external |
| - | Routes external traffic to internal Services based on hostname or URL path. | + | |
| - | Requires an Ingress Controller (nginx, traefik, etc.) to be installed. | + | |
| - | + | ||
| - | **apiVersion: | + | |
| <code yaml> | <code yaml> | ||
| Line 269: | Line 308: | ||
| kind: Ingress | kind: Ingress | ||
| metadata: | metadata: | ||
| - | name: main-ingress | + | name: web-ingress |
| annotations: | annotations: | ||
| nginx.ingress.kubernetes.io/ | nginx.ingress.kubernetes.io/ | ||
| spec: | spec: | ||
| - | ingressClassName: | ||
| - | tls: | ||
| - | - hosts: | ||
| - | - myapp.example.com | ||
| - | secretName: tls-secret | ||
| rules: | rules: | ||
| - host: myapp.example.com | - host: myapp.example.com | ||
| http: | http: | ||
| paths: | paths: | ||
| - | - path: /api | ||
| - | pathType: Prefix | ||
| - | backend: | ||
| - | service: | ||
| - | name: api-service | ||
| - | port: | ||
| - | number: 8080 | ||
| - path: / | - path: / | ||
| pathType: Prefix | pathType: Prefix | ||
| backend: | backend: | ||
| service: | service: | ||
| - | name: frontend-service | + | name: web-service |
| port: | port: | ||
| number: 80 | number: 80 | ||
| </ | </ | ||
| - | |||
| - | **pathType values:** | ||
| - | |||
| - | ^ Value ^ Behavior ^ | ||
| - | | '' | ||
| - | | '' | ||
| - | | '' | ||
| - | |||
| - | **Traffic flow:** | ||
| - | < | ||
| - | Client | ||
| - | └── Ingress Controller (nginx/ | ||
| - | └── Ingress rules | ||
| - | ├── /api → api-service → api Pods | ||
| - | └── / → frontend-service → frontend Pods | ||
| - | </ | ||
| - | |||
| - | > Use case: expose multiple services under one domain, TLS termination, | ||
| ---- | ---- | ||
| - | ===== ClusterRole | + | ===== Summary Table ===== |
| - | Defines permissions that apply cluster-wide (all namespaces + cluster-scoped resources). | + | ^ Kind ^ API Version |
| - | Part of Kubernetes RBAC (Role-Based Access Control). | + | | '' |
| - | + | | '' | |
| - | **apiVersion: | + | | '' |
| - | + | | '' | |
| - | <code yaml> | + | | '' |
| - | apiVersion: rbac.authorization.k8s.io/ | + | | '' |
| - | kind: ClusterRole | + | | '' |
| - | metadata: | + | | '' |
| - | name: pod-reader | + | | '' |
| - | rules: | + | | '' |
| - | - apiGroups: ["" | + | | '' |
| - | resources: [" | + | | '' |
| - | verbs: [" | + | |
| - | - apiGroups: [" | + | |
| - | resources: [" | + | |
| - | verbs: [" | + | |
| - | - apiGroups: ["" | + | |
| - | resources: [" | + | |
| - | verbs: [" | + | |
| - | </ | + | |
| - | + | ||
| - | **All available verbs:** | + | |
| - | + | ||
| - | ^ Verb ^ HTTP method equivalent ^ | + | |
| - | | '' | + | |
| - | | '' | + | |
| - | | '' | + | |
| - | | '' | + | |
| - | | '' | + | |
| - | | '' | + | |
| - | | '' | + | |
| - | | '' | + | |
| - | + | ||
| - | **Wildcard (grant all):** | + | |
| - | <code yaml> | + | |
| - | rules: | + | |
| - | - apiGroups: [" | + | |
| - | resources: [" | + | |
| - | verbs: [" | + | |
| - | </code> | + | |
| - | + | ||
| - | **ClusterRole vs Role:** | + | |
| - | + | ||
| - | ^ ^ Role ^ ClusterRole ^ | + | |
| - | | Scope | Single namespace | + | |
| - | | Can access Nodes | No | Yes | | + | |
| - | | Can access PersistentVolumes | + | |
| - | | Bound with | RoleBinding | ClusterRoleBinding (or RoleBinding) | | + | |
| - | + | ||
| - | > Use case: read-only cluster monitoring, CI/CD pipelines, operators, admin access. | + | |
| - | + | ||
| - | < | + | |
| - | ClusterRole alone does nothing. It must be bound via '' | + | |
| - | </ | + | |
| ---- | ---- | ||
| - | ===== ClusterRoleBinding | + | ===== How '' |
| - | Grants a ClusterRole to a user, group, or ServiceAccount across the entire cluster. | + | The '' |
| - | **apiVersion:** '' | + | ^ apiVersion |
| - | + | | '' | |
| - | <code yaml> | + | | '' |
| - | apiVersion: rbac.authorization.k8s.io/v1 | + | | '' |
| - | kind: ClusterRoleBinding | + | | '' |
| - | metadata: | + | | '' |
| - | name: pod-reader-binding | + | |
| - | subjects: | + | |
| - | - kind: ServiceAccount | + | |
| - | name: monitoring-agent | + | |
| - | namespace: monitoring | + | |
| - | - kind: User | + | |
| - | name: jane@example.com | + | |
| - | apiGroup: rbac.authorization.k8s.io | + | |
| - | - kind: Group | + | |
| - | name: devops-team | + | |
| - | apiGroup: rbac.authorization.k8s.io | + | |
| - | roleRef: | + | |
| - | kind: ClusterRole | + | |
| - | name: pod-reader | + | |
| - | apiGroup: rbac.authorization.k8s.io | + | |
| - | </ | + | |
| - | + | ||
| - | **subjects kinds:** | + | |
| - | + | ||
| - | ^ Kind ^ Example ^ | + | |
| - | | '' | + | |
| - | | '' | + | |
| - | | '' | + | |
| - | + | ||
| - | > Use case: grant monitoring agent read access to all Pods cluster-wide. | + | |
| ---- | ---- | ||
| - | ===== HorizontalPodAutoscaler | + | ===== Here's the relationship diagram. |
| + | Hierarchy flows top-down: | ||
| - | Automatically scales | + | Deployment → ReplicaSet → Pod (Deployment |
| - | Watches a target | + | |
| - | **apiVersion: | + | StatefulSet and DaemonSet go straight to their own Pods |
| - | <code yaml> | + | CronJob → Job (CronJob triggers Jobs on a schedule) |
| - | apiVersion: autoscaling/ | + | |
| - | kind: HorizontalPodAutoscaler | + | |
| - | metadata: | + | |
| - | name: web-hpa | + | |
| - | spec: | + | |
| - | scaleTargetRef: | + | |
| - | apiVersion: apps/v1 | + | |
| - | kind: Deployment | + | |
| - | name: web-app | + | |
| - | minReplicas: | + | |
| - | maxReplicas: | + | |
| - | metrics: | + | |
| - | - type: Resource | + | |
| - | resource: | + | |
| - | name: cpu | + | |
| - | target: | + | |
| - | type: Utilization | + | |
| - | averageUtilization: | + | |
| - | - type: Resource | + | |
| - | resource: | + | |
| - | name: memory | + | |
| - | target: | + | |
| - | type: AverageValue | + | |
| - | averageValue: | + | |
| - | </ | + | |
| - | **Custom metrics example (requests per second):** | + | Networking routes traffic inward: |
| - | <code yaml> | + | |
| - | metrics: | + | |
| - | - type: Pods | + | |
| - | pods: | + | |
| - | metric: | + | |
| - | name: requests_per_second | + | |
| - | target: | + | |
| - | type: AverageValue | + | |
| - | averageValue: | + | |
| - | </ | + | |
| - | **Scale behavior | + | Ingress → Service → Pod (external HTTP traffic funneled |
| - | <code yaml> | + | |
| - | behavior: | + | |
| - | scaleUp: | + | |
| - | stabilizationWindowSeconds: | + | |
| - | policies: | + | |
| - | - type: Pods | + | |
| - | value: 4 | + | |
| - | periodSeconds: | + | |
| - | scaleDown: | + | |
| - | stabilizationWindowSeconds: | + | |
| - | </ | + | |
| - | ^ Field ^ Purpose ^ | + | NetworkPolicy applies traffic rules at the Pod level |
| - | | '' | + | |
| - | | '' | + | |
| - | | '' | + | |
| - | | '' | + | |
| - | **How it works:** | + | Config and Storage are mounted into Pods: |
| - | < | + | |
| - | Metrics Server | + | |
| - | └── HPA checks metrics every 15s | + | |
| - | ├── CPU > 70% → scale UP (add Pods) | + | |
| - | └── CPU < 70% → scale DOWN (remove Pods) | + | |
| - | └── Deployment adjusts replicas | + | |
| - | </ | + | |
| - | > Use case: web traffic spikes, variable batch load, cost optimization | + | ConfigMap and Secret are injected as env vars or volume mounts |
| - | <note important> | + | PVC (PersistentVolumeClaim) binds to a PV (PersistentVolume) and mounts into Pods |
| - | HPA requires Metrics Server installed in the cluster. Resource requests must be set on containers for CPU/memory HPA to work. | + | |
| - | </ | + | |
| - | ---- | + | Everything lives inside a `Namespace for isolation. |
| - | ===== Quick comparison ===== | + | {{ :k8s: |
| - | + | ||
| - | ^ Kind ^ apiVersion ^ Scope ^ Controller ^ | + | |
| - | | '' | + | |
| - | | '' | + | |
| - | | '' | + | |
| - | | '' | + | |
| - | | '' | + | |
| - | | '' | + | |
| - | | '' | + | |
| - | | '' | + | |
| - | | '' | + | |
| - | + | ||
| - | ---- | + | |
| - | ===== Key rule ===== | + | ===== See Also ===== |
| - | <note important> | + | * [[https:// |
| - | Every '' | + | * [[https:// |
| - | Wrong '' | + | |
| - | </ | + | |
k8s/core/kind.1780198981.txt.gz · Last modified: by phong2018
