A presentation of the basics concepts of Kubernetes
Kubernetes kesako ?
Change context to switch between namespaces
cf: cheatsheet (kcontext, kns)
apiVersion: v1
kind: Pod
metadata:
name: nginx-web
labels:
app: nginx
spec:
containers:
- name: web
image: nginx
ports:
- name: web
containerPort: 80
protocol: TCP
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: web
image: nginx:1.14.2
ports:
- containerPort: 80
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 8100
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: minimal-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- http:
paths:
- path: /app
pathType: Prefix
backend:
service:
name: nginx-service
port:
number: 80
apiVersion: v1
kind: ConfigMap
metadata:
name: game-demo
data:
# property-like keys; each key maps to a simple value
ENVIRONMENT_NAME: "INDUS"
PROFILE: "indus-profile"
# file-like keys
application.properties: |
bootstrap.servers= "http://kafka-server-indus:9092"
max.poll.records=100
common.properties: |
log_level="info"
key.serializer="io.confluent.kafka.serializers.KafkaAvroSerializer"
value.serializer="io.confluent.kafka.serializers.KafkaAvroSerializer"
apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
data:
USERNAME: YWRtaW4=
PASSWORD: MWYyZDFlMmU2N2Rm
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: nginx-pdb
spec:
minAvailable: 2 # maxUnavailable # % or fixed value
selector:
matchLabels:
app: nginx
➜ kubectl config view
kind: Config
apiVersion: v1
preferences: {}
clusters:
- cluster:
certificate-authority-data: DATA+OMITTED
server: http://apiserver-my-cluster.my-domain.com:8443
name: dcluster
contexts:
- context:
cluster: dcluster
user: user_ident
namespace: dteam1
name: dteam1@dcluster
current-context: dteam1@dcluster
users:
- name: user_ident
user:
token: REDACTED
➜ kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-32j54h2 1/1 Running 0 48m25s
front-app-34s53d4 0/1 Init 0/1 0 5s
backend-app-45r65g6 0/1 CrashloopBackoff 4 19m58s
➜ kubectl describe pod nginx-32j54h2
Name: nginx-32j54h2
Namespace: dteam1
Priority: 0
Node: docker-desktop/192.168.65.4
Start Time: Fri, 25 Mar 2022 17:50:27 +0100
Labels: app=nginx
Annotations:
Status: Running
IP: 10.1.0.15
IPs:
IP: 10.1.0.15
Containers:
nginx:
Container ID: docker://ec3d2dc0aafbd88c7fba3a0d9bd3f4d49030bb88c28a3c0a6649c0a80794aeaf
Image: nginx:latest
Image ID: docker-pullable://nginx@sha256:4ed64c2e0857ad21c38b98345ebb5edb01791a0a10b0e9e3d9ddde185cdbd31a
Port:
Host Port:
State: Running
Started: Mon, 28 Mar 2022 13:24:49 +0200
Last State: Terminated
Reason: Error
Exit Code: 255
Started: Fri, 25 Mar 2022 17:50:29 +0100
Finished: Mon, 28 Mar 2022 13:24:38 +0200
Ready: True
Restart Count: 1
Environment:
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-m67dr (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
kube-api-access-m67dr:
Type: Projected (a volume that contains injected data from multiple sources)
TokenExpirationSeconds: 3607
ConfigMapName: kube-root-ca.crt
ConfigMapOptional:
DownwardAPI: true
QoS Class: BestEffort
Node-Selectors:
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SandboxChanged 29m kubelet Pod sandbox changed, it will be killed and re-created.
Normal Pulling 29m kubelet Pulling image "nginx:latest"
Normal Pulled 29m kubelet Successfully pulled image "nginx:latest" in 1.355729459s
Normal Created 29m kubelet Created container nginx
Normal Started 29m kubelet Started container nginx
Normal Killing 9s kubelet Container nginx definition changed, will be restarted
Normal Pulling 9s kubelet Pulling image "nginx:1.0.0"
Warning Failed 8s kubelet Failed to pull image "nginx:1.0.0": rpc error: code = Unknown desc = Error response from daemon: manifest for nginx:1.0.0 not found: manifest unknown: manifest unknown
Warning Failed 8s kubelet Error: ErrImagePull
Warning BackOff 7s kubelet Back-off restarting failed container
➜ kubectl logs nginx-32j54h2
2022/03/28 11:24:49 [notice] 1#1: using the "epoll" event method
2022/03/28 11:24:49 [notice] 1#1: nginx/1.21.6
2022/03/28 11:24:49 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2022/03/28 11:24:49 [notice] 1#1: OS: Linux 5.10.104-linuxkit
2022/03/28 11:24:49 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/03/28 11:24:49 [notice] 1#1: start worker processes
2022/03/28 11:24:49 [notice] 1#1: start worker process 31
...
➜ kubectl logs --help
# Retrieve only logs since Xh
➜ kubectl logs < name > --since Xh
# Specify a container to display his logs
➜ kubectl logs < name > -c < container_name >
# Follow the new logs printed by the pod
➜ kubectl logs < name > -f
➜ stern nginx
+ nginx-0 › nginx
+ nginx-1 › nginx
nginx-0 nginx 2022/03/28 13:15:29 [notice] 1#1: using the "epoll" event method
nginx-0 nginx 2022/03/28 13:15:29 [notice] 1#1: nginx/1.21.6
nginx-0 nginx 2022/03/28 13:15:29 [notice] 1#1: start worker processes
nginx-0 nginx 2022/03/28 13:15:29 [notice] 1#1: start worker process 31
nginx-0 nginx 2022/03/28 13:15:29 [notice] 1#1: start worker process 32
nginx-1 nginx 2022/03/28 13:15:05 [notice] 1#1: nginx/1.21.6
nginx-1 nginx 2022/03/28 13:15:05 [notice] 1#1: start worker processes
nginx-1 nginx 2022/03/28 13:15:05 [notice] 1#1: start worker process 32
nginx-1 nginx 2022/03/28 13:15:05 [notice] 1#1: start worker process 33
nginx-0 nginx 2022/03/28 13:15:29 [notice] 1#1: start worker process 33
nginx-0 nginx 2022/03/28 13:15:29 [notice] 1#1: start worker process 34
➜ kubectl delete pod nginx-32j54h2
pod "nginx-32j54h2" deleted
➜ kubectl edit deploy nginx
... open with vim
deployment/nginx edited
➜ kubectl scale deploy --replicas 2 nginx
deployment/nginx scaled
kind: Deployment
spec:
template:
spec:
containers:
- name: nginx
image: nginx:1.0.0 # image not exists
kind: Deployment
spec:
template:
spec:
containers:
- name: nginx
resources:
limits:
cpu: 12 # huge amount of cpu ! => forbidden
memory: 30Gi
requests:
cpu: 500m
memory: 175Mi
Thanks for your time ! ⏱
Some questions ? 🙋♂️