To define a Deployment and a StatefulSet in a Kubernetes cluster, you need to create YAML manifests that describe the desired state of these resources and apply them to the cluster using kubectl. Below are examples of YAML manifests for defining a Deployment and a StatefulSet:
- Deployment:
apiVersion: apps/v1
kind: Deployment
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:latest
ports:
- containerPort: 80
In this YAML manifest:
- apiVersion and kind specify the API version and resource type, respectively.
- metadata contains metadata such as the name of the Deployment.
- spec specifies the desired state of the Deployment.
- replicas defines the desired number of pod replicas.
- selector specifies how the Deployment selects Pods to manage.
- template defines the Pod template used to create new Pods.
- metadata.labels labels the Pods created by the Deployment.
- spec.containers defines the containers to run in the Pods, including the image and ports.
You can save this manifest to a file (e.g., deployment.yaml) and apply it to the cluster using kubectl apply -f deployment.yaml.
- StatefulSet:
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: my-statefulset
spec:
replicas: 3
selector:
matchLabels:
app: my-app
serviceName: "my-service"
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-container
image: nginx:latest
ports:
- containerPort: 80
volumeClaimTemplates:
- metadata:
name: my-volume
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 1Gi
In this YAML manifest:
- apiVersion, kind, and metadata are similar to the Deployment.
- spec specifies the desired state of the StatefulSet.
- replicas defines the desired number of pod replicas.
- selector specifies how the StatefulSet selects Pods to manage.
- serviceName specifies the name of the headless service used to control domain naming for the Pods.
- template defines the Pod template used to create new Pods.
- volumeClaimTemplates defines the PersistentVolumeClaims that Pods in the StatefulSet will have access to.
Save this manifest to a file (e.g., statefulset.yaml) and apply it to the cluster using kubectl apply -f statefulset.yaml.
These manifests will create a Deployment with three replicas running an NGINX container and a StatefulSet with three replicas running the same NGINX container with persistent volume claims. Adjust the specifications as needed to fit your specific use case.