Giter Site home page Giter Site logo

learning-kubernetes's Introduction

Getting started with Kubernetes on your local macOS.

๐ŸŒŸ Are you ready to dive into the world of Kubernetes from scratch? ๐Ÿ’ป

๐Ÿš€ Calling all aspiring developers! Whether you're an Angular, .NET, or Java enthusiast, a frontend or backend guru, or a full stack enthusiast, this step-by-step beginner's guide is perfect for you! ๐ŸŽ‰

๐Ÿ“š In this comprehensive tutorial, we will start from the very basics and guide you through the process of getting started with Kubernetes. You don't need any prior knowledge of Kubernetes or containerization - we'll cover everything from the ground up. You'll learn about the core conce# Getting started with Kubernetes on your local macOS. ๐ŸŒŸ Are you ready to dive into the world of Kubernetes from scratch? ๐Ÿ’ป

๐Ÿš€ Calling all aspiring developers! Whether you're an Angular, .NET, or Java enthusiast, a frontend or backend guru, or a full stack enthusiast, this step-by-step beginner's guide is perfect for you! ๐ŸŽ‰

๐Ÿ“š In this comprehensive tutorial, we will start from the very basics and guide you through the process of getting started with Kubernetes. You don't need any prior knowledge of Kubernetes or containerization - we'll cover everything from the ground up. You'll learn about the core concepts, containerization, Kubernetes architecture, and how to deploy and manage your applications using Kubernetes.

So, if you're excited to learn Kubernetes and take your application development skills to the next level, let's dive in and embark on this Kubernetes journey together! ๐ŸŒˆโœจ

Installing dependencies: Docker Desktop for MacOS

Before we proceed with setting up Minikube, it is essential to ensure that Docker is installed and running on your macOS machine. Here are the steps to install Docker Desktop on macOS and start the Docker service:

image

Next run docker to see if your docker is running

image

Installing minikube

Minikube is local Kubernetes, focusing on making it easy to learn and develop for Kubernetes.

curl -LO https://github.com/kubernetes/minikube/releases/download/v1.30.1/minikube-darwin-arm64  
sudo install minikube-darwin-arm64 /usr/local/bin/minikube

Start Minikube, before starting minikube make sure docker is running by openeing docker desktop on mac. Minikube start may take few minutes to generate the cluster.

minikube start

image

Next lets explore you Kubernetes(K8) cluster

kubectl cluster-info

You will see the ip address as localhost address since this cluster is setup on your machine running at https://127.0.0.1:53273

image

Next check all the nodes

kubectl get nodes

image

We get the error that means our cluster is not running yet. Let's run minikube start one more time. And then re-run kubectl get nodes

image

One node with name minikube with role of control-plane my k8 is running with version 1.26.

Let's check namespaces created by default

kubectl get namespaces

image

You isolate and manage applications and services using namespace.

Next let's see the pods created in all namespaces by passing -A.

kubectl get pods -A

image

These pods, how containers are running k8 these pods have softwares to run k8 cluster itself.

Now let's see all the services. Services act as the loadbalancer and direct the traffic to the pods.

kubectl get services -A

image

Creating Namespaces

If you are using single cluster for your applicaiton then You can namespace to isolate and organize workloads. Example create dev and prod namespaces to separate them.

Check existing namespaces

kubectl get namespaces 

K8 menefest to create a namespace called development in file namespace.yml

---
apiVersion: v1
kind: Namespace
metadata:
  name: development

Let's create the namesapce by runing command

kubectl apply -f namespace.yaml

image

Let's change the namespace.yaml to have both production and development namespaces and run kubectl apply -f namespace.yaml to create both of them.

---
apiVersion: v1
kind: Namespace
metadata:
  name: development
--- # way to signify new document in yaml
apiVersion: v1
kind: Namespace
metadata:
  name: production

image

Now let's delete these 2 namespaces

kubectl delete -f namespace.yaml

image

Deploy an Application

