Kubernetes: Usando Kubernetes na IBM Cloud: Difference between revisions

From Wiki
No edit summary
Line 187: Line 187:
1) Começando o Lab3
1) Começando o Lab3


  cd ..
  cd ../Lab3
cd Lab3


==Construindo as images do Watson==
==Construindo as images do Watson==
Line 204: Line 203:
  docker push registry.ng.bluemix.net/<namespace>/watson-talk
  docker push registry.ng.bluemix.net/<namespace>/watson-talk


3) Atualizar o watson-deployment.yml
3) Edite o arquivo '''watson-deployment.yml''' e altere a tag '''<namespace>''' para a que você está utilizando
 
Edite o arquivo watson-deployment.yml e altere a tag '''<namespace>'''.




Line 215: Line 212:
  ibmcloud login
  ibmcloud login


Se quiser alterar execute  
Se quiser alterar execute o comando '''ibmcloud target --cf'''
 
ibmcloud target --cf


2) Criando o serviço
2) Criando o serviço
Line 231: Line 226:
  ibmcloud cf services
  ibmcloud cf services


==Criando uma instância do Watson service==
==Bind do serviço para o Cluster==
 
Execute
 
ibmcloud cs cluster-service-bind <name-of-cluster> default tone
 
Verifique os secrets criado pelo comando anterior
kubectl get secrets.
 
==Criando pods e services==
 
Now that the service is bound to the cluster, you want to expose the secret to your pod so that it can utilize the service. To do this, create a secret datastore as a part of your deployment configuration. This has been done for you in watson-deployment.yml:
 
    volumeMounts:
            - mountPath: /opt/service-bind
              name: service-bind-volume
      volumes:
        - name: service-bind-volume
          secret:
            defaultMode: 420
            secretName: binding-tone
            # from the kubectl get secrets command above
 
Build the application using the yaml.
 
kubectl create -f watson-deployment.yml
 
Verify the pod has been created:
 
kubectl get pods
 
Your secret has now been created. Note that for this lab, this has been done for you.
 
Putting it all together - Run the application and service
 
By this time you have created pods, services, and volumes for this lab.
 
You can open the Kubernetes dashboard and explore all the new objects created or use the following commands.
 
kubectl get pods
kubectl get deployments
kubectl get services
 
Get the public IP for the worker node to access the application:
 
ibmcloud cs workers <name-of-cluster>
 
Now that the you have the container IP and port, go to your favorite web browser and launch the following URL to analyze the text and see output.
 
http://<public-IP>:30080/analyze/"Today is a beautiful day"
 
If you can see JSON output on your screen, congratulations! You are done with this lab!


= Ver também =
= Ver também =

Revision as of 15:40, 28 October 2018

Existe um curso sobre Kubernetes na IBM Cloud, o link é o seguinte https://courses.cognitiveclass.ai/

Os exemplos do curso estão no GitHub https://github.com/IBM/container-service-getting-started-wt


Importante: os comandos bx ou bluemix foram depreciados, agora utilizar o comando ibmcloud

Pré-requisitos

1) Instalando os pré-requisitos no MacOS

brew install kubectl 
brew cask install ibm-cloud-cli
brew install cfssl
brew install docker

2) Login na IBM Cloud

 ibmcloud login

Para Login Federado ou para Funcionário IBM use "ibmcloud login -sso"

3) Adicionando os plugins para criar e gerenciar clusters Kubernetes

ibmcloud plugin install container-service -r Bluemix
ibmcloud plugin install container-registry -r Bluemix

4) Listando plug-ins instalados...

ibmcloud plugin list




Provisionando um Cluster na IBM Cloud

  • Via linha de comando
ibmcloud cs cluster-create --name <name-of-cluster>
  • Via console
Vá no endereço https://console.bluemix.net/catalog/ e localize a entrada IBM Cloud Kubernetes Service.




Deploy da sua Primeira aplicação no IBM Cloud


No Lab1, temos o deploy de uma aplicação que exibe o hostname. Os arquivos principais do lab são:

  • Dockerfile : define os atributos da image. Busca uma image com o Node.js e instala a aplicação app.js
  • app.js : Aplicação em Node.js que pronta o hostmame
  • package.json: define as dependências necessários pela aplicação app.js

Clonando os exemplos do Github

mkdir Curso
cd Curso
git clone https://github.com/IBM/container-service-getting-started-wt.git



Push da imagem para o IBM Cloud Container Registry

1) Começando o Lab1

cd Lab1

2) Login na IBM Cloud

ibmcloud login --sso

3) Login na IBM Cloud Container Registry.

ibmcloud cr login

4) Criando uma namespace

ibmcloud cr namespace-add <my_namespace>

