lost and found ( for me ? )

Kubernetes : create a pod, service and replication controller

This is to be familiar with Kubernetes, pod, service, replication controller.

[ create pod, service, replication controller ]

Reference
https://kismatic.com/technical/configuring-flanneld-on-ubuntu-for-use-with-kubernetes/

In my environment, 5 CoreOS are running, one for controller, one for etcd, the other three for working nodes.
$ vagrant status
Current machine states:

e1                        running (virtualbox)
c1                        running (virtualbox)
w1                        running (virtualbox)
w2                        running (virtualbox)
w3                        running (virtualbox)

Define a service.
$ cat webserver/webserver-service.yaml
apiVersion: "v1"
kind: "Service"
metadata:
 name: "webserver"
spec:
 selector:
   name: "webserver"
 ports:
 -
   protocol: "TCP"
   port: 8080
   targetPort: 8080
$

Define a corresponding replication controller.
$ cat webserver/webserver-controller.yaml
apiVersion: "v1"
kind: "ReplicationController"
metadata:
 name: "webserver"
spec:
 replicas: 3
 selector:
   name: "webserver"
 template:
   metadata:
     name: "webserver"
     labels:
       name: "webserver"
   spec:
     containers:
     -
       name: "webserver"
       image: ae6rt/webserver
$

Create a service and a replication controller.
$ kubectl create -f webserver/webserver-service.yaml
service "webserver" created

$ kubectl create -f webserver/webserver-controller.yaml
replicationcontroller "webserver" created
$

Get a status
$ kubectl get pod
NAME              READY     STATUS    RESTARTS   AGE
webserver-6mj5p   1/1       Running   0          30s
webserver-gk5rf   1/1       Running   0          30s
webserver-z5rpn   1/1       Running   0          30s

$ kubectl get po -o wide
NAME              READY     STATUS    RESTARTS   AGE       NODE
webserver-6mj5p   1/1       Running   0          1m        172.17.4.201
webserver-gk5rf   1/1       Running   0          1m        172.17.4.202
webserver-z5rpn   1/1       Running   0          1m        172.17.4.203
$


$ kubectl get rc
CONTROLLER   CONTAINER(S)   IMAGE(S)          SELECTOR         REPLICAS   AGE
webserver    webserver      ae6rt/webserver   name=webserver   3          33s

$ kubectl get svc
NAME         CLUSTER_IP   EXTERNAL_IP   PORT(S)    SELECTOR         AGE
kubernetes   10.3.0.1     <none>        443/TCP    <none>           1d
webserver    10.3.0.21    <none>        8080/TCP   name=webserver   46s
$

$ kubectl get ep
NAME         ENDPOINTS                                      AGE
kubernetes   172.17.4.101:443                               1d
webserver    10.2.18.3:8080,10.2.52.3:8080,10.2.90.3:8080   5m
$

Service ip is 10.3.0.21
Three containers are running on working nodes, w1, w2 and w3.
End point IPs are 10.2.18.3, 10.2.52.3, 10.2.90.3

Webserver-6mj5p is running on the node 172.17.4.201.
Endpoint IP is 10.2.18.3
$ kubectl get pod
NAME              READY     STATUS    RESTARTS   AGE
webserver-6mj5p   1/1       Running   0          22m
webserver-gk5rf   1/1       Running   0          22m
webserver-z5rpn   1/1       Running   0          22m

$ kubectl describe pod webserver-6mj5p
Name: webserver-6mj5p
Namespace: default
Image(s): ae6rt/webserver
Node: 172.17.4.201/172.17.4.201
Start Time: Thu, 24 Mar 2016 15:10:22 +0900
Labels: name=webserver
Status: Running
Reason:
Message:
IP: 10.2.18.3
Replication Controllers: webserver (3/3 replicas created)
Containers:
 webserver:
   Container ID: docker://c8e1107d8e065b168a7af26c3bcc73141a732b248f07fb2886c7e086d3e95075
   Image: ae6rt/webserver
   Image ID: docker://sha256:d543631d51a4929931096366d7ac4f4be7241637c0c86c374d93778e5f605c79
   State: Running
     Started: Thu, 24 Mar 2016 15:10:23 +0900
   Ready: True
   Restart Count: 0
   Environment Variables:
Conditions:
 Type Status
 Ready True
Volumes:
 default-token-oexsv:
   Type: Secret (a secret that should populate this volume)
   SecretName: default-token-oexsv
Events:
 FirstSeen LastSeen Count From SubobjectPath Reason Message
 ───────── ──────── ───── ──── ───────────── ────── ───────
 22m 22m 1 {scheduler } Scheduled Successfully assigned webserver-6mj5p to 172.17.4.201
 22m 22m 1 {kubelet 172.17.4.201} implicitly required container POD Pulled Container image "gcr.io/google_containers/pause:0.8.0" already present on machine
 22m 22m 1 {kubelet 172.17.4.201} implicitly required container POD Created Created with docker id 8e023f0a3684
 22m 22m 1 {kubelet 172.17.4.201} implicitly required container POD Started Started with docker id 8e023f0a3684
 22m 22m 1 {kubelet 172.17.4.201} spec.containers{webserver} Pulled Container image "ae6rt/webserver" already present on machine
 22m 22m 1 {kubelet 172.17.4.201} spec.containers{webserver} Created Created with docker id c8e1107d8e06
 22m 22m 1 {kubelet 172.17.4.201} spec.containers{webserver} Started Started with docker id c8e1107d8e06


