rasti.hil@hilandco.com +41 79 367-9677

Search This Blog

Quotes

  • Stop dreaming, start doing!
  • Stop overthinking , start doing !
  • stop starting start finishing  (Agile Thinking)
  • Life is really simple, but we insist on making it complicated.
  • Keep calm and carry on.
  • Everything falls all the time
  • Great until they are not great
  • It is not that we have a short space of time, but that we waste much of it.

How to Execute Multiple Commands Using a Single sudo

 

sudo sh -c 'cmd1; cmd2'


The semicolon (;) serves as a special character, enabling us to segregate multiple commands within a single line. sudo sh -c 'cmd1 & cmd2'

The ampersand (&)triggers the commands to run concurrently, or simultaneously.

sudo sh -c 'mkdir DIR && cp -r $USER/* DIR'

The AND operator (&&) is employed to execute commands only if both of the specified conditions are true.


sudo sh -c 'apt-get install foo || apt-get update'

The || operator guarantees that the second command executes only if the first command fails.


sudo -- sh -c 'cmd1 & cmd2'

The double dash (--) signifies that sudo should halt processing command line arguments and interpret the remaining arguments as the command to execute.

K3s on MacBook M1 with multipass

Setting Up a Kubernetes Cluster with Multipass and K3s

In this tutorial, we will walk through the process of setting up a lightweight Kubernetes cluster using Multipass and K3s. Multipass is a tool that allows you to create and manage virtual machines on your local machine, and K3s is a lightweight Kubernetes distribution that is designed for resource-constrained environments. This combination makes it easy to create a local Kubernetes playground for development and testing purposes.

Prerequisites

Before you begin, make sure you have the following prerequisites installed on your machine:

  • Homebrew : A package manager for macOS.

Installation

To get started, open your terminal and execute the following commands to install Multipass using Homebrew:


brew install --cask multipass

Creating the Kubernetes Master Node

Let’s start by creating the Kubernetes master node using Multipass. Run the following commands to launch the VM and set its resources:


multipass launch --name k3s --mem 4G --disk 10G

After the VM is launched, access its shell using:


multipass shell k3s

Inside the VM’s shell, run the following commands to install K3s:


curl -sfL https://get.k3s.io | sh -

Retrieve the Kubernetes token and the VM’s IP address:


mytoken=$(multipass exec k3s sudo cat /var/lib/rancher/k3s/server/node-token)
myip=$(multipass info k3s | grep -i ip | tr -d ' ') ; myip=${myip/IPv4:/} ; echo $myip

Creating Worker Nodes

Next, let’s create two Kubernetes worker nodes named k3s-w1 and k3s-w2 with the following commands:


multipass launch --name k3s-w1 --mem 2G --disk 10G
multipass launch --name k3s-w2 --mem 2G --disk 10G

Run the following commands to install K3s on the worker nodes:


multipass exec k3s-w1 -- bash -c "curl -sfL https://get.k3s.io | K3S_URL=https://$myip:6443 K3S_TOKEN=$mytoken sh -"
multipass exec k3s-w2 -- bash -c "curl -sfL https://get.k3s.io | K3S_URL=https://$myip:6443 K3S_TOKEN=$mytoken sh -"

Verifying the Cluster

Now that the cluster is set up, let’s verify it by checking the nodes:


multipass exec k3s -- bash -c "sudo kubectl get nodes"

Cleanup

After you’ve finished working with the Kubernetes cluster, you can delete the VMs using the following commands:


multipass delete k3s k3s-w1 k3s-w2
multipass purge

Conclusion

In this tutorial, we’ve learned how to set up a Kubernetes cluster using Multipass and K3s. This lightweight setup is ideal for local development and testing purposes. It allows you to experiment with Kubernetes concepts without the need for a cloud-based environment. Remember that this setup is not suitable for production environments, but it’s a great tool for learning and exploration.

Remove Rancher



