Kubernetes Example

Nginx Server Deployment using Kubernetes

  1. Create a folder
$ mkdir nginx_kube_example
$ cd nginx_kube_example
  1. Create the yaml file in the editor of your choice which will be used to deploy nginx pod
$ vi nginx_pod.yaml
apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx
spec:
  replicas: 2
  selector:
    app: nginx
  template:
    metadata:
      name: nginx
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
  1. Create the nginx pod using kubectl
$ kubectl create -f nginx_pod.yaml
  1. In the above pod creation process, we have created two replicas of the nginx pod and its details can be listed as follow
$ kubectl get pods
NAME          READY     REASON    RESTARTS   AGE
nginx-karne   1/1       Running   0          14s
nginx-mo5ug   1/1       Running   0          14s
$ kubectl get rc
CONTROLLER   CONTAINER(S)   IMAGE(S)   SELECTOR    REPLICAS
nginx        nginx          nginx      app=nginx   2
$ docker ps
CONTAINER ID        IMAGE                                   COMMAND                CREATED             STATUS              PORTS               NAMES
1d3f9cedff1d        nginx:latest                            "nginx -g 'daemon of   41 seconds ago      Up 40 seconds                           k8s_nginx.6171169d_nginx-karne_default_5d5bc813-3166-11e5-8256-ecf4bb2bbd90_886ddf56
0b2b03b05a8d        nginx:latest                            "nginx -g 'daemon of   41 seconds ago      Up 40 seconds
  1. Deploy the nginx service using yaml file in order to expose the nginx pod on the host port “82”
$ vi nginx_service.yaml
apiVersion: v1
kind: Service
metadata:
  labels:
    name: nginxservice
  name: nginxservice
spec:
  ports:
    # The port that this service should serve on.
    - port: 82
  # Label keys and values that must match in order to receive traffic for this service.
  selector:
    app: nginx
  type: LoadBalancer
  1. Create the nginx service using kubectl
$kubectl create -f nginx_service.yaml
services/nginxservice
  1. The nginx service can be listed as follow
$ kubectl get services
NAME           LABELS                                    SELECTOR    IP(S)          PORT(S)
kubernetes     component=apiserver,provider=kubernetes   <none>      192.168.3.1    443/TCP
nginxservice   name=nginxservice                         app=nginx   192.168.3.43   82/TCP

Now the nginx server test page can be accessed on the following URL; http://127.0.0.1:82

  1. Now we will delete the nginx pod and service
$ kubectl delete service nginxservice
$ kubectl delete rc nginx

Pod Deployment with two containers

  1. In the below example we will be running the two containers in same pod
$ vi nginx-mysql.json
{
   "id":"nginx-mysql",
   "kind": "Pod",
   "apiVersion": "v1",
   "metadata": {
      "name": "nginx-mysql",
      "labels": {
         "name": "nginx-mysql"
      }
   },
   "spec": {
      "containers": [
         {
            "name": "nginx",
            "image": "nginx",
            "ports": [
               {
                  "hostPort": 85,
                  "containerPort": 80
               }
            ]
         },
         {
            "name": "mysql",
            "image": "mysql",
            "ports": [
               {
                  "hostPort": 3306,
                  "containerPort": 3306
               }
            ]
         }
      ]
   }
}
  1. Create the pod
$ kubectl create -f nginx-mysql.json
$ kubectl get pods