部署您的第一个 Pod
学习使用 YAML 清单和 `kubectl`,在 Kubernetes 集群中定义并部署一个简单应用作为 Pod。
部署您的第一个 Pod 是 CoddyKit 上的免费 Docker & Kubernetes for Developers 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Docker & Kubernetes for Developers 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Docker & Kubernetes for Developers 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Pods: Smallest K8s Unit
In Kubernetes, the smallest deployable unit is a Pod. Think of a Pod as a logical host for one or more containers.
While Docker deals with individual containers, Kubernetes orchestrates Pods. A Pod encapsulates application containers, storage resources, a unique network IP, and options that govern how containers run.
Why Pods, Not Just Containers?
Why do we need Pods when we already have containers?
- Shared Resources: Containers within the same Pod share the same network namespace and IP address. They can communicate using
localhost. - Co-location: Pods ensure that co-located containers (like an app and a helper 'sidecar') are always scheduled together on the same node.
- Abstraction: Pods provide an abstraction layer over individual containers, making orchestration simpler.
Anatomy of a Pod
A Pod is more than just a container. It includes:
- Application Containers: One or more containers (e.g., your app, a logging agent).
- Storage: Shared storage volumes for data persistence.
- Network: A unique IP address and network namespace.
- Configuration: Information on how to run the containers.
Pods are designed to be ephemeral. If a Pod dies, Kubernetes creates a new one (often managed by a higher-level object).
Defining Pods with YAML
Kubernetes resources, including Pods, are defined using YAML (YAML Ain't Markup Language) files. YAML is a human-readable data serialization standard.
These files act as blueprints, telling Kubernetes what you want to create and how.
Every Kubernetes object needs these top-level fields: apiVersion, kind, metadata, and spec.
Basic Pod YAML Structure
Let's look at the basic structure of a Pod definition in YAML:
apiVersion: v1
kind: Pod
metadata:
name: my-first-pod
labels:
app: webserver
spec:
containers:
- name: my-container
image: nginx:latest
ports:
- containerPort: 80Our First Pod: Nginx
We'll deploy a simple Nginx web server as our first Pod. Nginx is a popular open-source HTTP server.
This manifest describes a Pod named nginx-pod, running a single container based on the nginx:latest Docker image, exposing port 80.
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx
spec:
containers:
- name: nginx-container
image: nginx:latest
ports:
- containerPort: 80Introducing `kubectl`
To interact with your Kubernetes cluster, you'll use the command-line tool called kubectl (pronounced "kube-control" or "kube-cuddle").
kubectl allows you to run commands against Kubernetes clusters, including deploying applications, inspecting and managing cluster resources, and viewing logs.
Ensure your kubectl is configured to point to your cluster.
Deploying Your Pod
Once you have your Pod definition in a YAML file (e.g., nginx-pod.yaml), you can deploy it to your Kubernetes cluster using the kubectl apply command.
This command tells Kubernetes to create or update resources based on the provided configuration.
Try running this:
kubectl apply -f nginx-pod.yamlChecking Pod Status
After deploying, you'll want to check if your Pod is running correctly. Use kubectl get pods to see a list of all Pods in the current namespace.
To get more detailed information about a specific Pod, use kubectl describe pod <pod-name>.
Pending: Pod accepted, but containers not yet created.Running: Pod has been bound to a node and all containers have been created.Error: At least one container terminated in failure.
kubectl get pods
kubectl describe pod nginx-podAccessing Pod Logs
When debugging applications inside your Pod, checking the container logs is crucial. Use the kubectl logs command to retrieve logs from a container within a Pod.
This helps you see what your application is doing or if any errors occurred during startup or runtime.
kubectl logs nginx-podPod Deployment Question
You've just learned how to define and deploy a Pod using YAML and kubectl. Imagine you've created my-app.yaml and deployed it.
Which command would you use to check the detailed status and events of your newly deployed Pod named my-app-pod?
Recap: Your First Pod
Congratulations! You've deployed your first Kubernetes Pod.
- Pods are the smallest deployable units in Kubernetes, encapsulating containers, storage, and network.
- You define Pods using YAML manifests with fields like
apiVersion,kind,metadata, andspec. - The
kubectlcommand-line tool is used to manage Kubernetes resources. - You deploy Pods with
kubectl apply -f <filename>, check status withkubectl get podsandkubectl describe pod, and view logs withkubectl logs.
Next, we'll explore how Deployments manage Pods at scale!
常见问题解答
「部署您的第一个 Pod」课时是免费的吗?
是的 — 「部署您的第一个 Pod」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Docker & Kubernetes for Developers 课程的其余内容,请升级到 CoddyKit PRO。 Docker & Kubernetes for Developers 课程共包含 4 节课。
「部署您的第一个 Pod」这节课中我会学到什么?
学习使用 YAML 清单和 `kubectl`,在 Kubernetes 集群中定义并部署一个简单应用作为 Pod。 你通过在浏览器中直接运行的动手代码来练习 Docker & Kubernetes for Developers,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Docker & Kubernetes for Developers 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Docker & Kubernetes for Developers 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。
「部署您的第一个 Pod」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Docker & Kubernetes for Developers 课中编写并运行代码吗?
能。每节 Docker & Kubernetes for Developers 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。