kubernetes Practice 2

1. Sample message boards deployed on Kubernetes ...
1. Sample message boards deployed on Kubernetes
1.2 Create rc
2. Network Introduction

1. Sample message boards deployed on Kubernetes

This scenario illustrates how to use Kubernetes and Docker to launch a simple, multitier Web application. The Guestbook sample application stores visitor's notes in Redis through JavaScript API calls. Redis contains a master (for storage) and a set of replicated redis'slaves'.

1.1 Check Cluster

controlplane $ kubectl cluster-info Kubernetes master is running at https://172.17.0.29:6443 KubeDNS is running at https://172.17.0.29:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'. controlplane $ kubectl get nodes NAME STATUS ROLES AGE VERSION controlplane Ready master 2m57s v1.14.0 node01 Ready <none> 2m31s v1.14.0

1.2 Create rc

controlplane $ cat redis-master-controller.yaml apiVersion: v1 kind: ReplicationController metadata: name: redis-master labels: name: redis-master spec: replicas: 1 selector: name: redis-master template: metadata: labels: name: redis-master spec: containers: - name: master image: redis:3.0.7-alpine ports: - containerPort: 6379

Establish

controlplane $ kubectl create -f redis-master-controller.yaml replicationcontroller/redis-master created controlplane $ kubectl get rc NAME DESIRED CURRENT READY AGE redis-master 1 1 0 2s controlplane $ kubectl get pods NAME READY STATUS RESTARTS AGE redis-master-2j4qm 1/1 Running 0 4s

1.3 Redis Master Service

The second part is service. The Kubernetes service is a named load balancer that proxies traffic to one or more containers. The proxy works even if the container is on a different node.

Service proxies communicate within clusters and rarely expose ports to external interfaces.

When you start the service, it appears that you cannot connect using curl or netcat unless you start it as part of Kubernetes. The recommended method is to use the LoadBalancer service to handle external communications.]

controlplane $ cat redis-master-service.yaml apiVersion: v1 kind: Service metadata: name: redis-master labels: name: redis-master spec: ports: # the port that this service should serve on - port: 6379 targetPort: 6379 selector: name: redis-master

Establish

controlplane $ kubectl create -f redis-master-service.yaml service/redis-master created controlplane $ kubectl get services NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 6m58s redis-master ClusterIP 10.111.64.45 <none> 6379/TCP 1s controlplane $ kubectl describe services redis-master Name: redis-master Namespace: default Labels: name=redis-master Annotations: <none> Selector: name=redis-master Type: ClusterIP IP: 10.111.64.45 Port: <unset> 6379/TCP TargetPort: 6379/TCP Endpoints: 10.32.0.193:6379 Session Affinity: None Events: <none>

1.4 rc slave Pod

In this example, we will run Redis Slaves, which will copy data from the master. For more details on Redis replication, visit http://redis.io/topics/replication

As mentioned earlier, the controller defines how the service operates. In this example, we need to determine how the service discovers other pod s. YAML will GET_ HOSTS_ The FROM attribute is represented as DNS. You can change it to use environment variables in yaml, but this introduces a creation order dependency because you need to run the service to define environment variables.
In this case, we will use image:kubernetes/redis-slave:v2 to launch two instances of pod. It will link to redis-master through DNS.

controlplane $ cat redis-slave-controller.yaml apiVersion: v1 kind: ReplicationController metadata: name: redis-slave labels: name: redis-slave spec: replicas: 2 selector: name: redis-slave template: metadata: labels: name: redis-slave spec: containers: - name: worker image: gcr.io/google_samples/gb-redisslave:v1 env: - name: GET_HOSTS_FROM value: dns # If your cluster config does not include a dns service, then to # instead access an environment variable to find the master # service's host, comment out the 'value: dns' line above, and # uncomment the line below. # value: env ports: - containerPort: 6379

Execution:

controlplane $ kubectl create -f redis-slave-controller.yaml replicationcontroller/redis-slave created controlplane $ kubectl get rc NAME DESIRED CURRENT READY AGE redis-master 1 1 1 4m29s redis-slave 2 2 2 3s

1.5 Redis slave service

As before, we need to make incoming requests accessible to our slaves. This is done by starting a service that knows how to communicate with redis-slave.

Since we have two replicated pods, the service will also provide load balancing between the two nodes.

controlplane $ cat redis-slave-service.yaml apiVersion: v1 kind: Service metadata: name: redis-slave labels: name: redis-slave spec: ports: # the port that this service should serve on - port: 6379 selector: name: redis-slave

Execution:

controlplane $ kubectl create -f redis-slave-service.yaml controlplane $ kubectl get services NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 14m redis-master ClusterIP 10.111.64.45 <none> 6379/TCP 7m13s redis-slave ClusterIP 10.109.135.21 <none> 6379/TCP 41s

1.6 Front End rc

Now that the data service is started, we can deploy the Web application. The pattern for deploying a Web application is the same as the pod we deployed earlier. YAML defines a service named frontend that uses images_ gcr.io/google samples/gb-frontend:v3. The replication controller ensures that the three Pods always exist.

controlplane $ cat frontend-controller.yaml apiVersion: v1 kind: ReplicationController metadata: name: frontend labels: name: frontend spec: replicas: 3 selector: name: frontend template: metadata: labels: name: frontend spec: containers: - name: php-redis image: gcr.io/google_samples/gb-frontend:v3 env: - name: GET_HOSTS_FROM value: dns # If your cluster config does not include a dns service, then to # instead access environment variables to find service host # info, comment out the 'value: dns' line above, and uncomment the # line below. # value: env ports: - containerPort: 80

implement

controlplane $ kubectl create -f frontend-controller.yaml replicationcontroller/frontend created controlplane $ kubectl get rc NAME DESIRED CURRENT READY AGE frontend 3 3 1 2s redis-master 1 1 1 20m redis-slave 2 2 2 15m controlplane $ kubectl get pods NAME READY STATUS RESTARTS AGE frontend-bkcsj 1/1 Running 0 3s frontend-ftjrk 1/1 Running 0 3s frontend-jnckp 1/1 Running 0 3s redis-master-2j4qm 1/1 Running 0 20m redis-slave-79w2b 1/1 Running 0 15m redis-slave-j8zqj 1/1 Running 0 15m

PHP code uses HTTP and JSON to communicate with Redis. When a value is set, the request goes to redis-master, and the data read comes from the redis-slave node.

1.7 Guestbook Frontend Service

In order for the front end to be accessible, we need to start a service to configure the proxy.
YAML defines a service as NodePort. NodePort allows you to set well-known ports that are shared across the cluster. This is like -p 80:80 in Docker.

In this case, we define our Web application to run on port 80, but we will expose the service on 30080.

controlplane $ cat frontend-service.yaml apiVersion: v1 kind: Service metadata: name: frontend labels: name: frontend spec: # if your cluster supports it, uncomment the following to automatically create # an external load-balanced IP for the frontend service. # type: LoadBalancer type: NodePort ports: # the port that this service should serve on - port: 80 nodePort: 30080 selector: name: frontend controlplane $ kubectl create -f frontend-service.yaml service/frontend created controlplane $ kubectl get services NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE frontend NodePort 10.105.214.152 <none> 80:30080/TCP 2s kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 28m redis-master ClusterIP 10.111.64.45 <none> 6379/TCP 21m redis-slave ClusterIP 10.109.135.21 <none> 6379/TCP 15m

1.8 Access Guestbook Frontend

Once all the controllers and services are defined, Kubernetes will start starting them as Pods. Depending on what happens, Pods can have different states. For example, if the Docker image is still downloading, the Pod will be suspended because it cannot start. When ready, the status changes to running.

View Pod Status

controlplane $ kubectl get pods NAME READY STATUS RESTARTS AGE frontend-bkcsj 1/1 Running 0 4m55s frontend-ftjrk 1/1 Running 0 4m55s frontend-jnckp 1/1 Running 0 4m55s redis-master-2j4qm 1/1 Running 0 24m redis-slave-79w2b 1/1 Running 0 20m redis-slave-j8zqj 1/1 Running 0 20m

Find Node Port

controlplane $ kubectl describe service frontend | grep NodePort Type: NodePort NodePort: <unset> 30080/TCP

View user interface
Once Pod is running, you will be able to view the UI through port 30080. Use URL to view page https://2886795293-30080-elsy05.environments.katacoda.com

Behind the scenes, the PHP service discovers Redis instances through DNS. You have now deployed an effective multi-tier application on Kubernetes.

2. Network Introduction

Kubernetes has advanced network capabilities that allow Pod and services to communicate within and outside the cluster network.

In this scenario, you'll learn the following types of Kubernetes services.

  • Cluster IP
  • Target Port
  • Node Port
  • External IP
  • Load Balancer

The Kubernetes Service is an abstraction that defines how to access a set of Pods. The Pod set accessed through the Service is based on the label selector.

2.1 Cluster IP

7 November 2021, 11:45 | Views: 8005

Add new comment

For adding a comment, please log in
or create account

0 comments