Try Live Demo

Kubernetes is an open-source container orchestration tool that is widely used all over the world. In this Kubernetes Tutorial,  we’re going to tell you how to use Ant Media Server in Kubernetes.

Kubernetes Tutorial : How to Scale Ant Media Server with Kubernetes

Running Ant Media Server in Kubernetes is fully about clustering. If you are not familiar with Ant Media Server Clustering & Scaling, you may want to check out our Cluster & Scaling documentation

The scope of this Kubernetes tutorial is to give you the basics about how to run Ant Media Server in Kubernetes Cluster. If you’re not familiar with Kubernetes then you can first get started with Kubernetes and then continue with interactive tutorials.

After these notes, just before proceeding, we assume that you’ve installed Kubernetes in your test environment or in a cloud service like AWS. The scenario below is generally running Kubernetes with Minikube. It’s also tested in AWS EKS as well. If you use minikube, we start with sudo minikube start --driver=none in VPS and need to use sudo command in docker and kubectl. On the other hand, you don’t need to use sudo in kubectl commands if you’re running in AWS.

Let’s start step by step with this Kubernetes media server Tutorial about Ant Media Server.

1. Create an Image for Container to use in Kubernetes

First things first. We need to create a docker image to run our pods in Kubernetes.

docker

1. Get the Dockerfile. Dockerfile is available in Ant Media Server’s Scripts repository that is actively used in CI pipeline. Anyway, you can get it with the below command.

wget https://raw.githubusercontent.com/ant-media/Scripts/master/docker/Dockerfile_Process \
-O Dockerfile_Process

2. Download or Copy Ant Media Server Enterprise Edition ZIP file into the same directory that you download Dockerfile above.

3. Create the docker image. Before running the command below, please pay attention that you should replace {CHANGE_YOUR_ANT_MEDIA_SERVER_ZIP_FILE} in the command below with your exact Ant Media Server ZIP file name.

sudo docker build --network=host --file=Dockerfile_Process -t ant-media-server-enterprise-k8s:test \
--build-arg AntMediaServer={CHANGE_YOUR_ANT_MEDIA_SERVER_ZIP_FILE} .

The second thing we should point out is the image name and tag. The command above uses the ant-media-server-enterprise-k8s:test image name and tag. The image name is compatible with the deployment file. I mean you can absolutely change the image name and tag, just make sure it is compatible with the deployment file, which we’ll be mentioning soon.

If everything is OK, your image is available in your environment. If you’re going to use this image in AWS EKS or a similar service, you need to upload the image to repositories such as AWS ECR or you can run a local registry.

2. Run Ant Media Server K8s Deployment

1. Download the ams-k8s-deployment.yaml file with the following command.

wget https://raw.githubusercontent.com/ant-media/Scripts/master/kubernetes/ams-k8s-deployment.yaml -O ams-k8s-deployment.yaml

2. Change the deployment file according to your needs. In order to do that, let us give some more information about the ams-k8s-deployment.yaml The content of the file is like this

apiVersion: apps/v1
kind: Deployment
metadata:
  name: ant-media-server
spec:
  selector:
    matchLabels:
      run: ant-media-server
  replicas: 1
  template:
    metadata:
      labels:
        run: ant-media-server
    spec:
      hostNetwork: true
      containers:
      - name: ant-media-server
        imagePullPolicy: IfNotPresent  # change this value accordingly. It can be Never, Always or IfNotPresent
        image: ant-media-server-enterprise-k8s:test  #change this value according to your image.
# You basically need to update the mongodb server url(-h) below. You may also need to add -u and -p parameters for
# specifying mongodb username and passwords respectively 
        args: ["-g", "true", "-s", "true", "-r", "true", "-m", "cluster", "-h", "127.0.0.1"]  

If you want to learn more about the parameters above, please visit the related section here

So you can now change the ams-k8s-deployment.yaml file according to your environment.

3. Run the ams-k8s-deployment.yaml with following command.

sudo kubectl create -f ams-k8s-deployment.yaml

If everything is OK, you’ll have a running deployment. You can query your deployment with the following command.

sudo kubectl get deployments

Then you should see something like below.

NAME               READY   UP-TO-DATE   AVAILABLE   AGE
ant-media-server   1/1     1            1           12s

3. Run Ant Media Server K8s Service

Running Ant Media Server K8s Service is the easiest part.

1. Download the K8s Service file

wget https://raw.githubusercontent.com/ant-media/Scripts/master/kubernetes/ams-k8s-service.yaml -O ams-k8s-service.yaml

2. Create the Service

sudo kubectl create -f ams-k8s-service.yaml

3. Check the service if it’s working.

sudo kubectl get services

If it’s running, you should see something similar to this.

NAME               TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
ant-media-server   LoadBalancer   10.105.169.89   <pending>     5080:30240/TCP   6s
kubernetes         ClusterIP      10.96.0.1       <none>        443/TCP          6m50s