Let's create Highly available application that will be deployed in 3 different pods. We will declare our dpplicaiton inside the deployment.yaml file how we want our application to be deployed in development namespace, with 3 replicas. We will run container in the pod which will display the information of the pod that it is running within you will use kimschles/pod-info-app:latest image that will have that pod info app already created. We will setup container with 3 environment variables for pod name, namespace and ip that will be used by our app.

Let's create the development namespace first. Run previous command kubectl apply -f namespace.yaml to deploy namespace.

image

Next create deployment.yaml with below definition.

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: pod-info-deployment
  namespace: development
  labels:
    app: pod-info
spec:
  replicas: 3
  selector:
    matchLabels:
      app: pod-info
  template:
    metadata:
      labels:
        app: pod-info
    spec:
      containers:
      - name: pod-info-container
        image: kimschles/pod-info-app:latest
        ports:
        - containerPort: 3000
        env:
          - name: POD_NAME
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          - name: POD_NAMESPACE
            valueFrom:
              fieldRef:
                fieldPath: metadata.namespace                
          - name: POD_IP
            valueFrom:
              fieldRef:
                fieldPath: status.podIP        

We do not have any deployment in our cluster yet kubectl get deployments

image

Now Let's create the deployment

kubectl apply -f deployment.yaml

Next let's check if the deployment is created kubectl get deployments -n development

image

โš ๏ธ Notice pod-info-deployment is created with 3 containers not ready yet after 15s but wait for few more time and again check you should have 3 pods ready to go and available

image

Next let's check all the pods in the development namespace

kubectl get pods -n development 

image

Notice all the pods are running.

You have set the replica as 3 so always 3 pods will be running. To proove that go ahead and delete a pod and check.

Delete first pod by copy it's name and use delete pod command.

kubectl delete pod pod-info-deployment-757cb75bbb-l2jd8 -n development 

Check how much pods you left kubectl get pods -n development

image

๐Ÿ˜ฒ You see still 3 pods, K8 deployment automatically terminated the one that you asked to delete and created brand new pod to make sure at any time 3 pods are running.

๐ŸŽ‰ Pet yourself ๐Ÿถ, you created 3 pods with k8 deployment.

How to check health of your POD?

You check the health of a pod by looking at the event logs. Why pods may not work: container image not available, Worker nodes are out of space so pod can not be schedule.

# Find the pod that you want to check logs 
kubectl get pods -n development
 
kubectl describe pod <pod-name> -n development

# We will take first pod and check its logs 
kubectl describe pod pod-info-deployment-757cb75bbb-8cgf8 -n development

image

If your pod is not working check at the bottom and you will see the events with error messages.

How to check your application is Working?

You can use BusyBox tool to debug and troubleshoot issues in linux environment. For this you have to re-deploy your application with busybox tool.

In below definition busybox.yaml of deployment, we will use 1 replica and deploy application in default namespace. We will pull the image from busybox:latest.

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: busybox
  namespace: default
  labels:
    app: busybox
spec:
  replicas: 1
  selector:
    matchLabels:
      app: busybox
  template:
    metadata:
      labels:
        app: busybox
    spec:
      containers:
      - name: busybox-container
        image: busybox:latest
        # Keep the container running
        command: [ "/bin/sh", "-c", "--" ]
        args: [ "while true; do sleep 30; done;" ]
        resources:
          requests:
            cpu: 30m
            memory: 64Mi
          limits:
            cpu: 100m
            memory: 128Mi

Deploy it

kubectl apply -f busybox.yaml

image

Get pods kubectl get pods

Gettings started with Kubernetes on your local macOS.

๐ŸŒŸ Are you ready to dive into the world of Kubernetes from scratch? ๐Ÿ’ป

๐Ÿš€ Calling all aspiring developers! Whether you're an Angular, .NET, or Java enthusiast, a frontend or backend guru, or a full stack enthusiast, this step-by-step beginner's guide is perfect for you! ๐ŸŽ‰

๐Ÿ“š In this comprehensive tutorial, we will start from the very basics and guide you through the process of getting started with Kubernetes. You don't need any prior knowledge of Kubernetes or containerization - we'll cover everything from the ground up. You'll learn about the core concepts, containerization, Kubernetes architecture, and how to deploy and manage your applications using Kubernetes.

