Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Traefik to direct Kubernetes traffic

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "how to use Traefik to guide Kubernetes traffic", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use Traefik to guide Kubernetes traffic" this article.

Deploy a simple website

Previously, we used kubectl for direct deployment. However, this is not a typical deployment method. The YAML configuration file is typically used, which is the one we will use in this article. We will start at the top and create the profile in a top-down manner.

Deployment configuration

The first is to deploy the configuration. The configuration is shown below and described below. I usually start with the example in the Kubernetes document and then modify it as needed. For example, the following configuration is modified after copying the example in the deployment document.

Create a file mysite.yaml with the following contents:

ApiVersion: apps/v1kind: Deploymentmetadata: name: mysite-nginx labels: app: mysite-nginxspec: replicas: 1 selector: matchLabels: app: mysite-nginx template: metadata: labels: app: mysite-nginxspec: containers:-name: nginx image: nginx ports:-containerPort: 80

Most of them are samples. For the important part, we will name the deployment mysite-nginx and tag it with an app tag of the same name. We specified a copy of replica, which means that only one Pod will be created. We also specified a container, which we named nginx. We specify the mirror image as nginx. This means that when deployed, K3s will download the nginx image from DockerHub and create a Pod from it. Finally, we specify that the container port containerPort is 80, which only means that Pod will listen on port 80 inside the container.

I emphasized "inside the container" above, because this is an important difference. Because we are configured by container, we can only access it inside the container and further restrict it to the internal network. This is necessary to allow multiple containers to listen on the same container port. In other words, with this configuration, some other Pod can also listen on their container port 80 and will not conflict with this container. To provide formal access to the Pod, we need a service service configuration.

Service configuration

In Kubernetes, a service service is an abstraction. It provides a way to access Pod or Pod collections. When connecting to a service, the service is routed to a single Pod or, if multiple Pod replicas are defined, to multiple Pod through load balancing.

The service can be specified in the same configuration file, which is what we will do here. Separate the configuration area with-and add the following to the mysite.yaml:

-apiVersion: v1kind: Servicemetadata: name: mysite-nginx-servicespec: selector: app: mysite-nginx ports:-protocol: TCP port: 80

In this configuration, we named the service mysite-nginx-service. We provide a selector selector:app: mysite-nginx. This is how the service selects the application container to which it is routed. Remember, we provide the container with the app tag: mysite-nginx. This is how the service finds our containers. Finally, we specify the service protocol as TCP, which listens on port 80.

Entry configuration

