Skip to content

6. Quelques tests

Formation suivie via le blog de Stéphane Robert : parcours Kubernetes.

Je crée un pod contenant 2 containers et je tente de voir s’ils communiquent entre eux.

manifest.yml
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
- name: alpine
image: alpine:latest
command: ["/bin/sh", "-c", "echo 'Hello from Alpine!' && sleep 3600"]
Terminal window
kubectl apply -f manifest.yml
kubectl get pods
kubectl logs nginx -c alpine

On ouvre un shell dans le conteneur alpine, puis on appelle nginx via localhost :

Terminal window
$ kubectl exec -it nginx -c alpine -- /bin/sh
/ # wget -qO- http://localhost:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, nginx is successfully installed and working.
Further configuration is required for the web server, reverse proxy,
API gateway, load balancer, content cache, or other features.</p>
<p>For online documentation and support please refer to
<a href="https://nginx.org/">nginx.org</a>.<br/>
To engage with the community please visit
<a href="https://community.nginx.org/">community.nginx.org</a>.<br/>
For enterprise grade support, professional services, additional
security features and capabilities please refer to
<a href="https://f5.com/nginx">f5.com/nginx</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
Terminal window
# Pour changer le namespace courant
kubectl config set-context --current --namespace=<mon_namespace>
# Pour vérifier le namespace courant
kubectl config view --minify | grep namespace

Pour changer rapidement de contexte, possibilité d’utiliser kubectx et kubens (à installer) :

Outil Fonction Commande classique équivalente
kubectx Change de contexte kubectl config use-context ...
kubens Change de namespace kubectl config set-context --current --namespace=...
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx-replicaset
namespace: dev
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
Terminal window
# Modification dynamique du nombre de réplicas
kubectl scale rs nginx-replicaset --replicas=5
# Modification dynamique de l'image
kubectl set image rs nginx-replicaset nginx=nginx:1.23
# Check les pods existants
kubectl describe pod <pod_name> | grep Image