So, if you're excited to learn Kubernetes and take your application development skills to the next level, let's dive in and embark on this Kubernetes journey together! ๐ŸŒˆโœจ

Installing dependencies: Docker Desktop for MacOS

Before we proceed with setting up Minikube, it is essential to ensure that Docker is installed and running on your macOS machine. Here are the steps to install Docker Desktop on macOS and start the Docker service:

image

Next run docker to see if your docker is running

image

Installing minikube

Minikube is local Kubernetes, focusing on making it easy to learn and develop for Kubernetes.

curl -LO https://github.com/kubernetes/minikube/releases/download/v1.30.1/minikube-darwin-arm64  
sudo install minikube-darwin-arm64 /usr/local/bin/minikube

Start Minikube, before starting minikube make sure docker is running by openeing docker desktop on mac. Minikube start may take few minutes to generate the cluster.

minikube start

image

Next lets explore you Kubernetes(K8) cluster

kubectl cluster-info

You will see the ip address as localhost address since this cluster is setup on your machine running at https://127.0.0.1:53273

image

Next check all the nodes

kubectl get nodes

image

We get the error that means our cluster is not running yet. Let's run minikube start one more time. And then re-run kubectl get nodes

image

One node with name minikube with role of control-plane my k8 is running with version 1.26.

Let's check namespaces created by default

kubectl get namespaces

image

You isolate and manage applications and services using namespace.

Next let's see the pods created in all namespaces by passing -A.

kubectl get pods -A

image

These pods, how containers are running k8 these pods have softwares to run k8 cluster itself.

Now let's see all the services. Services act as the loadbalancer and direct the traffic to the pods.

kubectl get services -A

image

Creating Namespaces

If you are using single cluster for your applicaiton then You can namespace to isolate and organize workloads. Example create dev and prod namespaces to separate them.

Check existing namespaces

kubectl get namespaces 

K8 menefest to create a namespace called development in file namespace.yml

---
apiVersion: v1
kind: Namespace
metadata:
  name: development

Let's create the namesapce by runing command

kubectl apply -f namespace.yaml

image

Let's change the namespace.yaml to have both production and development namespaces and run kubectl apply -f namespace.yaml to create both of them.

---
apiVersion: v1
kind: Namespace
metadata:
  name: development
--- # way to signify new document in yaml
apiVersion: v1
kind: Namespace
metadata:
  name: production

image

Now let's delete these 2 namespaces

kubectl delete -f namespace.yaml

image

Deploy an Application

Let's create Highly available application that will be deployed in 3 different pods. We will declare our dpplicaiton inside the deployment.yaml file how we want our application to be deployed in development namespace, with 3 replicas. We will run container in the pod which will display the information of the pod that it is running within you will use kimschles/pod-info-app:latest image that will have that pod info app already created. We will setup container with 3 environment variables for pod name, namespace and ip that will be used by our app.

Let's create the development namespace first. Run previous command kubectl apply -f namespace.yaml to deploy namespace.

image

Next create deployment.yaml with below definition.

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: pod-info-deployment
  namespace: development
  labels:
    app: pod-info
spec:
  replicas: 3
  selector:
    matchLabels:
      app: pod-info
  template:
    metadata:
      labels:
        app: pod-info
    spec:
      containers:
      - name: pod-info-container
        image: kimschles/pod-info-app:latest
        ports:
        - containerPort: 3000
        env:
          - name: POD_NAME
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          - name: POD_NAMESPACE
            valueFrom:
              fieldRef:
                fieldPath: metadata.namespace                
          - name: POD_IP
            valueFrom:
              fieldRef:
                fieldPath: status.podIP        

We do not have any deployment in our cluster yet kubectl get deployments

image

Now Let's create the deployment

kubectl apply -f deployment.yaml

Next let's check if the deployment is created kubectl get deployments -n development

image

โš ๏ธ Notice pod-info-deployment is created with 3 containers not ready yet after 15s but wait for few more time and again check you should have 3 pods ready to go and available

image

Next let's check all the pods in the development namespace

kubectl get pods -n development 

image

