MalcolmChalmers.com

Container Tips and Tricks

HOME
Example pod yaml

# pod.yml
apiVersion: v1
kind: Pod
metadata:
  name: nginx-example
  labels:
    app: nginx
spec:
  containers:
    - name: nginx
      image: linuxserver/nginx
      volumeMounts:
      - mountPath: /config/www
        name: host-path-volume
      ports:
        - containerPort: 80
          name: "nginx-http"
  volumes:
  - name: host-path-volume
    hostPath:
      path: /pods/www
      type: DirectoryOrCreate


# service.yml
apiVersion: v1
kind: Service
metadata:
  name: nginx-example
spec:
  type: NodePort
  ports:
    - name: http
      port: 80
      nodePort: 8080
      targetPort: nginx-http
  selector:
    app: nginx

Install Kubernetes on Ubuntu

Installing Kubernetes on Ubuntu typically involves setting up a cluster with a control plane (master) node and one or more worker nodes. The following steps outline the general process using kubeadm, kubelet, and kubectl, along with containerd as the container runtime.

1. System Preparation (on all nodes):
Update and Upgrade.
Code
    sudo apt update && sudo apt upgrade -y

Disable Swap: Kubernetes requires swap to be disabled.
Code
    sudo swapoff -a
    sudo sed -i '/swap/ s/^/# /' /etc/fstab

Add Kernel Parameters: Load necessary kernel modules and configure network settings.
Code
    sudo tee /etc/modules-load.d/containerd.conf <<EOF
    overlay
    br_netfilter
    EOF

    sudo modprobe overlay
    sudo modprobe br_netfilter

    sudo tee /etc/sysctl.d/kubernetes.conf <<EOF
    net.bridge.bridge-nf-call-ip6tables = 1
    net.bridge.bridge-nf-call-iptables = 1
    net.ipv4.ip_forward = 1
    EOF

    sudo sysctl --system

Install Containerd Runtime.
Code
    sudo apt install -y curl gnupg2 software-properties-common apt-transport-https ca-certificates

# Add Docker's official GPG key and repository
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
    echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
    sudo apt update
    sudo apt install -y containerd.io
    sudo containerd config default | sudo tee /etc/containerd/config.toml

# Change SystemdCgroup to true in /etc/containerd/config.toml
    sudo sed -i 's/SystemdCgroup = false/SystemdCgroup = true/g' /etc/containerd/config.toml
    sudo systemctl restart containerd
    sudo systemctl enable containerd

Install Kubernetes Tools (kubeadm, kubelet, kubectl):
Code
    curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.29/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
    echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.29/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list
    sudo apt update
    sudo apt install -y kubelet kubeadm kubectl
    sudo apt-mark hold kubelet kubeadm kubectl

2. Control Plane (Master) Node Initialization:
Initialize the Kubernetes Cluster: Replace YOUR_POD_CIDR_BLOCK with your desired pod network CIDR (e.g., 10.244.0.0/16 for Flannel).
Code
    sudo kubeadm init --pod-network-cidr=10.244.0.0/16

Set up Kubectl for the current user:
Code
    mkdir -p $HOME/.kube
    sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
    sudo chown $(id -u):$(id -g) $HOME/.kube/config

Deploy a Pod Network Add-on: For example, Calico:
Code
    kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml


3. Worker Node Join:
Join Worker Nodes: On each worker node, run the kubeadm join command provided in the output of kubeadm init on the control plane node. It will look similar to:
Code
    sudo kubeadm join <control-plane-ip>:<control-plane-port> --token <token> --discovery-token-ca-cert-hash sha256:<hash>


4. Verification:
Check Cluster Status (from control plane).
Code

    kubectl get nodes -o wide

All nodes should eventually show as Ready.

5. Create pod.yml
Code
apiVersion: v1
kind: Pod
metadata:
  name: nginx-example
  labels:
    app: nginx
spec:
  containers:
    - name: nginx
      image: linuxserver/nginx
      ports:
        - containerPort: 80
          name: "nginx-http"


6. Apply the file with the following command:
Code
    kubectl apply -f pod.yml


7. You can check the status of this deployment with the following command:
Code
    kubectl get pods


8. Print join command
    kubeadm token create --print-join-command

Shell into container

kubectl exec -it my-app-pod -c my-app-container -- bash


HOME