Pods
Lab Objectives
In this lab we will configure a Kubernetes Pod hosting a mysql database from the command-line. We’ll also log into the container we deploy into this lab using the mysql-client. We’ll expose the pod in a follow-up lab.
Lab Structure - Overview
- Run a command to deploy a pod from the command-line
- Run a command to deploy a pod sourced from a YAML file
- Run a command to exec a shell into a Kubernetes hosted container
- Run a command to exec directly into the mysql-client from the command-line
Execute on the Leader node
The following commands should all be executed in the VS Code window connected to the leader node.
Deploy a Pod from the command-line
- Show the nodes of the cluster
kubectl get nodes
- Show information about the cluster
kubectl cluster-info
- Dump information about the cluster
kubectl cluster-info dump
- Show all Pods in the
kube-system
namespacekubectl get pods -n kube-system
- Deploy
nginx
Podkubectl run nginx-pod-lab --image=nginx:alpine --port=80 --restart=Never
Note: The
--restart=Never
flag ensures this creates a Pod (not a Deployment) - Confirm Pod is running’
kubectl get pods
- Get information about Pod in
json
formatkubectl get pod nginx-pod-lab -o json
- Now get info about it in
YAML
syntaxkubectl get pod nginx-pod-lab -o yaml
- Delete the pod.
kubectl delete pod nginx-pod-lab
Create Pod from manifest
- Enter the lab directory.
cd $HOME/core-k8s/labs/pods
- In the manifests directory, you will find
nginx-kube.yml
. This file will launch a simple nginx server. Deploy it with the following:
kubectl apply -f manifests/nginx-kube.yml
- Show the deployed Pods
kubectl get pods
You should see something like this:
NAME READY STATUS RESTARTS AGE
nginx-pod-lab 1/1 Running 0 18m
nginx-web 1/1 Running 0 15s
- Cleanup everything
kubectl delete pods --all
Create a Pod with 2 containers
- In the manifests directory you will find
two-containers.yml
. Review the manifest and see if you can figure out what it is doing, then deploy it:kubectl apply -f manifests/two-containers.yml
- Show the deployed Pods
kubectl get pods
- Look at the details of the Pod
kubectl describe pod two-containers
Sample output:
Name: two-containers
Namespace: default
Priority: 0
Node: ip-192-168-29-163.us-west-2.compute.internal/192.168.29.163
Start Time: Tue, 27 Aug 2019 22:38:37 -0700
Labels: <none>
Annotations: kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"name":"two-containers","namespace":"default"},"spec":{"containers":[{"image"...
kubernetes.io/psp: eks.privileged
Status: Running
IP: 192.168.22.118
Containers:
nginx-container:
Container ID: docker://d5f34b906d03eddc2521c3860558d14174d2e63b786a22471a3c818cb5e5ad73
<snip>...
- Log into the
nginx
container of thetwo-containers
Pod:kubectl exec -it two-containers -c nginx-container -- bash
- Validate
nginx
is running in the container:apt-get update && apt-get install -y procps && ps aux
Sample output:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 10624 5412 ? Ss 05:38 0:00 nginx: master process nginx -g daemon off;
nginx 7 0.0 0.0 11096 2604 ? S 05:38 0:00 nginx: worker process
root 8 0.0 0.0 3868 3244 pts/0 Ss 05:49 0:00 bash
root 338 0.0 0.0 7640 2672 pts/0 R+ 05:49 0:00 ps aux
- Install
curl
inside the containerapt install -y curl
- Check the default nginx page is loading using curl.
curl localhost
Output:
Hello from the debian container
- Exit the
nginx
containerexit
Deploy MySQLDB and connect to the client
- Deploy a MySQL DB image
kubectl run mysql-demo --image=mysql:5.7 --port=3306 --env="MYSQL_ROOT_PASSWORD=password" --restart=Never
- Show all of the running Pods, and note the name of the Pod you just created
kubectl get pods
Sample output:
NAME READY STATUS RESTARTS AGE
mysql-demo 1/1 Running 0 3m24s
two-containers 1/2 NotReady 0 4m54s
- Log into the new MySQL container:
kubectl exec -it mysql-demo -- mysql -ppassword
- Look around
SHOW DATABASES;
SELECT * FROM mysql.user LIMIT 1\G
- Exit MySQL:
quit
- Exit the container
exit
Clean up
Run the following to delete resources created in this lab:
kubectl delete --ignore-not-found=true manifests