Notice all the pods are running.

You have set the replica as 3 so always 3 pods will be running. To proove that go ahead and delete a pod and check.

Delete first pod by copy it's name and use delete pod command.

kubectl delete pod pod-info-deployment-757cb75bbb-l2jd8 -n development 

Check how much pods you left kubectl get pods -n development

image

๐Ÿ˜ฒ You see still 3 pods, K8 deployment automatically terminated the one that you asked to delete and created brand new pod to make sure at any time 3 pods are running.

๐ŸŽ‰ Pet yourself ๐Ÿถ, you created 3 pods with k8 deployment.

How to check health of your POD?

You check the health of a pod by looking at the event logs. Why pods may not work: container image not available, Worker nodes are out of space so pod can not be schedule.

# Find the pod that you want to check logs 
kubectl get pods -n development
 
kubectl describe pod <pod-name> -n development

# We will take first pod and check its logs 
kubectl describe pod pod-info-deployment-757cb75bbb-8cgf8 -n development

image

If your pod is not working check at the bottom and you will see the events with error messages.

How to check your application is Working?

You can use BusyBox tool to debug and troubleshoot issues in linux environment. For this you have to re-deploy your application with busybox tool.

In below definition busybox.yaml of deployment, we will use 1 replica and deploy application in default namespace. We will pull the image from busybox:latest.

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: busybox
  namespace: default
  labels:
    app: busybox
spec:
  replicas: 1
  selector:
    matchLabels:
      app: busybox
  template:
    metadata:
      labels:
        app: busybox
    spec:
      containers:
      - name: busybox-container
        image: busybox:latest
        # Keep the container running
        command: [ "/bin/sh", "-c", "--" ]
        args: [ "while true; do sleep 30; done;" ]
        resources:
          requests:
            cpu: 30m
            memory: 64Mi
          limits:
            cpu: 100m
            memory: 128Mi

Deploy it

kubectl apply -f busybox.yaml

image

Get pods kubectl get pods

image

Open new terminal and fetch all pods in development with its ip details -o wide gives pods extra info like IP address

kubectl get pods -n development -o wide 

image

Next, run command to get into the busybox pod and open bash shell. Copy the busybox pod name from previous open terminal.

kubectl exec -it busybox-6b95744666-zh9tx -- /bin/sh

You will see command shell type wget to see it is installed or not.

image

Next from busybox pod I will try to wget one of the IP address from development pod. Copy one of the IP of the pod from development namespace and run below script on busybox interactive terminal.

wget 10.244.0.13

You will get connection refused error

image

Notice that busybox is trying to access pod with default port 80 (10.244.0.13:80) however, our pod in development is deployed in port 3000 so lets change the script to include port number 3000 and run in busybox terminal

wget 10.244.0.13:3000

You are able to connect good news image

Check the output of the pod application saved in the index.html file. Wget saved info in the html file.

cat index.html

image

So you deployed an application in development namespace and used busybox to check if it is working or not. Next you will learn how to check application logs.

Run exit to go out of terminal of pod.

View Application logs

If you want to debug application issues use application logs.

kubectl get pods -n development

Inspect log

kubectl logs <pod-name> -n development
kubectl logs pod-info-deployment-757cb75bbb-pdtxc -n development

image

So we were able to access our pods using busybox however how do you access your pods from internet? You will learn next.

Expose your application to the internet with a LoadBalancer

K8 Service is a LoadBalancer service, it directs traffic from internet to pods. Loadbalancer service has public and private static IP addess. We had deployed pod with selector pod-info.

There are 3 types of kubernetes services: 1) LoadBalancer, 2) Nodeport 3) ClusterIP.

You will create LoadBalancer type service by creating service.yaml and running it.

---
apiVersion: v1
kind: Service
metadata:
  name: demo-service
  namespace: development
spec:
  selector:
    app: pod-info
  ports:
    - port: 80
      targetPort: 3000
  type: LoadBalancer
    

Creae Service

kubectl apply -f service.yaml

Get Service external/internal IP

kubectl get services -n development

image

Next in order to get external ip lets run below command in another terminal

