Useful kubectl commands

To get list of all kubernetes resources:

[email protected]:~/kube-deployments/java# kubectl get all
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
svc/javaservice 10.0.0.196 8000:30078/TCP 1h
svc/kubernetes 10.0.0.1 443/TCP 1d

To delete a service:
[email protected]:~/kube-deployments/java# kubectl delete service javaservice

To create pod, service or deployment from a yaml file:
[email protected]:~/kube-deployments/java# kubectl create -f javaservice.yaml
service "javaservice" created
deployment "javaservice" created

Contents of the deployment file used in previous command:

kind: Service
apiVersion: v1
metadata:
name: javaservice
spec:
selector:
app: javaservice
ports:
- protocol: "TCP"
port: 8000
targetPort: 8000
type: LoadBalancer
---
kind: Deployment
apiVersion: apps/v1beta1
metadata:
name: javaservice
spec:
replicas: 1
template:
metadata:
labels:
app: javaservice
spec:
containers:
- name: java
image: "java:javaservice"

If you are running the service on minikube, you will not be able to attain the external IP for the loadbalancer, instead you can get the service url by using the below command:
[email protected]:~/kube-deployments/java# minikube service javaservice --url
http://192.168.99.100:30891

Test the loadbalancing on the service with below commands:

[email protected]:~/kube-deployments/java# kubectl get all
NAME READY STATUS RESTARTS AGE
po/javaservice-3138962571-8qj66 1/1 Running 0 1m
po/javaservice-3138962571-ppmxn 1/1 Running 0 7s
po/javaservice-3138962571-qn12q 1/1 Running 0 7s

NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
svc/javaservice 10.0.0.156 8000:30597/TCP 1m
svc/kubernetes 10.0.0.1 443/TCP 1d

NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deploy/javaservice 3 3 3 3 1m

NAME DESIRED CURRENT READY AGE
rs/javaservice-3138962571 3 3 3 1m
[email protected]:~/kube-deployments/java# curl http://192.168.99.100:30597/gethostname
javaservice-3138962571-8qj66
[email protected]:~/kube-deployments/java# curl http://192.168.99.100:30597/gethostname
javaservice-3138962571-qn12q
[email protected]:~/kube-deployments/java# curl http://192.168.99.100:30597/gethostname
javaservice-3138962571-8qj66
[email protected]:~/kube-deployments/java# curl http://192.168.99.100:30597/gethostname
javaservice-3138962571-ppmxn
[email protected]:~/kube-deployments/java# curl http://192.168.99.100:30597/gethostname
javaservice-3138962571-qn12q
[email protected]:~/kube-deployments/java# curl http://192.168.99.100:30597/gethostname
javaservice-3138962571-ppmxn
[email protected]:~/kube-deployments/java# curl http://192.168.99.100:30597/gethostname
javaservice-3138962571-ppmxn
[email protected]:~/kube-deployments/java# curl http://192.168.99.100:30597/gethostname

How to generate jar file from Java class

You can generate jar file with three simple commands:

Firstly you need to compile the code that you have written with this command:

Generate the jar file with the class files that were generated with the previous command:

Note that if you don’t have the manifest file already you can create it simply by using the following content for the file:

Now you can test your jar file: