Tuesday, June 27, 2023

kubernetes pod sample files

simple nginx pod

apiVersion: v1
kind: Pod
metadata:
    name: nginx-pod
    labels:
        app: nginx
        tier: dev
spec:
    containers:
        - name: nginx-container
          image: nginx

multiple containers in one pod

apiVersion: v1
kind: Pod
metadata:
  name: nginx-caching-server
  labels:
    purpose: demonstrate-multi-container-pod
spec:
  containers:
  - name: nginx-container1
    image: nginx

  - name: busybox-container2
    image: busybox
    command:
      - sleep
      - "3600"

replica controller

apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx-rc
spec:
  replicas: 3
  template:
    metadata:
      name: nginx-pod
      labels:
        app: nginx-app
    spec:
      containers:
      - name: nginx-container
        image: nginx
        ports:
        - containerPort: 80
  selector:
    app: nginx-app

replica set

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: nginx-rs
spec:
  replicas: 3
  template:
    metadata:
      name: nginx-pod
      labels:
        app: nginx-app
        tier: frontend
    spec:
      containers:
      - name: nginx-container
        image: nginx
        ports:
        - containerPort: 80
  selector:
    matchLabels:
      app: nginx-app
    matchExpressions:
      - {key: tier, operator: In, values: [frontend]}

daemon set
daemon set guarantee to up and run one instance in every node

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: fluentd-ds
spec:
  template:
    metadata:
      labels:
        name: fluentd
    spec:
      containers:
      - name: fluentd
        image: gcr.io/google-containers/fluentd-elasticsearch:1.20
  selector:
    matchLabels:
      name: fluentd

up and run pods for special nodes only using daemon set(diskType=ssd)

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: nginx-ds
spec:
  template:
    metadata:
      labels:
        name: nginx
    spec:
      containers:
      - name: nginx-container
        image: nginx
      nodeSelector:
        disktype: ssd
  selector:
    matchLabels:
      name: nginx


batch execution pod. it will print 9 to 1 when execution.

apiVersion: batch/v1
kind: Job
metadata:
  name: countdown
spec:
  template:
    metadata:
      name: countdown
    spec:
      containers:
      - name: counter
        image: centos:7
        command:
         - "bin/bash"
         - "-c"
         - "for i in 9 8 7 6 5 4 3 2 1 ; do echo $i ; done"
      restartPolicy: Never

nginx deployment pod

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deploy
  labels:
    app: nginx-app
spec:
  replicas: 3
  template:
    metadata:
      labels:
        app: nginx-app
    spec:
      containers:
      - name: nginx-container
        image: nginx
        ports:
        - containerPort: 80
  selector:
    matchLabels:
      app: nginx-app

redis deployment using Recreate
In "Recreate", it will terminate all containers at once and restart required containers.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis-deployment
  labels:
    app: redis

spec:
  replicas: 10
  selector:
    matchLabels:
      app: redis
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
        - name: redis-container
          image: redis:5.0

redis deployment using RollingUpdate
in "RollingUpdate", requested number of containers will be updated.
new instance will up and old instance will down in this scenario.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis-deployment
  labels:
    app: redis

spec:
  replicas: 15
  selector:
    matchLabels:
      app: redis
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 2
      maxUnavailable: 2
  minReadySeconds: 10
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
        - name: redis-container
          image: redis:6.0 #Upgrade to 6.0 --> 6.0.16 --> 6.2.6

volume usage
when using volume, it can replicate to every containers config in pod file

apiVersion: v1
kind: Pod
metadata:
  name: sidecar-pod
spec:
  volumes:
  - name: logs
    emptyDir: {}

  containers:
  - name: app-container
    image: alpine
    command: ["/bin/sh"]
    args: ["-c", "while true; do date >> /var/log/app.log; sleep 5; done"]
    volumeMounts:
    - name: logs
      mountPath: /var/log
     
  - name: log-exporter-sidecar
    image: nginx
    ports:
      - containerPort: 80
    volumeMounts:
    - name: logs
      mountPath: /usr/share/nginx/html

volume "emptyDir" usage

apiVersion: v1
kind: Pod
metadata:
  name: nginx-emptydir
