Saturday, November 18, 2017

cookbook for kubernetes master

Attributes

#

# Cookbook Name:: kubernetes
# Attributes:: default
#
# Copyright 2016, whitestar
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
default['kubernetes']['version'] = '1.7.8'
default['kubernetes']['fullversion'] = '1.7.8-1'
default['kubernetes']['domainname'] = 'xx.xx.xx.xx'
default['kubernetes']['yum_repo'] = {
  'baseurl' => 'https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64',
  'gpgcheck' => '1',
  'repo_gpgcheck' => '1',
  'gpgkey' => 'https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg',

}
===========================================================

metadata.rb

name 'kubernetes'
maintainer 'The Authors'
maintainer_email 'you@example.com'
license 'All Rights Reserved'
description 'Installs/Configures kubernetes'
long_description 'Installs/Configures kubernetes'
version '0.2.0'
chef_version '>= 12.1' if respond_to?(:chef_version)
==========================================================
recipes

#
# Cookbook:: kubernetes
# Recipe:: default
#
# Copyright:: 2017, The Authors, All Rights Reserved.


template '/etc/yum.repos.d/kubernetes.repo' do
  source  'etc/yum.repos.d/kubernetes.repo'
  owner 'root'
  group 'root'
  mode '0644'
end
template '/opt/clusterrole.yml' do
  source  'opt/clusterrole.yml'
  owner 'root'
  group 'root'
  mode '0644'
end

bash 'systemctl_daemon-reload' do
  code <<-EOH
    systemctl daemon-reload
  EOH
  action :nothing
end
execute "disable selinux - running" do
      command "/usr/sbin/setenforce 0"

end

execute 'Disable selinux' do
  command "sed -i --follow-symlinks 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux"
