Menu

Dipesh Majumdar

Blog and Paintings

Creating a pod and a matching service imperatively

[dipesh.majumdar@demo ~]$ k run nginx --restart=Never --image=nginx --dry-run --port=80  --expose -o yaml
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  name: nginx
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    run: nginx
status:
  loadBalancer: {}
---
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: nginx
  name: nginx
spec:
  containers:
  - image: nginx
    name: nginx
    ports:
    - containerPort: 80
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Never
status: {}

take it in ps.yaml with > ps.yaml and then....

[dipesh.majumdar@demo ~]$ vi ps.yaml
[dipesh.majumdar@demo ~]$ k create -f ps.yaml
service/nginx created
pod/nginx created
[dipesh.majumdar@demo ~]$

so service is created is this 

[dipesh.majumdar@demo ~]$ k get svc nginx
NAME    TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)   AGE
nginx   ClusterIP   10.112.2.47   <none>        80/TCP    12m

 

this cluster ip seems useless to me as i can't connect from my local.

Hey Wait!

But the cluster ip can reached at from another pod

so create a busybox pod and run below from inside the pod....

wget -O- 10.112.2.47:80
Connecting to 10.112.2.47:80 (10.112.2.47:80)
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }

 

Now it's time to expose deployment. first create the deployment - 

[dipesh.majumdar@demo ~]$ cat d.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: simpleapp
  name: simpleapp
spec:
  replicas: 3
  selector:
    matchLabels:
      app: foo
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: foo
    spec:
      containers:
      - image: dgkanatsios/simpleapp
        name: simpleapp
        ports:
          - containerPort: 8080
        resources: {}
status: {}

 

k expose deploy simpleapp --port 6666 --target-port 8080

So the svc simpleapp can connect to 3 pod ips:ports (these are the endpoints) see them with a k describe svc simple command. 

[dipesh.majumdar@demo ~]$ k get svc simpleapp
NAME        TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
simpleapp   ClusterIP   10.112.14.183   <none>        6666/TCP   5m

[dipesh.majumdar@demo ~]$ k exec -it busybox -- wget -O- 10.112.14.183:6666
Connecting to 10.112.14.183:6666 (10.112.14.183:6666)
Hello world from simpleapp-5b6f9c5676-j8szn and version 2.0
-                    100% |*********************************************************************************************************************************************|    60  0:00:00 ETA

 

you can definitely connect to pod-ip from jumbox server busybox as shown below - 

 

[dipesh.majumdar@demo ~]$ k get po -o wide
NAME                         READY   STATUS    RESTARTS   AGE   IP           NODE                                                NOMINATED NODE
busybox                      1/1     Running   0          12m   10.48.0.32   gke-standard-cluster-1-default-pool-c7a16408-wk8x   <none>
simpleapp-5b6f9c5676-bxc5w   1/1     Running   0          20m   10.48.0.30   gke-standard-cluster-1-default-pool-c7a16408-wk8x   <none>
simpleapp-5b6f9c5676-j8szn   1/1     Running   0          20m   10.48.0.31   gke-standard-cluster-1-default-pool-c7a16408-wk8x   <none>
simpleapp-5b6f9c5676-tg9kv   1/1     Running   0          20m   10.48.1.63   gke-standard-cluster-1-default-pool-c7a16408-1bnp   <none>
[dipesh.majumdar@demo ~]$ k exec -it busybox -- wget -O- 10.48.0.30:8080
Connecting to 10.48.0.30:8080 (10.48.0.30:8080)
Hello world from simpleapp-5b6f9c5676-bxc5w and version 2.0
-                    100% |********************************|    60  0:00:00 ETA
[dipesh.majumdar@demo ~]$

 

Go Back

Comment