Skip to content

Commit aea6229

Browse files
committed
Merge branch 'main' of https://github.com/anveshmuppeda/kubernetes into feature/microservicesdemo
2 parents f7012aa + 1d85fcf commit aea6229

File tree

11 files changed

+1070
-0
lines changed

11 files changed

+1070
-0
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
---
2+
// filepath: docs/015-best-practices/001-default-container.md
3+
title: "Setting a Default Container in Kubernetes"
4+
description: "A quick and easy fix to streamline your kubectl commands for multi-container pods."
5+
sidebar_label: "Default Container"
6+
sidebar_id: "default-container"
7+
sidebar_position: 1
8+
---
9+
10+
# Setting a Default Container in Kubernetes
11+
12+
**A quick and easy fix to streamline your `kubectl` commands for multi-container pods.**
13+
14+
![Default Container Annotation](./img/default-container.png)
15+
16+
When working with Kubernetes, you've likely encountered pods running multiple containers. This is a common and powerful pattern, often used for sidecars that handle tasks like logging, monitoring, or service mesh proxying. However, this setup can introduce a small but persistent annoyance: `kubectl` commands like `logs` and `exec` require you to specify which container you want to target.
17+
18+
If you forget the `-c` or `--container` flag, `kubectl` simply defaults to the first container defined in the pod's manifest. This might not be the container you're interested in, leading to repeated command corrections and a clunky workflow.
19+
20+
Fortunately, there's a simple and elegant solution to this problem: the `kubectl.kubernetes.io/default-container` annotation.
21+
22+
---
23+
24+
## The Fix: A Simple Annotation
25+
26+
By adding this annotation to your pod's metadata, you can explicitly declare which container should be the default for `kubectl` commands. This eliminates the need to constantly specify the container name, making your interactions with multi-container pods much smoother.
27+
28+
The annotation is a key-value pair:
29+
30+
* **Key:** `kubectl.kubernetes.io/default-container`
31+
* **Value:** The name of the container you want to set as the default.
32+
33+
You can apply this annotation in a few ways:
34+
35+
* **During Pod Creation:** Include the annotation directly in your pod's YAML manifest.
36+
* **To an Existing Pod:** Use the `kubectl annotate` command to add or update the annotation on a running pod.
37+
38+
---
39+
40+
## Hands-On Example
41+
42+
Let's walk through a practical example to see how this works.
43+
44+
### 1. The Problem: A Pod with Two Containers
45+
46+
First, let's create a pod with two containers: a main application container and a sidecar container.
47+
48+
Create a file named `multi-container-pod.yaml` with the following content:
49+
50+
```yaml
51+
apiVersion: v1
52+
kind: Pod
53+
metadata:
54+
name: my-app
55+
spec:
56+
containers:
57+
- name: sidecar-container
58+
image: busybox
59+
command: ["/bin/sh", "-c", "while true; do echo 'I am a sidecar'; sleep 10; done"]
60+
- name: main-container
61+
image: nginx
62+
ports:
63+
- containerPort: 80
64+
```
65+
66+
Now, create the pod using `kubectl`:
67+
68+
```bash
69+
kubectl apply -f multi-container-pod.yaml
70+
```
71+
72+
If you try to get the logs from this pod without specifying a container, `kubectl` will default to the first one listed in the manifest, which is `sidecar-container`:
73+
74+
```bash
75+
kubectl logs my-app
76+
```
77+
78+
You'll see the output from the sidecar:
79+
80+
```
81+
I am a sidecar
82+
```
83+
84+
To see the logs for the `main-container` (the NGINX container), you would need to explicitly specify it:
85+
86+
```bash
87+
kubectl logs my-app -c main-container
88+
```
89+
90+
### 2. The Solution: Applying the Annotation
91+
92+
Now, let's fix this by adding the `kubectl.kubernetes.io/default-container` annotation to our pod. We'll set `main-container` as the default.
93+
94+
#### Option A: Editing the Manifest
95+
96+
You can add the annotation directly to your `multi-container-pod.yaml` file:
97+
98+
```yaml
99+
apiVersion: v1
100+
kind: Pod
101+
metadata:
102+
name: my-app
103+
annotations:
104+
kubectl.kubernetes.io/default-container: "main-container"
105+
spec:
106+
containers:
107+
- name: sidecar-container
108+
image: busybox
109+
command: ["/bin/sh", "-c", "while true; do echo 'I am a sidecar'; sleep 10; done"]
110+
- name: main-container
111+
image: nginx
112+
ports:
113+
- containerPort: 80
114+
```
115+
116+
Then, apply the changes:
117+
118+
```bash
119+
kubectl apply -f multi-container-pod.yaml
120+
```
121+
122+
#### Option B: Using `kubectl annotate`
123+
124+
If the pod is already running, you can add the annotation using the `kubectl annotate` command:
125+
126+
```bash
127+
kubectl annotate pod my-app kubectl.kubernetes.io/default-container="main-container"
128+
```
129+
130+
### 3. The Result: Simplified Commands
131+
132+
Now that the annotation is in place, try running the `kubectl logs` command again without specifying a container:
133+
134+
```bash
135+
kubectl logs my-app
136+
```
137+
138+
This time, you'll see the logs from the `main-container` by default, as you intended. Similarly, if you were to use `kubectl exec`, you would be dropped into the `main-container`.
139+
140+
By taking a moment to add this simple annotation, you can save yourself and your team a lot of time and frustration in the long run. It's a small change that can significantly improve your daily Kubernetes workflow.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"label": "Kubernetes Best Practices",
3+
"position": 15,
4+
"link": {
5+
"type": "generated-index",
6+
"description": "Are you looking to optimize your Kubernetes deployments? This series of guides will help you navigate the complexities of Kubernetes best practices, from resource management to security. Whether you're a beginner or an experienced user, these resources will provide you with the knowledge and tools to enhance your Kubernetes operations and ensure efficient, secure, and scalable applications."
7+
}
8+
}
693 KB
Loading

0 commit comments

Comments
 (0)