YAML Definition File for Kubernetes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
apiVersion: v1 kind: Pod metadata: name: myapp-pod labels: app: myapp type: front-end annotations: # Miscellaneous information (not used directly by Kubernetes) podVersion: 1.2 spec: containers: - name: nginx-container image: nginx |
Key Concepts in Kubernetes YAML Config Files:
apiVersion:
- Type: String
- Description: Specifies the API version for the Kubernetes object you’re creating. The correct API version depends on the kind of Kubernetes object you’re defining.
- Example:
apiVersion: v1
kind:
- Type: String
- Description: Indicates the type of Kubernetes object being created (e.g., Pod, Service, Deployment).
- Example:
kind: Pod
metadata:
- Type: Dictionary
- Description: Provides metadata about the Kubernetes object.
- name: The name of the Kubernetes object (required).
- Example:
name: myapp-pod
- labels: Key-value pairs used for grouping or selecting Kubernetes objects.
- Example:
- annotations: Key-value pairs for storing miscellaneous information that Kubernetes itself doesn’t use but could be utilized by other tools.
- Example:
1 2 3 4 5 6 7 |
labels: app: myapp type: front-end |
1 2 3 4 5 6 |
annotations: podVersion: 1.2 |
spec:
- Type: Dictionary
- Description: Defines the desired state of the Kubernetes object. The content here varies depending on the kind of object being created.
- Example for a Pod:
1 2 3 4 5 6 7 8 |
spec: containers: - name: nginx-container image: nginx |
Deployment Using kubectl
Command: To deploy the Kubernetes object defined in the YAML file, you use:
1 2 3 4 5 |
kubectl apply -f <filename>.yaml |
Overriding Dockerfile CMD and ENTRYPOINT in Kubernetes
Dockerfile Example:
1 2 3 4 5 6 7 |
FROM Ubuntu ENTRYPOINT sleep CMD 5 |
YAML Example:
If you want to override the CMD
or ENTRYPOINT
defined in a Dockerfile when deploying a container in Kubernetes, you can specify them in the command
and args
fields in the YAML file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
apiVersion: v1 kind: Pod metadata: name: ubuntu-sleeper-pod spec: containers: - name: ubuntu-sleeper image: ubuntu-sleeper command: ['sleeper'] # Overrides ENTRYPOINT args: ['100'] # Overrides CMD |
In this example, the command
field overrides the ENTRYPOINT
in the Dockerfile, and the args
field overrides the CMD
. This ensures that when the container is deployed in Kubernetes, it runs with the desired command and arguments.