Skip to content

Instantly share code, notes, and snippets.

@vfarcic
Last active February 21, 2022 04:58
Show Gist options
  • Save vfarcic/41c86eb385dfc5c881d910c5e98596f2 to your computer and use it in GitHub Desktop.
Save vfarcic/41c86eb385dfc5c881d910c5e98596f2 to your computer and use it in GitHub Desktop.
# Source: https://gist.github.com/41c86eb385dfc5c881d910c5e98596f2
cd k8s-specs
git pull
cd cluster
cat kops
source kops
export BUCKET_NAME=devops23-$(date +%s)
aws s3api create-bucket \
--bucket $BUCKET_NAME \
--create-bucket-configuration \
LocationConstraint=$AWS_DEFAULT_REGION
export KOPS_STATE_STORE=s3://$BUCKET_NAME
# Windows Only
alias kops="docker run -it --rm \
-v $PWD/devops23.pub:/devops23.pub \
-v $PWD/config:/config \
-e KUBECONFIG=/config/kubecfg.yaml \
-e NAME=$NAME -e ZONES=$ZONES \
-e AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID \
-e AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY \
-e KOPS_STATE_STORE=$KOPS_STATE_STORE \
vfarcic/kops"
kops create cluster \
--name $NAME \
--master-count 3 \
--master-size t2.small \
--node-count 2 \
--node-size t2.medium \
--zones $ZONES \
--master-zones $ZONES \
--ssh-public-key devops23.pub \
--networking kubenet \
--yes
kops validate cluster
# Windows only
kops export kubecfg --name ${NAME}
# Windows only
export KUBECONFIG=$PWD/config/kubecfg.yaml
kubectl apply \
--filename https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.1.1/deploy/static/provider/aws/deploy.yaml
CLUSTER_DNS=$(aws elb \
describe-load-balancers | jq -r \
".LoadBalancerDescriptions[] \
| select(.DNSName \
| contains (\"api-devops23\") \
| not).DNSName")
echo $CLUSTER_DNS
cd ..
cat pv/jenkins-no-pv.yml
kubectl create \
-f pv/jenkins-no-pv.yml \
--save-config
kubectl --namespace jenkins \
get events
kubectl --namespace jenkins \
create secret \
generic jenkins-creds \
--from-literal=jenkins-user=jdoe \
--from-literal=jenkins-pass=incognito
kubectl --namespace jenkins \
rollout status \
deployment jenkins
open "http://$CLUSTER_DNS/jenkins"
kubectl --namespace jenkins \
get pods \
--selector=app=jenkins \
-o json
POD_NAME=$(kubectl \
--namespace jenkins \
get pods \
--selector=app=jenkins \
-o jsonpath="{.items[*].metadata.name}")
echo $POD_NAME
kubectl --namespace jenkins \
exec -it $POD_NAME pkill java
open "http://$CLUSTER_DNS/jenkins"
aws ec2 describe-instances
aws ec2 describe-instances \
| jq -r \
".Reservations[].Instances[] \
| select(.SecurityGroups[]\
.GroupName==\"nodes.$NAME\")\
.Placement.AvailabilityZone"
aws ec2 describe-instances \
| jq -r \
".Reservations[].Instances[] \
| select(.SecurityGroups[]\
.GroupName==\"nodes.$NAME\")\
.Placement.AvailabilityZone" \
| tee zones
AZ_1=$(cat zones | head -n 1)
AZ_2=$(cat zones | tail -n 1)
VOLUME_ID_1=$(aws ec2 create-volume \
--availability-zone $AZ_1 \
--size 10 \
--volume-type gp2 \
--tag-specifications "ResourceType=volume,Tags=[{Key=KubernetesCluster,Value=$NAME}]" \
| jq -r '.VolumeId')
VOLUME_ID_2=$(aws ec2 create-volume \
--availability-zone $AZ_1 \
--size 10 \
--volume-type gp2 \
--tag-specifications "ResourceType=volume,Tags=[{Key=KubernetesCluster,Value=$NAME}]" \
| jq -r '.VolumeId')
VOLUME_ID_3=$(aws ec2 create-volume \
--availability-zone $AZ_2 \
--size 10 \
--volume-type gp2 \
--tag-specifications "ResourceType=volume,Tags=[{Key=KubernetesCluster,Value=$NAME}]" \
| jq -r '.VolumeId')
echo $VOLUME_ID_1
aws ec2 describe-volumes \
--volume-ids $VOLUME_ID_1
cat pv/pv.yml
cat pv/pv.yml \
| sed -e \
"s@REPLACE_ME_1@$VOLUME_ID_1@g" \
| sed -e \
"s@REPLACE_ME_2@$VOLUME_ID_2@g" \
| sed -e \
"s@REPLACE_ME_3@$VOLUME_ID_3@g" \
| kubectl create -f - \
--save-config
kubectl get pv
cat pv/pvc.yml
kubectl create -f pv/pvc.yml \
--save-config
kubectl --namespace jenkins \
get pvc
kubectl get pv
cat pv/jenkins-pv.yml
kubectl apply \
-f pv/jenkins-pv.yml
kubectl --namespace jenkins \
rollout status \
deployment jenkins
open "http://$CLUSTER_DNS/jenkins"
POD_NAME=$(kubectl \
--namespace jenkins \
get pod \
--selector=app=jenkins \
-o jsonpath="{.items[*].metadata.name}")
kubectl --namespace jenkins \
exec -it $POD_NAME pkill java
open "http://$CLUSTER_DNS/jenkins"
kubectl --namespace jenkins delete \
deploy jenkins
kubectl --namespace jenkins get pvc
kubectl get pv
kubectl --namespace jenkins \
delete pvc jenkins
kubectl get pv
kubectl delete -f pv/pv.yml
aws ec2 delete-volume \
--volume-id $VOLUME_ID_1
aws ec2 delete-volume \
--volume-id $VOLUME_ID_2
aws ec2 delete-volume \
--volume-id $VOLUME_ID_3
kubectl get sc
cat pv/jenkins-dynamic.yml
kubectl apply \
-f pv/jenkins-dynamic.yml
kubectl --namespace jenkins \
rollout status \
deployment jenkins
kubectl --namespace jenkins \
get events
kubectl --namespace jenkins get pvc
kubectl get pv
aws ec2 describe-volumes \
--filters 'Name=tag-key,Values="kubernetes.io/created-for/pvc/name"'
kubectl --namespace jenkins \
delete deploy,pvc jenkins
kubectl get pv
aws ec2 describe-volumes \
--filters 'Name=tag-key,Values="kubernetes.io/created-for/pvc/name"'
kubectl get sc
kubectl describe sc gp2
cat pv/jenkins-default.yml
diff pv/jenkins-dynamic.yml \
pv/jenkins-default.yml
kubectl apply \
-f pv/jenkins-default.yml
kubectl get pv
kubectl --namespace jenkins \
delete deploy,pvc jenkins
cat pv/sc.yml
kubectl create -f pv/sc.yml
kubectl get sc
cat pv/jenkins-sc.yml
kubectl apply \
-f pv/jenkins-sc.yml
aws ec2 describe-volumes \
--filters 'Name=tag-key,Values="kubernetes.io/created-for/pvc/name"'
kubectl delete ns jenkins
kops delete cluster \
--name $NAME \
--yes
aws s3api delete-bucket \
--bucket $BUCKET_NAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment