minikube tạo ra local cluster
$ minikube start --vm-driver=virtualbox Starting local Kubernetes v1.9.0 cluster... Starting VM... Getting VM IP address... Moving files into cluster... Setting up certs... Connecting to cluster... Setting up kubeconfig... Starting cluster components... Kubectl is now configured to use the cluster. Loading cached images from config file.
$ minikube version minikube version: v0.25.0
$ minikube dashboard
$ kubectl run alpine --image=alpine:latest --restart=Never -it
$ kubectl run busybox --image=busybox --restart=Never -it
Ứng dụng này in ra dòng chữ Hello Kubernetes! sau đó dọn dẹp, xoá deployment và service vừa tạo ra
$ ./helloworld.sh
$ ./nginx.sh
Chú ý lệnh sẽ lấy địa chỉ truy cập vào service
$ minikube service nginxbalancer --url
Định nghĩa pod-nginx.yaml như sau
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx:alpine
ports:
- containerPort: 80
Tạo pod nginx
$ kubectl create -f pod-nginx.yaml
Lấy danh sách chi tiết các pod
$ kubectl get pod -o wide
hoặc để lấy địa chỉ IP
$ kubectl get pod nginx -o go-template='{{.status.podIP}}'
172.17.0.11
Dùng lệnh describe để lấy thông tin chi tiết một pods
$ kubectl describe pod nginx | grep IP:
IP: 172.17.0.11
Sau đó khởi động một pod khác ở chế độ interactive terminal kết nối vào pod đang chạy
$ kubectl run -i -t alpine --image=alpine:latest --restart=Never
trong dòng lệnh bên trong alpine, gõ
$ wget -qO- http://ip_adress of nginx pod
Cách thứ 2 theo hướng dẫn
$ kubectl run busybox --image=busybox --restart=Never --tty -i --generator=run-pod/v1 --env "POD_IP=$(kubectl get pod nginx -o go-template='{{.status.podIP}}')"
u@busybox$ wget -qO- http://$POD_IP # Run in the busybox container
u@busybox$ exit # Exit the busybox container
$ kubectl delete pod busybox # Clean up the pod we created with "kubectl run"
Kết nối vào trong pod nginx thông qua terminal. Nếu pod có nhiều hơn một container, cần bổ xung option -c Lệnh này giống với docker exec
kubectl exec nginx -c nginx -it -- sh
kubectl delete deployment nginx webby my-nginx
Replication Controller dùng để vận hành nhóm các Pod chạy trong thời gian dài. Nếu RC phát hiện Pod nào bị lỗi, bị tắt, dừng, RC sẽ tạo ra Pod mới thay thế.
Chạy lệnh sau đây
$ kubectl create -f rc_caddy.yaml
$ kubectl get rc
$ kubectl get pod
NAME READY STATUS RESTARTS AGE
rccaddy-dmvgs 1/1 Running 0 6m
rccaddy-g2dnb 1/1 Running 1 2h
rccaddy-v8gzr 1/1 Running 1 3h
$ kubectl delete pod rccaddy-dmvgs
Hãy quan sát pod mới được tạo lại bằng cách liên tục gõ lệnh kubectl get pod
Xem label các pod
$ kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
rccaddy-g2dnb 1/1 Running 3 16h app=caddyweb
rccaddy-ll6h9 1/1 Running 0 22m app=caddyweb
$ kubectl get rc
NAME DESIRED CURRENT READY AGE
rccaddy 3 3 2 16h
$ kubectl get pod
NAME READY STATUS RESTARTS AGE
rccaddy-g2dnb 1/1 Running 3 16h
rccaddy-ll6h9 1/1 Running 0 18m
rccaddy-v8gzr 1/1 Running 3 16h
$ kubectl scale --replicas=5 rc/rccaddy
$ kubectl get pod
NAME READY STATUS RESTARTS AGE
rccaddy-g2dnb 1/1 Running 3 16h
rccaddy-l5s9z 1/1 Running 0 26s
rccaddy-ll6h9 1/1 Running 0 19m
rccaddy-rrfjj 1/1 Running 0 26s
rccaddy-v8gzr 1/1 Running 3 17h
$ kubectl scale --replicas=2 rc/rccaddy
NAME READY STATUS RESTARTS AGE
rccaddy-g2dnb 1/1 Running 3 16h
rccaddy-ll6h9 1/1 Running 0 20m
Xoá replica controller
$ kubectl delete rc rccaddy
replicationcontroller "rccaddy" deleted
$ kubectl get rc
No resources found.
nginx-app.yaml là file định nghĩa service và deployment tương tự như docker-compose.yml
Hiểu rõ hơn về deployment xem ở đây:
Khác biệt giữa Replica Set và Replica Controller. Replica Set hỗ trợ selector
$ kubectl create -f nginx-app.yaml
service "my-nginx-svc" created
deployment "my-nginx" created
$ kubectl get deploy
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
my-nginx 3 3 3 3 10h
$ kubectl get rs
NAME DESIRED CURRENT READY AGE
my-nginx-b477df957 3 3 3 3m
$ kubectl describe rs my-nginx-b477df957
$ minikube service my-nginx-svc --url
http://192.168.99.100:32496
$ curl http://192.168.99.100:32496
$ curl (minikube service my-nginx-svc --url)
Lệnh này tương đối giống docker exec container_name -it /bin/sh
$ kubectl get pod
NAME READY STATUS RESTARTS AGE
my-nginx-b477df957-25wcp 1/1 Running 1 11h
my-nginx-b477df957-6wn7f 1/1 Running 1 11h
my-nginx-b477df957-rsj8n 1/1 Running 1 11h
$ kubectl exec my-nginx-b477df957-25wcp -it /bin/sh
/ # uname -a
Linux my-nginx-b477df957-25wcp 4.9.64 #1...
$ kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4d
my-nginx-svc LoadBalancer 10.105.132.254 <pending> 80:32496/TCP 2d
Không thể ping địa chỉ Cluster-IP, 10.105.132.254 của my-nginx-svc, do đó ta cần vào một pod trong cluster thì mới truy cập được service này. Chú ý truy cập ở cổng 80
$ kubectl exec my-nginx-b477df957-25wcp -it /bin/sh
$ wget -qO- http://10.105.132.254
Ở trường hợp này, chúng ta không cần vào trong pod mà ở ngoài Mac, tuy nhiên cổng truy cập sẽ không phải là 80 mà 32496
$ mk service my-nginx-svc --url
http://192.168.99.100:32496
$ curl http://192.168.99.100:32496
- Lấy tên các pod trong cluster
- Chọn ra một pod để forward cổng từ host vào cổng container trong pod đó phục vụ
$ kubectl get pod
$ sudo kubectl port-forward myweb-5487d54c5-2g2p7 80:80
Kubernetes Webinar Series - Everything About Ingress
$ kubectl create -f ingress.yaml
ingress "ingress-nginx" created
$ kubectl get ing
NAME HOSTS ADDRESS PORTS AGE
ingress-nginx * 192.168.99.100 80 12s
$ curl http://192.168.99.100
Tìm hiểu thêm về ingress controller
$ kubectl describe ing ingress-nginx
Name: ingress-nginx
Namespace: default
Address: 192.168.99.100
Default backend: my-nginx-svc:80 (172.17.0.3:80,172.17.0.4:80,172.17.0.5:80)
Rules:
Host Path Backends
---- ---- --------
* * my-nginx-svc:80 (172.17.0.3:80,172.17.0.4:80,172.17.0.5:80)
Annotations:
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal CREATE 6m nginx-ingress-controller Ingress default/ingress-nginx
Normal UPDATE 5m nginx-ingress-controller Ingress default/ingress-nginx
Communicate Between Containers in the Same Pod Using a Shared Volume
Tham khảo file two-container-pod.yaml
$ kubectl create -f two-container-pod.yaml
pod "two-containers" created
$ kubectl exec -it two-containers -c nginx-container -- /bin/sh
/ # hostname
two-containers
/ # wget -qO- localhost
Hello from the debian container
hay thử thay args trong nginx-container
args: ["-c", "echo Hello from the debian container > /pod-data/index.html"]
# bằng dòng này
args: ["-c", "while true; do echo hello >> /pod-data/index.html; sleep 10;done"]
Sử dụng Kompose.io
Tài liệu chọn giải pháp cài đặt K8s
- Hãy vào thu mục nodeapp
- Tạo Node.js app, xem file nodeapp/index.js
npm init
yarn add express
node index.js
curl http://localhost:3000
- Tạo file shell script để tự động hoá việc build docker image và kiểm thử. Xem file nodeapp/build.sh
$ kubectl create -f node_app_service.yaml
service "node-app-service" created
$ kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 16d
my-nginx-svc LoadBalancer 10.105.132.254 <pending> 80:32496/TCP 14d
node-app-service LoadBalancer 10.105.244.118 <pending> 3000:32282/TCP 8s
$ minikube service node-app-service --url
http://192.168.99.100:32282
$ curl http://192.168.99.100:32282
https://cloud.google.com/kubernetes-engine/docs/tutorials/hello-app
Commands:
Step 0: Before you begin
gcloud components install kubectl
Step 1: Build the container image
git clone https://github.com/GoogleCloudPlatform/kubernetes-engine-samples
cd kubernetes-engine-samples/hello-app
export PROJECT_ID="$(gcloud config get-value project -q)"
# Build Docker App and push to a Container Registry
docker build -t gcr.io/${PROJECT_ID}/hello-app:v1 .
# Check
docker images
Step 2: Upload the container image
gcloud docker -- push gcr.io/${PROJECT_ID}/hello-app:v1
Step 4: Create a container cluster
gcloud container clusters create hello-cluster --num-nodes=3
gcloud compute instances list
Step 5: Deploy your application
kubectl run hello-web --image=gcr.io/${PROJECT_ID}/hello-app:v1 --port 8080
kubectl get pods
Step 6: Expose your application to the Internet
kubectl expose deployment hello-web --type=LoadBalancer --port 80 --target-port 8080
# Checking launching service (take awhile to get external IP assigned)
kubectl get service
Step 7: Scale up your application
kubectl scale deployment hello-web --replicas=3
kubectl get deployment hello-web
kubectl get pods
Step 8: Deploy a new version of your app
docker build -t gcr.io/${PROJECT_ID}/hello-app:v2 .
gcloud docker -- push gcr.io/${PROJECT_ID}/hello-app:v2
kubectl set image deployment/hello-web hello-web=gcr.io/${PROJECT_ID}/hello-app:v2
Cleaning up
kubectl delete service hello-web
gcloud compute forwarding-rules list
gcloud container clusters delete hello-cluster
- Tạo tài khoản Amazon Web Service. Link đây
Sample selection from https://www.youtube.com/watch?v=BznjDNxp4Hs
- Don't trust arbitrary image
- One process per Container