Now, create a new directory structure at manifests/elasticsearch, and in it, create a new file called deployment.yaml. Then, add the following Deployment configuration:
apiVersion: apps/v1
kind: Deployment
metadata:
name: elasticsearch
spec:
replicas: 3
selector:
matchLabels:
app: elasticsearch
template:
metadata:
name: elasticsearch
labels:
app: elasticsearch
spec:
containers:
- name: elasticsearch
image: docker.elastic.co/elasticsearch/elasticsearch-oss:6.3.2
ports:
- containerPort: 9200
- containerPort: 9300
The configuration file consists of several fields (fields marked * are required):
- apiVersion*: The version of the API. This affects the scheme expected for the configuration file. The API is broken into modular API Groups. This allows Kubernetes to develop newer features independently. It also provides Kubernetes cluster administrators more fine-grained control over which API features they want to be enabled.
The core Kubernetes objects are available in the core group (the legacy group), and you can specify this by using v1 as the apiVersion property value. Deployments are available under the apps group, and we can enable this by using apps/v1 as the apiVersion property value. Other groups include batch (provides the CronJob object), extensions, scheduling.k8s.io, settings.k8s.io, and many more. - kind*: The type of resource this manifest is specifying. In our case, we want to create a Deployment, so we should specify Deployment as the value. Other valid values for kind include Pod and ReplicaSet, but for reasons mentioned previously, you wouldn't normally use them.
- metadata: Metadata about the Deployment, such as:
- namespace: With Kubernetes, you can split a single physical cluster into multiple virtual clusters. The default namespace is default, which is sufficient for our use case.
- name: A name to identify the Deployment within the cluster.
- spec: Details the behavior of the Deployment, such as:
- replicas: The number of replica Pods, specified in the spec.template, to deploy
- template: The specification for each Pod in the ReplicaSet
- metadata: The metadata about the Pod, including a label property
- spec: The specification for each individual Pod:
- containers: A list of containers that belong in the same Pod and should be managed together.
- selector: The method by which the Deployment controller knows which Pods it should manage. We use the matchLabels criteria to match all Pods with the label app: elasticsearch. We then set the label at spec.template.metadata.labels.