Skip to content

Vente16/kubernetes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

Kubernetes ☸️

This repository contains some key concepts about Kubernetes and how to get started with it.

What is Kubernetes?

Kubernetes is an open-source tool for managing containers. It was initially released by Google in 2014. But what does managing containers exactly mean? Well, imagine you have 5 containers, each containing an image for your app. If you need to replicate these containers, it would be easy to do, right? Of course, it doesn't require a lot of effort. However, when you need to administer 100, 300, 500, or more containers, it becomes challenging to handle them all. This is where Kubernetes comes in. So, what does Kubernetes exactly do? These are some features of Kubernetes:

  • Service discovery
  • Rollouts and Rollbacks: Updates and deploys without downtime
  • Resource optimization in nodes
  • Self-healing: If any container fails, Kubernetes brings it back automatically

Kubernetes Architecture

kubernetes-architecture

How to Get Started with Kubernetes?

To simulate a cluster locally, you can use Minikube. To interact with the cluster, you need kubectl, which is the Kubernetes command-line tool. Here are the documentation links for both:

Key Concepts

  • Pod: It's the smallest unit of Kubernetes. In simple words, a pod is a container, similar to a Docker container. The difference is that you can have one or more containers in a pod, whereas in Docker, there is only one container per image. Here's an example to create a pod:

    1. Create a file called pod.yaml.
    2. Put the following content inside the file:
      apiVersion: v1
      kind: Pod
      metadata:
        name: first-pod
      spec:
        containers:
          - name: nginx-container
            image: nginx
            ports:
              - containerPort: 80
    3. This pod has a container named nginx-container with the nginx image. The image is downloaded from Dockerhub (it can be our own image as well), and it will run on port 80. To create the pod, run the following command:
      kubectl apply -f pod.yaml
    4. To check the pods, run:
      kubectl get pods
    5. To delete a pod, use the following command:
      kubectl delete pod [pod-name]

    If you would like to know more about pods, check the documentation here.

    Note: Typically, we won't use just a pod because if any container fails, the pod will also fail and won't be brought up again.

  • ReplicaSet: A ReplicaSet is an object that manages pods. For example, if you have 3 pods, and one of them fails, the ReplicaSet detects the failure and creates a new pod. Here's an example to create a ReplicaSet:

    1. Create a file called rs.yaml.
    2. Put the following content inside the file:
      apiVersion: apps/v1
      kind: ReplicaSet
      metadata:
        name: frontend
        labels:
          app: rs-test
          tier: frontend
      spec:
        replicas: 2
        selector:
          matchLabels:
            tier: frontend
        template:
          metadata:
            labels:
              tier: frontend
          spec:
            containers:
              - name: container1
                image: python:3.6-alpine
                command: ['sh', '-c', 'echo "container 1" > index.html && python -m http.server 8082']
              - name: container2
                image: python:3.6-alpine
                command: ['sh', '-c', 'echo "container 2" > index.html && python -m http.server 8083']
    3. This ReplicaSet will have two replicas (pods), each with two containers (container1, container2) using the python:3.6-alpine image (they can be different images). Labels are important for identifying the pods. To create the ReplicaSet, run the following command:
      kubectl apply -f rs.yaml
    4. To check the ReplicaSet, run:
      kubectl get replicaset

    If you would like to know more about ReplicaSets, check the documentation here.

  • Deployment: A Deployment is an object that manages ReplicaSets. It allows us to update the configuration of our YAML file. For example, if we need to change the version of a specific image or add a new image or any configuration, the Deployment creates a new ReplicaSet, keeping the previous ReplicaSet(s) in case we need to roll back. Here's an example to create a Deployment:

    1. Create a file called dev.yaml.
    2. Put the following content inside the file:
      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: nginx-deployment
        labels:
          app: front
      spec:
        replicas: 3
        selector:
          matchLabels:
            app: front
        template:
          metadata:
            labels:
              app: front
          spec:
            containers:
              - name: nginx
                image: nginx
                ports:
                  - containerPort: 80
    3. The name of the deployment is nginx-deployment, with a label app and value front. The spec part specifies 3 replicas, a selector, and a template. The template contains a container named nginx with the nginx image and port 80.
    4. To create the Deployment, run the following command:
      kubectl apply -f dev.yaml
    5. To check the Deployment, run:
      kubectl get deployment

    If you would like to know more about Deployments, check the documentation here.

I hope this helps! Let me know if you have any further questions.

About

Learning k8s

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published