Skip to content

Instantly share code, notes, and snippets.

@dmilan77
Last active August 8, 2019 11:10
Show Gist options
  • Save dmilan77/8139ffc7ccad9673b99e0a53b9fecd56 to your computer and use it in GitHub Desktop.
Save dmilan77/8139ffc7ccad9673b99e0a53b9fecd56 to your computer and use it in GitHub Desktop.
2b. Kubernetes Labs

Inspect the cluster

kubectl config view
kubectl cluster-info

Create your first pod and expose**

export DEPLOYMENT_NAME="nginx-kubeatl"
kubectl run ${DEPLOYMENT_NAME} --image nginx:1.17.2-alpine 
kubectl get pods
kubectl get pods -o json
export my_first_pod=$(kubectl get pods -o json |jq -r '.items[0].metadata.name')
echo $my_first_pod
kubectl describe pod $my_first_pod
echo "hello atlanta" > /tmp/ping.html
kubectl cp /tmp/ping.html $my_first_pod:/usr/share/nginx/html/ping.html

kubectl expose pod $my_first_pod --port 80 --type LoadBalancer --name ${DEPLOYMENT_NAME}-svc
kubectl get svc
curl http://{{loadbalancer ip}}/ping.html

Clean up

kubectl get svc
kubectl delete pods nginx-1-84b66c9fcf-h7dsj
# Obserrve the pods are getting auto created
kubectl get deployments
kubectl delete deployment nginx-kubeatl
kubectl delete svc nginx-kubeatl-svc

Create your first yaml and create deployment and services

# Create deployment file
cat <<EOF>> nginx-deployment-ClusterIP.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-yaml-deployment
  labels:
    app: nginx-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx-app
  template:
    metadata:
      labels:
        app: nginx-app
    spec:
      containers:
      - name: nginx-app
        image: nginx:1.17.2-alpine
        ports:
        - containerPort: 80
EOF
# Apply deployment
kubectl apply -f nginx-deployment-ClusterIP.yaml
# Check Deployment
kubectl get deployment

Create Default Service (ClusterIP)

# Default Services not accessible outside cluster
cat <<EOF>> nginx-yaml-service-clusterip.yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx-yaml-service-clusterip
spec:
  selector:
    app: nginx-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
EOF
# Apply service
kubectl apply -f nginx-yaml-service-clusterip.yaml
# Check service
kubectl get deployment

Test with Alpine image

kubectl run alpine-kubeatl --image=dmilan/alpine-plus -i --tty --rm
curl <<clusterip of nginx>>

Create Default Service (Loadbalancer)

# Default Services not accessible outside cluster
cat <<EOF>> nginx-yaml-service-loadbalancer.yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx-yaml-loadbalancer-service
spec:
  selector:
    app: nginx-app
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
  type: LoadBalancer
EOF
# Apply service
kubectl apply -f nginx-yaml-service-loadbalancer.yaml
# Check service
kubectl get deployment

Clean up

kubectl delete svc nginx-yaml-loadbalancer-service
kubectl delete svc nginx-yaml-service-clusterip
kubectl delete deployment nginx-yaml-deployment

Deploy Sample Rest App

git clone https://github.com/dmilan77/docker-sample-restapp-flask.git
cd docker-sample-restapp-flask
docker build -t dmilan/docker-sample-restapp-flask .
kubectl apply -f kube-yaml/deployment.yaml
kubectl apply -f kube-yaml/services.yaml
# For scaling
kubectl apply -f kube-yaml/deployment-scale.yaml

Test with Alpine image

kubectl run alpine-kubeatl --image=dmilan/alpine-plus -i --tty --rm
curl  --s <<clusterip of docker-sample-restapp-flask>>

for i in `seq 1 50`; do curl -s 10.4.3.178/ping |jq '.host_ip'; done

view logs

kubectl -n kube-system logs podname
# view full logs
kubectl -n kube-system logs -f podname

Clean up

kubectl delete -f kube-yaml/

Bonus

export PROJECT_DIR=~/project/kubeatl/kubelab
mkdir -p ${PROJECT_DIR}
cd ${PROJECT_DIR}
cd ${PROJECT_DIR}/kubernetes-engine-samples/guestbook

Install

kubectl apply -f redis-master-controller.yaml

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment