July 6, 2023

Kubernetes Summary for Basic Usage

Kubernetes Summary for Basic Usage

1. What and Why

Kubernetes (often abbreviated as K8s) is a powerful container orchestration platform that has become the de facto standard for managing containerized applications in production environments. Here are some key reasons why Kubernetes is widely used and considered essential in modern software development:

  1. Container Orchestration: Kubernetes provides a robust framework for orchestrating and managing containerized applications. It automates deployment, scaling, load balancing, and self-healing of containers, making it easier to run and maintain complex applications.

  2. Portability: Kubernetes abstracts away the underlying infrastructure, allowing you to run applications consistently across various environments, including on-premises data centers, public clouds (e.g., AWS, Azure, Google Cloud), and hybrid environments. This portability reduces vendor lock-in and provides flexibility.

  3. Scalability: Kubernetes can automatically scale your application up or down based on demand. It ensures that your application can handle increased traffic without manual intervention, improving performance and resource utilization.

  4. High Availability: Kubernetes offers built-in support for high availability. It can distribute your application across multiple nodes (servers) and recover from failures, ensuring that your application remains accessible even when some components fail.

  5. Resource Management: Kubernetes allows you to allocate resources (CPU, memory, storage) to containers and applications, ensuring efficient utilization of resources. This prevents one application from starving others of resources.

  6. Service Discovery and Load Balancing: Kubernetes provides service discovery and load balancing out of the box. It manages network traffic, automatically routing requests to the appropriate containers or pods.

  7. Rolling Updates and Rollbacks: Kubernetes simplifies the process of rolling out updates to your applications. It allows you to perform updates without downtime and provides the ability to roll back to a previous version if issues arise.

  8. Secrets and Configuration Management: Kubernetes provides a secure way to manage sensitive data, such as API keys and passwords, through Secrets. It also offers ConfigMaps for managing configuration settings separately from application code.

  9. Ecosystem and Extensibility: Kubernetes has a large and active community, which has led to a rich ecosystem of extensions and tools. You can extend Kubernetes’ functionality through custom resources and operators to fit your specific requirements.

  10. DevOps and CI/CD Integration: Kubernetes integrates seamlessly with DevOps practices and CI/CD pipelines. You can use tools like Helm and Jenkins to automate application deployments and updates.

  11. Observability: Kubernetes offers built-in monitoring and logging capabilities. You can integrate it with popular observability tools like Prometheus and Grafana to gain insights into your application’s performance.

  12. Cost Efficiency: Kubernetes helps optimize resource usage, which can lead to cost savings in cloud environments by preventing over-provisioning and allowing for better resource allocation.

In summary, Kubernetes simplifies the management of containerized applications at scale, making it an essential tool for organizations adopting microservices architecture, cloud-native development, and containerization. Its flexibility, scalability, and extensive ecosystem have made it a crucial part of modern software development and deployment workflows.

2. Important Concepts

  1. Node: virtual or physical machine;
  2. Pod: smallest unit of k8s, abstraction of container, has its own IP address;
  3. Service: permanent IP address, internal service for pods, external service for users;
  4. Ingress: external service for users, can be used to do load balancing;
  5. ConfigMap: some configuration which don’t need to be encrypted live here;
  6. Secret: some configuration which need to be encrypted live here;
  7. Volume: persistent storage;
  8. Deployment: for stateless app;
  9. StatefulSet: for stateful apps or databases;



3. Important file

Our example has a wep app and mongo database.

config.yaml

1
2
3
4
5
6
apiVersion: v1
kind: ConfigMap
metadata:
name: mongo-config
data:
mongo-url: mongo-service

secret.yaml

1
2
3
4
5
6
7
8
apiVersion: v1
kind: Secret
metadata:
name: mongo-secret
type: Opaque
data:
mongo-user: cm9vdA==
mongo-password: TGMxNTUwMzU==

You can use echo -n <content> | base64 to get easily encrypted content.

mongo.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
apiVersion: apps/v1
kind: Deployment
metadata:
name: mongo-deployment
labels:
app: mongo
spec:
replicas: 1
selector:
matchLabels:
app: mongo
template:
metadata:
labels:
app: mongo
spec:
containers:
- name: mongodb
image: mongo:5.0
ports:
- containerPort: 27017
env:
- name: MONGO_INITDB_ROOT_USERNAME
valueFrom:
secretKeyRef:
name: mongo-secret
key: mongo-user
- name: MONGO_INITDB_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mongo-secret
key: mongo-password
---
apiVersion: v1
kind: Service
metadata:
name: mongo-service
spec:
selector:
app: mongo
ports:
- protocol: TCP
port: 27017
targetPort: 27017

Label is used to manage several services or pods with different names but same label.

1
2
3
4
5
6
env:
-name:
valueFrom:
secretKeyRef/configMapKeyRef:
name:
key:

The section above can be used to set environment variables of pods in order to make some configuations.

webapp.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
apiVersion: apps/v1
kind: Deployment
metadata:
name: webapp-deployment
labels:
app: webapp
spec:
replicas: 1
selector:
matchLabels:
app: webapp
template:
metadata:
labels:
app: webapp
spec:
containers:
- name: webapp
image: nanajanashia/k8s-demo-app:v1.0
ports:
- containerPort: 3000
env:
- name: USER_NAME
valueFrom:
secretKeyRef:
name: mongo-secret
key: mongo-user
- name: USER_PWD
valueFrom:
secretKeyRef:
name: mongo-secret
key: mongo-password
- name: DB_URL
valueFrom:
configMapKeyRef:
name: mongo-config
key: mongo-url
---
apiVersion: v1
kind: Service
metadata:
name: webapp-service
spec:
type: NodePort
selector:
app: webapp
ports:
- protocol: TCP
port: 3000
targetPort: 3000
nodePort: 30100

--- can be used to split different services’ deployment.

4. Some Important Commands

start Minikube and check status

1
2
3
minikube start --vm-driver=hyperkit
minikube start --driver docker
minikube status

get minikube node’s ip address

1
minikube ip

get basic info about k8s components

1
2
3
4
kubectl get node
kubectl get pod
kubectl get svc
kubectl get all

get extended info about components

1
2
kubectl get pod -o wide
kubectl get node -o wide

get detailed info about a specific component

1
2
kubectl describe svc {svc-name}
kubectl describe pod {pod-name}

get application logs

1
kubectl logs {pod-name}

stop your Minikube cluster

1
minikube stop

About this Post

This post is written by Chen Li, licensed under CC BY-NC 4.0.

#tutorial