Declarative YAML
  • Kubernetes configuration files - YAML or JSON
  • Each file contains one or more manifests
  • Each manifest describes an API object (deployment, job, secret)
  • Each manifest needs 4 parts (root key:values in the file):
    • apiVersion - Which version of the Kubernetes API you’re using to create this object
    • kind - What kind of object you want to create
    • metadata - Data that helps uniquely identify the object, including a name string, UID, and optional namespace
    • spec - What state you desire for the object
1
apiVersion: v1
2
kind: Pod
3
metadata:
4
name: nginx
5
spec:
6
containers:
7
- name: nginx
8
image: nginx:1.17.3
9
ports:
10
- containerPort: 80
Copied!
1
apiVersion: apps/v1
2
kind: Deployment
3
metadata:
4
name: nginx-deployment
5
spec:
6
selector:
7
matchLabels:
8
app: nginx
9
replicas: 2
10
template:
11
metadata:
12
labels:
13
app: nginx
14
spec:
15
containers:
16
- name: nginx
17
image: nginx:1.17.3
18
ports:
19
- containerPort: 80
Copied!
Declaring multiple types in a single file - separate them with "---"
1
apiVersion: v1
2
kind: Service
3
metadata:
4
name: app-nginx-service
5
spec:
6
type: NodePort
7
ports:
8
- port: 80
9
selector:
10
app: app-nginx
11
--- # !!! Separator
12
apiVersion: apps/v1
13
kind: Deployment
14
metadata:
15
name: app-nginx-deployment
16
spec:
17
replicas: 3
18
selector:
19
matchLabels:
20
app: app-nginx
21
template:
22
metadata:
23
labels:
24
app: app-nginx
25
spec:
26
containers:
27
- name: nginx
28
image: nginx:1.17.3
29
ports:
30
- containerPort: 80
Copied!
  • kubectl api-resources
  • kubectl api-versions
  • We can get all the keys each kind supports: kubectl explain <kind> --recursive
  • We can drill down into subnodes of the kind:
    kubectl explain <kind>.<subnode>
    kubectl explain services.spec

Dry Runs with Apply YAML

  • dry run client-side only:
    kubectl apply -f file.yml --dry-run
  • dry run server-side (will compare current file with what's already deployed on server):
    kubectl apply -f file.yml --server-dry-run
  • see the diff visually:
    kubectl diff -f file.yml
Last modified 1yr ago
Copy link