spec:
  containers:
  - name: nginx-container
    image: nginx
    volumeMounts:
    - name: test-vol
      mountPath: /test-mnt
  volumes:
  - name: test-vol
    emptyDir: {}

nginx deploymnet with 2 replicas

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 2 # tells deployment to run 2 pods matching the template
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

 

Friday, June 2, 2023

Kubernetes Commands

kubectl get nodes returns available nodes in k8s cluster

kubectl get po returns available pods in k8s cluster

kubectl get po -o wide return ip address and other details of pod 

kubectl create -f poddemo.yml create pod using yml file. this will create a pod with nginx

Sample yaml file for pod creation 

apiVersion: v1
kind: Pod
metadata:
    name: nginx-pod
    labels:
        app: nginx
        tier: dev
spec:
    containers:
        - name: nginx-container
          image: nginx

kubectl describe pod nginx-pod returns details about pod

kubectl delete pod nginx-pod delete pod nginx-pod

kubectl delete pod --all delete all pods

kubectl expose pod nginx-pod --type="NodePort" --port=8080 expose pod in 8080 port as a service

kubectl get svc returns services exposed

kubectl delete svc nginx-pod delete service  nginx-pod

kubectl exec -it nginx-caching-server bash access to nginx-caching-server bash terminal

kubectl exec -it nginx-caching-server -c busybox-container2 sh access nginx-caching-server pod's container "busybox-container2" for sh

kubectl get rc returns returns replica controllers

kubectl get rs returns replica set

kubectl scale rc nginx-rc --replicas=5 scale replica controller size to 5 for nginx-rc

kubectl delete  rc --all delete all replica controllers

kubectl delete  rs --all delete all replica sets

kubectl label nodes node2 diskType=ssd change node 2's diskType to ssh

kubectl get nodes --show-labels returns all labels

kubectl get job return batch job details

kubectl describe jobs countdown retrieve details about countdown pod's job

kubectl logs countdown-rmc97 returns logs of "countdown-rmc97" container

kubectl get deploy returns deployments

kubectl delete deploy --all delete all deployments 

kubectl describe deploy nginx-deploy returns details of "nginx-deploy" deployment

kubectl set image deploy nginx-deploy nginx-container=nginx:1.9.1 --record change the image of nginx to 1.9.1

kubectl rollout status deployment/nginx-deploy returns rollout status of nginx-deploy

kubectl rollout history deployment/nginx-deploy returns rollout history of nginx-deploy

kubectl rollout undo deployment/nginx-deploy undo the changes 

kubectl rollout status deployment/nginx-deploy returns rollout status of nginx-deploy

kubectl describe deploy nginx-deploy | grep -i image returns image details of nginx-deploy

kubectl edit deployment redis-deployment edit the deployment configurations of "redis-deployment"

Thursday, June 1, 2023

Enable RabbitMQ plugins

In RabbitMQ command prompt, use following commands to enable plugins.

rabbitmq-plugins enable rabbitmq_management

Docker Commands-Updated

  • docker -v  returns docker version 
  • docker login login to docker hub using command line
  • docker pull hello-world retrieve hello-world docker image from docker hub
  • docker run hello-world execute hello-world docker image

  • docker ps -a list all processes in docker
  • docker rmi hello-world --force delete hello-world image from docker forcefully 
  • docker rm container_id --force delete container from docker forcefully 

  • docker run -d nginx run nginx server in background
  • docker ps list running processes in docker
  • docker container inspect container_id get the all information about docker container 
  • docker run -d -p 3600:80 --name sujith-nginx nginx port forwarding with docker images execution 
  • docker exec -it container_d redis redis-cli access redis-cli after docker image execution
    • set myvalue 5 set value redis
    • get myvalue retrieve value

  • docker run -it ubuntu bash run ubuntu with bash access in docker
    • apt-get install git tree -y install git inside ubuntu
    • ctrl + p + q exit to run git in background 

  • docker container commit --author "sujith" -m "my custom image" running_container_id my_ubuntu_git_image create image using running git installed ubuntu container
  • docker docker tag my_ubuntu_git_image sujithdc/my_ubuntu_git_image tagging docker image to push to docker hub
  • docker push sujithdc/my_ubuntu_image push image to docker hub