Tech Me More

To quench our thirst of sharing knowledge about our day to day experience & solution to techincal problems we face in our projects.

Advertise with us !
Send us an email at diehardtechy@gmail.com
Showing posts with label kubernetes. Show all posts
Showing posts with label kubernetes. Show all posts

Wednesday, April 22, 2020

How to run netflix eureka with one micro-service app in docker-container using docker-compose

Distributed systems are the need of the current time. From a faster, independent, resilient, highly available application it is not only necessary to adapt to newer technologies to build application logic but deploying them fast is also a core use case.

Spring boot is a highly used framework for developing microservices in the Java ecosystem. With dockers & Kubernetes it is also easy to deploy them anywhere. 

In this post, I will explain how can netflix-eureka and the microservice app communicate with each other in a container environment. 

For this post, I am using two images. 
  • Netflix-eureka : image for Netflix-eureka server listening on port 8762
  • Support-System: A simple microservice application running on default port 8080

for communication with netflix-eureka we need to provide netflix eureka.client.serviceUrl.defaultZone=http://eureka-server:8762/eureka property in our application, so that it understands where the netflix eureka service is running. 

Having it hard coded is a problem and it is discouraged for the stateless purpose. How do we solve this problem when both the applications are running in different containers. 

To solve the above problem we use the docker-compose and link two services together. 

docker-compose.yml


eureka-server:
    image: devakash/eureka:latest
    ports:
      - '80:8762'
    restart: always
        
supportsystem:
    image: devakash/supportsystem:latest
    ports:
      - '8080:8080'
    environment:
        - eureka.client.serviceUrl.defaultZone=http://eureka-server:8762/eureka
        - email.fromemail=username@gmail.com
        - email.password=Password
    links:
        - eureka-server
    restart: always

in the above file please notice the link key. Here we are linking our micro-service to eureka-server. Also, notice the defaultZone environment variable where in place of IP we are using the name of the container such as eureka-server which is nothing but an identifier for netflix-eureka server. 

Netflix-eureka default port is 8761 but we can change it using the below setting in application.properties file of netflix-eureka microservice application. 

server.port=8762

eureka.client.serviceUrl.defaultZone: http://localhost:${server.port}/eureka/

for more details please refer: https://hub.docker.com/r/devakash/supportsystem


Monday, April 20, 2020

How to set up Kubernetes cluster in freshly deployed Ubuntu virtual machine

Setup kubernetest cluster in ubuntu virtual machine

Kubernetes is the best available orchestration tool for dockers. To set up Kubernetes in the ubuntu box please follow the below steps. Docker is the prerequisite to install Kubernetes. The steps provided below will take care of the docker installation before proceeding with the Kubernetes installation.

Procedure
Prerequisite: Have root access to the ubuntu machine.
  1. ssh to ubuntu virtual machine. 
  2. Run the command apt-get update.
  3. Copy the script from Script Content as displayed below and save it as install_kubernetes.sh
  4. Edit the script and change the apiserver-advertise-address in the script.
  5. To get the IP address just run ip addr
  6. Run the script in the ubuntu machine as sh install_kubernetes.sh
Script Content:

#!/bin/bash

echo "installing docker"
apt-get update
apt-get install -y \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
add-apt-repository \
   "deb https://download.docker.com/linux/$(. /etc/os-release; echo "$ID") \
   $(lsb_release -cs) \
   stable"
apt-get update && apt-get install -y docker-ce=$(apt-cache madison docker-ce | grep 17.03 | head -1 | awk '{print $3}')

echo "installing kubernetes"
apt-get update && apt-get install -y apt-transport-https
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb http://apt.kubernetes.io/ kubernetes-xenial main
EOF
apt-get update
apt-get install -y kubelet=1.13.5-00 kubeadm=1.13.5-00 kubectl=1.13.5-00

echo "deploying kubernetes (with calico)..."
kubeadm init --pod-network-cidr=192.168.0.0/16 --apiserver-advertise-address="172.31.X.X" 
export KUBECONFIG=/etc/kubernetes/admin.conf



kubectl apply -f https://docs.projectcalico.org/v3.1/getting-started/kubernetes/installation/hosted/rbac-kdd.yaml
kubectl apply -f https://docs.projectcalico.org/v3.1/getting-started/kubernetes/installation/hosted/kubernetes-datastore/calico-networking/1.7/calico.yaml


The output of the above command will be like below: Please save the below output for future use. 

master_install_output 

Your Kubernetes control-plane has initialized successfully!

To start using your cluster, you need to run the following as a regular user:

  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config

You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  https://kubernetes.io/docs/concepts/cluster-administration/addons/

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join 10.149.X.X:6443 --token 6zqewj.0aycerc78v3es6gk \
    --discovery-token-ca-cert-hash sha256:4aa355c1340feccabaceda0ebaeab0996e040c998ed6255d8ec2357cf66e

Please run the above 3 commands on the master node.
Once done you can verify the installment using the below command.

kubectl cluster-info 
Output:

Now the cluster is set up with one node i.e. master, verify this information by running the command.

kubectl get nodes  
Output :


Add Nodes to Kubernetes cluster 


Now to add other nodes in this cluster, setup another ubuntu machine. 

Run the below script in that ubuntu machine. 

#!/bin/bash
echo "installing docker"
apt-get update
apt-get install -y \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
add-apt-repository \
   "deb https://download.docker.com/linux/$(. /etc/os-release; echo "$ID") \
   $(lsb_release -cs) \
   stable"
apt-get update && apt-get install -y docker-ce=$(apt-cache madison docker-ce | grep 17.03 | head -1 | awk '{print $3}')

echo "installing kubeadm and kubectl"
apt-get update && apt-get install -y apt-transport-https
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb http://apt.kubernetes.io/ kubernetes-xenial main
EOF
apt-get update
apt-get install -y kubelet=1.13.5-00 kubeadm=1.13.5-00 kubectl=1.13.5-00


after running the above script , copy the kubeadm join comment which we stored as part of the master node installation. as depicted under master_install_output heading , and run it in second ubuntu machine.


kubeadm join 10.149.X.X:6443 --token 6zqewj.0aycerc78v3es6gk \
    --discovery-token-ca-cert-hash sha256:4aa355c1340feccabaceda0ebaeab0996e040c998ed6255d8ec2357cf66e

This will join this machine to our already setup cluster.