etcd backup을 하여 /opt/etcd-backup.db
에 저장하도록 하자.
kubectl describe -n kube-system pod etcd-controlplane
...
Command:
etcd
--cert-file=/etc/kubernetes/pki/etcd/server.crt
...
--key-file=/etc/kubernetes/pki/etcd/server.key
--listen-client-urls=https://127.0.0.1:2379,https://192.8.79.8:2379
...
--trusted-ca-file=/etc/kubernetes/pki/etcd/ca.crt
...
ETCDCTL_API=3 etcdctl snapshot save /opt/etcd-backup.db \
--endpoints=https://127.0.0.1:2379 \
--cacert=/etc/kubernetes/pki/etcd/ca.crt \
--cert=/etc/kubernetes/pki/etcd/server.crt \
--key=/etc/kubernetes/pki/etcd/server.key
redis:alpine
image를 가진 redis-storage
라는 pod를 생성하고, volume으로는 emptyDir
를 가지고도록 한다.
pod이름은 redis-storage
volume type은 emptyDir
volumnMount는 /data/redis
pod manifest생성
kubectl run redis-storage --image redis:alpine -o yaml --dry-run=client > pod.yaml
vi ./pod.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: redis-storage
name: redis-storage
spec:
containers:
- image: redis:alpine
name: redis-storage
resources: {}
volumeMounts:
- name: redis-storage
mountPath: /data/redis
dnsPolicy: ClusterFirst
restartPolicy: Always
volumes:
- name: redis-storage
emptyDir:
status: {}
kubectl create -f ./pod.yaml
super-user-pod
라는 새로운 pod를 생성하되, image
로 busybox:1.28
을 만들고, pod가 system_time
을 설정할 수 있도록 하자. 단, container는 4800
초 sleep하도록 한다.
super-user-pod
busybox:1.28
SYS_TIME
을 container에 설정system_time
은 kernel관련된 변수로, kernel에 관련된 권한을 주기위해선 securiyContext
에서 설정을 해주어야 한다.
https://kubernetes.io/docs/tasks/configure-pod-container/security-context/
kubectl run super-user-pod --image busybox:1.28 -o yaml --dry-run=client --command -- sleep 4800 > pod2.yaml
vi ./pod2.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: super-user-pod
name: super-user-pod
spec:
containers:
- command:
- sleep
- "4800"
image: busybox:1.28
name: super-user-pod
resources: {}
securityContext:
capabilities:
add: ["SYS_TIME"]
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
kubectl create -f ./pod2.yaml
/root/CKA/use-pv.yaml
pod 정의가 있다. 여기에 pv
로 pv-1
를 마운트하도록 하자. 단, pod의 mountPath는 /data
이다.
PVC가 잘 설정되었는 지 확인하자
정확한 mountPath를 설정하도록 하자.
pod 확인
cat /root/CKA/use-pv.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: use-pv
name: use-pv
spec:
containers:
- image: nginx
name: use-pv
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
kubectl get pv pv-1
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS VOLUMEATTRIBUTESCLASS REASON AGE
pv-1 10Mi RWO Retain Available <unset> 3m19s
kubectl get pvc -A
No resources found
kubectl describe pv pv-1
Name: pv-1
Labels: <none>
Annotations: <none>
Finalizers: [kubernetes.io/pv-protection]
StorageClass:
Status: Available
Claim:
Reclaim Policy: Retain
Access Modes: RWO
VolumeMode: Filesystem
Capacity: 10Mi
Node Affinity: <none>
Message:
Source:
Type: HostPath (bare host directory volume)
Path: /opt/data
HostPathType:
Events: <none>
10Mi
""
ReadWriteOnce
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-claim-1
spec:
accessModes:
- ReadWriteOnce
volumeMode: Filesystem
resources:
requests:
storage: 10Mi
storageClassName: ""
kubectl create -f ./pvc.yaml
kubectl get pv pv-1
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS VOLUMEATTRIBUTESCLASS REASON AGE
pv-1 10Mi RWO Retain Bound default/pvc-claim-1 <unset> 7m3s
vi /root/CKA/use-pv.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: use-pv
name: use-pv
spec:
containers:
- image: nginx
name: use-pv
resources: {}
volumeMounts:
- mountPath: "/data"
name: mypd
dnsPolicy: ClusterFirst
restartPolicy: Always
volumes:
- name: mypd
persistentVolumeClaim:
claimName: pvc-claim-1
status: {}
kubectl create -f /root/CKA/use-pv.yaml
deployment를 만들되 이름은 nginx-deploy
이고 image
로는 nginx:1.16
을 가지고 replica는 1개이다. 이 다음 deployment를 rolling update하여 1.17
버전으로 만들자.
kubectl create deployment nginx-deploy --image=nginx:1.16 --dry-run=client -o yaml > deploy.yaml
kubectl apply -f deploy.yaml --record
주의: --record
를 안넣어주면 history가 안남는다.
kubectl rollout history deployment/nginx-deploy
deployment.apps/nginx-deploy
REVISION CHANGE-CAUSE
1 kubectl apply --filename=deploy.yaml --record=true
vi ./deploy.yaml
... nginx:1.16 -> nginx:1.17로 수정
kubectl apply -f ./deploy.yaml --record
john
이라는 새로운 user를 만들고, john
에게 cluster에 대한 권한을 주도록 하자. create, list, get, update, delete pods
에 대한 권한을 deployment
namespace에 주도록 하자. private key는 /root/CKA/john.key
에 있고, csr은 /root/CKA/john.csr
에 있다.
CertificateSigningRequest
은 클러스터 내부의 인증서 요청을 관리하기 위한 리소스이다. 보통 클라이언트 인증서를 발급받기 위해서 사용된다.
CSR 사용 주요 목적은 다음과 같다.
1. 클러스터 내부의 컴포넌트가 인증서를 필요로 할 때: 사용자, 서비스 계정이 특정 작업 수행시 TLS를 통한 통신이 필요. 이때 인증서를 발급 받기위해 CSR를 생성하여 클러스터의 인증 기관인 CA에게 서명
(모르겠다)
$ kubectl auth can-i update pods --as=john --namespace=development
nginx-resolver
pod를 만들고 nginx
image를 사용하자, 내부적으로 nginx-resolver-service
를 가지도록 하자. 이 다음 busybox:1.28
image를 가지는 test pod를 만들어서 dns lookup resolution 결과를 /root/CKA/nginx.svc
과 /root/CKA/nginx.pod
에 넣자.
kubectl run nginx-resolver --image nginx --port 80
apiVersion: v1
kind: Service
metadata:
name: nginx-resolver-service
spec:
selector:
run: nginx-resolver
ports:
- protocol: TCP
port: 80
targetPort: 9376
또는 다음과 같이 생성 가능
kubectl expose pod nginx-resolver --name=nginx-resolver-service --port=80 --target-port=80 --type=ClusterIP
kubectl create -f ./nginx-service.yaml
kubectl run test --image=busybox:1.28 --command -- sleep 3600
kubectl exec test -- nslookup nginx-resolver-service.default.svc.cluster.local > /root/CKA/nginx.svc
kubectl get po nginx-resolver -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-resolver 1/1 Running 0 14m 10.244.192.4 node01 <none> <none>
10.244.192.4
로 pod DNS확인을 할 수 있다.
kubectl exec test -- nslookup 10-244-192-4.default.pod.cluster.local > /root/CKA/nginx.pod
node01
에 nginx-critical
이라는 pod를 만들되 image는 nginx
를 가지도록 하자.이 다음 재생성, 재시작이 자동으로 가능한지 확인하자. 단, static pod로 배포해야한다.
static pod이므로 /etc/kubernetes/manifests
에 넣도록 하자.
ssh node01
그런데, node01에서 kubectl
시에 에러가 발생, controlplnae에서 pod manifest를 만들고 node01에 넣어주도록 하자.
kubectl run nginx-critical --image nginx -o yaml --dry-run=client > pod.yaml
cat ./pod.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx-critical
name: nginx-critical
spec:
containers:
- image: nginx
name: nginx-critical
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
sftp node01
Connected to node01.
sftp> put ./pod.yaml /etc/kubernetes/manifests
Uploading ./pod.yaml to /etc/kubernetes/manifests/pod.yaml
pod.yaml 100% 259 1.1MB/s 00:00
kubectl get po
NAME READY STATUS RESTARTS AGE
nginx-critical-node01 1/1 Running