If you’re running with minikube, you can directly connect to your platform with your host IP address in the browser and the port name above. Let’s assume your host IP address is xxx.yy.yy.zz, then you can connect to the Ant Media Server web panel via http://xxx.yy.yy.zz:30240. If you’re running this service in AWS or any other place, please read their documentation about Load Balancer in Kubernetes.

4. Autoscaling with K8s

Kubernetes lets you scale the pods automatically to optimize resource usage and make the backend ready according to the load in your service. Kubernetes has a Horizontal Pod Autoscaler which is a built-in component that can scale your pods automatically.

Firstly, we should have a Metrics Server to collect the metrics of the pods. To provide metrics via the Metrics API, metric server monitoring must be deployed on the cluster. Horizontal Pod Autoscaler uses this API to collect metrics.

1. Install Metrics Server

A Metrics Server is usually deployed by the cloud providers. If you are using a custom Kubernetes or the Metric Server is not deployed by your cloud provider you should deploy it manually as explained later in this Kubernetes Tutorial. Firstly, check if the metrics-server is installed using the command below.

kubectl get pods --all-namespaces | grep -i "metric"

If you see something below, it means you have metrics server.

kube-system   metrics-server-5bb577dbd8-7f58c           1/1     Running   7          23h`

Manual Installation

1. Download the components.yaml file on the master.

wget https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml

2. Add the following line to line 132 of the file. --kubelet-insecure-tls . The lines are going to seem exactly as below.

spec:
     containers:
     - args:
       - --kubelet-insecure-tls
       - --cert-dir=/tmp
       - --secure-port=4443
       - --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname
       - --kubelet-use-node-status-port
       image: k8s.gcr.io/metrics-server/metrics-server:v0.4.2

3. Deploy the yaml file that we have made changes.

kubectl apply -f components.yaml

4. Check whether everything is working properly.

kubectl get apiservices |grep "v1beta1.metrics.k8s.io"

The output of the command should be as follows.

v1beta1.metrics.k8s.io                 kube-system/metrics-server   True        21h

2. Create Horizontal Pod Autoscaling

1. Make a small change in our yaml file in Ant Media Server. kubectl edit deployment ant-media-server. Edit and save the following lines under the container according to yourself. Before proceeding let us tell about Millicores. Millicores is a metric which is used to measure CPU usage. It is a CPU core divided into 1000 units (milli = 1000). 1000 = 1 core. So the below configuration uses 1 cores.

resources:
  requests:
  cpu: 1000m

2. Check the accuracy of the value we entered using the command below.

kubectl describe deployment/ant-media-server

Now that the deployment is running, we’re going to create a Horizontal Pod Autoscaler for it.

3. Create Horizontal Pod AutoScaling

kubectl autoscale deployment ant-media-server --cpu-percent=60 --min=1 --max=10

or you can use the following yaml file.

kubectl create -f https://raw.githubusercontent.com/ant-media/Scripts/master/kubernetes/ams-k8s-hpa.yaml

In the above configuration, we set the CPU average as 60% and we set the pods as min 1 and maximum 10. A new pod will be created every time the CPU average passes 60%.

4. You can monitor the situation in the following output.

root@k8s-master:~# kubectl get hpa
NAME               REFERENCE                     TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
ant-media-server   Deployment/ant-media-server   3%/60%   1         10         1          20h

New pods are going to be created when we start loading and the cpu exceeds 60%. When the CPU average value decreases below 60%, then the pods are going to be terminated.

root@k8s-master:~# kubectl get hpa
NAME               REFERENCE                     TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
ant-media-server   Deployment/ant-media-server   52%/60%   1         10         4          20h

5. Check the number of pods running using the following command.

root@k8s-master:~# kubectl get pods
NAME                                READY   STATUS    RESTARTS   AGE
ant-media-server-7b9c6844b9-4dtwj   1/1     Running   0          42m
ant-media-server-7b9c6844b9-7b8hp   1/1     Running   0          19h
ant-media-server-7b9c6844b9-9rrwf   1/1     Running   0          18m
ant-media-server-7b9c6844b9-tdxhl   1/1     Running   0          47m
mongodb-9b99f5c-x8j5x               1/1     Running   0          20h

We hope this tutorial helps you get started Scaling Ant Media Server in Kubernetes. We can call it a Kubernetes media server :D. If you have any questions, please reach out to us by using contact@antmedia.io.

Categories: Tutorial

Ahmet Oguz Mermerkaya

Oguz is the co-founder of Ant Media. His tech stack includes VxWorks, UML, Rhapsody in C++. Java, OSGi, Swing, JSF, Web, PHP, FFmpeg API, Native WebRTC, Java EE, Hibernate, Spring, MongoDB, MySQL, Angular, JavaScript, HTML5, Android (Native) and iOS (Native). Oguz is one of the writers of "Merhaba Android", one of the first books published in Turkey about Android app programming. His second book is about HTML5 & CSS3. He has attended several conferences and universities talking about Android, business life and technical issues. He is a member of the GDG Community and also the founder of GDG Ankara.

chatsimple
Warning: Undefined array key "promocode" in /home/mirror.antmedia.io/public_html/wp-content/themes/hestia-child/footer.php on line 70