Kubernetes Practice
Install
kubectl
Pod

Replicaset
Deployment
Service
Last updated

Last updated
curl -sfL https://get.k3s.io | sh -
sudo chown donghyunlee:donghyunlee /etc/rancher/k3s/k3s.yamlkubectl get nodescp /etc/rancher/k3s/k3s.yaml ~/.kube/configkubectl apply -f https://raw.githubusercontent.com/rancher/local-path-provisioner/master/deploy/local-path-storage.yamlkubectl patch storageclass local-path -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'kubectl get storageclass# pod, replicaset, deployment, service 조회
kubectl get all
# node 조회
kubectl get no
kubectl get node
kubectl get nodes
# 결과 포맷 변경
kubectl get nodes -o wide
kubectl get nodes -o yaml
kubectl get nodes -o json# kubectl describe type/name
# kubectl describe type name
kubectl describe node/<node name>
kubectl describe node <node name>kubectl exec -it <POD_NAME>
kubectl logs -f <POD_NAME|TYPE/NAME>
kubectl apply -f <FILENAME>
kubectl delete -f <FILENAME>kubectl run whoami --image subicura/whoami:1 # deprecated soon..
# kubectl get po
# kubectl get pod
kubectl get pods
kubectl get pods -o wide
kubectl get pods -o yaml
kubectl get pods -o json
kubectl logs whoami-<xxxx>
kubectl logs -f whoami-<xxxx>
kubectl exec -it whoami-<xxxx> sh
kubectl describe pods whoami-<xxxx>
kubectl delete pods whoami-<xxxx>
kubectl get pods
kubectl get all
kubectl delete deployment/whoami# whoami-pod.yml
apiVersion: v1
kind: Pod
metadata:
name: whoami
labels:
type: app
spec:
containers:
- name: app
image: subicura/whoami:1kubectl apply -f <filename>
kubectl delete -f <filename># whoami-pod-lp.yml
apiVersion: v1
kind: Pod
metadata:
name: whoami-lp
labels:
type: app
spec:
containers:
- name: app
image: subicura/whoami:1
livenessProbe:
httpGet:
path: /not/exist
port: 8080
initialDelaySeconds: 5
timeoutSeconds: 2 # Default 1
periodSeconds: 5 # Defaults 10
failureThreshold: 1 # Defaults 3# whoami-pod-rp.yml
apiVersion: v1
kind: Pod
metadata:
name: whoami-rp
labels:
type: app
spec:
containers:
- name: app
image: subicura/whoami:1
readinessProbe:
httpGet:
path: /not/exist
port: 8080
initialDelaySeconds: 5
timeoutSeconds: 2 # Default 1
periodSeconds: 5 # Defaults 10
failureThreshold: 1 # Defaults 3# whoami-pod-health.yml
apiVersion: v1
kind: Pod
metadata:
name: whoami-redis
labels:
type: stack
spec:
containers:
- name: app
image: subicura/whoami-redis:1
env:
- name: REDIS_HOST
value: "localhost"
- name: db
image: redis# whoami-pod-redis.yml
apiVersion: v1
kind: Pod
metadata:
name: whoami-redis
labels:
type: stack
spec:
containers:
- name: app
image: subicura/whoami-redis:1
env:
- name: REDIS_HOST
value: "localhost"
- name: db
image: rediskubectl get all
kubectl logs whoami-redis
kubectl logs whoami-redis app
kubectl logs whoami-redis db
kubectl exec -it whoami-redis
kubectl exec -it whoami-redis -c db sh
kubectl exec -it whoami-redis -c app sh
apk add curl busybox-extras # install telnet
curl localhost:4567
telnet localhost 6379
dbsize
KEYS *
GET count
quit
kubectl get pod/whoami-redis
kubectl get pod/whoami-redis -o yaml
kubectl get pod/whoami-redis -o jsonpath="{.spec.containers[0].name}"
kubectl get pod/whoami-redis -o jsonpath="{.spec.containers[*].name}"
kubectl describe pod/whoami-rediskubectl delete -f ./# whoami-rs.yml
apiVersion: apps/v1beta2
kind: ReplicaSet
metadata:
name: whoami-rs
spec:
replicas: 1
selector:
matchLabels:
type: app
service: whoami
template:
metadata:
labels:
type: app
service: whoami
spec:
containers:
- name: whoami
image: subicura/whoami:1
livenessProbe:
httpGet:
path: /
port: 4567kubectl get pods --show-labels
kubectl label pod/whoami-rs-<xxxx> service-
kubectl label pod/whoami-rs-<xxxx> service=whoami
kubectl scale --replicas=3 -f whoami.yml# whoami-rs-scaled.yml
apiVersion: apps/v1beta2
kind: ReplicaSet
metadata:
name: whoami-rs
spec:
replicas: 4
selector:
matchLabels:
type: app
service: whoami
template:
metadata:
labels:
type: app
service: whoami
spec:
containers:
- name: whoami
image: subicura/whoami:1
livenessProbe:
httpGet:
path: /
port: 4567kubectl delete -f ./# whoami-deploy.yml
apiVersion: apps/v1beta2
kind: Deployment
metadata:
name: whoami-deploy
spec:
replicas: 3
selector:
matchLabels:
type: app
service: whoami
template:
metadata:
labels:
type: app
service: whoami
spec:
containers:
- name: whoami
image: subicura/whoami:1
livenessProbe:
httpGet:
path: /
port: 4567kubectl set image deploy/whoami-deploy whoami=subicura/whoami:2
kubectl apply -f whoami-deploy.yml
kubectl get rs -w
kubectl describe deploy/whoami-deploy
kubectl rollout history -f whoami-deploy.yml
kubectl set image deploy/whoami-deploy whoami=subicura/whoami:1 --record=true
kubectl rollout history -f whoami-deploy.yml
kubectl rollout history -f whoami-deploy.yml --revision=2
kubectl rollout status deploy/whoami-deploy
kubectl rollout undo deploy/whoami-deploy
kubectl rollout undo deploy/whoami-deploy --to-revision=3# whoami-deploy-strategy.yml
apiVersion: apps/v1beta2
kind: Deployment
metadata:
name: whoami-deploy
spec:
replicas: 3
selector:
matchLabels:
type: app
service: whoami
minReadySeconds: 5
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
template:
metadata:
labels:
type: app
service: whoami
spec:
containers:
- name: whoami
image: subicura/whoami:1
livenessProbe:
httpGet:
path: /
port: 4567kubectl describe deploy/whoami-deploy
kubectl set image deploy/whoami-deploy whoami=subicura/whoami:2
kubectl get rs -wkubectl delete -f ./# redis-app.yml
apiVersion: apps/v1beta2
kind: Deployment
metadata:
name: redis
spec:
selector:
matchLabels:
type: db
service: redis
template:
metadata:
labels:
type: db
service: redis
spec:
containers:
- name: redis
image: redis
ports:
- containerPort: 6379
protocol: TCP
---
apiVersion: v1
kind: Service
metadata:
name: redis
spec:
ports:
- port: 6379
protocol: TCP
selector:
type: db
service: redis# whoami-deploy.yml
apiVersion: apps/v1beta2
kind: Deployment
metadata:
name: whoami
spec:
selector:
matchLabels:
type: app
service: whoami
template:
metadata:
labels:
type: app
service: whoami
spec:
containers:
- name: whoami
image: subicura/whoami-redis:1
env:
- name: REDIS_HOST
value: "redis"
- name: REDIS_PORT
value: "6379"kubectl get ep
kubectl exec -it whoami-<xxxxx> sh
apk add curl busybox-extras # install telnet
curl localhost:4567
curl localhost:4567
telnet localhost 6379
telnet redis 6379
dbsize
KEYS *
GET count
quit# whoami-svc.yml
apiVersion: v1
kind: Service
metadata:
name: whoami
spec:
type: NodePort
ports:
- port: 4567
protocol: TCP
selector:
type: app
service: whoamikubectl delete -f ./