Monday, April 3, 2023

K8 Deployment Strategy


https://github.com/ContainerSolutions/k8s-deployment-strategies



Recreate:

https://dev.to/cloudskills/kubernetes-deployment-strategy-recreate-3kgn


Ramped/Rolling Update:

https://github.com/ContainerSolutions/k8s-deployment-strategies/tree/master/ramped


Blue-Green Deployment

https://dev.to/pavanbelagatti/kubernetes-deployments-rolling-vs-canary-vs-blue-green-4k9p



Canary Deployment:

https://github.com/NileshGule/canary-demo-kuberentes/tree/main/k8s


Monday, March 27, 2023

k8 Secret

 In Kubernetes, a Secret is an object that allows you to store and manage sensitive information, such as passwords, API keys, and certificates. 

Secrets are stored in a cluster, and they can be accessed by Pods or other Kubernetes objects.


To create a Secret, you need to encode the sensitive data as base64 and store it in a YAML file. 

Here's an example YAML file that creates a Secret named "mysecret" with a username and password:


apiVersion: v1

kind: Secret

metadata:

  name: mysecret

type: Opaque

data:

  username: dXNlcm5hbWU= # base64-encoded "username"

  password: cGFzc3dvcmQ= # base64-encoded "password"




In this example, the Secret is of type "Opaque," which means that Kubernetes doesn't understand its contents. The data field contains the encoded username and password.


Once you have created a Secret, you can reference it in your Pod's YAML file using environment variables or volumes. 


For example, to use the username and password from the "mysecret" Secret as environment variables in a Pod, you could add the following to your Pod's YAML file:


env:

- name: USERNAME

  valueFrom:

    secretKeyRef:

      name: mysecret

      key: username

- name: PASSWORD

  valueFrom:

    secretKeyRef:

      name: mysecret

      key: password


This would create two environment variables in the Pod named "USERNAME" and "PASSWORD," with values equal to the decoded contents of the "username" and "password" keys in the "mysecret" Secret, respectively.

Sunday, March 26, 2023

K8 ConfigMap

 ConfigMap:

A ConfigMap stores configuration settings that your Kubernetes Pods consume.

In Kubernetes, a ConfigMap is a resource object that stores configuration data in key-value pairs. 

This data can be used by containers running in a pod to configure applications and services. 

ConfigMaps provide a way to separate configuration data from application code, making it easier to manage configuration across multiple environments and deployments.

ConfigMaps can be created manually using the kubectl command-line tool or using YAML configuration files. 

Once a ConfigMap is created, it can be mounted as a volume or used as environment variables in a container. This allows applications to be configured without needing to rebuild or redeploy them.

ConfigMaps can be used to store any type of configuration data, including database connection strings, API endpoints, and application settings. 

They can be updated without restarting the associated containers, making it easy to make configuration changes on-the-fly. ConfigMaps can also be used in conjunction with Secrets to manage sensitive configuration data such as passwords and API keys.


How does a ConfigMap work?

A ConfigMap is a dictionary of key-value pairs that store configuration settings for your applications.

First, create a ConfigMap in your cluster by tweaking our sample YAML to your needs.

Second, consume to ConfigMap in your Pods and use its values.



Example:

This YAML creates a ConfigMap with the value database set to mongodb, and database_uri, and keys set to the values in the YAML example code.



Using a ConfigMap in Environment Variables

The key to adding your ConfigMap as environment variables to your pods is the envFrom property in your Pod’s YAML.




Friday, March 17, 2023

Minikube Setup Steps

Step-1) ubuntu@ip-172-31-3-187:~$ sudo apt-get update

Hit:1 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu jammy InRelease

Get:2 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu jammy-updates InRelease [119 kB]

Get:3 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]

Get:4 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu jammy-backports InRelease [107 kB]

Get:5 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu jammy/universe amd64 Packages [14.1 MB]

Get:6 http://security.ubuntu.com/ubuntu jammy-security/main amd64 Packages [692 kB]

Get:7 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu jammy/universe Translation-en [5652 kB]

Get:8 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu jammy/universe amd64 c-n-f Metadata [286 kB]

Get:9 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu jammy/multiverse amd64 Packages [217 kB]

Get:10 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu jammy/multiverse Translation-en [112 kB]

Get:11 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu jammy/multiverse amd64 c-n-f Metadata [8372 B]

