$ minikube dashboard
NOTE: Add the full URL in the Container Image field docker.io/library/nginx:alpine if any issues are encountered with the simple nginx:alpine image name (or use the k8s.gcr.io/nginx:alpine URL if it works instead)
$ kubectl get pods -L k8s-app,blabla
NAME READY STATUS RESTARTS AGE K8S-APP BLABLA
webserver-c8f4d5fbc-9j65j 1/1 Running 0 42m webserver
webserver-c8f4d5fbc-hm9xt 1/1 Running 0 42m webserver
webserver-c8f4d5fbc-mlzc9 1/1 Running 0 42m webserver
$ kubectl get pods -l k8s-app=webserver
NAME READY STATUS RESTARTS AGE
webserver-c8f4d5fbc-9j65j 1/1 Running 0 48m
webserver-c8f4d5fbc-hm9xt 1/1 Running 0 48m
webserver-c8f4d5fbc-mlzc9 1/1 Running 0 48m
$ kubectl get pods -l k8s-app=webserver1
No resources found in default namespace.
$ kubectl delete deploy webserver
deployment.apps "webserver" deleted
$ kubectl get replicasets
No resources found in default namespace.
$ kubectl get pods
No resources found in default namespace.
$ kubectl create deploy --help
Create a deployment with the specified name.
Aliases:
deployment, deploy
Examples:
# Create a new deployment named my-dep that runs the busybox image.
kubectl create deployment my-dep --image=busybox
Options:
--allow-missing-template-keys=true: If true, ignore any errors in templates when a field or map key is missing in
the template. Only applies to golang and jsonpath output formats.
--dry-run=false: If true, only print the object that would be sent, without sending it.
--generator='': The name of the API generator to use.
--image=[]: Image name to run.
-o, --output='': Output format. One of:
json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-file.
--save-config=false: If true, the configuration of current object will be saved in its annotation. Otherwise, the
annotation will be unchanged. This flag is useful when you want to perform kubectl apply on this object in the future.
--template='': Template string or path to template file to use when -o=go-template, -o=go-template-file. The
template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].
--validate=true: If true, use a schema to validate the input before sending it
Usage:
kubectl create deployment NAME --image=image [--dry-run] [options]
Use "kubectl options" for a list of global command-line options (applies to all commands).
$ kubectl create deploy webserver --image=nginx:alpine -o yaml --dry-run=true > webserver.yaml
$ kubectl get deploy
No resources found in default namespace.
$ cat webserver.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: webserver
name: webserver
spec:
replicas: 1
selector:
matchLabels:
app: webserver
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: webserver
spec:
containers:
- image: nginx:alpine
name: nginx
resources: {}
status: {}
$ cat webserver.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: nginx
name: webserver
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- image: nginx:alpine
name: nginx
ports:
- containerPort: 80
$ kubectl create -f webserver.yaml
deployment.apps/webserver created
$ kubectl get deploy,rs,pods
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/webserver 3/3 3 3 62s
NAME DESIRED CURRENT READY AGE
replicaset.apps/webserver-5c559d5697 3 3 3 62s
NAME READY STATUS RESTARTS AGE
pod/webserver-5c559d5697-7bz8p 1/1 Running 0 62s
pod/webserver-5c559d5697-c5ncg 1/1 Running 0 62s
pod/webserver-5c559d5697-rlb26 1/1 Running 0 62s
current state
Let's use the NodePort ServiceType while creating a Service.
ask for help
$ kubectl create service --help
Create a service using specified subcommand.
Aliases:
service, svc
Available Commands:
clusterip Create a ClusterIP service.
externalname Create an ExternalName service.
loadbalancer Create a LoadBalancer service.
nodeport Create a NodePort service.
Usage:
kubectl create service [flags] [options]
Use "kubectl <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all commands).
$ kubectl create service nodeport web-service --tcp=80 --dry-run=true -o yaml > webserver-svc.yaml
$ cat webserver-svc.yaml
apiVersion: v1
kind: Service
metadata:
labels:
run: web-service
name: web-service
spec:
ports:
- port: 80
protocol: TCP
selector:
app: nginx
type: NodePort
$ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4h44m
$ kubectl create -f webserver-svc.yaml
service/web-service created
$ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4h45m
web-service NodePort 10.100.52.43 <none> 80:30387/TCP 23s
$ kubectl expose deployment webserver --port=81 --target-port=80 --type=NodePort
$ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4h54m
web-service NodePort 10.100.52.43 <none> 80:30387/TCP 8m57s
webserver NodePort 10.107.217.161 <none> 81:31838/TCP 16s
$ kubectl describe service web-service
Name: web-service
Namespace: default
Labels: run=web-service
Annotations: <none>
Selector: app=nginx
Type: NodePort
IP: 10.100.52.43
Port: <unset> 80/TCP
TargetPort: 80/TCP
NodePort: <unset> 30387/TCP
Endpoints: 172.17.0.4:80,172.17.0.5:80,172.17.0.6:80
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
web-service uses app=nginx as a selector to logically group our three Pods, which are listed as endpoints.
When a request reaches our Service, it will be served by one of the Pods listed in the Endpoints section.
get Node ip
$ kubectl describe node minikube | grep IP
InternalIP: 192.168.99.101
$ kubectl describe services web-service | grep NodePort
Type: NodePort
NodePort: <unset> 30387/TCP
go to 192.168.99.101:30387
or we can find ip with minikube ip
$ minikube ip
192.168.99.101
$ minikube service web-service
|-----------|-------------|-------------|-----------------------------|
| NAMESPACE | NAME | TARGET PORT | URL |
|-----------|-------------|-------------|-----------------------------|
| default | web-service | | http://192.168.99.101:30387 |
|-----------|-------------|-------------|-----------------------------|
* Opening service default/web-service in default browser...
apiVersion: v1
kind: Pod
metadata:
labels:
test: liveness
name: liveness-exec
spec:
containers:
- name: liveness
image: k8s.gcr.io/busybox
args:
- /bin/sh
- -c
- touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600
livenessProbe:
exec:
command:
- cat
- /tmp/healthy
initialDelaySeconds: 5
periodSeconds: 5
$ cat exec-liveness.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
test: liveness
name: liveness-exec
spec:
containers:
- name: liveness
image: k8s.gcr.io/busybox
args:
- /bin/sh
- -c
- touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600
livenessProbe:
exec:
command:
- cat
- /tmp/healthy
initialDelaySeconds: 5
periodSeconds: 5
$ kubectl create -f exec-liveness.yaml
pod/liveness-exec created
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
liveness-exec 1/1 Running 0 6s
$ kubectl describe pod liveness-exec
Name: liveness-exec
Namespace: default
Priority: 0
Node: minikube/192.168.99.101
Start Time: Fri, 28 Feb 2020 16:39:56 +0700
Labels: test=liveness
Annotations: <none>
Status: Running
IP: 172.17.0.7
IPs:
IP: 172.17.0.7
Containers:
liveness:
Container ID: docker://7a4ace7f1b865ee7b8b06dd79c53f89cacdd7853c1a25ff97539f2b32e22cec2
Image: k8s.gcr.io/busybox
Image ID: docker-pullable://k8s.gcr.io/busybox@sha256:d8d3bc2c183ed2f9f10e7258f84971202325ee6011ba137112e01e30f206de67
Port: <none>
Host Port: <none>
Args:
/bin/sh
-c
touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600
State: Running
Started: Fri, 28 Feb 2020 16:42:29 +0700
Last State: Terminated
Reason: Error
Exit Code: 137
Started: Fri, 28 Feb 2020 16:41:14 +0700
Finished: Fri, 28 Feb 2020 16:42:28 +0700
Ready: True
Restart Count: 2
Liveness: exec [cat /tmp/healthy] delay=5s timeout=1s period=5s #success=1 #failure=3
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-42lts (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-42lts:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-42lts
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 2m48s default-scheduler Successfully assigned default/liveness-exec to minikube
Warning Unhealthy 46s (x6 over 2m11s) kubelet, minikube Liveness probe failed: cat: can't open '/tmp/healthy': No such file or directory
Normal Killing 46s (x2 over 2m1s) kubelet, minikube Container liveness failed liveness probe, will be restarted
Normal Pulling 16s (x3 over 2m47s) kubelet, minikube Pulling image "k8s.gcr.io/busybox"
Normal Pulled 15s (x3 over 2m46s) kubelet, minikube Successfully pulled image "k8s.gcr.io/busybox"
Normal Created 15s (x3 over 2m45s) kubelet, minikube Created container liveness
Normal Started 15s (x3 over 2m45s) kubelet, minikube Started container liveness
$ kubectl delete pod liveness-exec
pod "liveness-exec" deleted
livenessProbe:
httpGet:
path: /healthz
port: 8080
httpHeaders:
- name: X-Custom-Header
value: Awesome
initialDelaySeconds: 3
periodSeconds: 3
livenessProbe:
tcpSocket:
port: 8080
initialDelaySeconds: 15
periodSeconds: 20
readinessProbe:
exec:
command:
- cat
- /tmp/healthy
initialDelaySeconds: 5
periodSeconds: 5