sudo minikube tunnel 

Next rerun service command

kubectl get services -n development

Notice external IP is displayed as localhost. Since minikube has setup your cluster in localbox it will use localhost as external IP. If you create Loadbalancer from GCP, Azure or AWS then you would get real external internet exposed IP address.

image

Next, you will access the IP address to visit http://127.0.0.1 from your laptop. Notice site not loading so go to the terminal where you run tunnel command there you have to enter your password to start this app and refresh the browser again.

image

http://127.0.0.1 site is giving the output where it shows the pod details. image

You have used k8 loadbalancer service to expose the pod into the internet. Next you will restrict the resource request limit on the container in the pods.

pts, containerization, Kubernetes architecture, and how to deploy and manage your applications using Kubernetes.

So, if you're excited to learn Kubernetes and take your application development skills to the next level, let's dive in and embark on this Kubernetes journey together! ๐ŸŒˆโœจ

Installing dependencies: Docker Desktop for MacOS

Before we proceed with setting up Minikube, it is essential to ensure that Docker is installed and running on your macOS machine. Here are the steps to install Docker Desktop on macOS and start the Docker service:

image

Next run docker to see if your docker is running

image

Installing minikube

Minikube is local Kubernetes, focusing on making it easy to learn and develop for Kubernetes.

curl -LO https://github.com/kubernetes/minikube/releases/download/v1.30.1/minikube-darwin-arm64  
sudo install minikube-darwin-arm64 /usr/local/bin/minikube

Start Minikube, before starting minikube make sure docker is running by openeing docker desktop on mac. Minikube start may take few minutes to generate the cluster.

minikube start

image

Next lets explore you Kubernetes(K8) cluster

kubectl cluster-info

You will see the ip address as localhost address since this cluster is setup on your machine running at https://127.0.0.1:53273

image

Next check all the nodes

kubectl get nodes

image

We get the error that means our cluster is not running yet. Let's run minikube start one more time. And then re-run kubectl get nodes

image

One node with name minikube with role of control-plane my k8 is running with version 1.26.

Let's check namespaces created by default

kubectl get namespaces

image

You isolate and manage applications and services using namespace.

Next let's see the pods created in all namespaces by passing -A.

kubectl get pods -A

image

These pods, how containers are running k8 these pods have softwares to run k8 cluster itself.

Now let's see all the services. Services act as the loadbalancer and direct the traffic to the pods.

kubectl get services -A

image

Creating Namespaces

If you are using single cluster for your applicaiton then You can namespace to isolate and organize workloads. Example create dev and prod namespaces to separate them.

Check existing namespaces

kubectl get namespaces 

K8 menefest to create a namespace called development in file namespace.yml

---
apiVersion: v1
kind: Namespace
metadata:
  name: development

Let's create the namesapce by runing command

kubectl apply -f namespace.yaml

image

Let's change the namespace.yaml to have both production and development namespaces and run kubectl apply -f namespace.yaml to create both of them.

---
apiVersion: v1
kind: Namespace
metadata:
  name: development
--- # way to signify new document in yaml
apiVersion: v1
kind: Namespace
metadata:
  name: production

image

Now let's delete these 2 namespaces

kubectl delete -f namespace.yaml

image

Deploy an Application

Let's create Highly available application that will be deployed in 3 different pods. We will declare our dpplicaiton inside the deployment.yaml file how we want our application to be deployed in development namespace, with 3 replicas. We will run container in the pod which will display the information of the pod that it is running within you will use kimschles/pod-info-app:latest image that will have that pod info app already created. We will setup container with 3 environment variables for pod name, namespace and ip that will be used by our app.

Let's create the development namespace first. Run previous command kubectl apply -f namespace.yaml to deploy namespace.

image

Next create deployment.yaml with below definition.

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: pod-info-deployment
  namespace: development
  labels:
    app: pod-info
