Each configuration file has 3 parts being:
Metadata
Specification (spec)
Status
kubectl get deployment -o yaml
# Apply a configuration file
kubectl apply -f <FILENAME>
# Delete with a configuration file
kubectl delete -f <FILENAME>
nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.16
ports:
- containerPort: 80
<aside> 💡 If we need to change some configuration (i.e: replicas), we can just update the file and execute kubectl apply -f nginx-deployment.yaml again.
</aside>
The Service references the Deployment in the selector area, which is defined in the labels.app of the deployment.
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.16
ports:
- containerPort: 8080
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 8080
By default every component is created in the default namespace, we can define it by specifying a namespace in metadata.namespace:
apiVersion: v1
kind: Service
metadata:
name: nginx-service
**namespace: my-namespace**
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 8080