Introduction Kubernetes

Arkademylogo.svg

Our Website · Join With Us ·

🛠️ Installation K8s on aws

  1. Create k8s-controll Instance with image ubuntu

  2. Create Role

  • Go to iam
  • Click role
  • CLick add role
  • give tag for youre role (optional)
  • add premission (ec2 full, route53 full, s3 full, iam full, vps full)
  1. attach role to instance
  • go to ec2 dashbord
  • select youre instance
  • click action
  • instance seeting
  • Modify IAM role
  • select youre role
  • click save
  1. Create route 53
  • go to route 53
  • click hostedzone
  • click hosted zone
  • input Domain name
  • select youre instance region
  • select youre instance vpcid
  • click created hosted zone
  1. ssh to instance and install aws cli
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
apt install -y unzip python
unzip awscliv2.zip
sudo ./aws/install
  1. install kubectl
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin/kubectl
  1. install kops
curl -LO https://github.com/kubernetes/kops/releases/download/$(curl -s https://api.github.com/repos/kubernetes/kops/releases/latest | grep tag_name | cut -d '"' -f 4)/kops-linux-amd64
chmod +x kops-linux-amd64
sudo mv kops-linux-amd64 /usr/local/bin/kops

# update kops
sudo rm -rf /usr/local/bin/kops
# and to above step
  1. config aws cli
aws configure
  1. create s3 bucket
aws s3api create-bucket \
    --bucket k8s-sample-store \
    --region us-east-1

# for reqion other than use-east1 use command bellow

aws s3api create-bucket \
    --bucket k8s-example-store \
    --region us-west-1 \
    --create-bucket-configuration LocationConstraint=us-west-1

# or

aws s3 mb s3://k8s-example-store


# Note: We STRONGLY recommend versioning your S3 bucket in case you ever need to revert or recover a previous state store.

aws s3api put-bucket-versioning --bucket k8s-example-store --versioning-configuration Status=Enabled

# Delete bucket
aws s3 rm s3://k8s-example-store --recursive # empty buccket before delete
aws s3api delete-bucket --bucket k8s-example-store --region us-west-1
  1. add to .bashrc
export NAME=yudomain.com
export KOPS_STATE_STORE=s3://k8s-sample-store
  1. generate ssh no password
ssh-keygen
  1. list availibillity zone
aws ec2 describe-availability-zones
  1. create cluster with kops
# kops create cluster --cloud=aws --zones=us-east-1a --name=k8s.devops.com --dns-zone=k8s.devops.com --dns private

kops create cluster --cloud=aws --zones=us-east-1a --name=$NAME --node-size=t2.small --master-size=t2.small --dns-zone=$NAME --dns private
  1. edit configuration
kops edit cluster $NAME
kops edit ig --name=$NAME nodes-us-east-1a
kops edit ig --name=$NAME master-us-east-1a
  1. set ssh and create cluster
# connect with ssh
kops create secret --name $NAME sshpublickey admin -i ~/.ssh/id_rsa.pub

# apply cluster
kops update cluster --name $NAME --yes --admin
  1. check cluster
kops validate cluster --wait 10m
# or
kops validate cluster

kubectl get nodes --show-labels
  1. ssh to master node
ssh -i ~/.ssh/id_rsa ubuntu@api.$NAME
ssh -i ~/.ssh/id_rsa ubuntu@api.sunsummit.net
  1. delete cluster
kops delete cluster --name=$NAME --state=$KOPS_STATE_STORE --yes

Setup Helm,ingress & cert-manager

  1. setup youre helm like write on docs https://helm.sh/docs/intro/install/

  2. install nginx ingress https://hub.kubeapps.com/charts/ingress-nginx/ingress-nginx

helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm repo update

helm install nginx-ingress ingress-nginx/ingress-nginx

# make sure after install nginx thers alb created in your aws
  1. install cert-manager with helm follow the intruction here https://hub.kubeapps.com/charts/jetstack/cert-manager

  2. setup ingress with cert in here https://cert-manager.io/docs/tutorials/acme/ingress/

Setup promethous and grafana

  1. add prometheus Helm repo
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
  1. add grafana Helm repo
helm repo add grafana https://grafana.github.io/helm-charts
  1. Deploy Prometheus
kubectl create namespace prometheus

helm install prometheus prometheus-community/prometheus --namespace prometheus --set alertmanager.persistentVolume.storageClass="gp2" --set server.persistentVolume.storageClass="gp2"
  1. Prometheus components deployed as expected
kubectl get all -n prometheus
  1. kubectl port forwarding
kubectl port-forward -n prometheus deploy/prometheus-server 8080:9090
  1. Deploy Grafana using below command

save this to grafana.yaml

datasources:
    datasources.yaml:
        apiVersion: 1
        datasources:
            - name: Prometheus
              type: prometheus
              url: http://prometheus-server.prometheus.svc.cluster.local
              access: proxy
              isDefault: true
kubectl create namespace grafana
helm install grafana grafana/grafana --namespace grafana --set persistence.storageClassName="gp2" --set persistence.enabled=true --set adminPassword='abcd1234' --values ./grafana.yaml --set service.type=LoadBalancer
  1. Check if Grafana is deployed
kubectl get all -n grafana
  1. Get Grafana ELB URL using this command
kubectl get svc -n grafana grafana -o jsonpath='{.status.loadBalancer.ingress[0].hostname}'
  1. Access dashboard IDs

3119/6417