Get:12 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages [948 kB]

Get:13 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu jammy-updates/main Translation-en [205 kB]

Get:14 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu jammy-updates/main amd64 c-n-f Metadata [13.7 kB]

Get:15 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu jammy-updates/restricted amd64 Packages [684 kB]

Get:16 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu jammy-updates/restricted Translation-en [107 kB]

Get:17 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu jammy-updates/restricted amd64 c-n-f Metadata [584 B]

Get:18 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 Packages [890 kB]

Get:19 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu jammy-updates/universe Translation-en [177 kB]

Get:20 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 c-n-f Metadata [18.1 kB]

Get:21 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu jammy-updates/multiverse amd64 Packages [24.1 kB]

Get:22 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu jammy-updates/multiverse Translation-en [6312 B]

Get:23 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu jammy-updates/multiverse amd64 c-n-f Metadata [444 B]

Get:24 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu jammy-backports/main amd64 Packages [40.7 kB]

Get:25 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu jammy-backports/main Translation-en [9800 B]

Get:26 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu jammy-backports/main amd64 c-n-f Metadata [392 B]

Get:27 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu jammy-backports/restricted amd64 c-n-f Metadata [116 B]

Get:28 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu jammy-backports/universe amd64 Packages [19.5 kB]

Get:29 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu jammy-backports/universe Translation-en [14.0 kB]

Get:30 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu jammy-backports/universe amd64 c-n-f Metadata [392 B]

Get:31 http://security.ubuntu.com/ubuntu jammy-security/main Translation-en [142 kB]

Get:32 http://security.ubuntu.com/ubuntu jammy-security/main amd64 c-n-f Metadata [8832 B]

Get:33 http://security.ubuntu.com/ubuntu jammy-security/restricted amd64 Packages [642 kB]

Get:34 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu jammy-backports/multiverse amd64 c-n-f Metadata [116 B]

Get:35 http://security.ubuntu.com/ubuntu jammy-security/restricted Translation-en [100 kB]

Get:36 http://security.ubuntu.com/ubuntu jammy-security/universe amd64 Packages [710 kB]

Get:37 http://security.ubuntu.com/ubuntu jammy-security/universe Translation-en [115 kB]

Get:38 http://security.ubuntu.com/ubuntu jammy-security/universe amd64 c-n-f Metadata [13.8 kB]

Get:39 http://security.ubuntu.com/ubuntu jammy-security/multiverse amd64 Packages [19.4 kB]

Get:40 http://security.ubuntu.com/ubuntu jammy-security/multiverse Translation-en [4068 B]

Get:41 http://security.ubuntu.com/ubuntu jammy-security/multiverse amd64 c-n-f Metadata [240 B]

Fetched 26.3 MB in 5s (5744 kB/s)

Reading package lists... Done

Step 2) 

ubuntu@ip-172-31-3-187:~$ curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current

                                 Dload  Upload   Total   Spent    Left  Speed

100 45.8M  100 45.8M    0     0  56.9M      0 --:--:-- --:--:-- --:--:-- 56.8M

ubuntu@ip-172-31-3-187:~$ ls -l

total 46912

-rw-rw-r-- 1 ubuntu ubuntu 48037888 Mar 18 03:37 kubectl

ubuntu@ip-172-31-3-187:~$ chmod +x ./kubectl

ubuntu@ip-172-31-3-187:~$ sudo mv ./kubectl /usr/local/bin/kubectl

ubuntu@ip-172-31-3-187:~$ sudo apt-get install docker.io -y

Reading package lists... Done

Building dependency tree... Done

Reading state information... Done

The following additional packages will be installed:

  bridge-utils containerd dns-root-data dnsmasq-base pigz runc ubuntu-fan

Suggested packages:

  ifupdown aufs-tools cgroupfs-mount | cgroup-lite debootstrap docker-doc rinse zfs-fuse

  | zfsutils

The following NEW packages will be installed:

  bridge-utils containerd dns-root-data dnsmasq-base docker.io pigz runc ubuntu-fan

0 upgraded, 8 newly installed, 0 to remove and 53 not upgraded.

Need to get 72.4 MB of archives.

After this operation, 287 MB of additional disk space will be used.

Get:1 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu jammy/universe amd64 pigz amd64 2.6-1 [63.6 kB]

