/kubernetes-java-sample

Kubernetes Hands-on Workshop

Primary LanguageJavaApache License 2.0Apache-2.0

Kubernetes for Java Developers

Create Kubernetes Cluster

Create Resources

Pod

Using CLI

  1. Create pod: kubectl run wildfly --image=jboss/wildfly --port=8080

  2. Check status: kubectl get -w pods

    1. Show the pod name derived from the Deployment name

  3. Describe pod: kubectl describe pod <pod-name> or kubectl describe pod/<pod-name>

  4. Get logs: kubectl logs <pod-name>

    1. Use -f to tail logs

  5. Get deployments: kubectl get deployments

  6. Delete deployments: kubectl delete deployments/wildfly

  7. Get deployments: kubectl get deployments

  8. Get pods: kubectl get pods

Using Configuration File

  1. Create pod: kubectl create -f wildfly-pod.yaml

  2. Get pods: kubectl get pods

    1. Wide: kubectl get pods -o wide

    2. YAML: kubectl get pods -o yaml

    3. JSON: kubectl get pods -o json

  3. Describe pod: kubectl describe pod/wildfly-pod

  4. Delete pod: kubectl delete pod/wildfly-pod

  5. Get pods: kubectl get pods

Replication Controller

  1. Create RC: kubectl create -f wildfly-rc.yaml

  2. Get RC: kubectl get rc

  3. Get pods: kubectl get pods

    1. Show pod names

    2. Show image id downloaded on the second host

  4. Get pods created by RC: kubectl describe pods wildfly-rc

  5. Get pods with a specified label: kubectl get pods -l name=wildfly

  6. Delete RC: kubectl delete rc wildfly-rc

Service

  1. Create Service: kubectl create -f wildfly-service.yaml

  2. Get RC, Service and Pods: kubectl get rc,service,pods

  3. Describe service: kubectl describe service wildfly-service

  4. If Kubernetes is on cloud with LB

    1. Get the value of LoadBalancer Ingress, access the WildFly landing page at <IP>:8080

  5. Delete multiple resources: kubectl delete rc/wildfly-rc service/wildfly-service