The ingress Ingress configuration specifies how traffic is transferred from outside the cluster to the services inside the cluster. Remember that K3s is pre-configured with Traefik as the ingress controller. Therefore, we will write a Traefik-specific portal configuration. Add the following to the mysite.yaml (don't forget to separate it with--):

-apiVersion: networking.k8s.io/v1beta1kind: Ingressmetadata: name: mysite-nginx-ingress annotations: kubernetes.io/ingress.class: "traefik" spec: rules:-http: paths:-path: / backend: serviceName: mysite-nginx-service servicePort: 80

In this configuration, we name the entry record mysite-nginx-ingress. We told Kubernetes that we wanted traefik to be our entry controller, plus kubernetes.io/ingress.class 's comments.

In the rule rules section, we basically say that when http traffic comes in and the path matches / (or anything below it), route it to the back-end backend service specified by serviceName mysite-nginx-service and route it to servicePort 80. This connects the incoming HTTP traffic to the service we defined earlier.

What needs to be deployed

As far as configuration is concerned, this is it. If we deploy now, we'll get the default nginx page, but that's not what we want. Let's create some simple but customizable deployments. Create a file index.html with the following:

K3S! Html {font-size: 62.5%;} body {font-family: sans-serif; background-color: midnightblue; color: white; display: flex; flex-direction: column; justify-content: center; height: 100vh;} div {text-align: center; font-size: 8remr; text-shadow: 3px 3px 4px dimgrey;} Hello from K3S!

We haven't covered the storage mechanism in Kubernetes, so let's be lazy here and store only this file in the Kubernetes configuration map. This is not the way we recommend deploying the website, but it is feasible for our purpose. Run the following command:

Kubectl create configmap mysite-html-from-file index.html

This command creates a configuration mapping configmap resource named mysite-html from the local file index.html. This is actually storing a file (or set of files) in the Kubernetes resource, which we can call up in the configuration. It is usually used to store configuration files (hence the name), and we abuse it a little here. In future articles, we will discuss the appropriate storage solutions in Kubernetes.

After creating the configuration map, let's mount it in our nginx container. Let's do it in two steps. First, we need to specify a volume volume to call up the configuration mapping. Then we need to mount the volume into the nginx container. Complete the first step by adding the following under the spec tag after container in mysite.yaml:

Volumes:-name: html-volume configMap: name: mysite-html

This tells Kubernetes that we want to define a volume named html-volume, and that the volume should contain the contents of a configuration map named html-volume (which we created in the previous step).

Next, under port ports in the nginx container specification, add the following:

VolumeMounts:-name: html-volume mountPath: / usr/share/nginx/html

This tells Kubernetes that for the nginx container, we want to mount a volume named html-volume on the / usr/share/nginx/html path in the container. Why use / usr/share/nginx/html? That is where the nginx image provides HTML services. By mounting the volume on this path, we replaced the default content with the contents of the volume.

For reference, the deployment section of the configuration file should now look like this:

ApiVersion: apps/v1kind: Deploymentmetadata: name: mysite-nginx labels: app: mysite-nginxspec: replicas: 1 selector: matchLabels: app: mysite-nginx template: metadata: labels: app: mysite-nginxspec: containers:-name: nginx image: nginx ports:-containerPort: 80 volumeMounts:-name: html-volume mountPath: / usr / share/nginx/html volumes:-name: html-volume configMap: name: mysite-html deploy it!

Now we are ready to deploy! We can do this:

Kubectl apply-f mysite.yaml

You should see something like this:

Deployment.apps/mysite-nginx createdservice/mysite-nginx-service createdingress.networking.k8s.io/mysite-nginx-ingress created

This means that Kubernetes creates resources for each of the three configurations we specify. Check the status of the Pod using the following methods:

Kubectl get pods

If you see that the status is ContainerCreating, give it some time and run kubectl get pods again. Usually, it takes some time for the first time, because K3s must download the nginx image to create the Pod. After a while, you should see the status of Running.

Give it a try!

After Pod is running, it's time to try. Open a browser and enter kmaster in the address bar.

Congratulations! You have deployed a website on the K3s cluster!

The other one

So now we have an entire K3s cluster running a single website. But we can have more websites! What if we want to provide another website in the same cluster? Let's see how to do this.

Again, we need to deploy something. It happened that my dog had a piece of information that she wanted the world to know, and she had been thinking about it for a long time. So I made some HTML specifically for her (available from the sample zip file). Again, we will use the technique of configuration mapping to host these HTML. This time we will put the entire directory (the html directory) in the configuration map, but the invocation is the same.

Kubectl create configmap mydog-html-from-file html

Now we need to create a configuration file for this site. It's almost exactly the same as for mysite.yaml, so copy mysite.yaml to mydog.yaml first. Now change the mydog.yaml to:

ApiVersion: apps/v1kind: Deploymentmetadata: name: mydog-nginx labels: app: mydog-nginxspec: replicas: 1 selector: matchLabels: app: mydog-nginx template: metadata: labels: app: mydog-nginxspec: containers:-name: nginx image: nginx ports:-containerPort: 80 volumeMounts:-name: html-volume mountPath: / usr / share/nginx/html volumes:-name: html-volume configMap: name: mydog-html---apiVersion: v1kind: Servicemetadata: name: mydog-nginx-servicespec: selector: app: mydog-nginx ports:-protocol: TCP port: 80---apiVersion: networking.k8s.io/v1beta1kind: Ingressmetadata: name: mydog-nginx-ingress annotations: kubernetes.io/ingress.class: "traefik" traefik.frontend. Rule.type: PathPrefixStripspec: rules:-http: paths:-path: / mydog backend: serviceName: mydog-nginx-service servicePort: 80

We just need to search and replace mysite with mydog to make most of the changes. The other two changes are in the entry section. We changed path to / mydog and added an annotation traefik.frontend.rule.type: PathPrefixStrip.

The specification of the / mydog path instructs Traefik to route all incoming requests that begin with the / mydog path to mydog-nginx-service. Any other paths will continue to be routed to mysite-nginx-service.

The new annotation PathPrefixStrip tells Traefik to remove the prefix / mydog before sending the request to mydog-nginx-service. We do this because mydog-nginx applications do not require prefixes. This means that we can simply change the location of the mounted service by changing the prefix in the entry record.

Now we can deploy as before:

Kubectl apply-f mydog.yaml

Now, the news about my dog should be found on http://kmaster/mydog/.

Whoo-hoo! The message is out! Maybe we can all get some sleep tonight.

So now we have a K3s cluster that hosts two websites, and Traefik decides which service to pass the request to based on the pathname! However, it is not limited to path-based routing, but we can also use hostname-based routing, which we will discuss in a future article.

In addition, the site we just hosted is a standard unencrypted HTML site, and now all the content is encrypted using SSL/TLS. In our next article, we will add support for the K3s cluster to host SSL/TLS HTTPS sites!

Clear

Before we start, since this article is mainly about sample sites, I want to show you how to delete content in case you don't want to leave the examples in the cluster.

For most configurations, you can undo the configuration simply by running the delete command using the same configuration file used at the time of deployment. So let's clean up both mysite and mydog.

Kubectl delete-f mysite.yamlkubectl delete-f mydog.yaml

Since we manually create configuration mappings, we also need to delete them manually.

Kubectl delete configmap mysite-htmlkubectl delete configmap mydog-html

Now, if we execute kubectl get pods, we should see that our nginx Pod no longer exists.

$kubectl get podsNo resources found in default namespace. The above is all the contents of the article "how to use Traefik to direct Kubernetes traffic". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report