Skip to content

Kubernetes

Labels and Selectors

Grouping objects by label

kubectl get pods --selector app=APP1

Command examples

Create Pod

kubectl run nginx --image=nginx

Create Deployment

kubectl create deployment --image=nginx nginx

Generate YAML

Create Pod

kubectl run nginx --image=nginx --dry-run=client -o yaml
will create the output
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: nginx
  name: nginx
spec:
  containers:
  - image: nginx
    name: nginx
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always

Create Deployment

kubectl create deployment nginx --image=nginx --replicas=4 --dry-run=client -o yaml
will create the output:
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: nginx
  name: nginx
spec:
  replicas: 4
  selector:
    matchLabels:
      app: nginx
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}

Create Service

kubectl expose pod alpine -n falco  --port=6379 --name redis-service --dry-run=client -o yaml
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    run: alpine
  name: redis-service
  namespace: falco
spec:
  ports:
  - port: 6379
    protocol: TCP
    targetPort: 6379
  selector:
    run: alpine
status:
  loadBalancer: {}

Imperative and Declarative style

In Kubernetes you can both use declarative and imperative methods.

Imperative examples

kubectl run --image=nginx nginx
kubectl create deployment --image=nginx nginx
kubectl expose deployment nginx --port 80
kubectl edit deployment nginx
kubectl scale deployment nginx --replicas=5
kubectl set image deployment nginx nginx=nginx:1.18
kubectl create -f nginx.yaml
kubectl replace -f nginx.yaml
kubectl delete -f nginx.yaml

Declarative examples

You manage files directly and only files. Changes to the environment are only made by applying files, not using one of the above imperative examples.

kubectl apply -f nginx.yaml

apiVersion: v1
kind: Pod

metadata:
  name: nginx-pod
  labels:
    app: nginx
    type: frontend
spec:
  containers:
  - name: nginx-1
    image: nginx:1.18