Load Balancing

  1. ClusterIP: This is default, exposes service on cluster-internal IP.

    1. Create: kubectl create -f lb-clusterip.yaml

    2. Describe: kubectl get svc

      NAME              CLUSTER-IP    EXTERNAL-IP   PORT(S)    AGE
      kubernetes        100.64.0.1    <none>        443/TCP    48m
      wildfly-service   100.71.85.1   <none>        8080/TCP   13m

      Details: kubectl describe svc wildfly-service

      kubectl describe svc wildfly-service
      Name:			wildfly-service
      Namespace:		default
      Labels:			<none>
      Annotations:		<none>
      Selector:		app=wildfly-rc-pod
      Type:			ClusterIP
      IP:			100.71.85.1
      Port:			web	8080/TCP
      Endpoints:		100.96.1.4:8080,100.96.2.3:8080
      Session Affinity:	None
      Events:			<none>
    3. Access: Service is accessible only inside the cluster.

      1. Expose the service: kubectl expose service wildfly-service --port=8080 --target-port=8080 --name=web

      2. Start Kubernetes proxy: kubectl proxy

      3. Access the service: curl http://localhost:8001/api/v1/proxy/namespaces/default/services/web/index.html

    4. Delete:

      kubectl delete -f lb-clusterip.yaml
      kubectl delete svc/web
  2. NodePort: Expose service on each node of the cluster at a static port.

    1. Create: kubectl create -f lb-nodeport.yaml

    2. Describe: kubectl get svc

      NAME              CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
      kubernetes        100.64.0.1      <none>        443/TCP          2h
      wildfly-service   100.68.222.70   <nodes>       8080:32233/TCP   29m

      Details: kubectl describe svc wildfly-service

      Name:			wildfly-service
      Namespace:		default
      Labels:			<none>
      Annotations:		<none>
      Selector:		app=wildfly-rc-pod
      Type:			NodePort
      IP:			100.68.222.70
      Port:			web	8080/TCP
      NodePort:		web	32233/TCP
      Endpoints:		100.96.1.13:8080,100.96.2.12:8080
      Session Affinity:	None
      Events:			<none>
    3. Access: Service is accessible using <NodeIP>:<NodePort>. <NodePort> for us is 32233.

      1. Configure the worker node firewall to create a Custom TCP Rule to allow port 32233 accessible from Anywhere.

      2. Find IP address of the worker nodes using AWS Console.

      3. Service is accessible at <worker-node-ip>:32233.

    4. Delete: kubectl delete -f lb-nodeport.yaml

  3. LoadBalancer: Expose the service using a cloud provider’s load balancer.

    1. Create: kubectl create -f lb-loadbalancer.yaml

    2. Describe: kubectl get svc

      NAME              CLUSTER-IP       EXTERNAL-IP        PORT(S)          AGE
      kubernetes        100.64.0.1       <none>             443/TCP          2h
      wildfly-service   100.65.225.120   afa8056b14fc9...   8080:30229/TCP   4s

      Details:

      Name:			wildfly-service
      Namespace:		default
      Labels:			<none>
      Annotations:		<none>
      Selector:		app=wildfly-rc-pod
      Type:			LoadBalancer
      IP:			100.65.225.120
      LoadBalancer Ingress:	afa8056b14fc911e79b1906d8a9d4b8c-1413998286.us-west-2.elb.amazonaws.com
      Port:			web	8080/TCP
      NodePort:		web	30229/TCP
      Endpoints:		100.96.1.14:8080,100.96.2.13:8080
      Session Affinity:	None
      Events:
        FirstSeen	LastSeen	Count	From			SubObjectPath	Type		Reason			Message
        ---------	--------	-----	----			-------------	--------	------			-------
        11s		11s		1	service-controller			Normal		CreatingLoadBalancer	Creating load balancer
        8s		8s		1	service-controller			Normal		CreatedLoadBalancer	Created load balancer
    3. Access: Service is accessible at <LoadBalancer-Ingress>:8080. Wait for 3 mins for the load balancer to settle before accessing this URL. Firewall rules are created during the service creation.

    4. Delete: kubectl delete -f lb-loadbalancer.yaml

  4. ExternalName: Returns a CNAME record to an external service running outside the cluster. Allows for pods within the cluster to access the service outside the cluster. Redirection happens at DNS level, with no proxying or forwarding.

    1. Create

      1. Start a WildFly bitnami stack

      2. Get IP address of the provisioned host and replace the value in lb-external.yaml

      3. kubectl create -f lb-external.yaml

    2. Describe: kubectl get svc:

      NAME              CLUSTER-IP   EXTERNAL-IP                                         PORT(S)   AGE
      kubernetes        100.64.0.1   <none>                                              443/TCP   3h
      wildfly-service                ec2-34-210-79-105.us-west-2.compute.amazonaws.com   8080/TCP  2s

      Details: kubectl describe svc/wildfly-service

      Name:			wildfly-service
      Namespace:		default
      Labels:			<none>
      Annotations:		<none>
      Selector:		<none>
      Type:			ExternalName
      IP:
      External Name:		ec2-34-210-79-105.us-west-2.compute.amazonaws.com
      Port:			web	8080/TCP
      Endpoints:		<none>
      Session Affinity:	None
      Events:			<none>
    3. Access: This service is only accessible inside the cluster. kubectl expose only work for services with selectors.

    4. Delete: kubectl delete -f lb-external.yaml

Using Maven (Service + Replication Controller + Client Pod)

All the code is in maven directory:

  1. Create Couchbase service: kubectl create -f couchbase-service.yml

  2. Check status: kubectl get -w pods

  3. Run application: kubectl create -f bootiful-couchbase.yml

  4. Check status: kubectl get -w pods

    1. Show ContainerCreating

  5. Show all pods: kubectl get pods --show-all

  6. Check logs: kubectl logs -f <pod-name> to show the output Book{isbn=978-1-4919-1889-0, name=Minecraft Modding with Forge, cost=29.99}

  7. Delete all resources: kubectl delete -f couchbase-service.yml -f bootiful-couchbase.yml

Rolling Updates

All code in rolling-update directory:

Namespaces

  1. Create a new namespace: kubectl create -f dev-namespace.yaml

  2. Get namespaces: kubectl get namespace

  3. Create a new deployment in the namespace: kubectl --namespace=development run couchbase --image=arungupta/couchbase

  4. List deployments: kubectl get deployments

    1. No deployments shown

  5. List all resources in the namespace: kubectl get deployments --namespace=development

  6. List all resources in all namespaces: kubectl get deployments --all-namespaces

  7. Show pods in the namespaces: kubectl get pods --all-namespaces

Quota (broken)

  1. Create a constrained resource: kubectl create -f quota-wildfly.yaml

  2. Check for pods: kubectl get -w pods

  3. Broken: kubernetes/kubernetes#33621

Run-once/Batch Jobs

  1. Create a job: kubectl create -f runonce-job.yaml

  2. Check jobs: kubectl get jobs

  3. More details about job: kubectl describe jobs wait

  4. Check pods: kubectl get pods

  5. Show all completed pods: kubectl get pods --show-all

Couchbase Cluster

Daemon Set (work in progress)

  1. Create a daemon set: kubectl create -f prometheus-dameonset.yml .

Tips

  1. Create resources in all .json, .yaml and .yml files in dir: kubectl create -f ./dir