Kubernetes is a very mainstream container orchestration tool, in the application creation, application deployment, application expansion, application update and other aspects are very convenient, but also in the application failure, can quickly self-healing. So based on the micro-service architecture of the product, it is very necessary to understand the use of Kubernetes, I guess many people have tried to build their own Kubernetes environment, deploy a service to try the effect, but because of many reasons from the entry to give up again, Part of the reason is that Kubernetes has a relatively high cost of entry, a lot of concepts, and a little bit of complexity to build, so it often dies in the environment from the beginning, resulting in a lot of predetermined goals are not achieved.

On the concept of Kubernetes content here is no longer redundant, but the concept of the part is in use must be the first to master the knowledge, so it is recommended to view the information on the overall structure and use of a certain understanding of the actual operation.

The cluster structures,

Kubernetes cluster environment will be set up for the test, with 3 Linux servers (1 Master, 2 nodes). Online there are a lot of Kubernetes cluster construction article, find a lot of final discovery or Li Zhenliang based on kubeadm 30 minutes deployment of a Kubernetes cluster video tutorial is more reliable for beginners, although some operation commands need to be adjusted in the actual production environment, but as a learning problem is not big, The optimization of more details is still left to more professional operation and maintenance personnel.

In order to save the installation of the operating system, I directly bought 3 2C4G machines on Tencent Cloud, and it only cost a few yuan to choose the pay-per-view charging mode for most of the day (not advertising, just lazy). The following points should be set up on the security group of the console in advance for cloud server-based construction:

  1. The three servers communicate with each other on the Intranet.
  2. To allow the server to access the Internet, you need to download related dependencies.
  3. Allow the client machine to access the server through the external IP address.

For details on how to install it, please refer to the video tutorial, but I’m sure some people still have some weird problems, so good luck with that. Here are some notes:

  • Kubeadm Init and POD network plugins only need to be operated on the Master node;
  • In the kubeadm init--apiserver-advertise-addressSet it to the Intranet address.
  • throughhostnamectl set-hostname k8s-masterHostname name can be modified.

Run the kubectl get nodes command to check whether all nodes in the cluster are Ready.

Build.net Core service images

Next, Docker image of.NET Core service will be built directly in the Windows environment (there is no difference between image construction based on Windows or Linux theoretically), so Docker for Windows needs to be installed first. Start Docker after successful installation.

Create an API type ASP.NET Core Web application, enable Docker support, will automatically generate Dockerfile under the project, the command mainly involves dotnet restore, dotnet publish.

The FROM McR.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base WORKDIR/app EXPOSE EXPOSE 443 FROM 80 McR.microsoft.com/dotnet/core/sdk:3.1-buster AS build WORKDIR/SRC COPY ["K8SDemo/K8SDemo.csproj"."K8SDemo/"]
RUN dotnet restore "K8SDemo/K8SDemo.csproj"
COPY . .
WORKDIR "/src/K8SDemo"
RUN dotnet build "K8SDemo.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "K8SDemo.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet"."K8SDemo.dll"]
Copy the code

Docker build -f.\K8SDemo\Dockerfile. The essence is the same, but VS has been built in. After successful execution, you can view the existing Docker images in the CMD window through Docker images.

In order to get this image in Kubernetes cluster, now push k8sDemo image to personal Docker Hub public warehouse (in actual development should be the company’s private image warehouse).

  1. If you don’t already have a Docker Hub account, you need to register first.
  2. CMD window through the commandDocker login -u Username -p PasswordLogin;
  3. Set the TagDocker tag k8sdemo:latest user name /k8sdemo:1.0.0, because the push target can only be the current user;
  4. Push to the Docker Hub repositoryDocker push username /k8sdemo:1.0.0;

The deployment of

The following operations are performed on the Master node

  1. K8sdemo :1.0.0: k8sdemo:1.0.0: k8sdemo:1.0.0: k8sDemo :1.0.0: k8sDemo :1.0.0 And map port 80 in the container to a port on the server (a port mapped in NodePort mode is randomly selected in the range of 30000 to 32767).

    apiVersion: apps/v1 kind: Deployment metadata: name: k8sdemo spec: replicas: 1 selector: matchLabels: name: K8sdemo template: metadata: Labels: name: K8sdemo spec: containers: - name: K8sdemo image: beckjin/ K8sdemo :1.0.0 ports: - containerPort: 80 imagePullPolicy: IfNotPresent --- kind: Service apiVersion: v1 metadata: name: k8sdemo spec:type: NodePort
      ports:
        - port: 80
          targetPort: 80
      selector:
        name: k8sdemo
    Copy the code
  2. Create the service by using the command kubectl apply -f k8sdemo.yaml

    The configuration specified that when the dependent image does not exist automatically pull, but if the pull resource speed is slow, the startup may take a long time to complete, you can use the command kubectl get Pods and kubectl describe pod name to see the pod details.

  3. Run the kubectl get pod command to SVC to view the port on which the service is running

    You can then call the API at http://${exet IP}:31741/WeatherForecast to see the effect.

  4. Yaml kubectl apply -f k8sdemo.yaml kubectl apply -f k8sdemo.

  5. You can run the kubectl delete -f k8sdemo.yaml command to delete the service.

conclusion

In fact, I have been trying to play Kubernetes for a long time. To be honest, there is a lot of resistance, especially when I see a lot of introductions based on binary files. Therefore, a good tutorial can indeed bring us twice the result with half the effort. As an introduction to learning, we do not need a powerful and safe testing environment. The basic routines are to play first and then study in depth.