- kubectl get nodes -o=jsonpath='{XX}'
- This command retrieves information about the nodes in the cluster using the jsonpath output format. Replace {XX} with the desired path.
- kubectl get nodes -o=custom-columns=<Column name>
- This command retrieves information about the nodes in the cluster using custom columns output format. Replace <Column name> with the desired column name
- --sort-by=
- This option is used to sort the output based on a specified field.
- kubectl get node node01 -o json > /opt/outputs/node01.json
- This command retrieves information about a specific node and saves it as a JSON file.
- kubectl get nodes -o jsonpath='{.items[*].status.nodeInfo.osImage}' > /opt/outputs/nodes_os.txt
- This command retrieves the OS image of all the nodes in the cluster and saves it in a text file.
- kubectl config view --kubeconfig=my-kube-config -o jsonpath="{.users[*].name}" > /opt/outputs/users.txt
- This command retrieves the names of all users in the kubeconfig file and saves it in a text file.
- kubectl get pv --sort-by=.spec.capacity.storage > /opt/outputs/storage-capacity-sorted.txt
- This command retrieves the capacity of all persistent volumes and sorts the output by storage capacity.
- kubectl config view --kubeconfig=my-kube-config -o jsonpath="{.contexts[?(@.context.user=='aws-user')].name}" > /opt/outputs/aws-context-name
- This command retrieves the context name for a specific user in the kubeconfig file.
- kubectl run test-nslookup --image=busybox:1.28 --rm -it --restart=Never -- nslookup nginx-resolver-service
- This command creates a pod named test-nslookup and runs a DNS lookup on nginx-resolver-service.
- kubectl run test-nslookup --image=busybox:1.28 --rm -it --restart=Never -- nslookup nginx-resolver-service > /root/CKA/nginx.svc
- This command creates a pod named test-nslookup and redirects the output of the DNS lookup to a file.
- K get nodes -o jason | jq -c paths |grep type
- This command retrieves the paths of all fields in the node objects in the cluster that contain the word "type".
- kubectl create deployment --image=nginx nginx --replicas=4 --dry-run=client -o yaml > nginx-deployment.yaml
- This command creates a deployment named nginx with 4 replicas and saves the deployment manifest as a YAML file. The --dry-run=client flag is used to simulate the deployment without actually creating it.
Monday, April 10, 2023
Kubernetes(k8s) Sample Commands - 02
Monday, August 8, 2022
PodMan
Podman is a container engine that allows you to create, run, and manage containers on a Linux host. It is similar to other container runtimes such as Docker, Rocket, Drawbridge, and LXC. Podman has a command-line interface that is similar to Docker, making it easy to switch from Docker to Podman.
If you're new to Podman, here are some basic commands that will help you get started:
- podman login -u username -p password registry.access.redhat.com: Log in to a container registry.
- podman pull <image-name>: Download a container image.
- podman ps -a: List all containers, both running and stopped.
- podman search <image-name>: Search for a container image.
- podman images: List all container images.
- podman run <image-name> echo 'Hello world!': Run a container with a specific image and command.
- podman run -d -p 8080 httpd: Run a container with an image in the background and map port 8080.
- podman port -l: Display the port details of the last used container.
- podman run -it ubi8/ubi:8.3 /bin/bash: Run a container and enter into its bash shell.
- podman run --name MySQL-custom -e MYSQL_USER=Ruser -e MYSQL_PASSWORD=PASS -e MYSQL_ROOT_PASSWORD=PASS -d MySQL: Run a container with a custom name and environment variables.
- podman ps --format "{{.ID}} {{.Image}} {{.Names}}": List containers with custom output formatting.
In Podman, you can create both root and rootless containers. Root containers run with elevated privileges, while rootless containers run without elevated privileges and are isolated from the host system.
Here are some commands to create and manage root and rootless containers using Podman:
- sudo podman run --rm --name asroot -ti httpd /bin/bash: Run a container as root.
- podman run --rm --name asuser -ti httpd /bin/bash: Run a container as a regular user.
- podman run --name my-httpd-container httpd: Run a container with a custom name.
- podman exec httpd-container cat /etc/hostname: Run a command inside a running container.
- podman stop my-httpd-container: Stop a running container.
- podman kill -s SIGKILL my-httpd-container: Send a custom kill signal to a running container.
- podman restart my-httpd-container: Restart a container that has been stopped.
- podman rm my-httpd-container: Remove a container.
- podman rm -a: Remove all containers.
- podman stop -a: Stop all running containers.
- podman exec mysql /bin/bash -c 'mysql -uuser1 -pmypa55 -e "select * from items.Projects;"': Run a command inside a running container.
Sharing a local directory with a container is a common task in containerization. Podman makes this process simple by allowing you to mount a local directory to a container using the -v option.
Create a local directory with proper SELinux permissions
mkdir /home/student/dbfiles
podman unshare chown -R 27:27 /home/student/dbfiles
sudo semanage fcontext -a -t container_file_t '/home/student/dbfiles(/.*)?'
sudo restorecon -Rv /home/student/dbfiles
ls -ldZ /home/student/dbfiles
podman run -v /home/student/dbfiles:/var/lib/mysql rhmap47/mysql
podman unshare chown 27:27 /home/student/local/mysql
Port management
Port management is an important aspect of containerization, and Podman provides a simple way to manage ports for containers. You can use the -p option to map ports between the container and the host system.
Here's an explanation of the commands used in port management with Podman:
- podman run -d --name apache1 -p 8080:8080 httpd: Run a container with the httpd image, map port 8080 on the host system to port 8080 in the container, and name the container apache1.
- podman run -d --name apache2 -p 127.0.0.1:8081:8080 httpd: Run a container with the httpd image, map port 8081 on the localhost interface of the host system to port 8080 in the container, and name the container apache2.
- podman run -d --name apache3 -p 127.0.0.1::8080 httpd: Run a container with the httpd image, map a random port on the localhost interface of the host system to port 8080 in the container, and name the container apache3.
podman port apache3: Display the port details of the apache3 container.
In the first command, the -p option is used to map port 8080 on the host system to port 8080 in the container. This means that if you access port 8080 on the host system, you will be accessing the container's port 8080.
In the second command, the -p option is used to map port 8081 on the localhost interface of the host system to port 8080 in the container. This means that if you access port 8081 on the localhost interface of the host system, you will be accessing the container's port 8080.
In the third command, the -p option is used to map a random port on the localhost interface of the host system to port 8080 in the container. This means that a random port on the host system will be mapped to the container's port 8080.
The podman port command displays the port details of a container, including the mapping between the container's ports and the host system's ports.
By using these commands, you can easily manage ports for containers in Podman.
[registries.search]registries = ["registry.redhat.io","quay.io"]
- podman save [-o FILE_NAME] IMAGE_NAME[:TAG]: Save an image to a file. You can use the -o option to specify the output file name. For example, podman save -o mysql.tar quay.io/mysql:latest saves the quay.io/mysql:latest image to a file named mysql.tar.
- podman load [-i FILE_NAME]: Load an image from a file. You can use the -i option to specify the input file name. For example, podman load -i mysql.tar loads the mysql.tar file and creates an image.
- podman rmi [OPTIONS] IMAGE [IMAGE...]: Remove one or more images. You can use the -a option to remove all images. For example, podman rmi -a removes all images.
- podman commit [OPTIONS] CONTAINER [REPOSITORY[:PORT]/]IMAGE_NAME[:TAG]: Create a new image from a container. You can use the -a option to specify the author name. For example, podman commit -a 'Your Name' httpd httpd-new creates a new image named httpd-new from the httpd container with author name Your Name.
Here's an explanation of the few of the Podman commands:
- podman diff container-name: This command shows the differences between the container's current state and its original state at the time of its creation. The diff subcommand tags any added file with an A, any changed ones with a C, and any deleted file with a D. This is useful for troubleshooting issues or for auditing the changes made to a container.
- podman tag [OPTIONS] IMAGE[:TAG] [REGISTRYHOST/][USERNAME/]NAME[:TAG]: This command is used to tag an image with a new name or repository. You can use the [REGISTRYHOST/][USERNAME/] part to specify the registry where you want to tag the image. For example, podman tag mysql-custom devops/mysql tags the mysql-custom image with the name devops/mysql.
- podman rmi devops/mysql:snapshot: This command removes an image with the specified name and tag. For example, podman rmi devops/mysql:snapshot removes the devops/mysql image with the snapshot tag.
- podman push [OPTIONS] IMAGE [DESTINATION]: This command pushes an image to a specified destination, such as a container registry. You can use the [DESTINATION] part to specify the registry where you want to push the image. For example, podman push quay.io/bitnami/nginx pushes the quay.io/bitnami/nginx image to the specified registry.
Friday, August 5, 2022
Kubernets Components
- Kubernetes API Server: The Kubernetes API server acts as the primary management hub for the Kubernetes cluster. It exposes the Kubernetes API, which is used by other components to interact with the cluster. The API server validates and processes API requests, and updates the cluster state accordingly.
- etcd: etcd is a distributed key-value store that stores the configuration data and state of the Kubernetes cluster. It provides a reliable and consistent data store that is used by the Kubernetes API server and other components to store and retrieve data.
- kubelet: The kubelet is responsible for managing and monitoring individual nodes (worker machines) in the Kubernetes cluster. It communicates with the Kubernetes API server to ensure that the containers running on a node are healthy and running as intended.
- kube-proxy: The kube-proxy is responsible for managing network communication within the Kubernetes cluster. It sets up and maintains network routes and load balancing for Kubernetes services running on the cluster.
- Kubernetes Scheduler: The Kubernetes scheduler is responsible for scheduling workloads (containers) onto worker nodes in the cluster. It considers factors such as resource availability, workload constraints, and affinity rules to make optimal scheduling decisions.
Control Plane: Master Node, where the k8s components run
- Apiserver
- Apiserver service act as the connection between all the components in the Control Plane and Data Plane
- Orchestrating all operations in the cluster
- Expose the K8s API which end users use for operation and monitoring
- Collect data from Kubelet for Monitoring
- Authenticates - Validates - retrieve data
- Give data or do the operations with data
- Pass data to kubelet to perform operations in the Worker node
- etcd
- etcd service is mainly used for the storage of all the details. Etcd is basically a key-value pair data store.
- Store Data not limited to the following details
- Registry
- Nodes
- Pods
- Config
- Secrets
- Accounts
- Roles
- -- other components as well
- Kube scheduler
- Identify the right worker nodes in which containers can be deployed and give data back to API Servers, then kubelet get data from API server and deploys the container.
- Keeps on monitoring the API Server for operations
- Identify the right worker node for mentioned operation and give it back to API Server
- Filter nodes
- Ranks nodes :
- Resource requirements, resources left after container placement
- Taints and Tolerations
- Node Selectors/Affinity
- Labels and Selectors
- Resource limits
- Manual Scheduling
- Daemon Sets
- Multiple Schedulers
- Scheduler Events
- Kube-controller-Manager
- Watch Status
- Remediate Situations
- Monitor the state of the system and try to bring it to the desired state
- Kubectl
- Client used to connect to API Server
- Kubelet
- Agent runs on each Worker nodes
- Listens to the Kube APIs and Performs the Operation
- give back data to Kube API Server for monitoring of operation
- Kube-proxy
- Enable communication between services in Worker nodes
- Pod-Network
- by Default All pods connect to each other
- Create Iptable rules to allow communication between pods and services
Friday, January 28, 2022
Kubernetes(k8s) with Containerd Using Ansible Over Ubuntu Machines
Environment
- Ubuntu VM's running on Vmware
- K8s with Containerd Runtime
User Creation
- Asks for the User Name which has to be created
- Create's the user
- Adds a dedicated Sudo entry
- Setting up Password less sudo for user
- Copy the local uses ssh key to server for password less auth
- Print the details
- Updates the System
- Steps added for the Package Cleaning as well.
Package Installation in Master and Worker Nodes
- Copy the local host files to all the server for name resolution
- update the hostnames of the machines based on the names in host file
- Temporary Swap off
- Swap off in fstab
- Create a empty file for containerd module.
- Configure module for containerd.
- Create a empty file for kubernetes sysctl params.
- Configure sysctl params for Kubernetes.
- Apply sysctl params without reboot
- Installing Prerequisites for Kubernetes
- Add Docker’s official GPG key
- Add Docker Repository
- Install containerd.
- Configure containerd.
- Configure containerd.
- Creating containerd Config file
- Enable containerd service, and start it.
- Add Google official GPG key
- Add Kubernetes Repository
- Installing Kubernetes Cluster Packages.
- Enable service kubelet, and enable persistently
- Reboot all the Kubernetes nodes.
Master Configuration
- Pulls all needed images
- Reset Kubeadm if its already configured
- Initialize K8s cluster
- Create Directory for Kube config file in master
- Create a local kube config file in master
- Copy the kube config file to ansible local server
- Genarates the Kube toke for workers and stores it
- Copy the token to master's tmp directory
- Copy the toke to ansible local tmp direcotry
- Initialize the pod network with fannel
- Copy the output to mater file
- Copy the output to ansible local server
Worker Configuration
- Copy the token from ansible local file to worker nodes
- Reset the kubeadm
- Join the Worker node to Master by running the command
K8s should be up with the worker nodes now.
Friday, January 21, 2022
Setting up MetalLB Load Balancer with Kubernetes k8s.
- Initialize the master with Metallb Clusters
- Copy the metallb Configuration to master
- Kube apply the configuration on master.
Thursday, January 20, 2022
Kubernetes(k8s) With Ansible Over Ubuntu Machines with Docker
Kubernetes(k8s) is a popular container orchestration system that provides a powerful platform for managing containerized applications. Docker is a lightweight, yet powerful container runtime that provides the underlying infrastructure for many Kubernetes deployments. In this, we can see how to set up Kubernetes with Docker using Ansible over Ubuntu machines.
Environment
- Ubuntu VM's running on Vmware
- K8s with Docker Runtime
User Creation
- Asks for the User Name which has to be created
- Create's the user
- Adds a dedicated Sudo entry
- Setting up Password less sudo for user
- Copy the local uses ssh key to server for password less auth
- Print the details
- Updates the System
Package Installation in Master and Worker Nodes
- Copy the local host files to all the server for name resolution
- update the hostnames of the machines based on the names in host file
- Temporary Swap off
- Swap off in fstab
- Installing Kubernetes Pre-requisites packages
- Adding Docker Packages Keys
- Adding Docker Respository
- Install Docker packages
- Enables Docker Services
- Add Google repositories keys
- Create Directory for Docker deamon file
- Create the docker deamon file with Overlay details
- Restart Docker Services
- Install Kubernetes Packages
- Enabled K8s Services
- Reboot the Servers
Master Configuration
- Pulls all needed images
- Reset Kubeadm if its already configured
- Initialize K8s cluster
- Create Directory for Kube config file in master
- Create a local kube config file in master
- Copy the kube config file to ansible local server
- Genarates the Kube toke for workers and stores it
- Copy the token to master's tmp directory
- Copy the toke to ansible local tmp direcotry
- Initialize the pod network with fannel
- Copy the output to mater file
- Copy the output to ansible local server
Worker Configuration
- Copy the token from ansible local file to worker nodes
- Reset the kubeadm
- Join the Worker node to Master by running the command
K8s should be up with the worker nodes now.