kubeadm token create --print-join-command
read -p 'master ip address : ' ipaddr
sha_token = "$(openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //')"
token = "$(kubeadm token list | awk '{print $1}' | sed -n '2 p')"
echo "kubeadm join $ipaddr:6443 --token=$token --discovery-token-ca-cert-hash sha256:$sha_token"
kubectl api-resources | grep pods
watch kubectl get all -o wide
kubectl get nodes
kubectl get events
kubectl config view
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.7.0/aio/deploy/recommended.yaml
kubectl get services --all-namespaces
kubectl get all -n kubernetes-dashboard
kubectl port-forward -n kubernetes-dashboard service/kubernetes-dashboard 10443:443 --address 0.0.0.0
We will have to change the type of service from ClusterIp to NodePort. So, give the following command to edit the service and make the following changes.
kubectl edit service/kubernetes-dashboard -n kubernetes-dashboard
After: You can give the IP of your wish if 32321 is occupied
Now, check if the service was changed successfully by giving the following command:
kubectl get svc
It will open the Kubernetes dashboard in the web browser.
kubectl proxy --address='0.0.0.0' --disable-filter=true
kubectl proxy --address='0.0.0.0' --accept-hosts='^$'
kubectl proxy --address='0.0.0.0' --port=8001 --accept-hosts='^$'
kubectl -n kubernetes-dashboard describe service kubernetes-dashboard
kubectl create deployment nginx-web --image=nginx
kubectl expose deployment nginx-web --type NodePort --port=80
kubectl get deployment,pod,svc
nano nginx.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
kubectl apply -f nginx.yaml
Port-Forward Syntax
kubectl port-forward POD_NAME LOCAL_PORT:REMOTE_POD_PORT
Command Description
POD_NAME: Give our pod name.
LOCAL_PORT: Give a port which we access from the internet.
REMOTE_POD_PORT: Give our pods default port.
Example:
kubectl port-forward nginx-pod 8085:80 --address 0.0.0.0
kubectl port-forward nginx-pod 8085:80 &
kubectl port-forward nginx-pod 8085:80 --address 0.0.0.0 &
kubectl port-forward --address 192.168.0.100 my-pod 8080:80
kubectl port-forward --address 192.168.0.100 service/my-service 8080:80
kubectl port-forward pod/nginx :80
ps -ef | grep port-forward
netstat -antp
kubectl run nginx -–image nginx
kubectl port-forward nginx 8080:80
kubectl logs nginx
kubectl logs -f pod-name
kubectl run myshell -it –image busybox –sh
kubectl run myshell —rm –it –image busybox –sh
kubectl exec pod-name date
kubectl exec pod-name -c container-name date
kubectl exec -it pod-name /bin/bash
kubectl delete pod myportfolio
kubectl delete pods –all
kubectl delete pods,services -l name=label-name
kubectl describe pod nginx
kubectl create deployment nginx --image nginx:latest --replicas 3
kubectl get deployment
kubectl scale deployment nginx --replicas 5
kubectl delete deploy myshell
kubectl run myportfolio --image=myportfolio:latest --image-pull-policy=Never --restart=Never
kubectl expose deployment nginx –type NodePort –port 80
kubectl describe svc ngnix
kubectl get pods -o wide
kubectl create namespace devops-tool-suite
kubectl get storageclass
nano nginx-hostpath.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: nginxpv
spec:
accessModes:
- ReadWriteOnce
capacity:
storage: 1Gi
storageClassName: standard
hostPath:
path: /nfsstorage/nginxdata
---
#Create a Persistent Volume Claim
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: nginxpvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: standard
volumeName: nginxpv
---
#Attach PVCs to Pods
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx:latest
volumeMounts:
name: data
- mountPath: /usr/share/nginx/html
volumes:
- name: data
persistentVolumeClaim:
claimName: nginxpvc
kubectl create -f nginx-hostpath.yaml
kubectl get pv,pvc,pod
nano nginx-nfs.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: mynfspv
spec:
capacity:
storage: 2Gi
accessModes:
- ReadWriteOnce
nfs:
server: 192.168.0.96
path: /nfsstorage/jenkins_home
persistentVolumeReclaimPolicy: Retain
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mynfspvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 2Gi
---
#Attach PVCs to Pods
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx:latest
volumeMounts:
name: data
- mountPath: /usr/share/nginx/html
volumes:
- name: data
persistentVolumeClaim:
claimName: mynfspvc
kubectl create -f nginx-nfs.yaml
kubectl get pv,pvc,pod
nano jenkins-k8s.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myjenkins
spec:
replicas: 1
selector:
matchLabels:
app: myjenkins
template:
metadata:
labels:
app: myjenkins
spec:
containers:
- name: myjenkins
image: jenkins/jenkins:lts
ports:
- name: http-port
containerPort: 8080
- name: jnlp-port
containerPort: 50000
#command: ["sh", "-c", "chown -R 777:777 /var/jenkins_home"]
#command: ["sh", "-c", "chown root:root /var/jenkins_home"]
volumeMounts:
- name: jenkins-home
mountPath: /var/jenkins_home
volumes:
- name: jenkins-home
persistentVolumeClaim:
claimName: pvcnfs
kubectl create -f jenkins-k8s.yaml
kubectl get pod
kubectl get sc
kubectl describe sc StorageClass-Name
kubectl get pv
kubectl get pvc
kubectl describe pv pv-name
kubectl describe pvc pvc-name
kubectl delete sc sc-name
kubectl delete pvc pvc-name
kubectl delete pv pv-name
nano nginx-nfs.yaml
apiVersion: v1
kind: Pod
metadata:
name: web1
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
volumeMounts:
- mountPath: /usr/share/nginx/html
name: nginxstorage
volumes:
- name: nginxstorage
nfs:
server: 192.168.0.96
path: /nfs-share/kubernetes
nano pod-localimage.yaml
apiVersion: v1
kind: Pod
metadata:
name: web1
spec:
containers:
- name: myportfolio
image: myportfolio:latest
imagePullPolicy: Never
ports:
- containerPort: 80
volumeMounts:
- mountPath: /usr/share/nginx/html
name: nginxstorage
volumes:
- name: nginxstorage
nfs:
server: 192.168.0.96
path: /nfs-share/kubernetes