Get:2 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu jammy/main amd64 bridge-utils amd64 1.7-1ubuntu3 [34.4 kB]

Get:3 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu jammy-updates/main amd64 runc amd64 1.1.4-0ubuntu1~22.04.1 [4241 kB]

Get:4 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu jammy-updates/main amd64 containerd amd64 1.6.12-0ubuntu1~22.04.1 [34.4 MB]

Get:5 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu jammy/main amd64 dns-root-data all 2021011101 [5256 B]

Get:6 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu jammy-updates/main amd64 dnsmasq-base amd64 2.86-1.1ubuntu0.1 [354 kB]

Get:7 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 docker.io amd64 20.10.21-0ubuntu1~22.04.2 [33.2 MB]

Get:8 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu jammy/universe amd64 ubuntu-fan all 0.12.16 [35.2 kB]

Fetched 72.4 MB in 17s (4327 kB/s)

Preconfiguring packages ...

Selecting previously unselected package pigz.

(Reading database ... 63605 files and directories currently installed.)

Preparing to unpack .../0-pigz_2.6-1_amd64.deb ...

Unpacking pigz (2.6-1) ...

Selecting previously unselected package bridge-utils.

Preparing to unpack .../1-bridge-utils_1.7-1ubuntu3_amd64.deb ...

Unpacking bridge-utils (1.7-1ubuntu3) ...

Selecting previously unselected package runc.

Preparing to unpack .../2-runc_1.1.4-0ubuntu1~22.04.1_amd64.deb ...

Unpacking runc (1.1.4-0ubuntu1~22.04.1) ...

Selecting previously unselected package containerd.

Preparing to unpack .../3-containerd_1.6.12-0ubuntu1~22.04.1_amd64.deb ...

Unpacking containerd (1.6.12-0ubuntu1~22.04.1) ...

Selecting previously unselected package dns-root-data.

Preparing to unpack .../4-dns-root-data_2021011101_all.deb ...

Unpacking dns-root-data (2021011101) ...

Selecting previously unselected package dnsmasq-base.

Preparing to unpack .../5-dnsmasq-base_2.86-1.1ubuntu0.1_amd64.deb ...

Unpacking dnsmasq-base (2.86-1.1ubuntu0.1) ...

Selecting previously unselected package docker.io.

Preparing to unpack .../6-docker.io_20.10.21-0ubuntu1~22.04.2_amd64.deb ...

Unpacking docker.io (20.10.21-0ubuntu1~22.04.2) ...

Selecting previously unselected package ubuntu-fan.

Preparing to unpack .../7-ubuntu-fan_0.12.16_all.deb ...

Unpacking ubuntu-fan (0.12.16) ...

Setting up dnsmasq-base (2.86-1.1ubuntu0.1) ...

Setting up runc (1.1.4-0ubuntu1~22.04.1) ...

Setting up dns-root-data (2021011101) ...

Setting up bridge-utils (1.7-1ubuntu3) ...

Setting up pigz (2.6-1) ...

Setting up containerd (1.6.12-0ubuntu1~22.04.1) ...

Created symlink /etc/systemd/system/multi-user.target.wants/containerd.service → /lib/systemd/system/containerd.service.

Setting up ubuntu-fan (0.12.16) ...

Created symlink /etc/systemd/system/multi-user.target.wants/ubuntu-fan.service → /lib/systemd/system/ubuntu-fan.service.

Setting up docker.io (20.10.21-0ubuntu1~22.04.2) ...

