Deploy app in Kubernetes cluster

Setup kubernetes cluster (Ubuntu 18.04.5 LTS)

install docker-ce

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
sudo apt-get install -y docker-ce=18.06.1~ce~3-0~ubuntu
sudo apt-mark hold docker-ce
sudo docker version

install kubeadm, kubelet, kubectl

curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
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=1.15.7-00 kubeadm=1.15.7-00 kubectl=1.15.7-00
sudo apt-mark hold kubelet kubeadm kubectl
kubeadm version

on master node

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
sudo kubeadm init --pod-network-cidr=10.244.0.0/16
kubectl version

on worker nodes: join the cluster with generated code

sudo kubeadm join 172.31.23.220:6443 --token 74vgm6.g7o9e5th7i5h0yih --discovery-token-ca-cert-hash <hash code>

Check if the cluster works

kubectl get nodes

config networking with Flannel

echo "net.bridge.bridge-nf-call-iptables=1" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p

on master node

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/bc79dd1505b0c8681ece4de4c0d86c5cd2643275/Documentation/kube-flannel.yml

POD
– pod is basic building block of Kubernetes model
– each pod includes multiple containers which make up an application

Note that following commands are executed on master node

create pod

cat << EOF | kubectl create -f -
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
name: nginx
image: nginx
EOF

show pods

kubectl get pods
kubectl get pods -n kube-system
kubectl describe pod nginx

to destroy a pod

kubectl delete pod nginx

A new pod will then be created automatically to replace the destroyed pod.

NETWORKING

Networking model of kubernetes cluster based on a virtual network which help pods to communicate with nodes


We’ll deploy 2 nginx pods

cat << EOF | kubectl create -f -
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
labels:
app: nginx
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.15.4
ports:
- containerPort: 80
EOF

create a busybox pod

cat << EOF | kubectl create -f -
apiVersion: v1
kind: Pod
metadata:
name: busybox
spec:
containers:
name: busybox
image: radial/busyboxplus:curl
args: sleep "1000"
EOF

get IP of pods

kubectl get pods -o wide

get IP of nginx pod then contact that pod from busybox pod

kubectl exec busybox -- curl $nginx_pod_ip

Show Architecture

kubectl get pods -n kube-system
sudo systemctl status kubelet

create deployment

cat <<EOF | kubectl create -f -
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.15.4
ports:
- containerPort: 80
EOF

get list of deployments

kubectl get deployments

get more info about a deployment

kubectl describe deployment nginx-deployment
kubectl get pods

SERVICE
Kubernetes cluster uses services instead of IP to identify a nodes because IP changes when pods being recreated.

create NodePort service

cat << EOF | kubectl create -f -
kind: Service
apiVersion: v1
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
protocol: TCP
port: 80
targetPort: 80
nodePort: 30080
type: NodePort
EOF

— get list of services in the cluster

kubectl get services

check if service works

curl localhost:30080

Deploy app

kubectl delete service nginx-service
cd ~/
git clone https://github.com/linuxacademy/robot-shop.git
kubectl create namespace robot-shop
kubectl -n robot-shop create -f ~/robot-shop/K8s/descriptors/
kubectl get pods -n robot-shop -w

run app

http://$kube_server_public_ip:30080

Deploy app with Helm

install helm

sudo snap install helm --classic

install helm chart

helm repo add stable https://kubernetes-charts.storage.googleapis.com/
helm search repo stable
helm install stable/mysql --generate-name

helm install my-wordpress-x --set -service.type=clusterip --set persistence.storageClass=rook-ceph-block stable/wordpress --set mariadb.master.persistence.storageClass=rook-ceph-block

Done!

Leave a comment