Skip to content

Kubernetes_Commands_Yamls

devopswithcloud edited this page Jul 4, 2021 · 2 revisions

Imperative Commands - Pods

kubectl run nginx --image nginx

# Attach a label to a pod
kubectl run nginx --image nginx --labels app=frontend,owner=siva

#Filter pods based on labels
kubectl get pods -l app=frontend

# Label an existing pod
kubectl label pod my-pod day=thursday billing=free

# Remove a label
 kubectl label pod my-pod day-

# Delete a pod based on labels
kubectl delete pod -l app=frontend,owner=siva

# Overwrite an existing label
kubectl label pod my-pod4 app=backend billing=myown --overwrite

# Describe the pods
kubectl describe pod <podname>

# Expose a pod
kubectl expose pod my-pod --port 80 --target-port 80 --type NodePort

# Port Forwarding to test in localhost
 sudo kubectl port-forward my-pod 8084:80

# Login to pod
kubectl exec -it podname -- /bin/bash

# Dry run pod
 kubectl run yamlpod --image nginx --dry-run=client -o yaml > yamlpo.yaml 

# Explain Pod
kubectl explain pods

# Gives Verbose logging
kubectl get pods --v 6
kubectl get pods -o wide --sort-by .spec.nodeName

# Kubectl cheatsheet
https://kubernetes.io/docs/reference/kubectl/cheatsheet/

Pods with yaml

# First Pod Example
apiVersion: v1
kind: Pod
metadata: 
  name: my-pod
  labels: 
    app: frontend
spec:
  containers:
  - name: my-container
    image: nginx

Replication Controller

apiVersion: v1
kind: ReplicationController
metadata:
  name: new-rc
spec:
  template:
    metadata:
      name: new-pod
      labels:
        env: test
        dc: mum
    spec:
      containers:
        - image: nginx
          name: nginximage
  replicas: 4
  selector: # This acts like an and operator
    env: test
    dc: mum

Replication Set

# kubectl run hydpod --image nginx -l env=dev,tier=frontend,dc=hyd
# kubectl run mumpod --image nginx -l env=dev,tier=frontend,dc=mum
# kubectl run chennaipod --image nginx -l env=dev,tier=frontend,dc=chen
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: frontend
spec:
  template:
    metadata:
      labels:
        tier: frontend
        env: dev
    spec:
      containers:
      - image: nginx
        name: frontend-image
  replicas: 3
  selector:
    matchLabels:
      env: dev
    matchExpressions:
    - { key: tier, operator: In, values: [frontend, rs]}
    - { key: dc, operator: NotIn, values: [mum, hyd]}

Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deploy
spec:
  template:
    metadata:
      labels:
        app: frontend
    spec:
      containers:
        - image: devopswithcloudhub/python_webpage:blue
          name: nginx
          ports:
            - containerPort: 80
  replicas: 2
  selector:
    matchLabels:
      app: frontend
# To scale the pods
# 1. to edit replicas count in the yaml file.
      #kubectl replace -f deploy.yaml
# 2. kubectl scale --replicas=6 -f deploy.yaml
    #or
    # kubectl scale --replicas=6 deploy frontend 

---

### Deployment - ROlling Update
```yaml
# Deployment Rolling Update
apiVersion: apps/v1
kind: Deployment
metadata:
  name: rol-deploy
spec:
  template:
    metadata:
      name: rol-pod
      labels:
        type: rol
    spec:
      containers:
        - image: nginx:1.16
          name: nginx
  replicas: 6
  selector:
    matchLabels:
      type: rol
  minReadySeconds: 10
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 50%
      maxUnavailable: 0

---

# Deployment Recreate Update
apiVersion: apps/v1
kind: Deployment
metadata:
  name: recreate-deploy
spec:
  template:
    metadata:
      name: recreate-pod
      labels:
        type: recreate
    spec:
      containers:
        - image: nginx
          name: nginx
  replicas: 5
  selector:
    matchLabels:
      type: recreate
  strategy:
    type: Recreate

### Set/Rollout/Undo Image

# kubectl set image deployment/my-deploy nginx=mginx:1.17.0
# kubectl rollout status deployment/my-deploy
# kubectl rollout pause deploy/my-deploy
# kubectl rollout resume deploy/my-deploy

Service

# Deployment
apiVersion: apps/v1
kind: Deployment
metadata: 
  name: deployment-svc-ex
spec:
  selector:
    matchLabels:
      app: test-svc
  replicas: 2
  template:
    metadata:
      labels:
        app: test-svc
    spec:
      containers:
      - image: nginx:1.19.1
        name: nginx
        ports:
        - containerPort: 80
---
# Service CLusterIP
apiVersion: v1
kind: Service
metadata:
  name: svc-clusterIP
spec:
  seletor:
    app: test-svc
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: ClusterIP
---
# Pod to test CLusterIP within cluster using svc dns name
apiVersion: v1
kind: Pod
metadata:
  name: pod-svc-test
spec:
  containers:
  - image: radial/busyboxplus:curl
    name: busybox
    command: ['sh', '-c', 'while true; do sleep 10; done']
  # kubectl exec pod-svc-test -- curl svc-clusterip
---
  ports:
Clone this wiki locally