Kubernetes Command/args 활용


시나리오

1. httpd 이미지로 컨터이너를 올리고 curl로 접속시 ip를 보여줄 수 있도록 index.html에 넣기

2. 잘 되면 윈도우에서도 접속 가능하도록 service까지 넣어주기

 

1. httpd 올리기

[vagrant@master work]$ cat z.yaml
---
apiVersion: v1
kind: Pod
metadata:
  name: test
  labels:
    app: apache
spec:
  containers:
  - name: apache-container
    image: httpd
    ports:
      - containerPort: 80
    command: ["/bin/sh", "-c"]
    args:
    - |
      apt-get update
      apt-get install net-tools
      apt-get install apache2 -y
      service apache2 restart
      ifconfig |grep inet > /var/www/html/index.html
      service apache2 restart
      sleep 3600

 

뭔가 args 부분이 이상할 것이다.

이미지를 httpd로 올렸는데 왜 apache2를 또 설치하는 것인지?

apache2를 설치 안하고 

index.html도, /usr/local/apache2/htdocs/index.html로 쓰면 되는 것이 아닌가?

 

안되더라...

command / args를 안쓰면 curl로 편하게 접속이 가능한데

command를 쓰는 순간 apache2가 인식이 안되어버린다. 왜지... 왜일까... 왜... 

해결 방법을 찾아보려 했는데 도저히 모르겠어서 어거지로 해결했다

 

httpd 이미지로 올리고, 우분투 처럼 사용해서(....)

apache2 설치하고 (apache2 index.html경로는 /var/www/html/index.html)

ifconfig 명령어를 사용하기 위해 net-tools도 설치했다.

마지막 sleep 3600은 안쓰면 자꾸 completed가 되면서 컨테이너가 중지되기에 sleep 걸어놓음

 

 

결과

 

주소를 확인하고

[vagrant@master work]$ kubectl get pods -o wide
NAME   READY   STATUS    RESTARTS   AGE   IP            NODE      NOMINATED NODE   READINESS GATES
test   1/1     Running   0          10s   10.244.2.80   worker2   <none>           <none>

 

curl로 접속하면 제대로 된 것을 확인할 수 있다.

(바로 되는 것은 아니고, 커맨드 들이 순차적으로 실행되기에 시간이 좀 필요)

[vagrant@master work]$ curl 10.244.2.80
        inet 10.244.2.80  netmask 255.255.255.0  broadcast 10.244.2.255
        inet6 fe80::4cf2:e1ff:fe4b:ed2b  prefixlen 64  scopeid 0x20<link>
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>

 

추가

image를 ubuntu로 하는 것도 똑같겠지

[vagrant@master work]$ cat z.yaml
---
apiVersion: v1
kind: Pod
metadata:
  name: test
  labels:
    app: apache
spec:
  containers:
  - name: apache-container
    image: ubuntu
    ports:
      - containerPort: 80
    command: ["/bin/sh", "-c"]
    args:
    - |
      apt-get update
      apt-get install net-tools
      apt-get install apache2 -y
      ifconfig |grep inet > /var/www/html/index.html
      service apache2 restart
      sleep 3600

 

올라온거 확인한뒤

[vagrant@master work]$ kubectl get pods -o wide
NAME   READY   STATUS    RESTARTS   AGE   IP            NODE      NOMINATED NODE   READINESS GATES
test   1/1     Running   0          19s   10.244.2.92   worker2   <none>           <none>
x      1/1     Running   0          24m   10.244.2.88   worker2   <none>           <none>

 

한 1분정도 기다렸다가 apache2의 status가 running 상태인것을 ad-hoc으로 확인한 뒤에

[vagrant@master work]$ kubectl exec -it test -- /bin/bash -c "service apache2 status"
 * apache2 is running

 

curl로 접속해보면 된다.

