Giter Club home page Giter Club logo

run-my-nginx's Introduction

Kubernetes Service

In this demo, we would like to deploy an image using Kubernetes and configure a service to access the application image.

Steps to reproduce solution

1. Ensure Docker desktop is configured with Kubernetes

  • Go to settings โ†’ Kubernetes
  • Check the option 'Enable Kubernetes'
  • Click 'Apply & Restart'

2. Expose pods to the cluster

Pods are configured in the yaml file. The image used is nginx. Create Deployment by running the following command:

$ kubectl apply -f ./run-my-nginx.yaml
deployment.apps/my-nginx created

After a few moments, the Deployments should be ready. You can check running Deployments by executing the command:

$ kubectl get deployments
NAME       READY   UP-TO-DATE   AVAILABLE   AGE
my-nginx   2/2     2            2           23m

In this Deployment, we have specified 2 replicas in run-my-nginx.yaml. You can check running Pods too by running:

$ kubectl get pods
NAME                        READY   STATUS    RESTARTS   AGE
my-nginx-5b56ccd65f-4sv4j   1/1     Running   0          9m26s
my-nginx-5b56ccd65f-nhrvb   1/1     Running   0          9m26s

3. Create a Service (and expose it to external IP address)

As mentioned in the documentation, Kubernetes supports two ways of exposing a Service onto an external IP address: NodePorts and LoadBalancers. In this case, we use LoadBalancers. Run the following command to expose it:

$ kubectl expose deployment/my-nginx --port=80 --target-port=80 --type=LoadBalancer
service/my-nginx exposed

This specification will create a Service which targets TCP port 80 (target-port) on any Pod on the specified Deployment, and expose it on an abstracted Service port (port).

The Service created and configured can be checked with the following command:

$ kubectl get svc
NAME         TYPE           CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP      10.96.0.1      <none>        443/TCP        50m
my-nginx     LoadBalancer   10.97.81.204   localhost     80:30906/TCP   3m47s

After having connected the configured service, the nginx server can now be reached from any node by doing curl, which cannot be done without a configured service.

$ curl localhost:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

References

run-my-nginx's People

Contributors

kelvinnharris avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.