end
[
    "kubeadm",
  ].each {|pkg|
    package pkg do
========================================================================

/xxxxxx/xxxxxxxxx/src/xxxxxxx/cookbooks/kubernetes/templates/default/etc/systemd/system/docker.service.d

cat override.conf 

[Service]
ExecStart=
ExecStart=/usr/bin/dockerd overlay 

============================================================
/xxxxxx/xxxxxxx/xxxx/xxxxxx/cookbooks/kubernetes/templates/default/opt

cat clusterrole.yml 
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: kubernetes-dashboard
  labels:
    k8s-app: kubernetes-dashboard
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
- kind: ServiceAccount
  name: kubernetes-dashboard
  namespace: kube-system
========================================================================

cookbook for monitoring-script to know the ecs-agent connectivity status

Berksfile

# frozen_string_literal: true
source 'https://supermarket.chef.io'


metadata

Attributes

#
# Cookbook Name:: checkagentconnect
# Attributes:: default

default['CLUSTER']['ENV'] = 'Dev'
Metadata.rb

name 'checkagentconnect'
maintainer 'The Authors'
maintainer_email 'kaushikvhm@gmail.com'
license 'All Rights Reserved'
description 'Installs/Configures checkagentconnect'
long_description 'Installs/Configures checkagentconnect'
version '0.1.0'
chef_version '>= 12.1' if respond_to?(:chef_version)

recipes

#
# Cookbook:: checkagentconnect
# Recipe:: default
#
# Copyright:: 2017, The Authors, All Rights Reserved.


template '/tmp/get-pip.py' do
source 'tmp/get-pip.py.erb'
mode '0755'
end

bash 'Install python pip and awscli' do

  code <<-EOH
       python /tmp/get-pip.py
       pip install awscli
    EOH
  not_if { ::File.exist?('/tmp/get-pip.py') }
end

template '/tmp/AgentStatusWithInstance.sh' do
source 'tmp/AgentStatusWithInstance.sh.erb'
mode '0755'
end
cron 'AgentNotification' do
hour '*'
minute '*/1'
command "/tmp/AgentStatusWithInstance.sh"
#user centos
end
======================================================================

Templates 

#!/bin/bash
getLocalIpaddress=`hostname -I`
arrayIpaddress=($getLocalIpaddress)
validIpaddress=${arrayIpaddress[0]}
#echo $validIpaddress
aws ec2 describe-instances >> /tmp/jsontxt
var=$(jq --arg validIpaddress "$validIpaddress" '.Reservations[].Instances[]|select(.PrivateIpAddress==$validIpaddress)|.InstanceId' jsontxt)
var2=$(echo $var|tr -d '"')
#echo $var2
ECS_CLUSTER=<%= node['CLUSTER']['ENV']%>
CONTAINERS_ID=$(aws ecs list-container-instances --cluster $ECS_CLUSTER --output text --query 'containerInstanceArns')
DATE=$(date +%Y-%m-%d-%H:%M)
for container in $CONTAINERS_ID
do
STATUS=$(aws ecs describe-container-instances --container-instances $container --cluster $ECS_CLUSTER --output json --query 'containerInstances[0].agentConnected')
CHECK_INSTANCE_ID=$(aws ecs describe-container-instances --container-instances $container --cluster $ECS_CLUSTER --output text --query 'containerInstances[0].ec2InstanceId')
#echo "check instance $CHECK_INSTANCE_ID"
if [ "$var2"  = "$CHECK_INSTANCE_ID" ]
then
if [ $STATUS == "true" ]
then
#echo $validIpaddress
#echo $var2
Agentversion=$(aws ecs describe-container-instances --container-instances $container --cluster $ECS_CLUSTER --output text --query 'containerInstances[0].versionInfo.agentVersion')
echo $Agentversion
dockerversion=$(aws ecs describe-container-instances --container-instances $container --cluster $ECS_CLUSTER --output text --query 'containerInstances[0].versionInfo.dockerVersion')
#echo $dockerversion
tags=$(jq --arg instanceid "$var2" '.Reservations[].Instances[]|select(.InstanceId==$instanceid)|.Tags[]|select(.Key=="Name")|.Value' /tmp/jsontxt)
#echo $tags
echo -e "Ipaddress=$validIpaddress\n Agentversion=$Agentversion\n dockerversion=$dockerversion\n tags=$tags\n" | mailx -s "ECS-Agent is not connected with the following host" xxxxxxxxxxxx@mail.com
fi
=====================================================================


Get container logs on to the host machine using the VOLUMES

ecs register-task-definition --family xxxx-xxxx-xxxxapp --volumes "[{"name": "xxxxx-xxxx-appname","host": { "sourcePath": "/opt/logs/appname" } }]" --container-definitions "[{"name":"appname_container","image":"xxxxx.dkr.ecr.us-east-1.amazonaws.com/appname:${buildNumber}","cpu": 10,"memory":300,"workingDirectory": "/opt/appname","essential": true,"environment": [{"name": "NODE_ENV","value": "xxxx"}],"mountPoints": [ {"sourceVolume": "applicationname","containerPath": "/opt/applicationname/logs","readOnly": false} ],"portMappings": [ {"hostPort": 8080,"containerPort": 8080,"protocol": "tcp" } ],"command": ["node","bin/www"]}]" --network-mode "bridge".


VOLUME:

--volumes "[{"name": "xxxxx-xxxx-appname","host": { "sourcePath": "/opt/logs/appname" } }]"


Container path for logs (mounted to volume in the ecs-taskdefintion in the ECS)

"mountPoints": [ {"sourceVolume": "applicationname","containerPath": "/opt/applicationname/logs","readOnly": false} ]



ECS-Agent connectivity status and notify the agent connectivity through mail

#!/bin/bash
getLocalIpaddress=`hostname -I`
arrayIpaddress=($getLocalIpaddress)
validIpaddress=${arrayIpaddress[0]}
#echo $validIpaddress
aws ec2 describe-instances >> jsontxt
var=$(jq --arg validIpaddress "$validIpaddress" '.Reservations[].Instances[]|select(.PrivateIpAddress==$validIpaddress)|.InstanceId' jsontxt)
var2=$(echo $var|tr -d '"')
#echo $var2
ECS_CLUSTER="Dev"
CONTAINERS_ID=$(aws ecs list-container-instances --cluster $ECS_CLUSTER --output text --query 'containerInstanceArns')
DATE=$(date +%Y-%m-%d-%H:%M)
for container in $CONTAINERS_ID
do
STATUS=$(aws ecs describe-container-instances --container-instances $container --cluster $ECS_CLUSTER --output json --query 'containerInstances[0].agentConnected')
CHECK_INSTANCE_ID=$(aws ecs describe-container-instances --container-instances $container --cluster $ECS_CLUSTER --output text --query 'containerInstances[0].ec2InstanceId')
if [ "$var2"  = "$CHECK_INSTANCE_ID" ]
then
if [ $STATUS == "true" ]
then
echo $validIpaddress
echo $var2
Agentversion=$(aws ecs describe-container-instances --container-instances $container --cluster $ECS_CLUSTER --output text --query 'containerInstances[0].versionInfo.agentVersion')
echo $Agentversion   
dockerversion=$(aws ecs describe-container-instances --container-instances $container --cluster $ECS_CLUSTER --output text --query 'containerInstances[0].versionInfo.dockerVersion')
echo $dockerversion
tags=$(jq --arg instanceid "$var2" '.Reservations[].Instances[]|select(.InstanceId==$instanceid)|.Tags[]|select(.Key=="Name")|.Value' jsontxt)
echo $tags
echo -e "Ipaddress=$validIpaddress\n Agentversion=$Agentversion\n dockerversion=$dockerversion\n tags=$tags\n" | mailx -s "This is the subject" xxxxxxxxxx@gmail.com
#uuencode /home/centos/bodyattach.txt #/home/centos/bodyattach.txt | cat #/home/centos/bodyattach.txt | mailx -s "subject" -a #/home/centos/bodyattach.txt xxxxxxxx@gmail.com
fi
fi
done

rm jsontxt