Thursday, June 11, 2020

Complete Guide to Setting-up Kubernetes



Installing Kubernetes Using Kubeadm

===========================================
#
Topic: Setting up Control-plane/Master node
#############################################
1)
# Install container runtime - Docker
Follow this source.
Source:
https://docs.docker.com/install/linux/docker-ce/ubuntu/
2)
# Installed Kubeadm
Follow this source or below commands.
Source:
https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/install-kubeadm/
Commands:
- sudo apt-get update
- sudo apt-get install -y apt-transport-https curl
- Add repository keys to download tools:
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
- Add repository:
cat <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF
- sudo apt-get update
- sudo apt-get install -y kubelet kubeadm kubectl
- sudo apt-mark hold kubelet kubeadm kubectl
3) 
# Initialize a new Kubernetes cluster
sudo kubeadm init
NOTE: Save this output as it's required to add worker nodes.
NOTE:
If you get 'swap on' error do below:
$ sudo swapoff -a
$ sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
6)
# As per the instruction from 'kubeadm init' command, To make kubectl work for your non-
root user, run these commands.
mkdir -p $HOME/.kube

sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
#8) Verify if cluster is initialized successfully
$ kubectl get nodes
O/P:
NAME STATUS ROLES AGE VERSION
node1 NotReady master 2m43s v1.12.1
#9) Run the following kubectl command to find the reason why the cluster STATUS is
showing as NotReady.
- This command shows all Pods in all namespaces - this includes system Pods in the
system (kube-system) namespace.
- As we can see, none of the coredns Pods are running
- This is preventing the cluster from entering the Ready state, and is happening
because we haven’t created the Pod network yet.
O/P:
$ kubectl get pods --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-...vt 0/1 ContainerCreating 0 8m33s
kube-system coredns-...xw 0/1 ContainerCreating 0 8m33s
kube-system etcd... 1/1 Running 0 7m46s
kube-system kube-api... 1/1 Running 0 7m36s
#7) Create Pod Network. You must install a pod network add-on so that your pods can
communicate with each other. (As per kubeadm init output)
$ sudo sysctl net.bridge.bridge-nf-call-iptables=1
$ kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version |
base64 | tr -d '\n')"
#8) Check if the status of Master is changed from 'NotReady' to 'Ready'
$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
node1 Ready master 3m51s v1.12.1
GREAT - the cluster is ready and all dns system pods are now working. Cluster is ready
now.

Now that the cluster is up and running, it’s time to add some nodes.

#

Topic: Worker Node Setup & Joining to the cluster:
#############################################
1
# Create a worker node machine in GCP / AWS cloud platform.
2
# Install kubeadm
https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/install-kubeadm/

3
# Install container runtime
https://docs.docker.com/install/linux/docker-ce/ubuntu/
4
# To bootstrap a Kubernetes worker node and join it to the cluster run below command from
$kubeadm init output.
Note: bootstrap: It automatically installs kubectl and kubelet.
sudo kubeadm join 192.168.254.129:6443 --token lc1x37.v1dt857dfny4pszd \
--discovery-token-ca-cert-hash
sha256:c4b751ebfefc8ea842f654daab07504ad033873eee87d137c1eb95720c2db8de
# Verify node Join (Run below in Control-plane node)
$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
control-plane Ready master 26m v1.16.3
worker-node1 Ready <none> 3m18s v1.16.3
$ kubectl get nodes -o wide
--> this will display IP, OS, Kernel and more details about all Nodes

#
Project-1 [Nginx]
#############################################
Deploying/Creating a pod
#############################################
#
1.) Create Pod manifest file
$ mkdir nginx
$ vim pod.yaml
pod.yaml
=========
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
zone: prod
version: v1
spec:
containers:
- name: nginx-container
image: nginx
ports:
- containerPort: 80

pod.yaml - Manifest file description:
----------------------
- Straight away we can see four top-level resources.
• .apiVersion
• .kind
• .metadata
• .spec
--> .apiVersion:
- Tells API Server about what version of Yaml is used to create the object (Pod object
in this case)
- Pods are currently in v1 API group
--> .kind:
- Tell us kind of object being deployed. In this case we are creating a POD object.
- It tells the control plane what type of object is being defined.
--> .metadata:
- this section again has two sub-sections i.e name & labels
- You can name the Pod using "name" key.
- Using labels, we can identify a particular pod.
--> .spec:
- This is where we specify details about the containers that will run in the Pod.
- In this section we specify container name, image, ports ..etc.
#
2.) Creating a Pod
- Check if all Nodes are ready before creating a Pod
$ kubectl get nodes
- This POSTs the manifest file to API server and deploy/create a Pod from it
$ kubectl apply -f pod.yml
Note: Your Pod has been scheduled to a healthy node in the cluster and
is being monitored by the local kubelet process on the node.
- Check if Pod is created
$ kubectl get pods
$ kubectl get pods --watch (monitor the status continuously)
# Introspecting Running Pods
- Get IP and worker node of the Pod
$ kubectl get pod -o wide
- Get a full copy of the Pod manifest from cluster store. desired state is (.spec) and observed
state will be under (.status)
$ kubectl get pod -o yaml

- Another great kubernetes introspection command. Provides Pods(object's) lifecycle events.
$ kubectl describe pod nginx-pod
- Launch nginx server application running in the Pod from Control-plane node
$ curl http://10.44.0.1:80
- You can also log into the Pod container to get more information.
$ kubectl exec -it nginx-pod -- /bin/bash
Note: Let's add some code and launch our nginx application
- $ echo "Gamut Gurus Technologies" > /usr/share/nginx/html/index.html
- Launch nginx application
$ curl 10.44.0.1
- Login into a specific container in case you have multi container Pod
using --container or -c option.
$ kubectl exec -it nginx-pod --container nginx-container -- /bin/bash
#
3.) Deleting a Pod
$ kubectl get pods
$ kubectl delete pods nginx-pod
$ kubectl delete -f pod.yml
# Misc:
4.) Get all nodes IPs in Kubernetes cluster
$ kubectl get nodes -o wide
NOTE:
kubelet takes the PodSpec and is responsible for pulling all images and starting all
containers in the Pod.
What Next?
- If a Pod fails, it is not automatically rescheduled. Because of this, we usually deploy
them via higher-level objects such as Deployments.
- This adds things like "scalability" (scale-up/down), "self-healing", "rolling updates" and "roll
backs" and makes Kubernetes so powerful.

#
Project-2 [Nginx]
#############################################
Creating Deployments & Services
#############################################
- Pods don’t self-heal, they don’t scale, and they don’t allow for easy updates

- Deployments do all things like
- "scale" (scale-up/down)
- "self-heal"
- "rolling updates"
- "rollbacks"
- That's why we almost always deploy Pods via 'Deployments"
#
Creating Deployments
--------------------
# List all nodes in K8s cluster
$ kubectl get nodes
# List all pods in K8s cluster
$ kubectl get pods
$ kubectl get pods --watch
# Create the deployment
$ kubectl create -f deploy.yml
Note:
IP Range: 30,000 - 32,767
Networking --> VPC network --> Firewall rules
#
3.) Deleting a Pod
$ kubectl get pods
$ kubectl delete pods nginx-pod
$ kubectl delete -f pod.yml
# Misc:
4.) Get all nodes IPs in Kubernetes cluster
$ kubectl get nodes -o wide

No comments: