Setting Up Kubernetes on Ubuntu

Setting Up Kubernetes on Ubuntu

·

3 min read

To set up Kubernetes on an Ubuntu instance (t2.medium) with the provided security groups, follow these detailed steps:

  1. Launch Ubuntu Instance (t2.medium)

    • Launch an Ubuntu instance with the specified security groups (80, 8080, 443, 6783, 6784, 6443).
  2. Update Packages and Install Dependencies

     sudo su
     sudo apt-get update -y
     sudo apt-get install -y apt-transport-https
     sudo curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
    
  3. Configure Kubernetes Repository

     vi /etc/apt/sources.list.d/kubernetes.list
    

    Add this line to the file:

     deb http://apt.kubernetes.io/ kubernetes-xenial main
    

    Save and exit.

  4. Install Docker

     apt-get update -y
     apt-get install docker.io -y
     systemctl enable docker
     systemctl start docker
     usermod -a -G docker ubuntu
    
  5. Install Kubernetes Components

     apt-get install -y kubelet kubeadm kubectl kubernetes-cni
    

    If there's an error with Kubernetes CNI, run:

     sudo dpkg -i --force-overwrite /var/cache/apt/archives/kubernetes-cni_0.7.5-00_amd64.deb
    
  6. Configure cgroup Driver for Kubelet

     vi /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
    

    Add this line:

     makefileCopy codeEnvironment="cgroup-driver=systemd/cgroup-driver=cgroupfs"
    

    Save and exit.

  7. Create an AMI (e.g., devops-k8s)

  8. Initialize Kubernetes Cluster

     kubeadm init
     kubectl get nodes  # This might show an error for now
    

    Exit from the root user.

  9. Configure kubectl

     mkdir -p $HOME/.kube
     sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
     sudo chown $(id -u):$(id -g) $HOME/.kube/config
     kubectl get nodes  # This should display your nodes
    
  10. Enable IP Forwarding

    sudo su
    sysctl net.bridge.bridge-nf-call-iptables=1
    exit
    
  11. Set Up Networking (Weave)

    export kubever=$(kubectl version | base64 | tr -d '\n')
    kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$kubever"
    kubectl get pods --all-namespaces
    kubectl get nodes
    
  12. Launch Instances with the Created AMI

    • Launch instances with the specified security groups (80, 8080, 8081, 8083).
  13. Join Worker Nodes to the Cluster

    • SSH into each worker node and run the join command obtained from the master node. Example:
    kubeadm join 172.31.38.233:6443 --token oiqur0.u6actvi9k6bc5oex \
        --discovery-token-ca-cert-hash sha256:a869eb97f1f6f2759a39645f5976130aeddb2604fc45bb1e949e67e04f3fc3f5

To generate a new token, use kubeadm token create --print-join-command.

  1. Access Kubernetes Dashboard

    • To install the Kubernetes dashboard, use:
    kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml
  • Create a service account and cluster role binding:
    vi service.yaml

Add:

    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: admin-user
      namespace: kube-system

Save and exit.

    kubectl apply -f service.yaml
    vi role.yaml

Add:

    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRoleBinding
    metadata:
      name: admin-user
    roleRef:
      apiGroup: rbac.authorization.k8s.io
      kind: ClusterRole
      name: cluster-admin
    subjects:
    - kind: ServiceAccount
      name: admin-user
      namespace: kube-system

Save and exit.

    kubectl apply -f role.yaml
  • Get the dashboard token:
    kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep admin-user | awk '{print  $1}')
  • Start the proxy:
    nohup kubectl proxy --address 0.0.0.0 --accept-hosts '.*' &
  • Find the Kubernetes dashboard ClusterIP:
    kubectl -n kube-system get service kubernetes-dashboard

Edit the service to use NodePort instead of ClusterIP:

    kubectl -n kube-system edit service kubernetes-dashboard

Change Type: ClusterIP to Type: NodePort.

  • Access the dashboard via a web browser at https://ip:30293 and provide the dashboard token when prompted.
  1. Deploy and Manage Applications

    • You can deploy and manage applications using kubectl. For example:
    kubectl run testk8s --image=nginx
    kubectl get pods
    kubectl run test1k8s --image=nginx
    kubectl delete pod testk8s
  1. Understanding Kubernetes Resources

    • Kubernetes has several resources, including Pods, ReplicaSets, and Deployments, to manage your applications.

    • To create a Pod:

    kubectl run kuard --generator=run-pod/v1 --image gcr.io/kuar-demo/kuard-amd64:1
  • To find a Pod's IP address:
    kubectl get pods -o wide
  • To forward ports to a Pod:
    kubectl port-forward kuard 8080:8080
  • To delete a Pod:
    kubectl delete pod kuard
  1. Hope you like my blog...!

    If you like the content follow me on LinkedIn: https://www.linkedin.com/in/ashok-sana

    Follow my Whatsapp & telegram community: https://chat.whatsapp.com/BzX1aruZIH645l29LxvgO3

    https://t.me/ExplorewithAshok

    Happy learning......!

Did you find this article valuable?

Support Ashoksana by becoming a sponsor. Any amount is appreciated!