How to access Kubernetes deployment

1/21/2020

I have created Docker images and deployed in k8s cluster with a minimum number of machines, setup one master and worker and both machines are up and running and talking to each other with the same VLAN network.

Please find the below pod and deployment services with described status

root@jenkins-linux-vm:/home/admin# kubectl describe services angular-service
Name:                     angular-service
Namespace:                pre-release
Labels:                   <none>
Annotations:              kubectl.kubernetes.io/last-applied-configuration:
                            {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"angular-service","namespace":"pre-release"},"spec":{"ports":[{"no...
Selector:                 app=frontend-app
Type:                     NodePort
IP:                       10.96.151.155
Port:                     <unset>  80/TCP
TargetPort:               80/TCP
NodePort:                 <unset>  31000/TCP
Endpoints:                10.32.0.6:80
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

root@jenkins-linux-vm:/home/admin# kubectl get pods
NAME                                  READY   STATUS    RESTARTS   AGE
angular-deployment-7b8d45f48d-b59pv   1/1     Running   0          51m

root@jenkins-linux-vm:/home/admin# kubectl get svc
NAME              TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
angular-service   NodePort   10.96.151.155   <none>        80:31000/TCP   64m


root@jenkins-linux-vm:/home/admin# kubectl get pods --selector="app=frontend-app" --output=wide
NAME                                  READY   STATUS    RESTARTS   AGE   IP          NODE               NOMINATED NODE   READINESS GATES
angular-deployment-7b8d45f48d-b59pv   1/1     Running   0          52m   10.32.0.6   poc-worker2   <none>           <none>

root@jenkins-linux-vm:/home/admin# kubectl describe pods angular-deployment-7b8d45f48d-b59pv
Name:         angular-deployment-7b8d45f48d-b59pv
Namespace:    pre-release
Priority:     0
Node:         poc-worker2/10.0.0.6
Start Time:   Tue, 21 Jan 2020 05:15:49 +0000
Labels:       app=frontend-app
              pod-template-hash=7b8d45f48d
Annotations:  <none>
Status:       Running
IP:           10.32.0.6
IPs:
  IP:           10.32.0.6
Controlled By:  ReplicaSet/angular-deployment-7b8d45f48d
Containers:
  frontend-app:
    Container ID:   docker://751a9fb4a5e908fa1a02eb0460ab1659904362a727a028fdf72489df663a4f69
    Image:          frontend-app:future-master-fix-d1afa608
    Image ID:       docker://sha256:0099587db89de9ef999a7d1f087d4781e73c491b17e89392e92b08d2f935ad27
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Tue, 21 Jan 2020 05:15:54 +0000
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-r67p7 (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-r67p7:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-r67p7
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:          <none>

Now the problem is I'm not able to access my application using a port, even though its not working in a web browser as well.

curl http://<public-node-ip>:<node-port>

curl http://10.0.0.6:31000

Dockr file

FROM node:latest as node
WORKDIR /app
COPY . .
RUN npm install
RUN npm run build --prod

# stage 2
FROM nginx:alpine
COPY --from=node /app/dist/hello-angular /usr/share/nginx/html
root@jenkins-linux-vm:/home/admin# kubectl exec -it angular-deployment-7b8d45f48d-b59pv curl 10.96.151.155:80
curl: (7) Failed to connect to 10.96.151.155 port 80: Connection refused
command terminated with exit code 7
root@jenkins-linux-vm:/home/admin/kubernetes# kubectl run busybox --image=busybox --restart=Never -it --rm --command -- /bin/sh -c "wget 10.96.208.252:80;cat index.html"
Connecting to 10.96.208.252:80 (10.96.208.252:80)
saving to 'index.html'
index.html           100% |********************************|   593  0:00:00 ETA
'index.html' saved
<!doctype html><html lang="en"><head><meta charset="utf-8"><title>AngularApp</title><base href="/"><meta name="viewport" content="width=device-width,initial-scale=1"><link rel="icon" type="image/x-icon" href="favicon.ico"><link href="styles.9c0ad738f18adc3d19ed.bundle.css" rel="stylesheet"/></head><body><app-root></app-root><script type="text/javascript" src="inline.720eace06148cc3e71aa.bundle.js"></script><script type="text/javascript" src="polyfills.f20484b2fa4642e0dca8.bundle.js"></script><script type="text/javascript" src="main.11bc84b3b98cd0d00106.bundle.js"></script></body></html>pod "busybox" deleted
root@jenkins-linux-vm:/home/admin/kubernetes# kubectl run busybox --image=busybox --restart=Never -it --rm --command -- /bin/sh -c "wget 10.0.0.6:32331;cat index.html"
Connecting to 10.0.0.6:32331 (10.0.0.6:32331)
wget: can't connect to remote host (10.0.0.6): Connection refused
cat: can't open 'index.html': No such file or directory
pod "busybox" deleted
pod pre-release/busybox terminated (Error)
-- Anonymuss
kubernetes

4 Answers

1/21/2020

The service is defined as NodePort type.

it is using nodeport: 31000

Try hitting the below url in your browser

http://HOSTNAME:31000

hostname could be any hostname of the cluster nodes

-- P Ekambaram
Source: StackOverflow

1/21/2020

I am taking a pre-built angular image from docker hub with thanks to https://github.com/nheidloff/web-apps-kubernetes/tree/master/angular-app we will use this image as baseline below.

Create and deployment and service using below yamls

Deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: angular-app
spec:
  replicas: 1
  selector:
    matchLabels:
      run: angular-app
  template:
    metadata:
      labels:
        run: angular-app
    spec:
      containers:
      - name: angular-app
        image: nheidloff/angular-app
        ports:
        - containerPort: 80
        - containerPort: 443

Service.yaml

apiVersion: v1
kind: Service
metadata:
  name: angular-app
  labels:
    run: angular-app
spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: 80
    protocol: TCP
    name: http
  - port: 443
    protocol: TCP
    name: https
  selector:
    run: angular-app

Run as below on your cluster to create the resources

$ kubectl create -f Deployment.yaml
$ kubectl create -f Service.yaml

Should result in below deployment and service configuration

    $ kubectl get all -o wide
NAME                               READY   STATUS    RESTARTS   AGE     IP            NODE         NOMINATED NODE   READINESS GATES

pod/angular-app-694d97d56c-7m4x4   1/1     Running   0          8m23s   10.244.3.10   k8s-node-3   <none>           <none>

NAME                  TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGE     SELECTOR
service/angular-app   NodePort    10.96.150.136   <none>        80:32218/TCP,443:30740/TCP   8m23s   run=angular-app
service/kubernetes    ClusterIP   10.96.0.1       <none>        443/TCP                      8d      <none>

NAME                          READY   UP-TO-DATE   AVAILABLE   AGE     CONTAINERS    IMAGES                  SELECTOR
deployment.apps/angular-app   1/1     1            1           8m23s   angular-app   nheidloff/angular-app   run=angular-app

NAME                                     DESIRED   CURRENT   READY   AGE     CONTAINERS    IMAGES                  SELECTOR
replicaset.apps/angular-app-694d97d56c   1         1         1       8m23s   angular-app   nheidloff/angular-app   pod-template-hash=694d97d56c,run=angular-app

From above we can see the pod is running node-3 , so identify the ip of node 3 and we see that service has exposed below ports 32218/TCP and 30740/TCP

$ kubectl get nodes -o wide
NAME           STATUS   ROLES    AGE   VERSION   INTERNAL-IP       EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION      CONTAINER-RUNTIME
master-1   Ready    master   8d    v1.17.0   111.112.113.107   <none>        Ubuntu 16.04.6 LTS   4.4.0-169-generic   docker://18.6.2
node-1     Ready    <none>   8d    v1.17.0   111.112.113.108   <none>        Ubuntu 16.04.6 LTS   4.4.0-169-generic   docker://18.6.2
node-2     Ready    <none>   8d    v1.17.0   111.112.113.109   <none>        Ubuntu 16.04.6 LTS   4.4.0-169-generic   docker://18.6.2
node-3     Ready    <none>   8d    v1.17.0   111.112.113.110   <none>        Ubuntu 16.04.6 LTS   4.4.0-169-generic   docker://18.6.2

So we need to access the app vi node3:NodePort i.e 111.112.113.110:32218 as url check below screen shot as well on how i access the app.

enter image description here

I have below rules open on cluster level to allow browser access the apps on default NodePort range.

NOTE : Ingress IPv4 TCP 30000 - 32767 0.0.0.0/0

-- DT.
Source: StackOverflow

1/21/2020

To ensure you are able to open your app by nodeport in browser you should try to establish that

There are no rules blocking the default node-port range (i.e from port 30000 - to port 32767) on security rules or firewall on cluster network.

For example verify you have below security rule open on Cluster Network for nodeport range to work in browser.

Ingress IPv4    TCP 30000 - 32767   0.0.0.0/0

Once you have confirmed you have no security group rule issue. I will take below approach to debug and find whats wrong with port reachablity at node level. perform a basic Test and check if i can get nginx web server installed and reachable on browser via node port:

Steps:

Deploy a NGINX deployment using below nginx.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-nginx
spec:
  selector:
    matchLabels:
      run: my-nginx
  replicas: 1
  template:
    metadata:
      labels:
        run: my-nginx
    spec:
      containers:
      - name: my-nginx
        image: nginx
        ports:
        - containerPort: 80

Verify deployment is up and running

$ kubectl apply -f nginx.yaml

$ kubectl get all
NAME                            READY   STATUS        RESTARTS   AGE
pod/my-nginx-75897978cd-ptqv9   1/1     Running       0          32s

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   4d11h

NAME                       READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/my-nginx   1/1     1            1           33s

NAME                                  DESIRED   CURRENT   READY   AGE
replicaset.apps/my-nginx-75897978cd   1         1         1       33s

Now create service to expose the nginx deployment using below example

apiVersion: v1
kind: Service
metadata:
  name: my-nginx
  labels:
    run: my-nginx
spec:
  type: NodePort
  ports:
  - port: 8080
    targetPort: 80
    protocol: TCP
    name: http
  selector:
    run: my-nginx

Verify service is created and identify the nodeport assigned (since we did not provide any fixed port in service.yaml ( like below the node port is 32502)

$ kubectl apply -f service.yaml

$ kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP          4d11h
my-nginx     NodePort    10.96.174.234   <none>        8080:32502/TCP   12s

In addition to the nodeport identify the ip of your master node i.e 131.112.113.101 below

$ kubectl get nodes -o wide
NAME           STATUS   ROLES    AGE     VERSION   INTERNAL-IP       EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION      CONTAINER-RUNTIME
master-1   Ready    master   4d11h   v1.17.0   131.112.113.101   <none>        Ubuntu 16.04.6 LTS   4.4.0-169-generic   docker://18.6.2
node-1     Ready    <none>   4d11h   v1.17.0   131.112.113.102   <none>        Ubuntu 16.04.6 LTS   4.4.0-169-generic   docker://18.6.2
node-2     Ready    <none>   4d11h   v1.17.0   131.112.113.103   <none>        Ubuntu 16.04.6 LTS   4.4.0-169-generic   docker://18.6.2

Now if you try to access the nginx application using the IP of your masternode with nodeport value like <masternode>:<nodeport> (i.e. 131.112.113.101:32502) in your browser you should get result similar to below

enter image description here

Note the container port used on nginx.yaml and targetPort on service.yaml (i.e. 80) you should be able to figure out this for your app better. Hope this will help you understand the issue at your node/cluster level if any.

-- DT.
Source: StackOverflow

1/21/2020

I am not sure if I understood what you are trying to do.

Below command is to open a bash shell in the pod:
kubectl exec -it angular-deployment-7b8d45f48d-b59pv -- /bin/bash

You can connect to a pod, then try curl.

-- ffran09
Source: StackOverflow