spec:
  replicas: 3
  selector:
    matchLabels:
      app: pod-info
  template:
    metadata:
      labels:
        app: pod-info
    spec:
      containers:
      - name: pod-info-container
        image: kimschles/pod-info-app:latest
        ports:
        - containerPort: 3000
        env:
          - name: POD_NAME
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          - name: POD_NAMESPACE
            valueFrom:
              fieldRef:
                fieldPath: metadata.namespace                
          - name: POD_IP
            valueFrom:
              fieldRef:
                fieldPath: status.podIP        

We do not have any deployment in our cluster yet kubectl get deployments

image

Now Let's create the deployment

kubectl apply -f deployment.yaml

Next let's check if the deployment is created kubectl get deployments -n development

image

โš ๏ธ Notice pod-info-deployment is created with 3 containers not ready yet after 15s but wait for few more time and again check you should have 3 pods ready to go and available

image

Next let's check all the pods in the development namespace

kubectl get pods -n development 

image

Notice all the pods are running.

You have set the replica as 3 so always 3 pods will be running. To proove that go ahead and delete a pod and check.

Delete first pod by copy it's name and use delete pod command.

kubectl delete pod pod-info-deployment-757cb75bbb-l2jd8 -n development 

Check how much pods you left kubectl get pods -n development

image

๐Ÿ˜ฒ You see still 3 pods, K8 deployment automatically terminated the one that you asked to delete and created brand new pod to make sure at any time 3 pods are running.

๐ŸŽ‰ Pet yourself ๐Ÿถ, you created 3 pods with k8 deployment.

How to check health of your POD?

You check the health of a pod by looking at the event logs. Why pods may not work: container image not available, Worker nodes are out of space so pod can not be schedule.

# Find the pod that you want to check logs 
kubectl get pods -n development
 
kubectl describe pod <pod-name> -n development

# We will take first pod and check its logs 
kubectl describe pod pod-info-deployment-757cb75bbb-8cgf8 -n development

image

If your pod is not working check at the bottom and you will see the events with error messages.

How to check your application is Working?

You can use BusyBox tool to debug and troubleshoot issues in linux environment. For this you have to re-deploy your application with busybox tool.

In below definition busybox.yaml of deployment, we will use 1 replica and deploy application in default namespace. We will pull the image from busybox:latest.

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: busybox
  namespace: default
  labels:
    app: busybox
spec:
  replicas: 1
  selector:
    matchLabels:
      app: busybox
  template:
    metadata:
      labels:
        app: busybox
    spec:
      containers:
      - name: busybox-container
        image: busybox:latest
        # Keep the container running
        command: [ "/bin/sh", "-c", "--" ]
        args: [ "while true; do sleep 30; done;" ]
        resources:
          requests:
            cpu: 30m
            memory: 64Mi
          limits:
            cpu: 100m
            memory: 128Mi

Deploy it

kubectl apply -f busybox.yaml

image

Get pods kubectl get pods

Gettings started with Kubernetes on your local macOS.

๐ŸŒŸ Are you ready to dive into the world of Kubernetes from scratch? ๐Ÿ’ป

๐Ÿš€ Calling all aspiring developers! Whether you're an Angular, .NET, or Java enthusiast, a frontend or backend guru, or a full stack enthusiast, this step-by-step beginner's guide is perfect for you! ๐ŸŽ‰

๐Ÿ“š In this comprehensive tutorial, we will start from the very basics and guide you through the process of getting started with Kubernetes. You don't need any prior knowledge of Kubernetes or containerization - we'll cover everything from the ground up. You'll learn about the core concepts, containerization, Kubernetes architecture, and how to deploy and manage your applications using Kubernetes.

So, if you're excited to learn Kubernetes and take your application development skills to the next level, let's dive in and embark on this Kubernetes journey together! ๐ŸŒˆโœจ

Installing dependencies: Docker Desktop for MacOS

Before we proceed with setting up Minikube, it is essential to ensure that Docker is installed and running on your macOS machine. Here are the steps to install Docker Desktop on macOS and start the Docker service:

image

Next run docker to see if your docker is running

image

Installing minikube

Minikube is local Kubernetes, focusing on making it easy to learn and develop for Kubernetes.

curl -LO https://github.com/kubernetes/minikube/releases/download/v1.30.1/minikube-darwin-arm64  
sudo install minikube-darwin-arm64 /usr/local/bin/minikube

