使用kubernetes扩展Signalr核心Web应用程序

Signal R with ASP.Net Core is an open-source library providing real-time communications between the client and the server. With just a couple of lines of code, we can easily add this capability to any asp.net core web application and leverage a powerful feature-set.

带有ASP.Net Core的Signal R是一个开放源代码库,可在客户端和服务器之间进行实时通信。 只需几行代码,我们就可以轻松地将此功能添加到任何asp.net核心Web应用程序中,并利用强大的功能集。

In this blog, I’ll use a Microk8s cluster running locally to deploy the app, but the files and commands should work with any cluster without requiring too many changes. The full source code for the example described in this blog can be found here.

在此博客中,我将使用在本地运行的Microk8s群集来部署应用程序,但是文件和命令应可在任何群集上使用,而无需进行太多更改。 在此博客中描述的示例的完整源代码可以在这里找到。

The application we’ll be working with is a simple .Net core web app with a Signal R hub and an Angular frontend. The frontend is a simple chat application exchanging messages with all connected clients.

我们将使用的应用程序是一个简单的.Net核心Web应用程序,它具有Signal R集线器和Angular前端。 前端是一个简单的聊天应用程序,可与所有连接的客户端交换消息。

Let’s deploy it and see it work. Pull down the source code and navigate to the Kube folder on a terminal window. I already have the images available publicly so it should be easy to fire it up in our cluster. Before you get started, make sure you have the correct context set,

让我们部署它,看看它是否有效。 下拉源代码并导航到终端窗口上的Kube文件夹。 我已经可以公开使用这些图像,因此应该很容易在我们的集群中启动它们。 在开始之前,请确保您具有正确的上下文集,

kubectl config current-context

This should return the context you are on currently. If you are not on the correct context, then you can set it using the command,

这应该返回您当前所在的上下文。 如果您使用的不是正确的上下文,则可以使用以下命令进行设置,

kubectl config use-context enter-context-name

Once on the correct context, lets create a namespace where all our resources will go,

在正确的上下文中,让我们创建一个名称空间,所有资源都将移至该名称空间,

kubectl create namespace signalrredis

Once the namespace is created, lets run the secret yml which really isnt used right now but will be used in later steps,

创建命名空间后,让我们运行秘密yml,它实际上目前尚未使用,但将在以后的步骤中使用,

kubectl apply -f secret.yml --namespace signalrredis

Now, to run the deployment,

现在,要运行部署,

kubectl apply -f deployment.yml --namespace signalrredis

The deployment should create a single pod. We can now create the service, (alternatively use -n instead of — namespace, as a shortcut)

部署应创建单个吊舱。 现在,我们可以创建服务(或者使用-n代替-名称空间作为快捷方式)

kubectl apply -f service.yml --namespace signalrredis

We can also create an ingress to access the app, (make sure to enable ingress on your microk8s cluster or on minikube).

我们还可以创建一个访问应用程序的入口(请确保在您的microk8s集群minikube上启用入口)。

kubectl apply -f ingress.yml --namespace signalrredis

Once the ingress is created, we now need to update our hosts file with the cluster’s IP and map it to the ingress host (signalrredis.local). With microk8s, the IP would just be 127.0.0.1. With minikube, you can find the cluster’s ip by using the command

创建入口后,我们现在需要使用群集的IP更新我们的hosts文件,并将其映射到入口主机(signalrredis.local)。 使用microk8,IP仅为127.0.0.1。 使用minikube,您可以使用以下命令找到集群的ip

minikube ip

Once you have the IP, update the hosts file by mapping the local address to the ip. On windows the hosts file is located at %systemroot%\system32\drivers\etc\hosts. On Linux, it is located at /etc/hosts.

获得IP后,通过将本地地址映射到ip更新主机文件。 在Windows上,主机文件位于%systemroot%\ system32 \ drivers \ etc \ hosts。 在Linux上,它位于/ etc / hosts。

Once the mapping is added, the app can now be accessed from a browser using the local host name (http://signarredis.local). Create multiple instances to see it work,

添加映射后,现在可以使用本地主机名( http://signarredis.local )从浏览器访问该应用程序。 创建多个实例以查看其工作情况,

Image for post

The messages should be exchanged successfully and the pod that sent the message should also be displayed. Since we have only one pod, its the same name thats displayed in both instances.

消息应该成功交换,并且还应该显示发送消息的窗格。 由于我们只有一个Pod,因此在两个实例中显示的名称相同。

Great! Now, lets scale it by re-deploying with a higher pod count, so lets change the replicas property in the deployment.yml file to 10,

大! 现在,让我们通过使用更高的Pod数量重新部署来扩展它,因此让我们将Deployment.yml文件中的副本属性更改为10,

...app: signalrredisspec:  replicas: 10...

Now, lets re-deploy,

现在,让我们重新部署

kubectl apply -f deployment.yml --namespace signalrredis

After the pods have been re-deployed, lets test the app again,

重新部署Pod后,请再次测试该应用,

Image for post
Image for post

Or what didnt happen? When we scaled, we ended up creating multiple hubs but since each client connects to a single hub, the messages will only be propagated to the clients connected to that hub. Any client connected to any other hub will not receive that message since the hubs do not talk to each other. If multiple browser instances happen to connect to the same pod, then the solution might appear to work, but the moment a connection is made to a different pod, the messaging will stop working since the client connected to a different hub.

还是什么没发生? 扩展后,我们最终创建了多个集线器,但是由于每个客户端都连接到单个集线器,因此消息将仅传播到连接到该集线器的客户端。 连接到任何其他集线器的任何客户端都不会收到该消息,因为集线器不会互相通信。 如果多个浏览器实例碰巧连接到同一个Pod,则该解决方案似乎可以正常工作,但是当连接到另一个Pod时,由于客户端连接到不同的集线器,消息传递将停止工作。

To fix this, we need a backplane, which will enable the hubs to communicate with each other. We can use an instance of redis as the backplane. It only requires a single code change to our code base in the startup.cs file,

要解决此问题,我们需要一个底板 ,它将使集线器能够相互通信。 我们可以使用redis实例作为背板。 仅需对startup.cs文件中的代码库进行一次代码更改,

services.AddSignalR().AddStackExchangeRedis(“<redis_conn_str>”);

The sample aplication already has this code change and we can use the backplane by changing an env variable, RedisConfig__UseAsBackplane to true in the deployment.yml, so lets change it,

示例应用程序已经进行了此代码更改,我们可以通过将deploy.yml中的env变量RedisConfig__UseAsBackplane更改true来使用背板,因此让我们对其进行更改,

...containers:- name: signalrredis  image: ashwin027/signalrredis:latest  env:    - name: RedisConfig__UseAsBackplane      value: "true"...

But before we run this, we need redis to be running in the cluster. To install redis, we’ll use helm. The instructions on installing the helm CLI can be found here. Once helm is installed, we need to enable it in the cluster.

但是在运行此命令之前,我们需要在集群中运行Redis。 要安装redis,我们将使用helm 。 可以在此处找到有关安装头盔CLI的说明。 安装头盔后,我们需要在集群中启用它。

On microk8s, enable the addon along with the storage addon (required for redis) using the command,

在microk8上,使用以下命令启用插件以及存储插件(redis必需),

microk8s enable helm3 storage

On minikube,

在minikube上,

minikube addons enable helm-tiller

Once we have helm enabled, while on the Kube folder on a terminal window, run the below command to get redis running,

启用舵机后,在终端窗口的Kube文件夹中,运行以下命令以使Redis运行,

helm upgrade sigredis ./redis/ --install --namespace signalrredis

Note: With Microk8s, If you haven’t merged your microk8s config into your kubeconfig, then use the command,

注意:对于Microk8,如果尚未将microk8s配置合并到kubeconfig中,请使用以下命令,

microk8s.helm3 upgrade sigredis ./redis/ --install --namespace signalrredis

After the helm install, ensure that the pods for redis are up and running using either the kubernetes dashboard or lens.

安装头盔后,请确保使用kubernetes仪表板或Lens来启动和运行Redis吊舱。

Now that we have redis working, lets get the password for the redis cluster,

现在我们可以使用redis了,让我们获取redis集群的密码,

kubectl get secret --namespace signalrredis sigredis -o jsonpath=”{.data.redis-password}”

This should output a base 64 encoded password that needs to be copied over to the secret.yml file in your kube folder,

这应该输出一个以64为底的编码密码,该密码需要复制到您kube文件夹中的secret.yml文件中,

apiVersion: v1kind: Secretmetadata:name:  redispassworddata:redispassword: PASTE_PASSWORD_HEREtype: Opaque

Once the secret yml file has been updated with the password, lets run it,

使用密码更新yml机密文件后,请运行它,

kubectl apply -f secret.yml --namespace signalrredis

We can now run the deployment again with the redis backplane flag set to true,

现在,我们可以将redis背板标志设置为true,再次运行部署,

kubectl apply -f deployment.yml --namespace signalrredis

Once all the pods are deployed, we can test the app again,

部署完所有Pod之后,我们可以再次测试该应用,

Image for post

When a message is submitted, you can see the pod name change in each browser showing you which pod the message came from. Our backplane is now fully functional!

提交消息后,您可以在每个浏览器中看到窗格名称的更改,显示消息来自哪个窗格。 我们的背板现在可以正常使用了!

If you have further questions on the topic, feedback on the article or just want to say hi you can hit me up on twitter or linkedin.

如果您对此主题还有其他疑问,请反馈该文章,或者只是想打个招呼,可以在TwitterLinkedin上打我。

翻译自: https://medium.com/swlh/scaling-signalr-core-web-applications-with-kubernetes-fca32d787c7d

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值