preload
Apr 29

To fetch current ENI ip-address for container running on ECS fargate, you can use container metadata service described here: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-metadata-endpoint.html

This can be used for example in environment value on runtime in task definition:


"Command": [
"/bin/bash",
"-c",
"export HTTP_ADDRESS=http://$(curl -s http://169.254.170.2/v2/metadata | jq -r .Containers[0].Networks[0].IPv4Addresses[0])/my/cool/api && /entrypoint.sh run"
],

Aug 31

I needed script that generates dynamic inventory for ansible with hostnames provided by nova. I found this nice little script that generates valid inventory format though there was few missing features that i had to add for the script:

1) There was only one hardcoded network where one could retrieve address.
2) Only ip-addresses were generated for hostnames.

We use rfc6598 networks for management and instances might have two floating addresses from multiple network options due the multiple availability zones. Also I would need hostnames for inventory because some of the playbooks I use depends on variables like ansible_fqdn. Script reads ansible_host_groups and ansible_host_vars metadata from instances described ie. in heat template:

node1:
  type: OS::Nova::Server
    properties:
      name: node1.example.com
      metadata:
        ansible_host_groups: galera_cluster 
        ansible_host_vars: enviroment->Prod

And generates valid json format for Ansible dynamic inventory:

    "galera_cluster": {
        "hosts": [
            "100.80.129.107",
            "100.81.129.43",
            "100.80.129.109"
        ]
    },
    "_meta": {
        "hostvars": {
            "100.81.129.43": {
                "enviroment": [
                    "Prod"
                ]
            },

But I need to have nova provided hostname because of the nature of the current setup. I modified the script to take multiple networks as a list and get the instance name from nova. After this, inventory is generated with ansible_ssh_host option:

    "galera_cluster": {
        "hosts": [
            "node1.example.com",
            "node2.example.com",
            "node3.example.com"
        ]
    },
    "_meta": {
        "hostvars": {
            "node1.example.com": {
                "ansible_ssh_host": "100.80.129.107",
                "enviroment": [
                    "Prod"
                ]
            },

You can find the modified script here and original script and copyright from here

Sep 11

It has been a while since last post 🙂 The site is running on AWS nowadays and i wanted to test out my nginx and php-fpm setup on CentOS7. I did not want to install virtual machine from AMI image and reconfigure the server all over again. This is where docker came handy. Here are some features I noticed during the test.

So the target was to build container with PHP5.4 without affect to the actual site. For this, docker has nice feature which allows you to map directory from host machine to the container.

Here is the Dockerfile to build CentOS7 image with my comments.

FROM centos:centos7
MAINTAINER J.Berg contact@mceith.com
RUN rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
RUN yum install epel-release -y
RUN yum update -y && yum install nginx php-fpm php-mysql -y
RUN mkdir /var/wwwlogs
# Copy the original settigs. Note that the files must be inside the build directory.
COPY nginx.conf /etc/nginx/nginx.conf
COPY sites-enabled /etc/nginx/sites-enabled
COPY sites-available /etc/nginx/sites-available
COPY conf.d /etc/nginx/conf.d
ADD run.sh /run.sh
# Do not start nginx as daemon.
RUN sed -i '1 i\daemon off;' /etc/nginx/nginx.conf
RUN sed -ie 's/apache/mceith/g' /etc/php-fpm.d/www.conf
# Match user id with running system for php-fpm.
RUN groupadd -g 501 mceith && useradd -M -u 501 -g 501 mceith -s /sbin/nologin
EXPOSE 80
ENTRYPOINT /run.sh

Since container does not have systemd or whatever to handle running processes, we need to make script which starts them for us:

run.sh

#!/bin/bash
/usr/sbin/php-fpm -D && /usr/sbin/nginx

Build the image:


# docker build -t nginx_test .

And run it on port 8080:


# docker run -t -i -d -p 8080:80 -v /var/www/mceith/public_html:/var/www/mceith/public_html -v /var/wwwlogs:/var/wwwlogs nginx_test

Site runs parallel with CentOS6/PHP5.3 on port 8080. Seems to work with CentOS7/PHP5.4 also! 🙂

Tagged with: