[Cheat Sheet] K8s on VMs with Calico

This is the cheat sheet for the post : https://netmemo.github.io/post/k8s-on-vms-with-calico/

The following post contain raw entry only for reminder purpose.

Bellow are the links I’ve used to understand/did my lab

https://fr.wikipedia.org/wiki/Kubernetes#/media/File:Kubernetes.png https://kubernetes.io/docs/setup/independent/install-kubeadm/ https://kubernetes.io/docs/setup/independent/create-cluster-kubeadm/ https://kubernetes.io/docs/tutorials/k8s101/ https://kubernetes.io/docs/tutorials/k8s201/ https://kubernetes.io/docs/reference/kubectl/cheatsheet/

Join a node/worker to the master

kubeadm join 10.0.1.10:6443 --token d34b9i.v03t2yiozio63cq6 --discovery-token-ca-cert-hash sha256:c21d04ea23790a0bf81cf64118e3a9075ffb63ed90bc697acef5793386e9eb16

Delete a deployment

kubectl delete deployment nginx-deployment-nbo

To get the logs of a specific container. -n is to specify the namespace

kubectl logs calico-node-zxvjv -n kube-system calico-node

Allow to launch a shell for a specific container

kubectl exec -it nginx-deployment-nbo-fd57b7b88-l8xsv -- /bin/bash

Create a static page in the container to differentiate it from the others. The -c option is to ask bash to execute the command.

kubectl exec -it nginx-deployment-nbo-fd57b7b88-kkw9s -- /bin/bash -c "echo Hello shell demo SRV1 > /usr/share/nginx/html/index.html" 
kubectl exec -it nginx-deployment-nbo-fd57b7b88-kkw9s cat /usr/share/nginx/html/index.html

To troubleshhot

journalctl -r

Display all pods, even with the system name space, -o wide allow to see the IP addresses

kubectl get pods --all-namespaces -o wide

To see the last messages of container associated to the pode

kubectl describe pod -n kube-system calico-node-zxvjv

Allow to see the node/server/worker ip addresses (-o wide)

sudo kubectl get node -o wide

by default kubernetes don’t work with swap, so I needed to disable it with the command swapoff and to comment the swap line in the fstab file.

swapoff

vi /etc/fstab

# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# /dev/mapper/ubuntu--srv--base--vg-root / ext4 errors=remount-ro 0 1
#/dev/mapper/ubuntu--srv--base--vg-swap_1 none swap sw 0 0

Not related to Kubernets but you need to modify the interfaces

vi /etc/netplan/01-netcfg.yaml

Add interfaces to ubuntu

/etc/netplan/01-netcfg.yaml

This file describes the network interfaces available on your system For more information, see netplan(5).

network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
      dhcp4: no
      addresses:
         - 10.0.1.10/24
      routes:
         - to: 0.0.0.0/0
           via: 10.0.1.253
      nameservers:
         addresses: [1.1.1.1]

apply the /etc/netplan/01-netcfg.yaml configuration

netplan apply

display ip addresses on interfaces

ip address show

display all interfaces

ip link show

display routes

route -n

In order for Kubernetes to work, you need container runtime to be started

systemctl enable docker.service
systemctl start docker.service

Download calicoctl, to be able to interact with calico with CLI

sudo curl -O -L https://github.com/projectcalico/calicoctl/releases/download/v3.2.1/calicoctl
sudo chmod +x calicoctl

To see the state of calico on nodes (BGP,Peer-type,up/down,time)

sudo calicoctl node status

The following commands allow to export a variables with the IP address and ports of nginx-service previously created and access the content from the host or the container

export SERVICE_IP=$(kubectl get service nginx-service -o go-template='{{.spec.clusterIP}}')
export SERVICE_PORT=$(kubectl get service nginx-service -o go-template='{{(index .spec.ports 0).port}}')
wget -qO- http://$SERVICE_IP:$SERVICE_PORT
kubectl run busybox --generator=run-pod/v1 --image=busybox --restart=Never --tty -i --env "SERVICE_IP=$SERVICE_IP" --env "SERVICE_PORT=$SERVICE_PORT"

u@busybox$ wget -qO- http://$SERVICE_IP:$SERVICE_PORT # Run in the busybox container
u@busybox$ exit # Exit the busybox container
noel@ubuntu-srv-1:~$ cat nginx-test.yaml

apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: nginx-deployment-nbo
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 3 # tells deployment to run 3 pods matching the template
  template:
    metadata:
      labels:
        app: nginx
    spec:
      volumes:
      - name: shared-data
        emptyDir: {}
      containers:
      - name: nginx
        image: nginx:1.7.9
        volumeMounts:
        - name: shared-data
          mountPath: /usr/share/nginx/html
        ports:
        - containerPort: 80

https://kubernetes.io/docs/tutorials/k8s201/

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  ports:
  - port: 8000 # the port that this service should serve on
    # the container on each pod to connect to, can be a name
    # (e.g. 'www') or a number (e.g. 80)
    targetPort: 80
    protocol: TCP
  # just like the selector in the deployment,
  # but this time it identifies the set of pods to load balance
  # traffic to.
  selector:
    app: nginx

These commands are to configure calicoctl in order to work with the local k8s

export CALICO_DATASTORE_TYPE=kubernetes
export CALICO_KUBECONFIG=~/.kube/config
Pour le root
export CALICO_KUBECONFIG=/home/noel/.kube/config

Move the Calico mode from Always to CrossSubnet. First we get the calico ippool configuration, then we need to modify the ipipMode in the yaml file and eventually to apply the new configuration

calicoctl get ippool -o yaml > ippool.yaml 

Change the mode ipipMode: CrossSubnet

calicoctl apply -f ippool.yaml