git clone https://github.com/rancher/rancher-cleanup.git
cd rancher-cleanup/
kubectl create -f deploy/rancher-cleanup.yaml
kubectl -n kube-system logs -l job-name=cleanup-job -f

kubectl -n cattle-system get pods | grep ^rancher | head -n 1 | awk '{ print $1 }'

kubectl create -f deploy/verify.yaml
kubectl -n kube-system logs -l job-name=verify-job -f
kubectl -n kube-system logs -l job-name=verify-job -f | grep -v "is deprecated"


kubectl get namespace



Installing Rancher on Azure Kubernetes Service


brew update && brew install azure-cli && brew install helm 


az login

az group create --name rancher02-rg --location eastus

az aks get-versions --location eastus --output table

az aks create --resource-group rancher02-rg --name rancher-server --kubernetes-version 1.19.13 --node-count 1 --node-vm-size Standard_D2_v3

az aks get-credentials --resource-group rancher02-rg --name rancher-server


helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm repo update
helm upgrade --install \
  ingress-nginx ingress-nginx/ingress-nginx \
  --namespace ingress-nginx \
  --set controller.service.type=LoadBalancer \
  --version 3.12.0 \
  --create-namespace
  
  
kubectl get service ingress-nginx-controller --namespace=ingress-nginx

https://rancher42.eastus.cloudapp.azure.com

helm repo add rancher-stable https://releases.rancher.com/server-charts/stable

kubectl create namespace cattle-system

kubectl apply -f https://github.com/jetstack/cert-manager/releases/download/v1.5.1/cert-manager.crds.yaml

#kubectl apply -f https://github.com/jetstack/cert-manager/releases/download/v1.1.0/cert-manager.yaml


helm repo add jetstack https://charts.jetstack.io

helm repo update

helm install cert-manager jetstack/cert-manager \
  --namespace cert-manager \
  --create-namespace \
  --version v1.5.1
  

kubectl get pods --namespace cert-manager

# we use letsEncrypt
# OK:
helm install rancher rancher-stable/rancher \
  --namespace cattle-system \
  --set hostname=rancher42.eastus.cloudapp.azure.com \
  --set replicas=1 \
  --set ingress.tls.source=letsEncrypt \
  --set letsEncrypt.email=mail@gmail.com

kubectl -n cattle-system rollout status deploy/rancher

kubectl -n cattle-system describe pod
kubectl -n cattle-system get pods
kubectl -n cattle-system logs -f rancher-784d94f59b-vgqzh


curl -v rancher42.eastus.cloudapp.azure.com
  

How to view SSL certificate (PEM file) using openssl ?

openssl x509 -noout -text -in cert.pem

Docker Commands



System information


docker version

docker info

Pull images

docker pull <image>

Login to the docker hub repository

docker login

List images

docker images

Remove images

doceker rmi


Run Container

docker run -p 8080:80  <container>

Run Detached Mode

docker run -it -d  --name  <container> <image>:latest bash

List Containers

docker ps -a

Stop Container

docker stop <container>

Start Container

docker start <container>

Restart Container

docker restart <container>

Remove Containers

docker rm -f  <container>

Exec Bash

 docker exec -it <container> /bin/bash

Checking History

docker history  <container>

Low-level information

docker inspect  <container>

Copy Files

docker cp file.txt <container>:/tmp

Own Docker Images

docker commit <container>

docker build <dockerfile>

List Service 

docker service ls 

Update Service 


docker service update <service_name>

List Stack

docker stack ls

Remove Stack

docker stack rm <stack>

Deploy Stack 

docker stack deploy -c <docker-stack.yml> <stack>




Using MkDocs with git on Windows and Linux



MkDocs is  static site generator.

Windows  

choco install python
choco install pip

set https_proxy=http://localhost:3128

pip install --upgrade pip --user

pip install mkdocs
setx PATH "%PATH%;C:\Users\%USERNAME%\AppData\Roaming\Python\Python39\Scripts"


cd git _folder
mkdir docs
notepad  mkdocs.yml
site_name: My Docs
nav:
    - Home: readme.md
theme: readthedocs


mkdocs build --clean


git  commit -a -m "mkdocs updates"

git push

Linux

sudo su
export HTTPS_PROXY=http://localhost:3128
pip3 install mkdocs

pip3 install docker-compose

cd git _folder

mkdir docs

echo -e "
site_name: My Docs
nav:
    - Home: readme.md
theme: readthedocs" >mkdocs.yml

echo -e "# test" > docs/readme.md

mkdocs build --clean

git  commit -a -m "mkdocs updates"

git push

Kubernetes command



# Rancher
kubectl get -n cattle-system pods 


kubectl get secret --namespace cattle-system bootstrap-secret -o go-template='{{.data.bootstrapPassword|base64decode}}{{"\n"}}'

kubectl -n cattle-system logs -f $(kubectl -n cattle-system get pods | grep ^rancher | head -n 1 | awk '{ print $1 }') 


kubectl delete namespace cattle-system


# Pods
kubectl get pods --show-labels

kubectl describe pod -l app=app1

kubectl exec deploy/app1 -- hostname
kubectl exec deploy/app1 -- killall5

kubectl get pods
kubectl delete pods -l app=app1

# Deployment
kubectl apply -f file.yaml

kubectl apply -f web/


# Services
kubectl get svc
kubectl get pods -l app=web

kubectl logs -l app=web
kubectl get svc web

# Execute
kubectl exec deploy/app1 -- nslookup web
kubectl exec deploy/app1 -- curl http://web:8080

# Output 
kubectl get svc bb-web -o jsonpath='{}'
kubectl get svc bb-web -o jsonpath='http://{.status.loadBalancer.ingress[0].*}:8080'


# Endpoint
kubectl get endpoints
kubectl get all -l demo=workshop1
kubectl delete all -l demo=workshop1

# ConfigMap
kubectl get configmaps -A




kubectl describe  configmap ample -n ns


# Container Deployment
kubectl run http --image=docker-http-server:latest --replicas=1
kubectl get deployments
kubectl describe deployment http
kubectl expose deployment http --external-ip="172.17.0.29" --port=8000 --target-port=80
kubectl get svc

# Replication Controller
kubectl create -f redis-master-controller.yaml
kubectl get rc

kubectl get pods



Install Elasticsearch + Kibana ( ELK) on Windows

choco install curl -y
choco install 7zip -y

curl https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.6.2-windows-x86_64.zip -o elasticsearch-7.6.2-windows-x86_64.zip
mkdir  "c:/elastic"
7z x elasticsearch-7.6.2-windows-x86_64.zip -oc:/elastic
cd c:/elastic/elasticsearch-7.6.2/bin
elasticsearch-service.bat install
elasticsearch-service.bat start
curl http://localhost:9200
GUI: elasticsearch-service.bat manager


choco install nssm

curl https://artifacts.elastic.co/downloads/kibana/kibana-7.6.2-windows-x86_64.zip -o kibana-7.6.2-windows-x86_64.zip
7z x kibana-7.6.2-windows-x86_64.zip -oc:/elastic
nssm install kibana C:\elastic\kibana-7.6.2-windows-x86_64\bin\kibana.bat
sc start kibana

curl http://localhost:5601

How to verify checksum on a Mac - SHA1, SHA256


SHA1 checksum

shasum -a 1 file


SHA256 checksum

shasum -a 256 file



Installing Grafana on Windows

install

choco install grafana

Config

copy c:\ProgramData\chocolatey\lib\grafana\tools\grafana-6.3.6\conf\defaults.ini c:\ProgramData\chocolatey\lib\grafana\tools\grafana-6.3.6\conf\custom.ini

"C:\Program Files\Notepad++\notepad++.exe" c:\ProgramData\chocolatey\lib\grafana\tools\grafana-6.3.6\conf\custom.ini

http_port = 8080

Windows Service 

