brew update
brew install hyperkit
brew install minikube (It has kubectl as
dependency)
To check
kubectl
and
minikube
minikube start (for starting)
or
minikube start --vm-driver=hyperkit
To get the status of the nodes
kubectl get nodes
minikube status
kubectl version
Basic kubectl commands
kubectl get nodes
kubectl get pod
kubectl get services
nginx deployment
kubectl create deployment nginx-depl --
image=nginx
kubectl get deployment (check name 1)
kubectl get replicaset (check name 2)
kubectl get pod (check name 3)
Let's make a change in deployment
kubectl edit deployment nginx-depl
In the file scroll down to image and
change to nginx:1.16 and save it
kubectl get pod
kubectl get replicaset
Debug
kubectl logs <pod name>
kubectl create deployment mongo-depl --
image=mongo
kubectl get pod
kubectl logs <pod name>
For additional info
kubectl describe pod <pod name>
kubectl get pod
kubectl logs <pod name> (for mongodb)
kubectl get pod
kubectl exec -it <pod name> -- bin/bash
exit
Delete Deployment
kubectl get deployment
kubectl get pod
kubectl delete deployment mongo-depl
kubectl get pod
kubectl get replicaset
kubectl delete deployment nginx-depl
kubectl get replicaset
To apply config file to create deployment
kubectl apply -f config-file.yaml
kubectl apply -f nginx-deployment.yaml
vi nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.16
ports:
- containerPort: 80
kubectl apply -f nginx-deployment.yaml
Kubernetes nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.16
ports:
- containerPort: 8080
Kubernetes nginx-service.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 8080
kubectl apply -f nginx-deployment.yaml
kubectl apply -f nginx-service.yaml
kubectl get pod
kubectl get service
kubectl describe service nginx-service
kubectl get pod -o wide (-o for output wide - for
more info like endpoints)
Status automatically generated
kubectl get deployment nginx-deployment -o yaml
kubectl get deployment nginx-deployment -o yaml
> nginx-deployment-result.yaml
Check the generated file
vi nginx-deployment-result.yaml
kubectl delete -f nginx-deployment.yaml
kubectl delete -f mongo.yaml
Application Example
kubectl get all
Create mongo.yaml file
apiVersion: apps/v1
kind: Deployment
metadata:
name: mongodb-deployment
labels:
app: nginx
spec:
replicas: 1
selector:
matchLabels:
app: mongodb
template:
metadata:
labels:
app: mongodb
spec:
containers:
- name: mongodb
image: mongo
ports:
- containerPort: 27017
env:
- name: MONGO_INITDB_ROOT_USERNAME
value:
- name: MONGO_INITDB_ROOT_PASSWORD
value:
Create mongo-secret.yaml
apiVersion: v1
kind: Secret
metadata:
name: mongodb-secret
type: Opaque
data:
mongo-root-username:
mongo-root-password:
Now username and password are encoded. So open terminal (bash)
(Also show the erro case)
echo -n 'username' | base64
Copy the generated value and paste it
echo -n 'password' | base64
Copy the generated value and paste it
apiVersion: v1
kind: Secret
metadata:
name: mongodb-secret
type: Opaque
data:
mongo-root-username: dXNlcm5hbWU=
mongo-root-password: cGFzc293cmQ=
(Password from bash prompt)
kubectl apply -f mongo-secret.yaml
kubectl get secret
The secret object is created in K8s
Now in mongo.yaml file
apiVersion: apps/v1
kind: Deployment
metadata:
name: mongodb-deployment
labels:
app: mongodb
spec:
replicas: 1
selector:
matchLabels:
app: mongodb
template:
metadata:
labels:
app: mongodb
spec:
containers:
- name: mongodb
image: mongo
ports:
- containerPort: 27017
env:
- name: MONGO_INITDB_ROOT_USERNAME
valueFrom:
secretKeyRef:
name: mongodb-secret
key: mongo-root-username
- name: MONGO_INITDB_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mongodb-secret
key: mongo-root-password
kubectl apply -f mongo.yaml
kubectl get pod
kubectl get pod --watch (to watch container creation)
kubectl describe pod <podname>
kubectl get all
mongo2.yaml with service
apiVersion: apps/v1
kind: Deployment
metadata:
name: mongodb-deployment
labels:
app: mongodb
spec:
replicas: 1
selector:
matchLabels:
app: mongodb
template:
metadata:
labels:
app: mongodb
spec:
containers:
- name: mongodb
image: mongo
ports:
- containerPort: 27017
env:
- name: MONGO_INITDB_ROOT_USERNAME
valueFrom:
secretKeyRef:
name: mongodb-secret
key: mongo-root-username
- name: MONGO_INITDB_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mongodb-secret
key: mongo-root-password
---
apiVersion: v1
kind: Service
metadata:
name: mongodb-service
spec:
selector:
app: mongodb
ports:
- protocol: TCP
port: 27017
targetPort: 27017
targetPort is container port and port is Service port
If editing the same mongo.yaml file
kubectl apply -f mongo.yaml
kubectl delete -f mongo.yaml
kubectl apply -f mongo2.yaml
kubectl get service
kubectl describe service mongodb-service
kubectl get pod -o wide
kubectl get all | grep mongodb
For mongo-express
First make mongo-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: mongodb-configmap
data:
database_url: mongodb-service
database_url in server is actually the name of the service
(that is going to be database server URL)
Make mongo-express.yaml file
apiVersion: apps/v1
kind: Deployment
metadata:
name: mongo-express
labels:
app: mongo-express
spec:
replicas: 1
selector:
matchLabels:
app: mongo-express
template:
metadata:
labels:
app: mongo-express
spec:
containers:
- name: mongo-express
image: mongo-express
ports:
- containerPort: 8081
env:
- name: ME_CONFIG_MONGODB_ADMINUSERNAME
valueFrom:
secretKeyRef:
name: mongodb-secret
key: mongo-root-username
- name: ME_CONFIG_MONGODB_ADMINPASSWORD
valueFrom:
secretKeyRef:
name: mongodb-secret
key: mongo-root-password
- name: ME_CONFIG_MONGODB_SERVER
valueFrom:
configMapKeyRef:
name: mongodb-configmap
key: database_url
kubectl apply -f mongo-configmap.yaml
kubectl apply -f mongo-express.yaml
kubectl get pod
kubectl logs <mongo-express-pod>
Now we need external service to connect to mongo-express
Again edit mongo-express.yaml file
apiVersion: apps/v1
kind: Deployment
metadata:
name: mongo-express
labels:
app: mongo-express
spec:
replicas: 1
selector:
matchLabels:
app: mongo-express
template:
metadata:
labels:
app: mongo-express
spec:
containers:
- name: mongo-express
image: mongo-express
ports:
- containerPort: 8081
env:
- name: ME_CONFIG_MONGODB_ADMINUSERNAME
valueFrom:
secretKeyRef:
name: mongodb-secret
key: mongo-root-username
- name: ME_CONFIG_MONGODB_ADMINPASSWORD
valueFrom:
secretKeyRef:
name: mongodb-secret
key: mongo-root-password
- name: ME_CONFIG_MONGODB_SERVER
valueFrom:
configMapKeyRef:
name: mongodb-configmap
key: database_url
---
apiVersion: v1
kind: Service
metadata:
name: mongo-express-service
spec:
selector:
app: mongo-express
type: LoadBalancer
ports:
- protocol: TCP
port: 8081
targetPort: 8081
nodePort: 30000
kubectl get service
It lists two mongo services. One from
mongo.yaml file (Internal service
(CLUSTERIP)) and we do not specify type
(beacause it is default) over there and
in mongo-express.yaml (External service)
we specified type: Loadbalancer (which is
actually misleading)
Difference - CLUSTERIP gives the service
internal IP address and LoadBalancer will
also give internal IP address but in
addition to that it also give an
external IP address to service (where the
external request be coming from) and here
it says <pending> because we are in
Minikube and it it little bit different
from regular
K8s setup (where you'll see IP address).
With Internal IP address you have port
for that IP address but for both Internal
and External IP address you have ports
for both of them and that's why we have
to define third port which is for
external IP address.
Now in minikube
minikube service mongo-express-service
(The above command will assign External
service a public IP address)
It will open up the browser window. You
can also use IP address for opening that
browser page
Also check
minikube dashboard
minikube addons enable dashboard
Namespace
kubectl get namespace
kubectl cluster-info
kubectl create namespace my-namespace
kubectl api-resources --namespaced=false
brew install kubectx
kubens
Change active namespace
kubens my-namespace
kubens
Ingress
minikube addons enable ingress
kubectl get pod -n kube-system
kubectl get ns (To see kubernetes dashboard in list you need
to run once kubernetes dashboard and then run the command
again or minikube addons enable dashboard )
Configuring an Ingress rule for dashboard so that we can
access it from browser using some domain name
kubectl get all -n kubernetes-dashboard
Creating Ingress for Kubernetes dashboard
Create dashboard-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: dashboard-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
rules:
- host: dashboard.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: kubernetes-dashboard
port:
number: 80
docker run -p 5000:5432 -e POSTGRES_PASSWORD=pwd postgres:9.6.17
docker run -p 5001:5432 -e POSTGRES_PASSWORD=pwd postgres:9.6.17
kubectl apply -f postgres.yaml
kubectl apply -f nginx-sidecar-container.yaml
kubectl exec -it nginx -c sidecar -- /bin/sh
netstat -ln. (Inside container)
curl localhost:80 (Inside container)
exit (Inside container)
kubectl logs nginx -c nginx-container
For pause container
docker ps
echo $(minikube docker-env)
eval $(minikube docker-env)
docker ps
docker ps | grep k8s_POD
docker ps | grep k8s_POD_nginx
(To unlink)
eval "$(docker-machine env -u)"
Play with Kubernetes
https://labs.play-with-k8s.com