skip to Main Content

I am trying to learn kubernetes. I can deploy containers.
How can we publish/show container through web browser?

I think we can create nodePort through service.

How can we do this?
Thank you for helping me.

2

Answers


  1. Try using the below manifest to deploy a sample nginx application in Kubernetes(using a LoadBlancer IP type). I have used Minikube so the way of accessing the application would be different. This is one way of accessing the application there are other ways 1.Using Nodeport 2. Using Ingress

    Deployment.yaml

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx-deployment
      namespace: dev
    spec:
      selector:
        matchLabels:
          app: nginx
      replicas: 2 # tells deployment to run 2 pods matching the template
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
          - name: nginx
            image: nginx:1.14.2
            ports:
            - containerPort: 80
            resources:
              requests:
                memory: "64Mi"
                cpu: "200m"
              limits:
                memory: "128Mi"
                cpu: "350m"
    

    Service.yml

    apiVersion: v1
    kind: Service
    metadata:
      name: nginx-service
      namespace: dev
    spec:
      type: LoadBalancer
      ports:
      - port: 80
      selector:
       app: nginx
    

    Once these two files are created apply them using the below command

    kubectl apply -f nginx.yml,nginx-service.yml
    

    Accessing the application on K8s provided by cloud providers

    If you are using kubernetes provided by some cloud provider then you will need to access the application from the LoadBalancer IP. This could be found by using the below command(I have deployed the application on dev namespace):

    kubectl get svc -n dev
    

    This should list a service called nginx-service and there will be an External IP for this service you have to access the application using that External IP.

    Accessing application on Minikube

    In Minikube if you want to access the application use the below command:

    minikube service nginx-service --url
    

    Nginx pod's output

    To understand more about services refer : Types of service in Kubernetes

    Login or Signup to reply.
  2. Define service type as NodePort. This makes the service accessible on a static port on each Node in the cluster. This means that the service can handle requests that originate from outside the cluster.

    sample is given below

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

    You can also define service type as LoadBalancer if you are hosting your apps in Cloud. The service becomes accessible externally through a cloud provider’s load balancer functionality. GCP, AWS, Azure, and OpenStack offer this functionality. The cloud provider will create a load balancer, which then automatically routes requests to your Kubernetes Service.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search