{"id":26502,"date":"2019-04-02T17:20:25","date_gmt":"2019-04-02T22:20:25","guid":{"rendered":"https:\/\/centricconsulting.com\/?p=26502"},"modified":"2021-12-15T00:15:59","modified_gmt":"2021-12-15T05:15:59","slug":"part-4-deploying-docker-containers-to-aws-fargate-using-jenkins_devops","status":"publish","type":"post","link":"https:\/\/centricconsulting.com\/blog\/part-4-deploying-docker-containers-to-aws-fargate-using-jenkins_devops\/","title":{"rendered":"Part 4: Deploying Docker Containers to AWS Fargate using Jenkins"},"content":{"rendered":"
In the first three parts of this series, I demonstrated building out a Virtual Private Cloud (VPC) with the networking, servers, databases, filesystems, load balancers, container registry, container service, security controls and a host of other services required for an application to securely operate in the AWS Cloud.<\/p>\n
With Jenkins and GitLab servers in place on the AWS infrastructure, this final part will demonstrate:<\/strong><\/p>\n At the end of part three, we ran a \u2018terraform apply\u2019 command to build out the container services in a VPC. As a result, we produced the following terraform output:<\/p>\n <\/a><\/p>\n Several important variables are printed out at the end, including the Bastion\u2019s Public IP address and the Load Balancer\u2019s Public IP address. We will reference these variables to configure our applications. After we deploy our Jenkins job, we will point our browser to this DNS Name to test the application deployment, so we will need to take note of the DNS Name.<\/p>\n A couple of one-time tasks are required to setup the GitLab and Jenkins applications. The Jenkins root password must be entered into the Jenkins console and the GitLab database must be initialized and populated using the gitlab-rake command. I will begin by walking you through the initial configuration of both services, beginning with Jenkins.<\/p>\n To access the Jenkins web console, enter this DNS Name (printed above) into your favorite browser and append :8080 to the end of the URL. From there, you set the root password, install desired plugins, customize the configuration and begin to build your pipelines.<\/p>\n Example: http:\/\/private-apps-alb-1114129372.us-east-2.elb.amazonaws.com:8080\/<\/strong><\/p>\n <\/a><\/p>\n When you first install the Jenkins Master server, you will be required to locate the auto-generated root password which is stored on the server in \/var\/lib\/jenkins\/secrets\/initialAdminPassword.<\/p>\n Log into the Bastion host using SSH. From there SSH into the private IP address (obtained from the EC2 console) of the Jenkins Master server and display the contents of the password file. Use this password to log into the Jenkins Master web console by pasting the password into the Jenkins console at the \u2018Administrator password\u2019 prompt shown above.<\/p>\n <\/a><\/p>\n <\/a><\/p>\n Once the password is entered, you will be led through a few initial configuration screens (shown below). When you see the main user interface, the HA configuration will be complete. By HA, we mean that all data is stored externally on the EFS filesystems, allowing you to complete destroy the EC2 instance without losing data.<\/p>\n The autoscaling group will automatically deploy a new replacement EC2 instance and attach it to the shared data store allowing you to resume where you left off.<\/p>\n <\/a><\/p>\n <\/a><\/p>\n <\/a><\/p>\n Jenkins is now ready!<\/strong><\/p>\n When the terraform script completed successfully, it printed out the Public DNS Name of the Application Load Balancer. Enter this DNS Name into your favorite browser to access the GitLab web console. Once in the console, you can create users, groups and projects and test remote access from your workstation.<\/p>\n Example: http:\/\/private-apps-alb-1114129372.us-east-2.elb.amazonaws.com\/<\/strong><\/p>\n Until you complete the initial configuration tasks below, you will see this screen.<\/p>\n <\/a><\/p>\n Log into the Bastion host from your workstation using the previously created SSH key. From the Bastion host, you will SSH into the first GitLab servers to initialize the database with the gitlab-rake command.<\/p>\n <\/a><\/p>\n You will see a long list of commands running, which is populating the external PostgreSQL database.\u00a0 A snip of the output is below.<\/p>\n <\/a><\/p>\n After the gitlab-rake is complete, you can enter the public DNS name again into your browser and GitLab will be ready for you to enter your email address, change the root password and login with the root account. Sample screen flows follow.<\/p>\n <\/a><\/p>\n Next, sign in using Username \u201croot\u201d and the password you entered above.<\/p>\n <\/a><\/p>\n The Home screen will be presented, and you can create a new user, log in as the new user and create a new project to house your application code.<\/p>\n The application code you upload will be used to later build a docker image which will be run in as a container in the AWS serverless container service known as Fargate.<\/p>\n <\/a><\/p>\n At this point, you have a working GitLab instance with all configuration data stored on shared EFS Storage.\u00a0 It is time to scale up the autoscaling group for GitLab from 1 to 2.\u00a0 This will spin up another GitLab instance behind the load balancer and provide for a highly available architecture.<\/p>\n To do this, simply update the variables.tf file as indicated below and run terraform apply<\/em><\/strong>. Shortly after, a 2nd<\/sup> EC2 instance will be deployed for the second GitLab instance.\u00a0 Verify the new instance in the AWS console.<\/p>\n <\/a><\/p>\n Congratulations! The DevOps platform is setup and ready. Because all of the servers, databases and cache servers are highly available, you can safely terminate any component and within a few minutes, a replacement will be spun up and back to normal operation with no user interaction.<\/p>\n Define a new project named \u201chello-world\u201d in GitLab to house your application code. Click the \u201c+\u201d sign, then \u201cNew project.\u201d<\/p>\n <\/a><\/p>\n Enter project name \u201chello-world.\u201d Set Visibility to \u201cInternal\u201d and select \u201cCreate project.\u201d<\/p>\n <\/a><\/p>\n The new project will be created and a link to the project will be presented for SSH and HTTP access. The HTTP link will be utilized for the demo purposes to upload code and integrate with Jenkins.<\/p>\n <\/a><\/p>\n Clone Hello-World PHP application from the provided demo GitHub repo. This code will then be pushed up to your new GitLab repository created above.<\/p>\n Create a new working directory for the local repository and run the following \u2018git clone\u2019 command. Verify the code is downloaded.<\/p>\n <\/a><\/p>\n The sample application code is now cloned from the public GitHub repository to your local repository and can be uploaded to your GitLab repository.<\/p>\n Change your remote repository to the GitLab repository, changing the repo URL to match the repo name you created in GitLab.<\/p>\n Note: Change the highlighted repository name to the name of the GitLab repository you created above and ensure you are using HTTP protocol. Next, verify the remote repository and finally push the code to GitHub.<\/p>\n <\/a><\/p>\n Verify the GitLab project was updated to include the sample PHP application files cloned from GitLab by accessing the URL of your GitLab instance and refreshing the page. (e.g. http:\/\/private-apps-alb-869593036.us-east-2.elb.amazonaws.com\/rich\/hello-temp<\/a>)<\/p>\n\n
Let\u2019s get started\u2026<\/h2>\n
Configure Jenkins<\/h2>\n
Accessing the Jenkins Web Console<\/h4>\n
Jenkins one-time setup tasks<\/h4>\n
Example output<\/h4>\n
Jenkins Initial Configuration Screens \u2013 One-Time Task<\/h4>\n
Configure GitLab<\/h2>\n
Accessing the GitLab Web Console<\/h4>\n
Gitlab one-time setup tasks<\/h4>\n
GitLab Define a Project<\/h2>\n
Create a new user and project to upload your PHP application code.<\/h4>\n