choco install nssm
nssm install grafana c:\ProgramData\chocolatey\lib\grafana\tools\grafana-6.3.6\bin\grafana-server.exe
sc start grafana

Config Grafana
Configuration - Data Source - name : telegraf  - url: http://localhost:8086
Dashboard - Import - https://grafana.com/grafana/dashboards/1902

Install Telegraf

choco install telegraf
"C:\Program Files\Notepad++\notepad++.exe"  C:\Program Files\telegraf\telegraf.conf
url: http://localhost:8086
database: telegraf

sc start telegraf

Installing InfluxDB on Windows

install

choco install influxdb


"C:\Program Files\Notepad++\notepad++.exe" C:\influxdata\influxdb-1.7.10-1\influxdb.conf

Storage

dir = "C:/influxdata/storage/meta"
dir = "C:/influxdata/storage/data"
wal-dir = "C:/influxdata/storage/wal"

logging

logging-enabled = true
trace-logging-enabled = false 
query-log-enabled = true

HTTP endpoint

[http]
enabled = true

c:\influxdata\influxdb-1.7.10-1/influxd.exe -config influxdb.conf

Windows Service 

choco install nssm
nssm install influxdb c:\influxdata\influxdb-1.7.10-1\influxd.exe "-config c:\influxdata\influxdb-1.7.10-1\influxdb.conf"
sc start influxdb

Installing JMeter on Windows with Influxdb and Grafana


install

choco install jmeter

Config JMeter

download:https://github.com/NovatecConsulting/JMeter-InfluxDB-Writer/releases/download/v-1.2/JMeter-InfluxDB-Writer-plugin-1.2.jar
copy to: c:\ProgramData\chocolatey\lib\jmeter\tools\apache-jmeter-5.2.1\lib\ext\

Config InfluxDB

c:\influxdata\influxdb-1.7.10-1/influx.exe
CREATE DATABASE jmeter
SHOW DATABASES

Config Grafana

Configuration - Data Source - name : jmeter  - url: http://localhost:8086
Dashboard - Import -https://grafana.com/grafana/dashboards/5496


Config Test


Ansible


Adhoc

Lookup

ansible -m debug localhost -a msg="{{ lookup('url', 'http://localhost:8090/nslookup?host=blog.hilandco.com') }}"

Docker shell2http nslookup


Dockerfile

FROM shell2http:bind-tools
RUN apk --no-cache add bind-tools


docker-compose.yml

version: '2'
services:
  nslookup_api:
    image: shell2http:bind-tools
    ports:
      - "8090:8080"
    restart: unless-stopped
    command:  [ "-form", "/nslookup", "nslookup -q=A $$v_host | tail -2 | head -1 | awk '{print $$2\" \" $$3\" \" $$4}'" ]


Test
 curl localhost:8090/nslookup?host=blog.hilandco.com

enable hyper-v on windows 2109

Enable-WindowsOptionalFeature –Online -FeatureName Microsoft-Hyper-V –All -NoRestart

Install-WindowsFeature RSAT-Hyper-V-Tools -IncludeAllSubFeature

Install-WindowsFeature RSAT-Clustering -IncludeAllSubFeature

Install-WindowsFeature Multipath-IO

Restart-Computer

Install AWX

yum install -y git

mkdir ansible
cd ansible/
export http_proxy=http://<proxy>:3128
export https_proxy=http://<proxy>:3128
export HTTP_PROXY=http://<proxy>:3128
export HTTPS_PROXY=http://<proxy>:3128
git clone https://github.com/ansible/awx.git
cd awx/installer/

yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum install -y ansible
yum install -y python-pip python-devel gcc
pip install --upgrade pip

pip uninstall docker docker-py docker-compose
pip install docker-compose

ansible-playbook -i inventory install.yml

URL

http://<awx_server>/#/home

Rest API Call


http://<awx_server>/api/v2/job_templates/33/launch/

Launching from Job Templates via the API follows the following workflow:
GET https://your.tower.server/api/v2/job_templates/&lt;your job template id>/launch/