Adding group `docker' (GID 122) ...

Done.

Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /lib/systemd/system/docker.service.

Created symlink /etc/systemd/system/sockets.target.wants/docker.socket → /lib/systemd/system/docker.socket.

Processing triggers for dbus (1.12.20-2ubuntu4.1) ...

Processing triggers for man-db (2.10.2-1) ...

Scanning processes...

Scanning linux images...


Running kernel seems to be up-to-date.


No services need to be restarted.


No containers need to be restarted.


No user sessions are running outdated binaries.


No VM guests are running outdated hypervisor (qemu) binaries on this host.

ubuntu@ip-172-31-3-187:~$

ubuntu@ip-172-31-3-187:~$

ubuntu@ip-172-31-3-187:~$

ubuntu@ip-172-31-3-187:~$

ubuntu@ip-172-31-3-187:~$ ls -=l

ls: invalid option -- '='

Try 'ls --help' for more information.

ubuntu@ip-172-31-3-187:~$ ls -l

total 0


Step -3) Download minikube

ubuntu@ip-172-31-3-187:~$ curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current

                                 Dload  Upload   Total   Spent    Left  Speed

100 77.3M  100 77.3M    0     0  51.2M      0  0:00:01  0:00:01 --:--:-- 51.2M

ubuntu@ip-172-31-3-187:~$ ls -l

total 79212

-rw-rw-r-- 1 ubuntu ubuntu 81109436 Mar 18 03:40 minikube-linux-amd64

ubuntu@ip-172-31-3-187:~$ mv minikube-linux-amd64 minikube


ubuntu@ip-172-31-3-187:~$ sudo install minikube /usr/local/bin/minikube


ubuntu@ip-172-31-3-187:~$ minikube version

minikube version: v1.29.0

commit: ddac20b4b34a9c8c857fc602203b6ba2679794d3

ubuntu@ip-172-31-3-187:~$ : v1.29.0^C


Step 4) add current user in docker group

ubuntu@ip-172-31-3-187:~$ sudo usermod -aG docker $USER && newgrp docker


Step 5) Start Minikube

ubuntu@ip-172-31-3-187:~$ minikube start

😄  minikube v1.29.0 on Ubuntu 22.04 (xen/amd64)

✨  Automatically selected the docker driver. Other choices: ssh, none

📌  Using Docker driver with root privileges

👍  Starting control plane node minikube in cluster minikube

🚜  Pulling base image ...

💾  Downloading Kubernetes v1.26.1 preload ...

    > preloaded-images-k8s-v18-v1...:  397.05 MiB / 397.05 MiB  100.00% 60.18 M

    > gcr.io/k8s-minikube/kicbase...:  407.19 MiB / 407.19 MiB  100.00% 21.24 M

🔥  Creating docker container (CPUs=2, Memory=2200MB) ...

🐳  Preparing Kubernetes v1.26.1 on Docker 20.10.23 ...

    ▪ Generating certificates and keys ...

    ▪ Booting up control plane ...

    ▪ Configuring RBAC rules ...

🔗  Configuring bridge CNI (Container Networking Interface) ...

    ▪ Using image gcr.io/k8s-minikube/storage-provisioner:v5

🔎  Verifying Kubernetes components...

🌟  Enabled addons: default-storageclass, storage-provisioner

🏄  Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default

ubuntu@ip-172-31-3-187:~$ kubectl get nodes

NAME       STATUS   ROLES           AGE   VERSION

minikube   Ready    control-plane   25s   v1.26.1

ubuntu@ip-172-31-3-187:~$

Kubernetes POD

 

What are Pods in Kubernetes?

What are Kubernetes Pods? How can we use them to run our containers? How can we interact with them using kubectl and YAML?

What are Pods?

What’s the difference between single container and multi-container Pods?

How can we create Pods in Kubernetes?

apiVersion: v1
kind: Pod
metadata:
name: nginx-2
labels:
name: nginx-2
env: production
spec:
containers:
- name: nginx
image: nginx

How can we deploy and interact with our Pods using kubectl?

kubectl apply -f mypod.yaml
kubectl get pods
kubectl port-forward mypod 8080:80
kubectl delete pod mypod
kubectl delete deployment mydeployment

How can we ensure that our Pods in Kubernetes are healthy?

How to SpinUp K8s cluster

 How to Spin Up a Kubernetes Cluster

Currently, several services around the globe provide different Kubernetes implementations. Among the most popular ones, you will find:

K8 Initial Namespaces

 Initial namespaces

Kubernetes starts with four initial namespaces:

default
Kubernetes includes this namespace so that you can start using your new cluster without first creating a namespace.
kube-node-lease
This namespace holds Lease objects associated with each node. Node leases allow the kubelet to send heartbeats so that the control plane can detect node failure.
kube-public
This namespace is readable by all clients (including those not authenticated). This namespace is mostly reserved for cluster usage, in case that some resources should be visible and readable publicly throughout the whole cluster. The public aspect of this namespace is only a convention, not a requirement.
kube-system
The namespace for objects created by the Kubernetes system.


Sample Game App Deployment on EKS cluster

 https://padmakshi.medium.com/setting-up-an-eks-cluster-and-deploying-a-game-application-a-step-by-step-guide-08790e0be117