Skip to content

Kubernetes

This is a Kubernetes Section!

Official Site

Cheat Sheet - really good one!

Kubectl Cheat sheet

Manifests | YAML

Basic structure

YAML
apiVersion:
kind:
metadata:
spec:

Pod

  • Simple example (Pod)
YAML
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    app: nginx
    tier: frontend
spec:
  containers:
  - name: nginx
    image: nginx

ReplicaSet

YAML
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: my-replicaset
  labels:
    name: my-replicaset-app
    type: frontend
spec:
  template:
    metadata:
      name: my-replicaset-pod
      labels:
        app: myapp-replicated
        type: frontend
    spec:
      containers:
      - name: nginx-controller
        image: nginx
  replicas: 3
  selector:
    matchLabels:
      type: frontend 

Deplyoment

This is a way to go

YAML
apiVersion: apps/v1
kind: Deployment
metadata:
  name: frontend-deployment
  labels:
    app: myapp
    type: frontend
spec:
  template:
    metadata:
      name: myapp-test
      labels:
        app: myapp
        type: frontend
    spec:
      containers:
      - name: nginx-controller
        image: nginx
  replicas: 3
  selector:
    matchLabels:
      app: myapp
 

Service

YAML
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
  labels:
    app: nginx
    svc: nginx
spec:
  type: LoadBalancer
  loadBalancerIP: 100.100.100.150
  selector:
    app: nginx # Target the deplyoment/pod label
  ports:
    - protocol: TCP
      port: 80

kubectl basics

Create an NGINX Pod

Bash
kubectl run nginx --image=nginx

Generate POD Manifest YAML file (-o yaml). Don't create it(--dry-run)

Bash
kubectl run nginx --image=nginx --dry-run=client -o yaml

Create a deployment

Bash
kubectl create deployment --image=nginx nginx

Generate Deployment YAML file (-o yaml). Don't create it(--dry-run)

Bash
kubectl create deployment --image=nginx nginx --dry-run=client -o yaml

Generate Deployment YAML file (-o yaml). Don’t create it(–dry-run) and save it to a file.

Bash
kubectl create deployment --image=nginx nginx --dry-run=client -o yaml > nginx-deployment.yaml

Deployment with 4 replicas.

Bash
kubectl create deployment --image=nginx nginx --replicas=4 --dry-run=client -o yaml > nginx-deployment.yaml

Scale replicas

Bash
kubectl scale deployment nginx --replicas=4

Create Services | ClusterIP

Bash
kubectl expose pod redis --port=6379 --name redis-service --dry-run=client -o yaml
Bash
kubectl create service clusterip redis --tcp=6379:6379 --dry-run=client -o yaml

This will not use the pods labels as selectors, instead it will assume selectors as app=redis

Create Services | NodePort

Bash
kubectl expose pod nginx --type=NodePort --port=80 --name=nginx-service --dry-run=client -o yaml

This will automatically use the pod's labels as selectors, but you cannot specify the node port.

Bash
kubectl create service nodeport nginx --tcp=80:80 --node-port=30080 --dry-run=client -o yaml

This will not use the pods labels as selectors