[vagrant@master work]$ curl 10.244.2.92
        inet 10.244.2.92  netmask 255.255.255.0  broadcast 10.244.2.255
        inet6 fe80::b80b:acff:fea9:c54c  prefixlen 64  scopeid 0x20<link>
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>

 

2. 윈도우에서 접속 가능하도록 (multi-playbook 활용)

ubuntu에 apach2를 설치하고 하겠다.

노드포트를 활용해서 윈도우에서 접속이 가능하도록 만듦

[vagrant@master work]$ cat z.yaml
---
apiVersion: v1
kind: Pod
metadata:
  name: test
  labels:
    app: apache
spec:
  containers:
  - name: apache-container
    image: ubuntu
    ports:
      - containerPort: 80
    command: ["/bin/sh", "-c"]
    args:
    - |
      apt-get update
      apt-get install net-tools
      apt-get install apache2 -y
      ifconfig |grep inet > /var/www/html/index.html
      service apache2 restart
      sleep 3600
...
---
apiVersion: v1
kind: Service
metadata:
  name: apache-nodeport
spec:
  ports:
  - port: 8004
    targetPort: 80
  selector:
    app: apache
  type: NodePort
...

 

파드와 서비스가 잘 적용되었는지 확인하고

[vagrant@master work]$ kubectl get pods -o wide
NAME   READY   STATUS    RESTARTS   AGE   IP            NODE      NOMINATED NODE   READINESS GATES
test   1/1     Running   0          35s   10.244.2.93   worker2   <none>           <none>
x      1/1     Running   0          31m   10.244.2.88   worker2   <none>           <none>
[vagrant@master work]$ kubectl get services
NAME              TYPE       CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
apache-nodeport   NodePort   10.111.57.113    <none>        8004:30628/TCP   41s
myweb-service     NodePort   10.100.183.106   <none>        8001:32041/TCP   4d19h

 

서비스의 세부사항 확인하고

[vagrant@master work]$ kubectl describe services apache-nodeport
Name:                     apache-nodeport
Namespace:                myns
Labels:                   <none>
Annotations:              <none>
Selector:                 app=apache
Type:                     NodePort
IP Family Policy:         SingleStack
IP Families:              IPv4
IP:                       10.111.57.113
IPs:                      10.111.57.113
Port:                     <unset>  8004/TCP
TargetPort:               80/TCP
NodePort:                 <unset>  30628/TCP
Endpoints:                10.244.2.88:80,10.244.2.93:80
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

 

파드의 apache2가 running 상태인지 확인한 다음

[vagrant@master work]$ kubectl exec -it test -- /bin/bash -c "service apache2 status"
 * apache2 is running

 

접속 확인

[vagrant@master work]$ curl 10.244.2.93
        inet 10.244.2.93  netmask 255.255.255.0  broadcast 10.244.2.255
        inet6 fe80::688d:a4ff:fec2:60fb  prefixlen 64  scopeid 0x20<link>
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>

[vagrant@master work]$ curl 192.168.98.10:30628
        inet 10.244.2.93  netmask 255.255.255.0  broadcast 10.244.2.255
        inet6 fe80::688d:a4ff:fec2:60fb  prefixlen 64  scopeid 0x20<link>
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>

 

끝.

추가

index.html에 한줄 추가하고 싶은데 컨테이너로 들어가기 귀찮다면 ad-hoc을 활용해보자

[vagrant@master work]$ kubectl exec -it test -- /bin/bash -c "echo '<hr><h1>hello</h1>' >> /var/www/html/index.html"

 

<hr><h1>hello</h1>이 추가됨

[vagrant@master work]$ curl 192.168.98.10:30628
        inet 10.244.2.93  netmask 255.255.255.0  broadcast 10.244.2.255
        inet6 fe80::688d:a4ff:fec2:60fb  prefixlen 64  scopeid 0x20<link>
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
<hr><h1>hello</h1>

 

추가 완료

 

+ Recent posts