Start Minikube, before starting minikube make sure docker is running by openeing docker desktop on mac. Minikube start may take few minutes to generate the cluster.

minikube start

image

Next lets explore you Kubernetes(K8) cluster

kubectl cluster-info

You will see the ip address as localhost address since this cluster is setup on your machine running at https://127.0.0.1:53273

image

Next check all the nodes

kubectl get nodes

image

We get the error that means our cluster is not running yet. Let's run minikube start one more time. And then re-run kubectl get nodes

image

One node with name minikube with role of control-plane my k8 is running with version 1.26.

Let's check namespaces created by default

kubectl get namespaces

image

You isolate and manage applications and services using namespace.

Next let's see the pods created in all namespaces by passing -A.

kubectl get pods -A

image

These pods, how containers are running k8 these pods have softwares to run k8 cluster itself.

Now let's see all the services. Services act as the loadbalancer and direct the traffic to the pods.

kubectl get services -A

image

Creating Namespaces

If you are using single cluster for your applicaiton then You can namespace to isolate and organize workloads. Example create dev and prod namespaces to separate them.

Check existing namespaces

kubectl get namespaces 

K8 menefest to create a namespace called development in file namespace.yml

---
apiVersion: v1
kind: Namespace
metadata:
  name: development

Let's create the namesapce by runing command

kubectl apply -f namespace.yaml

image

Let's change the namespace.yaml to have both production and development namespaces and run kubectl apply -f namespace.yaml to create both of them.

---
apiVersion: v1
kind: Namespace
metadata:
  name: development
--- # way to signify new document in yaml
apiVersion: v1
kind: Namespace
metadata:
  name: production

image

Now let's delete these 2 namespaces

kubectl delete -f namespace.yaml

image

Deploy an Application

Let's create Highly available application that will be deployed in 3 different pods. We will declare our dpplicaiton inside the deployment.yaml file how we want our application to be deployed in development namespace, with 3 replicas. We will run container in the pod which will display the information of the pod that it is running within you will use kimschles/pod-info-app:latest image that will have that pod info app already created. We will setup container with 3 environment variables for pod name, namespace and ip that will be used by our app.

Let's create the development namespace first. Run previous command kubectl apply -f namespace.yaml to deploy namespace.

image

Next create deployment.yaml with below definition.

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: pod-info-deployment
  namespace: development
  labels:
    app: pod-info
spec:
  replicas: 3
  selector:
    matchLabels:
      app: pod-info
  template:
    metadata:
      labels:
        app: pod-info
    spec:
      containers:
      - name: pod-info-container
        image: kimschles/pod-info-app:latest
        ports:
        - containerPort: 3000
        env:
          - name: POD_NAME
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          - name: POD_NAMESPACE
            valueFrom:
              fieldRef:
                fieldPath: metadata.namespace                
          - name: POD_IP
            valueFrom:
              fieldRef:
                fieldPath: status.podIP        

We do not have any deployment in our cluster yet kubectl get deployments

image

Now Let's create the deployment

kubectl apply -f deployment.yaml

Next let's check if the deployment is created kubectl get deployments -n development

image

โš ๏ธ Notice pod-info-deployment is created with 3 containers not ready yet after 15s but wait for few more time and again check you should have 3 pods ready to go and available

image

Next let's check all the pods in the development namespace

kubectl get pods -n development 

image

Notice all the pods are running.

You have set the replica as 3 so always 3 pods will be running. To proove that go ahead and delete a pod and check.

Delete first pod by copy it's name and use delete pod command.

kubectl delete pod pod-info-deployment-757cb75bbb-l2jd8 -n development 

Check how much pods you left kubectl get pods -n development

image

๐Ÿ˜ฒ You see still 3 pods, K8 deployment automatically terminated the one that you asked to delete and created brand new pod to make sure at any time 3 pods are running.

๐ŸŽ‰ Pet yourself ๐Ÿถ, you created 3 pods with k8 deployment.

How to check health of your POD?

You check the health of a pod by looking at the event logs. Why pods may not work: container image not available, Worker nodes are out of space so pod can not be schedule.

