유용한 쿠버네티스 명령어를 알아보자~
실습 환경
Amazon EKS환경에서 실습한다.
비용 절감을 위해 node는 1개로 줄여 생성하자 = 콘솔에서 node수를 1개로 조정하자.
참고
https://brunch.co.kr/@topasvga/3572
kubectl scale deploy webserver --replicas=6
# 디플로이 다음 디플로이먼트 이름이 나와야 한다.
실습
1
터미널1
k ns default
watch -d kubectl get deploy,rs,po
터미널2
디플로이먼트 만들자.
cat <<EOT > eks-sample-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: eks-sample-linux-deployment
namespace: eks-sample-app
labels:
app: eks-sample-linux-app
spec:
replicas: 3
selector:
matchLabels:
app: eks-sample-linux-app
template:
metadata:
labels:
app: eks-sample-linux-app
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/arch
operator: In
values:
- amd64
- arm64
containers:
- name: nginx
image: public.ecr.aws/nginx/nginx:1.23
ports:
- name: http
containerPort: 80
imagePullPolicy: IfNotPresent
nodeSelector:
kubernetes.io/os: linux
EOT
kubectl apply -f eks-sample-deployment.yaml
2
k get deploy,po
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/eks-sample-linux-deployment 2/2 2 2 15m
NAME READY STATUS RESTARTS AGE
pod/eks-sample-linux-deployment-66cfbc47b7-4g9xw 1/1 Running 0 15m
pod/eks-sample-linux-deployment-66cfbc47b7-rp7cg 1/1 Running 0 15m
kubectl scale deploy eks-sample-linux-deployment --replicas=6
# 디플로이 다음 디플로이먼트 이름이 나와야 한다.
k get deploy,po
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/eks-sample-linux-deployment 6/6 6 6 15m
NAME READY STATUS RESTARTS AGE
pod/eks-sample-linux-deployment-66cfbc47b7-4g9xw 1/1 Running 0 15m
pod/eks-sample-linux-deployment-66cfbc47b7-5fqqv 1/1 Running 0 5s
pod/eks-sample-linux-deployment-66cfbc47b7-9stlj 1/1 Running 0 5s
pod/eks-sample-linux-deployment-66cfbc47b7-fzw4p 1/1 Running 0 5s
pod/eks-sample-linux-deployment-66cfbc47b7-rp7cg 1/1 Running 0 15m
pod/eks-sample-linux-deployment-66cfbc47b7-x7phw 1/1 Running 0 5s
또는
kubectl scale deployment.apps/eks-sample-linux-deployment --replicas=6
3
k ns
default
eks-sample-app
kube-node-lease
kube-public
kube-system
k ns eks-sample-app
Context "12-24-user@myeks.ap-northeast-2.eksctl.io" modified.
Active namespace is "eks-sample-app".
참고
https://brunch.co.kr/@topasvga/1652
k get pods -A
k get pods -A > pod-list.yaml
more pod-list.yaml
또는
kubectl get pods --all-namespaces > pod-list.yaml
k get pods -n eks-sample-app -o custom-columns=":metadata.name"
eks-sample-linux-deployment-66cfbc47b7-4g9xw
eks-sample-linux-deployment-66cfbc47b7-5fqqv
eks-sample-linux-deployment-66cfbc47b7-9stlj
eks-sample-linux-deployment-66cfbc47b7-fzw4p
eks-sample-linux-deployment-66cfbc47b7-rp7cg
eks-sample-linux-deployment-66cfbc47b7-x7phw
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
k top pods
# 단일 컨테이너로 nginx2 pod 하나 생성해보자.
# pod안에 3개의 컨테이너가 있도록 생성해 보자.
1
# 단일 컨테이너로 nginx2 pod 하나 생성해보자.
k run nginx --image=nginx --dry-run=client -o yaml > nginx.yaml
kubectl apply -f nginx.yaml
pod/nginx created
k get pods
NAME READY STATUS RESTARTS AGE
eks-sample-linux-deployment-66cfbc47b7-4g9xw 1/1 Running 0 132m
eks-sample-linux-deployment-66cfbc47b7-5fqqv 1/1 Running 0 116m
eks-sample-linux-deployment-66cfbc47b7-9stlj 1/1 Running 0 116m
eks-sample-linux-deployment-66cfbc47b7-fzw4p 1/1 Running 0 116m
eks-sample-linux-deployment-66cfbc47b7-rp7cg 1/1 Running 0 132m
eks-sample-linux-deployment-66cfbc47b7-x7phw 1/1 Running 0 116m
nginx 1/1 Running 0 4s
2
# pod안에 3개의 컨테이너가 있도록 생성해 보자.
vi nginx.yaml
more nginx.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx2
spec:
containers:
- image: nginx
name: nginx
- image: redis
name: redis
- image: memcached
name: memcached
kubectl apply -f nginx.yaml
pod/nginx2 created
k get pods
NAME READY STATUS RESTARTS AGE
eks-sample-linux-deployment-66cfbc47b7-4g9xw 1/1 Running 0 133m
eks-sample-linux-deployment-66cfbc47b7-5fqqv 1/1 Running 0 117m
eks-sample-linux-deployment-66cfbc47b7-9stlj 1/1 Running 0 117m
eks-sample-linux-deployment-66cfbc47b7-fzw4p 1/1 Running 0 117m
eks-sample-linux-deployment-66cfbc47b7-rp7cg 1/1 Running 0 133m
eks-sample-linux-deployment-66cfbc47b7-x7phw 1/1 Running 0 117m
nginx 1/1 Running 0 59s
nginx2 0/3 ContainerCreating 0 3s
k get pods
NAME READY STATUS RESTARTS AGE
eks-sample-linux-deployment-66cfbc47b7-4g9xw 1/1 Running 0 134m
eks-sample-linux-deployment-66cfbc47b7-5fqqv 1/1 Running 0 118m
eks-sample-linux-deployment-66cfbc47b7-9stlj 1/1 Running 0 118m
eks-sample-linux-deployment-66cfbc47b7-fzw4p 1/1 Running 0 118m
eks-sample-linux-deployment-66cfbc47b7-rp7cg 1/1 Running 0 134m
eks-sample-linux-deployment-66cfbc47b7-x7phw 1/1 Running 0 118m
nginx 1/1 Running 0 2m10s
nginx2 3/3 Running 0 74s
1
k create ns myweb
namespace/myweb created
2
k run mongo --image=mongo -n myweb
pod/mongo created
3
k get pods -n myweb
NAME READY STATUS RESTARTS AGE
mongo 1/1 Running 0 18s
# 추가 유틸 설치후 사용
4
k ns
default
eks-sample-app
kube-node-lease
kube-public
kube-system
myweb
k ns myweb
Context "12-24-user@myeks.ap-northeast-2.eksctl.io" modified.
Active namespace is "myweb".
myweb) [root@myeks-bastion-EC2 ~]# k get pods
NAME READY STATUS RESTARTS AGE
mongo 1/1 Running 0 3m37s
kubectl run busybox --image=busybox --restart=Never -- /bin/sh -c "sleep 1"
pod/busybox created
k get pods
NAME READY STATUS RESTARTS AGE
busybox 0/1 Completed 0 22s
mongo 1/1 Running 0 18m
Pod가 삭제 되더라고 데이터가 보관 되도록 영구 스토리지를 사용하는 것이다.
k get pvc,pv
설정법
https://brunch.co.kr/@topasvga/3486
pod이름 a ,b ,c 순으로 정렬해서 보는 명령어는?
--sort-by=.metadata.name
k get pods --sort-by=.metadata.name -A
NAMESPACE NAME READY STATUS RESTARTS AGE
myweb app 0/1 Pending 0 10m
kube-system aws-node-6qqlh 2/2 Running 0 3h38m
kube-system aws-node-bzrgc 2/2 Running 0 3h38m
kube-system aws-node-nbwkq 2/2 Running 0 3h38m
myweb busybox 0/1 Completed 0 17m
kube-system coredns-7484ff4b98-lxsww 1/1 Running 0 3h36m
kube-system coredns-7484ff4b98-vxvt6 1/1 Running 0 3h36m
kube-system ebs-csi-controller-699448cdd-hlf76 6/6 Running 0 3h34m
kube-system ebs-csi-controller-699448cdd-zswgn 6/6 Running 0 3h34m
kube-system ebs-csi-node-6wvbn 3/3 Running 0 3h34m
kube-system ebs-csi-node-85n57 3/3 Running 0 3h34m
kube-system ebs-csi-node-xpw7d 3/3 Running 0 3h34m
kube-system efs-csi-controller-54d4ddf454-ffqcw 3/3 Running 0 3h33m
kube-system efs-csi-controller-54d4ddf454-gbzqh 3/3 Running 0 3h33m
kube-system efs-csi-node-9fgrg 3/3 Running 0 3h33m
kube-system efs-csi-node-56rrs 3/3 Running 0 3h33m
kube-system efs-csi-node-n9zjk 3/3 Running 0 3h33m
eks-sample-app eks-sample-linux-deployment-66cfbc47b7-dm97n 1/1 Running 0 3m37s
eks-sample-app eks-sample-linux-deployment-66cfbc47b7-klp7f 1/1 Running 0 3m37s
eks-sample-app eks-sample-linux-deployment-66cfbc47b7-pkdzp 1/1 Running 0 3m37s
kube-system kube-proxy-qwmx7 1/1 Running 0 3h37m
kube-system kube-proxy-s8v88 1/1 Running 0 3h37m
kube-system kube-proxy-vswvg 1/1 Running 0 3h37m
kube-system metrics-server-fbb469ccc-n5rhr 1/1 Running 0 84m
myweb mongo 1/1 Running 0 35m
eks-sample-app nginx 1/1 Running 0 56m
eks-sample-app nginx2 3/3 Running 0 55m
# nginx 이미지
# nginx-web01 웹서버를 만들자
k run nginx --image=nginx --dry-run=client -o yaml > nginx.yaml
vi nginx.yaml
kubectl apply -f nginx.yaml
k get pods
vi nginx.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx
name: nginx-web01
spec:
containers:
- image: nginx
name: nginx
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
kubectl apply -f nginx.yaml
pod/nginx-web01 created
k get pods
NAME READY STATUS RESTARTS AGE
eks-sample-linux-deployment-66cfbc47b7-dm97n 1/1 Running 0 17m
eks-sample-linux-deployment-66cfbc47b7-klp7f 1/1 Running 0 17m
eks-sample-linux-deployment-66cfbc47b7-pkdzp 1/1 Running 0 17m
nginx-web01 1/1 Running 0 10s
1
k run nginx --image=nginx --dry-run=client -o yaml > nginx.yaml
2
vi nginx.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx
name: nginx-web01-ssd
spec:
containers:
- image: nginx
name: nginx
imagePullPolicy: IfNotPresent
nodeSelector:
disk: ssd
2
kubectl apply -f nginx.yaml
pod/nginx-web01-ssd created
3
k get pods
NAME READY STATUS RESTARTS AGE
eks-sample-linux-deployment-66cfbc47b7-dm97n 1/1 Running 0 25m
eks-sample-linux-deployment-66cfbc47b7-klp7f 1/1 Running 0 25m
eks-sample-linux-deployment-66cfbc47b7-pkdzp 1/1 Running 0 25m
nginx-web01 1/1 Running 0 7m59s
nginx-web01-ssd 0/1 Pending 0 24s
kubectl run nginx123 --image=nginx:1.23 --restart=Never --port=80
kubectl run nginxlast --image=nginx:latest --restart=Never --port=80
1
cat << EOT > pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: efs-pv
spec:
capacity:
storage: 5Gi
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
storageClassName: ""
persistentVolumeReclaimPolicy: Retain
csi:
driver: efs.csi.aws.com
volumeHandle: fs-073d77123471b2917
EOT
kubectl apply -f pv.yaml
kubectl get pv efs-pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
efs-pv 5Gi RWO Retain Available 2s
참고 사이트
https://aws.amazon.com/ko/blogs/tech/persistent-storage-for-kubernetes/
kubectl run redis --image=redis --restart=Never --port=6379 --dry-run=client -o yaml > re.yaml
kubectl apply -f re.yaml
k get pods
more re.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: redis
name: redis
spec:
containers:
- image: redis
name: redis
ports:
- containerPort: 6379
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}
alias k='kubectl'
k get pods
NAME READY STATUS RESTARTS AGE
redis 1/1 Running 0 20m
k logs redis
1:C 26 Dec 2023 01:01:28.115 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 26 Dec 2023 01:01:28.115 * Redis version=7.2.3, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 26 Dec 2023 01:01:28.115 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 26 Dec 2023 01:01:28.115 * monotonic clock: POSIX clock_gettime
1:M 26 Dec 2023 01:01:28.116 * Running mode=standalone, port=6379.
1:M 26 Dec 2023 01:01:28.117 * Server initialized
1:M 26 Dec 2023 01:01:28.117 * Ready to accept connections tcp
k logs redis |grep Running
1:M 26 Dec 2023 01:01:28.116 * Running mode=standalone, port=6379.
k get pods
NAME READY STATUS RESTARTS AGE
redis 1/1 Running 0 26m
k describe pod redis
Name: redis
Namespace: default
Priority: 0
Service Account: default
Node: ip-192-168-1-11.ap-northeast-2.compute.internal/192.168.1.11
Start Time: Tue, 26 Dec 2023 10:01:25 +0900
Labels: run=redis
Annotations: <none>
Status: Running
IP: 192.168.1.187
IPs:
IP: 192.168.1.187
Containers:
redis:
Container ID:
# top 명령어는 메트릭 pod를 배포해야함!!!
k top po -l run=redis
NAME CPU(cores) MEMORY(bytes)
redis 2m 2Mi
k top po -l run=redis > pod-cpu.txt
more pod-cpu.txt
NAME CPU(cores) MEMORY(bytes)
redis 2m 2Mi
cat << EOT > pv-path1.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: task-pv-volume
labels:
type: local
spec:
storageClassName: manual
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/mnt/data"
EOT
kubectl apply -f pv-path1.yaml
kubectl get pv task-pv-volume
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
task-pv-volume 10Gi RWO Retain Available manual 2s
# 참고 사이트
https://kubernetes.io/docs/tasks/configure-pod-container/configure-persistent-volume-storage/
kubectl run busybox --image=busybox --restart=Never --dry-run=client -o yaml -- /bin/sh -c "sleep 10" > busybox2.yaml
more busybox2.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: busybox
name: busybox
spec:
containers:
- args:
- /bin/sh
- -c
- sleep 10
image: busybox
name: busybox
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}
kubectl apply -f busybox2.yaml
pod/busybox created
k get pods
NAME READY STATUS RESTARTS AGE
busybox 1/1 Running 0 4s
redis 1/1 Running 0 72m
# db-user-pass 이름으로 스크릿 만들기
kubectl create secret generic db-user-pass --from-literal=username=admin --from-literal=password='S!B\*d$zDsb='
secret/db-user-pass created
kubectl get secrets
NAME TYPE DATA AGE
db-user-pass Opaque 2 30s
참고 사이트
https://kubernetes.io/docs/tasks/configmap-secret/managing-secret-using-kubectl/
https://brunch.co.kr/@topasvga/3303
1
kubectl create deployment nginx-project --image=nginx --dry-run=client -o yaml --port=80 > nginx-deploy.yaml
more nginx-deploy.yaml
kubectl apply -f nginx-deploy.yaml
2
kubectl expose deployment nginx-project --port=80 --type=NodePort
kubectl expose deployment nginx-project --port=80 --type=NodePort --dry-run=client -o yaml > nodeport.yaml
more nodeport.yaml
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
app: nginx-project
name: nginx-project
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: nginx-project
type: NodePort
status:
loadBalancer: {}
k get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.100.0.1 <none> 443/TCP 19h
nginx-project NodePort 10.100.138.121 <none> 80:30314/TCP 4m31s
or
kubectl expose deployment nginx-project --port=80 --type=LoadBalancer
https://brunch.co.kr/@topasvga/3580
같이보면 좋은 자료
https://brunch.co.kr/@topasvga/1656
감사합니다.