본문 바로가기
카테고리 없음

[cka] 시험시 유용한 명령형 커맨드라인/개인적인 정리

by Geunny 2024. 9. 8.
반응형

 

1. dry-run 변수화

controlplane ~ ➜  export do="--dry-run=client -o yaml"

# 사용시
controlplane ~ ➜  k run nginx --image=nginx $do
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: nginx
  name: nginx
spec:
  containers:
  - image: nginx
    name: nginx
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

 

 

2. 파드 expose 하기 (service 명령형으로 생성하기)

#ClusterIP
kubectl expose pod nginx --name=nginx-service --port=80 --target-port=8080 --type=ClusterIP -n <namespace>

#NodePort
kubectl expose pod nginx --name=nginx-service --port=80 --target-port=8080 --type=NodePort -n <namespace>

# NodePort 설정해야 할때
controlplane ~ ➜  kubectl expose pod nginx --name=nginx-service --port=80 --target-port=8080 --type=NodePort  -n <namespace> $do > nginx-service.yaml 
vi nginx-service.yaml

apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    run: nginx
  name: nginx-service
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 8080
    nodePort: 30007 # 추가하기
  selector:
    run: nginx
  type: NodePort
status:
  loadBalancer: {}

 

 

3. busybox 를 이용한 /bin/sh 사용

## /bin/sh 쉘 안에서 명령어 직접 입력할때

controlplane ~ ✖ k run test-po --image=busybox -it --restart=Never --rm -- /bin/sh
#If you don't see a command prompt, try pressing enter.
/ $ 
# 이후 사용할 명령어 사용 ex) nslookup / ping / etc...
/ $ nslookup 10.108.77.204
Server:         10.96.0.10
Address:        10.96.0.10:53

204.77.108.10.in-addr.arpa      name = nginx-service.test.svc.cluster.local


## 쉘 접속 없이 바로 커맨드 라인 사용하기
## 1. pod 를 실행한 후 해당 pod를 이용하여 커맨드 라인 실행하기.
controlplane ~ ✖ k run busybox --image=busybox -- sleep 4000
pod/busybox created

controlplane ~ ➜  k get pods
NAME      READY   STATUS    RESTARTS   AGE
busybox   1/1     Running   0          47s

controlplane ~ ➜  k exec busybox -- nslookup 10.108.77.204
Server:         10.96.0.10
Address:        10.96.0.10:53

204.77.108.10.in-addr.arpa      name = nginx-service.test.svc.cluster.local

## 2. 생성과 동시에 실행후 결과 확인후 삭제하기
k run test-po --image=busybox -it --restart=Never --rm -- nslookup 10.108.77.204
controlplane ~ ➜  k run test-po --image=busybox -it --restart=Never --rm -- nslookup 10.108.77.204
Server:         10.96.0.10
Address:        10.96.0.10:53

204.77.108.10.in-addr.arpa      name = nginx-service.test.svc.cluster.local

pod "test-po" deleted

 

 

4. RBAC 문제 풀이시 유저 생성하기

https://kubernetes.io/docs/reference/access-authn-authz/certificate-signing-requests/#create-certificatessigningrequest

 

Certificates and Certificate Signing Requests

Kubernetes certificate and trust bundle APIs enable automation of X.509 credential provisioning by providing a programmatic interface for clients of the Kubernetes API to request and obtain X.509 certificates from a Certificate Authority (CA). There is als

kubernetes.io

 

4-1. RBAC 생성후 유저 권환 확인하기

controlplane ~ ➜  k auth can-i delete po
yes

controlplane ~ ➜  k auth can-i delete po --namespace test
yes

controlplane ~ ➜  k auth can-i delete po --namespace test --as tester
no

 

더 정리예정..

댓글