namespaces é um forma de organização de cluster. No dia a dia, seria projeto1_prod, projeto1_dev, projeto2_prod, ...

5) Criando uma docker image

docker build --tag registry.ng.bluemix.net/<my_namespace>/hello-world:1 .

6) Verificando a imagem

docker images

7) Fazendo o push da image

docker push registry.ng.bluemix.net/<my_namespace>/hello-world:1

8) Verifique que o cluster está pronto pra uso

ibmcloud cs clusters

verificar se o estado está NORMAL

ibmcloud cs workers <yourclustername>

Verifica se os workers estão no estado NORMAL com o status READY

Anotar o ip público do worker.

Deploy da aplicação

1) Execute o comando e defina as variáveis de ambiente

ibmcloud cs cluster-config <yourclustername>

Exemplo no Mac:

export KUBECONFIG=/Users/<user_name>/.bluemix/plugins/container-service/clusters/pr_firm_cluster/kube-config-prod-par02-pr_firm_cluster.yml

2) Iniciando a aplicação

kubectl run hello-world --image=registry.ng.bluemix.net/<my_namespace>/hello-world:1

3) Verifique o status pelo comando

kubectl get pods

4) Quando o status estiver como RUNNING, execute

kubectl expose deployment/hello-world --type="NodePort" --port=8080


Com o navegador, acesse o ip e porta para visualizar

Outros comandos: Para achar a porta, Para remover o Deployment , Para remover o Service


Escalando e atualizando aplicações -- services, replica sets e health checks

1) Começando o Lab2

cd ..
cd Lab2

Adicionando Replicas

  • Adicionando replicas via linha de comando

Adicionando via linha de comando

$ kubectl scale --replicas=10 deployment hello-world


  • Adicionando replicas via arquivo de configuração

Adicionando via edição da configuração

kubectl edit deployment/<name-of-deployment>

Alterar o item replicas

...
   spec:
     replicas: 1

Salve e feche o arquivo

Verificando o rollout da alteração

Verificando o rollout da alteração

kubectl rollout status deployment/hello-world

Após o rollout verifique se os pods estão executando.

kubectl get pods

o STATUS deve ser RUNNING.


Deploy de uma aplicação com IBM Watson

1) Começando o Lab3

cd ../Lab3

Construindo as images do Watson

1) Build das images

docker build -t registry.ng.bluemix.net/<namespace>/watson ./watson

docker build -t registry.ng.bluemix.net/<namespace>/watson-talk ./watson-talk

2) push da image Watson para o IBM Cloud Container Registry.

docker push registry.ng.bluemix.net/<namespace>/watson

docker push registry.ng.bluemix.net/<namespace>/watson-talk

3) Edite o arquivo watson-deployment.yml e altere a tag <namespace> para a que você está utilizando


Criando uma instância do Watson service

1) Verifique o space e o org

ibmcloud login

Se quiser alterar execute o comando ibmcloud target --cf

2) Criando o serviço

ibmcloud cf create-service tone_analyzer standard tone

onde tone é o nome do serviço

Note: When you add the Tone Analyzer service to your account, a message is displayed that the service is not free. If you limit your API calls, this course does not incur charges from the Watson service.

3) Conferindo se o serviço foi criado

ibmcloud cf services

Bind do serviço para o Cluster

Execute

ibmcloud cs cluster-service-bind <name-of-cluster> default tone

Verifique os secrets criado pelo comando anterior

kubectl get secrets.

Criando pods e services

Now that the service is bound to the cluster, you want to expose the secret to your pod so that it can utilize the service. To do this, create a secret datastore as a part of your deployment configuration. This has been done for you in watson-deployment.yml:

   volumeMounts:
           - mountPath: /opt/service-bind
             name: service-bind-volume
     volumes:
       - name: service-bind-volume
         secret:
           defaultMode: 420
           secretName: binding-tone
           # from the kubectl get secrets command above

Build the application using the yaml.

kubectl create -f watson-deployment.yml

Verify the pod has been created:

kubectl get pods

Your secret has now been created. Note that for this lab, this has been done for you.

Putting it all together - Run the application and service

By this time you have created pods, services, and volumes for this lab.

You can open the Kubernetes dashboard and explore all the new objects created or use the following commands.

kubectl get pods
kubectl get deployments
kubectl get services

Get the public IP for the worker node to access the application:

ibmcloud cs workers <name-of-cluster>

Now that the you have the container IP and port, go to your favorite web browser and launch the following URL to analyze the text and see output.

http://<public-IP>:30080/analyze/"Today is a beautiful day"

If you can see JSON output on your screen, congratulations! You are done with this lab!

Ver também