$

$ kubectl exec -ti webserver-6mj5p ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:0A:02:12:03  
         inet addr:10.2.18.3  Bcast:0.0.0.0  Mask:255.255.255.0
         UP BROADCAST RUNNING MULTICAST  MTU:1450  Metric:1
         RX packets:10 errors:0 dropped:0 overruns:0 frame:0
         TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:0
         RX bytes:816 (816.0 B)  TX bytes:348 (348.0 B)

lo        Link encap:Local Loopback  
         inet addr:127.0.0.1  Mask:255.0.0.0
         UP LOOPBACK RUNNING  MTU:65536  Metric:1
         RX packets:0 errors:0 dropped:0 overruns:0 frame:0
         TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:1
         RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

$

$ kubectl exec -it webserver-gk5rf ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:0A:02:5A:03  
         inet addr:10.2.90.3  Bcast:0.0.0.0  Mask:255.255.255.0
         UP BROADCAST RUNNING MULTICAST  MTU:1450  Metric:1
         RX packets:10 errors:0 dropped:0 overruns:0 frame:0
         TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:0
         RX bytes:816 (816.0 B)  TX bytes:348 (348.0 B)

lo        Link encap:Local Loopback  
         inet addr:127.0.0.1  Mask:255.0.0.0
         UP LOOPBACK RUNNING  MTU:65536  Metric:1
         RX packets:0 errors:0 dropped:0 overruns:0 frame:0
         TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:1
         RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

Pods share the same network namespace, so each container can talk to other containers
$ kubectl get po
NAME              READY     STATUS    RESTARTS   AGE
webserver-6mj5p   1/1       Running   0          30m
webserver-gk5rf   1/1       Running   0          30m
webserver-z5rpn   1/1       Running   0          30m

$ kubectl exec -ti webserver-6mj5p sh
/ #

/ # ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:0A:02:12:03  
         inet addr:10.2.18.3  Bcast:0.0.0.0  Mask:255.255.255.0
         UP BROADCAST RUNNING MULTICAST  MTU:1450  Metric:1
         RX packets:15 errors:0 dropped:0 overruns:0 frame:0
         TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:0
         RX bytes:1138 (1.1 KiB)  TX bytes:628 (628.0 B)

lo        Link encap:Local Loopback  
         inet addr:127.0.0.1  Mask:255.0.0.0
         UP LOOPBACK RUNNING  MTU:65536  Metric:1
         RX packets:0 errors:0 dropped:0 overruns:0 frame:0
         TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:1
         RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

/ # ping 10.2.90.3 -c 3
PING 10.2.90.3 (10.2.90.3): 56 data bytes
64 bytes from 10.2.90.3: seq=0 ttl=62 time=1.308 ms
64 bytes from 10.2.90.3: seq=1 ttl=62 time=1.198 ms
64 bytes from 10.2.90.3: seq=2 ttl=62 time=1.426 ms

--- 10.2.90.3 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 1.198/1.310/1.426 ms
/ # netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         10.2.18.1       0.0.0.0         UG        0 0          0 eth0
10.2.18.0       0.0.0.0         255.255.255.0   U         0 0          0 eth0
/ #
/ #


$ kubectl get po
NAME              READY     STATUS    RESTARTS   AGE
webserver-6mj5p   1/1       Running   0          32m
webserver-gk5rf   1/1       Running   0          32m
webserver-z5rpn   1/1       Running   0          32m

$ kubectl exec -ti webserver-z5rpn sh
/ # ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:0A:02:34:03  
         inet addr:10.2.52.3  Bcast:0.0.0.0  Mask:255.255.255.0
         UP BROADCAST RUNNING MULTICAST  MTU:1450  Metric:1
         RX packets:10 errors:0 dropped:0 overruns:0 frame:0
         TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:0
         RX bytes:816 (816.0 B)  TX bytes:348 (348.0 B)

lo        Link encap:Local Loopback  
         inet addr:127.0.0.1  Mask:255.0.0.0
         UP LOOPBACK RUNNING  MTU:65536  Metric:1
         RX packets:0 errors:0 dropped:0 overruns:0 frame:0
         TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:1
         RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

/ # ping 10.2.90.3 -c 3
PING 10.2.90.3 (10.2.90.3): 56 data bytes
64 bytes from 10.2.90.3: seq=0 ttl=62 time=2.835 ms
64 bytes from 10.2.90.3: seq=1 ttl=62 time=1.941 ms
64 bytes from 10.2.90.3: seq=2 ttl=62 time=1.196 ms

--- 10.2.90.3 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 1.196/1.990/2.835 ms
/ #

Create busybox pods to check if we can access to service “webserver” from busybox pods.
$ cat busybox.yaml
apiVersion: v1
kind: Pod
metadata:
 name: busybox-sleep
spec:
 containers:
 - name: busybox
   image: busybox
   args:
   - sleep
   - "1000000"
---
apiVersion: v1
kind: Pod
metadata:
 name: busybox-sleep-less
spec:
 containers:
 - name: busybox
   image: busybox  

Run busybox pod
$ kubectl create -f busybox.yaml

Access to the busybox pod
$ kubectl exec -it busybox-sleep sh
/ #

Confirm that you can access to service IP from busybox
/ # wget http://10.3.0.21:8080
Connecting to 10.3.0.21:8080 (10.3.0.21:8080)
index.html           100% |*******************************|    19   0:00:00 ETA
/ #

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.