Kubernetes API Groups
Kubernetes API Groups play a crucial role in organizing and structuring the various APIs that Kubernetes provides to manage the different aspects of a Kubernetes cluster. Understanding API groups is essential for effectively interacting with the Kubernetes API and managing resources within a cluster. This guide will cover what API groups are, how they are structured, provide examples of using them, and explain how they relate to managing resources in Kubernetes.
What Are API Groups?
An API group in Kubernetes is a collection of RESTful APIs that are logically grouped together to handle a specific set of resources. API groups help in organizing the API endpoints in Kubernetes by grouping related functionalities. This modular approach allows Kubernetes to evolve without breaking backward compatibility, making it easier to introduce new features or deprecate old ones.
API groups are part of the Kubernetes API server, which is the central component of the control plane that exposes the Kubernetes API. Each API group has its own set of resources, versions, and endpoint paths.
Why Use API Groups?
Structure of API Groups
Kubernetes API groups are identified by their names and versions. The combination of an API group name and a version forms the apiVersion that you specify in your Kubernetes YAML manifests.
The structure of API groups is:
The apiVersion field in Kubernetes manifests combines the API group and version, for example, apps/v1 or batch/v1.
Example of apiVersion in Kubernetes Manifest
apiVersion: apps/v1 # API Group: apps, Version: v1
kind: Deployment # Resource type within the API group
metadata:
name: my-deployment
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-container
image: nginx:1.14.2
In this example:
Common API Groups in Kubernetes
Here are some of the most commonly used API groups in Kubernetes:
Core Group (v1): Often referred to as the legacy API group. It includes fundamental resources such as:
Example usage:
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: nginx
apps/v1: Contains resources for managing applications, such as:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment
spec:
replicas: 2
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-container
image: nginx
batch/v1: Deals with batch and job-related operations:
Example usage:
领英推荐
apiVersion: batch/v1
kind: Job
metadata:
name: my-job
spec:
template:
spec:
containers:
- name: my-job-container
image: busybox
command: ["echo", "Hello Kubernetes!"]
restartPolicy: Never
networking.k8s.io/v1: Focuses on networking features:
Example usage:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-ingress
spec:
rules:
- host: myapp.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: my-service
port:
number: 80
rbac.authorization.k8s.io/v1: Manages role-based access control (RBAC) resources:
Example usage:
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: default
name: pod-reader
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "watch", "list"]
storage.k8s.io/v1: Deals with storage management:
Example usage:
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: standard
provisioner: kubernetes.io/aws-ebs
parameters:
type: gp2
API Group Versioning
Kubernetes uses versioning within API groups to manage the lifecycle of features. The versioning follows these conventions:
How to Use API Versions
Examples of Using API Groups and Versions
Here are some practical examples of how to use different API groups and versions:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
In this example, the Deployment is using the apps/v1 API group, which is the stable version for Deployments.
Creating a Network Policy (networking.k8s.io/v1)
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-nginx
namespace: default
spec:
podSelector:
matchLabels:
app: nginx
policyTypes:
- Ingress
- Egress
ingress:
- from:
- podSelector:
matchLabels:
app: nginx
Here, a NetworkPolicy is defined to allow traffic only between Pods labeled with app: nginx. The apiVersion: networking.k8s.io/v1 indicates that this is using the stable version of the Networking API group.
Using a Beta API Group (batch/v1beta1)
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: hello
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox
args:
- /bin/sh
- -c
- date; echo Hello from the Kubernetes cluster
restartPolicy: OnFailure
in this example, a CronJob is created using the batch/v1beta1 API group. Since this is a beta version, the CronJob feature might still have some changes in future versions.
Summary