tranhoangnguyen-agilityio / kubernetes

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Kubernetes / Kops Demo

Setup vagrant

  1. Pull the source code
git clone https://github.com/TranHoang/kubernetes.git
vagrant up
vagrant ssh

Use kops to deploy a simple echoserver to AWS

Following these steps to create a micro cluster with one master and one node on AWS

  1. Config enviroment variable
kubernetes
└───bin
│   │   create_aws_cluster.sh
└───env
│   │   aws_rsa.pub
│   │   aws.env
│   │   cluster.env
│   README.md
│   setup.sh
|   Vagrantfile
  • aws_rsa.pub kops use public key to create the cluster on AWS

  • aws.env AWS enviroment variable

    AWS_ACCESS_KEY_ID="AWS Access key id goes here."
    AWS_SECRET_ACCESS_KEY="AWS secret access key goes here."
    S3_BUCKET_NAME="kops use this bucketname to store the cluster state."
    AWS_AVAILABE_ZONE_NAME="AWS region availability-zones. Example: ap-southeast-1a."
  • cluster.env AWS enviroment variable

    CLUSTER_NAME="kops need a cluster name to deploy. It's also a domain name."
  1. Config AWS Route 53

    Create a public hosted zones in AWS Route 53

  2. Config DNS in the domain panel Added NS Record with host name is the AWS Route 53 values. For example DNS

  3. Create 1 micro cluster

./bin/create_aws_cluster.sh
  1. Wait for a few minutes then check the cluster is ready for deployment
kops validate cluster --state=s3://kops-state-b24b
  1. Once the cluster is up, master and node are ready then run the scrips below to deploy a simple echoserver to AWS
kubectl run hello-minikube --image=gcr.io/google_containers/echoserver:1.4 --port 8080
kubectl expose deployment hello-minikube --type=NodePort
kubectl get service

Create helloworld pod on minukube

  1. Start minikube cluster
minikube start
  1. Build the helloworld image
./bin/build_image.sh
./bin/push_image.sh
  1. Create pod on minikube
./bin/create_pods.sh
  1. Verify pod status
kubectl describe pod helloworld.example.com
  1. There are 2 ways to expose the pod

5.1 Forward pod's port then we can access via localhost

kubectl port-forward helloworld.example.com 8081:3000
curl http://localhost:8081

5.2 Create a service

kubectl expose pod helloworld.example.com --type=NodePort --name helloworld-service
minikube service helloworld-service --url

Create helloworld pod on AWS

  1. Build the helloworld image
./bin/build_image.sh
./bin/push_image.sh
  1. Create a cluster
./bin/create-aws-cluster.sh
  1. Check cluster status
./bin/validate-cluster.sh
  1. Once the cluster is up, master and node are ready then we deploy the helloworld application cluster
./bin/create-pods.sh
./bin/create-services.sh
  1. Create an alias record set in hosted zones and select the load balancing service in the Alias Target dropdown.

DNS

  1. Access the http://helloworld.kubernetes.fullstack.ws

Deploy helloworld by k8s deployment

./bin/deploy.sh
kubectl expose deployment helloworld-deployment --type=NodePort
minikube service helloworld-deployment --url

Reference Link

Useful command

  1. Attach to a process that is already running inside an existing container.
kubectl attach {POD_NAME} -c [CONTAINER_NAME]
  1. Execute a shell in a container inside a pod
kubectl exec -it {POD_NAME} -c [CONTAINER_NAME] -- /bin/bash
  1. Describe a service
kubectl describe {SERVICE_NAME}
  1. Update an image for a specific container in a deployment
./bin/deploy-update-image.sh -c [CONTAINER_NAME] -i [Docker hub image uri]

Example

./bin/deploy-update-image.sh -c k8s-demo -i tranhoang/helloworld:2.0

Tips:

  1. Grant permission for vagrant ubuntu user to execute the docker. Execute the following command then re-ssh to the vagrant box
sudo usermod -a -G docker $USER

About


Languages

Language:Shell 93.2%Language:JavaScript 6.8%