Skip to content

Make a deployment accessible using a public URL #13

@sbose78

Description

@sbose78

Depends on #11

What

As a user, I wish to access my application deployed in a container/pod/Deployment using a URL.

How

Improve the kodo deploy command to create the following behind the scenes:

  1. A corresponding Service ( makes the app accessible from inside the cluster.. )
  2. A corresponding Route to the Service ( makes the app accessible from the internet )

Hint

Creating a Service

This is a yaml representation of a Service:

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: should-be-same-as-that-in-Deployment
  ports:
    - protocol: TCP 
      port: 80  # incoming traffic, will be directed to 'targetPort'
      targetPort: 8081 # port the app is listening on, in the pod, obtain from `--port=`

You could create the same by doing the following:

// use the following imports
// "k8s.io/api/core/v1"
// "k8s.io/apimachinery/pkg/util/intstr"
// metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"


...
...

svc := &v1.Service{
		ObjectMeta: metav1.ObjectMeta{
			Name:      "unique-service-name",
		},
		Spec: v1.ServiceSpec{
			Ports: []v1.ServicePort{
				{
					Port:       80, // use correct datatype, hint: int32
					Protocol:   v1.ProtocolTCP,
					TargetPort: intstr.FromInt(int(port)), // port is to be obtained from the command flag.
				},
			},
			Selector: map[string]string{
				"app": "should-be-same-as-that-in-Deployment",
			},
		},
	}

clientset.CoreV1().Services(NAMESPACE).Create(svc)

Creating a route

Here's how a yaml representation of a Route looks like

apiVersion: v1
kind: Route
metadata:
  name: host-route
spec:
  to:
    kind: Service
    name: service-name

Here's how to do the same using Golang APIs

Pull openshift client-go's master commit.

go get github.com/openshift/client-go@584632b8fc73a646310252d82c303f23e325ab4f

This needs kubernetes client-go to be at 0.18.3 #12

Create a route client

routeObj := &routev1.Route{
		ObjectMeta: metav1.ObjectMeta{
			Name: "myroute",
		},
		Spec: routev1.RouteSpec{
			To: routev1.RouteTargetReference{
				Kind: "Service",
				Name: svc.Name,
			},
                       Port: &routev1.RoutePort{
				TargetPort: intstr.IntOrString{IntVal: conventionalPort}, // conventionalPort is 80
			},
	}
routeClient, _ := routev1Client.NewForConfig(&restConfig)
...
...
routeClient.Routes(NAMESPACE).Create(context.TODO(), routeObj, metav1.CreateOptions{})

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions