How to Install, Configure, and Deploy NGINX on a Kubernetes Cluster


How to Install, Configure, and Deploy NGINX on a Kubernetes Cluster 



[root@kubemaster k8s]# kubectl get nodes
NAME                     STATUS   ROLES    AGE   VERSION
kubemaster.cloud.local   Ready    master   34d   v1.16.3-k3s.2
kubeworker.cloud.local   Ready    <none>   34d   v1.16.3-k3s.2

Create a Deployment

~/nginx.yaml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-server
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.13-alpine
        ports:
        - containerPort: 80
[root@kubemaster k8s]# kubectl create -f nginx.yaml --record
deployment.apps/nginx-server created

List your deployments:
root@kubemaster k8s]# kubectl get deployments
NAME              READY   UP-TO-DATE   AVAILABLE   AGE
nginx-server      0/1     1            0           42s
[root@kubemaster k8s]# kubectl get deployments
NAME              READY   UP-TO-DATE   AVAILABLE   AGE
nginx-server      1/1     1            1           3m16s

 Check that your pod is present:
[root@kubemaster k8s]# kubectl get pods -o wide
NAME                               READY   STATUS    RESTARTS   AGE     IP          NODE                     NOMINATED NODE   READINESS GATES
nginx-server-75c79dc96f-ch4bn      1/1     Running   0          5m31s   10.42.1.6   kubeworker.cloud.local   <none>           <none>


Scale Deployments
[root@kubemaster k8s]# kubectl scale deployment nginx-server --replicas=8
deployment.apps/nginx-server scaled

Check the availability of your new replicas:
[root@kubemaster k8s]# kubectl get pods -o wide
NAME                               READY   STATUS              RESTARTS   AGE     IP          NODE                     NOMINATED NODE   READINESS GATES
nginx-server-75c79dc96f-ch4bn      1/1     Running             0          7m23s   10.42.1.6   kubeworker.cloud.local   <none>           <none>
nginx-server-75c79dc96f-9hfz5      0/1     ContainerCreating   0          42s     <none>      kubemaster.cloud.local   <none>           <none>
nginx-server-75c79dc96f-ms6qs      0/1     ContainerCreating   0          42s     <none>      kubemaster.cloud.local   <none>           <none>
nginx-server-75c79dc96f-rggqr      0/1     ContainerCreating   0          42s     <none>      kubemaster.cloud.local   <none>           <none>
nginx-server-75c79dc96f-rtb9j      0/1     ContainerCreating   0          42s     <none>      kubemaster.cloud.local   <none>           <none>
nginx-server-75c79dc96f-zg8h2      1/1     Running             0          42s     10.42.1.9   kubeworker.cloud.local   <none>           <none>
nginx-server-75c79dc96f-wb62z      1/1     Running             0          42s     10.42.1.7   kubeworker.cloud.local   <none>           <none>
nginx-server-75c79dc96f-kp4cr      1/1     Running             0          42s     10.42.1.8   kubeworker.cloud.local   <none>           <none>
[root@kubemaster k8s]# kubectl get pods -o wide
NAME                               READY   STATUS    RESTARTS   AGE     IP           NODE                     NOMINATED NODE   READINESS GATES
nginx-server-75c79dc96f-ch4bn      1/1     Running   0          12m     10.42.1.6    kubeworker.cloud.local   <none>           <none>
nginx-server-75c79dc96f-zg8h2      1/1     Running   0          5m38s   10.42.1.9    kubeworker.cloud.local   <none>           <none>
nginx-server-75c79dc96f-wb62z      1/1     Running   0          5m38s   10.42.1.7    kubeworker.cloud.local   <none>           <none>
nginx-server-75c79dc96f-kp4cr      1/1     Running   0          5m38s   10.42.1.8    kubeworker.cloud.local   <none>           <none>
nginx-server-75c79dc96f-rtb9j      1/1     Running   0          5m38s   10.42.0.22   kubemaster.cloud.local   <none>           <none>
nginx-server-75c79dc96f-rggqr      1/1     Running   0          5m38s   10.42.0.21   kubemaster.cloud.local   <none>           <none>
nginx-server-75c79dc96f-ms6qs      1/1     Running   0          5m38s   10.42.0.20   kubemaster.cloud.local   <none>           <none>
nginx-server-75c79dc96f-9hfz5      1/1     Running   0          5m38s   10.42.0.19   kubemaster.cloud.local   <none>           <none>

Decrease the number of replicas

[root@kubemaster k8s]# kubectl scale deployment nginx-server --replicas=3
deployment.apps/nginx-server scaled
[root@kubemaster k8s]# kubectl get pods -o wide
NAME                               READY   STATUS    RESTARTS   AGE    IP          NODE                     NOMINATED NODE   READINESS GATES
nginx-server-75c79dc96f-ch4bn      1/1     Running   0          15m    10.42.1.6   kubeworker.cloud.local   <none>           <none>
nginx-server-75c79dc96f-zg8h2      1/1     Running   0          9m3s   10.42.1.9   kubeworker.cloud.local   <none>           <none>
nginx-server-75c79dc96f-kp4cr      1/1     Running   0          9m3s   10.42.1.8   kubeworker.cloud.local   <none>           <none>

Upgrade:

[root@kubemaster k8s]# kubectl set image deployment/nginx-server nginx=nginx:1.17.6-alpine
deployment.apps/nginx-server image updated

Check the update status:
[root@kubemaster k8s]# kubectl get pods -o wide
NAME                               READY   STATUS              RESTARTS   AGE    IP          NODE                     NOMINATED NODE   READINESS GATES
nginx-server-75c79dc96f-ch4bn      1/1     Running             0          112m   10.42.1.6   kubeworker.cloud.local   <none>           <none>
nginx-server-75c79dc96f-zg8h2      1/1     Running             0          106m   10.42.1.9   kubeworker.cloud.local   <none>           <none>
nginx-server-75c79dc96f-kp4cr      1/1     Running             0          106m   10.42.1.8   kubeworker.cloud.local   <none>           <none>
nginx-server-f84985557-t5jbf       0/1     ContainerCreating   0          42s    <none>      kubemaster.cloud.local   <none>           <none>
[root@kubemaster k8s]# kubectl get pods -o wide
NAME                               READY   STATUS    RESTARTS   AGE   IP           NODE                     NOMINATED NODE   READINESS GATES
nginx-server-f84985557-t5jbf       1/1     Running   0          24m   10.42.0.24   kubemaster.cloud.local   <none>           <none>
nginx-server-f84985557-wzsgl       1/1     Running   0          23m   10.42.1.12   kubeworker.cloud.local   <none>           <none>
nginx-server-f84985557-rw4nq       1/1     Running   0          22m   10.42.1.13   kubeworker.cloud.local   <none>           <none>

You can manually check the application version:
[root@kubemaster k8s]# kubectl describe pod nginx-server-f84985557-rw4nq
Name:         nginx-server-f84985557-rw4nq
Namespace:    default
Priority:     0
Node:         kubeworker.cloud.local/192.168.100.6
Start Time:   Tue, 31 Dec 2019 21:24:49 +0200
Labels:       app=nginx
              pod-template-hash=f84985557
Annotations:  <none>
Status:       Running
IP:           10.42.1.13
IPs:
  IP:           10.42.1.13
Controlled By:  ReplicaSet/nginx-server-f84985557
Containers:
  nginx:
    Container ID:   containerd://6d1a999ed7bbe479bfe587c75d286b11a227f0ac8477ca76d54720aaae43a18b
    Image:          nginx:1.17.6-alpine
    Image ID:       docker.io/library/nginx@sha256:0e61b143db3110f3b8ae29a67f107d5536b71a7c1f10afb14d4228711fc65a13
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Tue, 31 Dec 2019 21:24:49 +0200
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-d4t58 (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-d4t58:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-d4t58
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type    Reason   Age   From                             Message
  ----    ------   ----  ----                             -------
  Normal  Pulled   60m   kubelet, kubeworker.cloud.local  Container image "nginx:1.17.6-alpine" already present on machine
  Normal  Created  60m   kubelet, kubeworker.cloud.local  Created container nginx
  Normal  Started  60m   kubelet, kubeworker.cloud.local  Started container nginx

Kubernetes Services

Configure a test service:



~/nginx-service.yaml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
  labels:
    run: nginx
spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: 80
    protocol: TCP
    name: http
  selector:
    app: nginx


Permalink

Create the service:
[root@kubemaster k8s]# kubectl create -f nginx-service.yaml
service/nginx-service created

Check the status of the new service:
[root@kubemaster k8s]# kubectl get services
NAME              TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE
kubernetes        ClusterIP   10.43.0.1     <none>        443/TCP        34d
nginx-service     NodePort    10.43.3.116   <none>        80:30582/TCP   25s

Test the service:
[root@kubemaster k8s]# curl http://192.168.100.5:30582/
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

================================================================
Good Luck https://www.linkedin.com/in/ahmedms/

Comments

Popular posts from this blog

Kubernetes 104: Create a 2-node k3s cluster with k3sup

DevNet 103: Network Automation Using Python

How to configure OSPF on Palo Alto Networks Firewall?