# Find the pod that you want to check logs 
kubectl get pods -n development
 
kubectl describe pod <pod-name> -n development

# We will take first pod and check its logs 
kubectl describe pod pod-info-deployment-757cb75bbb-8cgf8 -n development

image

If your pod is not working check at the bottom and you will see the events with error messages.

How to check your application is Working?

You can use BusyBox tool to debug and troubleshoot issues in linux environment. For this you have to re-deploy your application with busybox tool.

In below definition busybox.yaml of deployment, we will use 1 replica and deploy application in default namespace. We will pull the image from busybox:latest.

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: busybox
  namespace: default
  labels:
    app: busybox
spec:
  replicas: 1
  selector:
    matchLabels:
      app: busybox
  template:
    metadata:
      labels:
        app: busybox
    spec:
      containers:
      - name: busybox-container
        image: busybox:latest
        # Keep the container running
        command: [ "/bin/sh", "-c", "--" ]
        args: [ "while true; do sleep 30; done;" ]
        resources:
          requests:
            cpu: 30m
            memory: 64Mi
          limits:
            cpu: 100m
            memory: 128Mi

Deploy it

kubectl apply -f busybox.yaml

image

Get pods kubectl get pods

image

Open new terminal and fetch all pods in development with its ip details -o wide gives pods extra info like IP address

kubectl get pods -n development -o wide 

image

Next, run command to get into the busybox pod and open bash shell. Copy the busybox pod name from previous open terminal.

kubectl exec -it busybox-6b95744666-zh9tx -- /bin/sh

You will see command shell type wget to see it is installed or not.

image

Next from busybox pod I will try to wget one of the IP address from development pod. Copy one of the IP of the pod from development namespace and run below script on busybox interactive terminal.

wget 10.244.0.13

You will get connection refused error

image

Notice that busybox is trying to access pod with default port 80 (10.244.0.13:80) however, our pod in development is deployed in port 3000 so lets change the script to include port number 3000 and run in busybox terminal

wget 10.244.0.13:3000

You are able to connect good news image

Check the output of the pod application saved in the index.html file. Wget saved info in the html file.

cat index.html

image

So you deployed an application in development namespace and used busybox to check if it is working or not. Next you will learn how to check application logs.

Run exit to go out of terminal of pod.

View Application logs

If you want to debug application issues use application logs.

kubectl get pods -n development

Inspect log

kubectl logs <pod-name> -n development
kubectl logs pod-info-deployment-757cb75bbb-pdtxc -n development

image

So we were able to access our pods using busybox however how do you access your pods from internet? You will learn next.

Expose your application to the internet with a LoadBalancer

K8 Service is a LoadBalancer service, it directs traffic from internet to pods. Loadbalancer service has public and private static IP addess. We had deployed pod with selector pod-info.

There are 3 types of kubernetes services: 1) LoadBalancer, 2) Nodeport 3) ClusterIP.

You will create LoadBalancer type service by creating service.yaml and running it.

---
apiVersion: v1
kind: Service
metadata:
  name: demo-service
  namespace: development
spec:
  selector:
    app: pod-info
  ports:
    - port: 80
      targetPort: 3000
  type: LoadBalancer
    

Creae Service

kubectl apply -f service.yaml

Get Service external/internal IP

kubectl get services -n development

image

Next in order to get external ip lets run below command in another terminal

sudo minikube tunnel 

Next rerun service command

kubectl get services -n development

Notice external IP is displayed as localhost. Since minikube has setup your cluster in localbox it will use localhost as external IP. If you create Loadbalancer from GCP, Azure or AWS then you would get real external internet exposed IP address.

image

Next, you will access the IP address to visit http://127.0.0.1 from your laptop. Notice site not loading so go to the terminal where you run tunnel command there you have to enter your password to start this app and refresh the browser again.

image

http://127.0.0.1 site is giving the output where it shows the pod details. image

You have used k8 loadbalancer service to expose the pod into the internet. Next you will restrict the resource request limit on the container in the pods.

learning-kubernetes's People

Contributors

rupeshtiwari avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.