Kubernetes YAML Configuration Examples
apiVersion : v1
kind : Pod
metadata :
name : nginx-pod
labels :
app : nginx
spec :
containers :
- name : nginx
image : nginx:latest
ports :
- containerPort : 80
resources :
requests :
cpu : 100m
memory : 128Mi
limits :
cpu : 250m
memory : 256Mi
2. Deployment Configuration
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:latest
ports :
- containerPort : 80
readinessProbe :
httpGet :
path : /
port : 80
initialDelaySeconds : 5
periodSeconds : 10
livenessProbe :
httpGet :
path : /
port : 80
initialDelaySeconds : 15
periodSeconds : 20
apiVersion : v1
kind : Service
metadata :
name : nginx-service
spec :
selector :
app : nginx
type : LoadBalancer
ports :
- protocol : TCP
port : 80
targetPort : 80
apiVersion : v1
kind : ConfigMap
metadata :
name : app-config
data :
DATABASE_HOST : postgres-service
DATABASE_PORT : " 5432"
LOG_LEVEL : info
apiVersion : v1
kind : Secret
metadata :
name : app-secrets
type : Opaque
stringData :
DB_PASSWORD : your-secure-password
API_KEY : your-api-key
6. Persistent Volume Claim
apiVersion : v1
kind : PersistentVolumeClaim
metadata :
name : database-storage
spec :
accessModes :
- ReadWriteOnce
resources :
requests :
storage : 5Gi
apiVersion : apps/v1
kind : StatefulSet
metadata :
name : postgres-statefulset
spec :
serviceName : " postgres"
replicas : 3
selector :
matchLabels :
app : postgres
template :
metadata :
labels :
app : postgres
spec :
containers :
- name : postgres
image : postgres:13
ports :
- containerPort : 5432
volumeMounts :
- name : postgres-storage
mountPath : /var/lib/postgresql/data
volumeClaimTemplates :
- metadata :
name : postgres-storage
spec :
accessModes : [ "ReadWriteOnce" ]
resources :
requests :
storage : 5Gi
8. Horizontal Pod Autoscaler
apiVersion : autoscaling/v2beta1
kind : HorizontalPodAutoscaler
metadata :
name : nginx-autoscaler
spec :
scaleTargetRef :
apiVersion : apps/v1
kind : Deployment
name : nginx-deployment
minReplicas : 1
maxReplicas : 10
metrics :
- type : Resource
resource :
name : cpu
targetAverageUtilization : 70
apiVersion : networking.k8s.io/v1
kind : NetworkPolicy
metadata :
name : allow-nginx
spec :
podSelector :
matchLabels :
app : nginx
ingress :
- from :
- podSelector :
matchLabels :
allow-nginx-access : " true"
ports :
- protocol : TCP
port : 80
# Role
apiVersion : rbac.authorization.k8s.io/v1
kind : Role
metadata :
namespace : default
name : pod-reader
rules :
- apiGroups : [""]
resources : ["pods"]
verbs : ["get", "watch", "list"]
# RoleBinding
apiVersion : rbac.authorization.k8s.io/v1
kind : RoleBinding
metadata :
name : read-pods
namespace : default
subjects :
- kind : User
name : jane
apiGroup : rbac.authorization.k8s.io
roleRef :
kind : Role
name : pod-reader
apiGroup : rbac.authorization.k8s.io
apiVersion : networking.k8s.io/v1
kind : Ingress
metadata :
name : minimal-ingress
annotations :
nginx.ingress.kubernetes.io/rewrite-target : /
spec :
rules :
- http :
paths :
- path : /service1
pathType : Prefix
backend :
service :
name : service1
port :
number : 80
Always validate YAML files using kubectl apply --dry-run=client -f your-file.yaml
Use proper indentation (2 or 4 spaces)
Match API versions with your Kubernetes cluster
Keep sensitive information in Secrets
Use labels and selectors effectively
Follow least privilege principle in RBAC